-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsimplex.rs
354 lines (310 loc) · 9.46 KB
/
simplex.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
#![allow(unused_attributes)]
#![feature(register_tool)]
#![register_tool(lr)]
#[path = "lib/rmat.rs"]
pub mod rmat;
use rmat::RMat;
///* step 1 */
#[lr::sig(fn (arr2: &RMat<f32>[m,n], m:usize{0 < m}, n: usize{0 < n}) -> bool)]
pub fn is_neg(arr2: &RMat<f32>, _m:usize, n: usize) -> bool {
let mut j = 1;
while j < n - 1 {
if *arr2.get(0, j) < 0.0 {
return true
}
j += 1;
}
false
}
///* step 2 */
#[lr::sig(fn (m:usize{0 < m}, n:usize{0 < n}, arr2: &RMat<f32>[m, n]) -> bool)]
pub fn unb1(m:usize, n:usize, arr2: &RMat<f32>) -> bool {
let mut i = 0;
let mut j = 1;
// INV: 0 < i <= m, 0 <= j < n
while j < n - 1 {
if *arr2.get(0, j) < 0.0 {
i = i + 1;
loop {
if i < m {
if *arr2.get(i, j) < 0.0 {
i = i + 1
} else {
i = 0;
j = j + 1;
break;
}
} else {
return true
}
}
} else {
i = 0;
j = j + 1;
}
}
false
}
///* step 3 */
#[lr::sig(fn (m:usize{0<m}, n:usize{2<n}, arr2: &RMat<f32>[m,n]) -> usize{v: 0<v && v+1<n})]
pub fn enter_var(_m:usize, n:usize, arr2: &RMat<f32>) -> usize {
let mut c = *arr2.get(0, 1);
let mut j = 1;
let mut j_ = 2;
while j_ < n - 1 {
// INV j+1 < n, j_ < n
let c_ = *arr2.get(0, j_);
if c_ < c {
j = j_;
c = c_;
}
j_ += 1
}
j
}
///* step 4 */
#[lr::sig(fn(m:usize, n:usize, arr2: &RMat<f32>[m, n], j:usize{0 < j && j < n}, i0:usize{0 < i0 && i0 < m}, r0:f32) -> usize{v:0 < v && v < m})]
pub fn depart_var(m:usize, n:usize, arr2: &RMat<f32>, j:usize, i0:usize, r0:f32) -> usize {
let mut i = i0;
let mut r = r0;
let mut i_ = i + 1;
while i_ < m {
let c_ = *arr2.get(i_, j);
if 0.0 < c_ {
let r_ = *arr2.get(i_, n-1) / c_;
if r_ < r {
i = i_;
r = r_;
}
i_ += 1;
} else {
i_ += 1
}
}
i
}
#[lr::assume]
#[lr::sig(fn () -> usize{x: false})]
pub fn die () -> usize {
unimplemented!();
}
#[lr::sig(fn (m:usize{0 < m}, n:usize{0 < n}, arr2: &RMat<f32>[m, n], j: usize{0 < j && j < n}) -> usize{v:0 < v && v < m})]
pub fn init_ratio_i(m:usize, _n:usize, arr2: &RMat<f32>, j: usize) -> usize {
let mut i = 1;
while i < m {
let c = *arr2.get(i, j);
if 0.0 < c {
return i
}
i += 1;
}
die() // abort ("init_ratio: negative coefficients!")
}
#[lr::sig(fn(m:usize{0 < m}, n:usize{0 < n}, arr2: &RMat<f32>[m, n], j: usize{0 < j && j < n}, i:usize{0 < i && i < m}) -> f32)]
pub fn init_ratio_c(_m:usize, n:usize, arr2: &RMat<f32>, j: usize, i: usize) -> f32 {
*arr2.get(i, j) / *arr2.get(i, n-1)
}
///* step 5 */
#[lr::sig(fn (m:usize, n:usize, arr2:&mut RMat<f32>[m,n], i:usize{0 < i && i < m}, j:usize{0 < j && j < n}) -> i32)]
fn row_op(m:usize, n:usize, arr2:&mut RMat<f32>, i:usize, j:usize) -> i32 {
// norm(m, n, arr2, i, j);
// RJ: rename `jj` to `j` to see an error!
let c = *arr2.get(i, j);
let mut jj = 1;
while jj < n {
*arr2.get_mut(i, jj) /= c;
jj += 1;
}
// ro_op_aux3(m, n, arr2, i, j, 0)
let mut i_ = 0;
while i_ < m {
if i_ != i {
let c_ = *arr2.get(i_, j);
let mut j = 1;
while j < n {
let cj = *arr2.get(i, j);
let cj_ = *arr2.get(i_, j);
*arr2.get_mut(i_, j) = cj_ - cj * c_;
j += 1
}
}
i_ += 1
}
0
}
#[lr::sig(fn (m:usize{1 < m}, n:usize{2 < n}, arr2:&mut RMat<f32>[m, n]) -> i32)]
pub fn simplex(m:usize, n:usize, arr2:&mut RMat<f32>) -> i32 {
while is_neg(arr2, m, n) {
if unb1(m, n, arr2) {
die();
return 0
} else {
let j = enter_var(m, n, arr2);
let i = init_ratio_i(m, n, arr2, j);
let r = init_ratio_c(m, n, arr2, j, i);
let i = depart_var(m, n, arr2, j, i, r);
row_op(m, n, arr2, i, j);
}
}
0
}
// /*
// (*
// (* An implementation of the simplex method in DML *)
// datatype 'a array2D with (nat,nat) =
// {m:nat,n:nat} A(m,n) of ('a array(n)) array(m) * int(m) * int(n)
// fun('a) nRows (A (_, m, _)) = m
// withtype {m:nat,n:nat} <> => 'a array2D(m,n) -> int(m)
// fun('a) nCols (A (_, _, n)) = n
// withtype {m:nat,n:nat} <> => 'a array2D(m,n) -> int(n)
// (* step 1 *)
// fun is_neg_aux (arr2, n, j) =
// if j < n - 1 then
// if sub2 (arr2, 0, j) <. 0.0 then true
// else is_neg_aux (arr2, n, j+1)
// else false
// withtype {m:pos,n:pos,j:nat | j <= n} <n-j> =>
// (float array(n)) array(m) * int(n) * int(j) -> bool
// fun is_neg (arr2, n) = is_neg_aux (arr2, n, 1)
// withtype {m:pos,n:pos} <> => (float array(n)) array(m) * int(n) -> bool
// (* step 2 *)
// fun unb1 (arr2, m, n, i, j) =
// if j < n-1 then
// if sub2 (arr2, 0, j) <. 0.0 then unb2 (arr2, m, n, i+1, j)
// else unb1 (arr2, m, n, 0, j+1)
// else false
// withtype {m:pos,n:pos,i:nat,j:nat | i < m, j <= n} <n-j, m-i> =>
// (float array(n)) array(m) * int (m) * int(n) * int(i) * int(j) -> bool
// and unb2 (arr2, m, n, i, j) =
// if i < m then
// if sub2 (arr2, i, j) <. 0.0 then unb2 (arr2, m, n, i+1, j)
// else unb1 (arr2, m, n, 0, j+1)
// else true
// withtype {m:pos,n:pos,i:nat,j:nat | i <= m, j < n} <n-j,m-i> =>
// (float array(n)) array(m) * int (m) * int(n) * int(i) * int(j) -> bool
// (* step 3 *)
// fun enter_var (arr2, n, j, c, j') =
// if j' < n-1 then
// let
// val c' = sub2 (arr2, 0, j')
// in
// if c' <. c then enter_var (arr2, n, j', c', j'+1)
// else enter_var (arr2, n, j, c, j'+1)
// end
// else j
// withtype {m:pos,n:pos,j:pos,j':pos | j+1 < n, j' < n} <n-j'> =>
// (float array(n)) array(m) * int(n) * int(j) * float * int(j') ->
// [j:pos | j+1 < n] int(j)
// (* step 4 *)
// fun depart_var (arr2, m, n, j, i, r, i') =
// if i' < m then
// let
// val c' = sub2 (arr2, i', j)
// in
// if c' >. 0.0 then
// let
// val r' = sub2(arr2, i', n-1) /. c'
// in
// if r' <. r then depart_var(arr2, m, n, j, i', r', i'+1)
// else depart_var (arr2, m, n, j, i, r, i'+1)
// end
// else depart_var (arr2, m, n, j, i, r, i'+1)
// end
// else i
// withtype {m:pos,n:pos,i:pos,i':pos,j:pos | i < m, i' <= m, j < n} <m-i'> =>
// (float array(n)) array(m) * int(m) * int(n) * int(j) * int(i) * float * int(i') ->
// [i:pos | i < m] int(i)
// fun init_ratio (arr2, m, n, j, i) =
// if i < m then
// let
// val c = sub2 (arr2, i, j)
// in
// if c >. 0.0 then (i, sub2 (arr2, i, n-1) /. c)
// else init_ratio (arr2, m, n, j, i+1)
// end
// else abort ("init_ratio: negative coefficients!")
// withtype {m:pos,n:pos,j:pos,i:pos | j < n, i <= m} <m-i> =>
// (float array(n)) array(m) * int(m) * int(n) * int(j) * int(i) ->
// [i:pos | i < m] int(i) * float
// (* step 5 *)
// fun norm_aux (arr2, n, i, c, j) =
// if j < n then
// let
// val _ = update2 (arr2, i, j, sub2 (arr2, i, j) /. c)
// in
// norm_aux (arr2, n, i, c, j+1)
// end
// else ()
// withtype {m:pos,n:pos,i:pos,j:pos | i < m, j <= n} <n-j> =>
// (float array(n)) array(m) * int(n) * int(i) * float * int(j) -> unit
// fun norm (arr2, n, i, j) =
// let
// val c = sub2 (arr2, i, j)
// in
// norm_aux (arr2, n, i, c, 1)
// end
// withtype {m:pos,n:pos,i:pos,j:pos | i < m, j < n} <> =>
// (float array(n)) array(m) * int(n) * int(i) * int(j) -> unit
// fun row_op_aux1 (arr2, n, i, i', c, j) =
// if j < n then
// let
// val cj = sub2 (arr2, i, j)
// val cj' = sub2 (arr2, i', j)
// val _ = update2 (arr2, i', j, cj' -. cj *. c)
// in
// row_op_aux1 (arr2, n, i, i', c, j+1)
// end
// else ()
// withtype {m:pos,n:pos,i:pos,i':nat, j:pos | i < m, i' < m, j <= n} <n-j> =>
// (float array(n)) array(m) * int(n) * int(i) * int(i') * float * int(j) -> unit
// fun row_op_aux2 (arr2, n, i, i', j) =
// let
// val c' = sub2 (arr2, i', j)
// in
// row_op_aux1 (arr2, n, i, i', c', 1)
// end
// withtype {m:pos,n:pos,i:pos,i':nat, j:pos | i < m, i' < m, j < n} <> =>
// (float array(n)) array(m) * int(n) * int(i) * int(i') * int(j) -> unit
// fun row_op_aux3 (arr2, m, n, i, j, i') =
// if i' < m then
// if i' <> i then
// let
// val _ = row_op_aux2(arr2, n, i, i', j)
// in
// row_op_aux3 (arr2, m, n, i, j, i'+1)
// end
// else row_op_aux3 (arr2, m, n, i, j, i'+1)
// else ()
// withtype {m:pos,n:pos,i:pos,j:pos,i':nat | i < m, j < n, i' <= m} <m-i'> =>
// (float array(n)) array(m) * int(m) * int(n) * int(i) * int(j) * int(i') -> unit
// fun row_op (arr2, m, n, i, j) =
// let
// val _ = norm (arr2, n, i, j)
// in
// row_op_aux3 (arr2, m, n, i, j, 0)
// end
// withtype {m:pos,n:pos,i:pos,j:pos| i < m, j < n} <> =>
// (float array(n)) array(m) * int(m) * int(n) * int(i) * int(j) -> unit
// fun simplex (arr2, m, n) =
// if is_neg (arr2, n) then
// if unb1 (arr2, m, n, 0, 1) then abort ("simplex: unbound solution!")
// else
// let
// val j = enter_var (arr2, n, 1, sub2 (arr2, 0, 1), 2)
// val (i, r) = init_ratio (arr2, m, n, j, 1)
// val i = depart_var (arr2, m, n, j, i, r, i+1)
// val _ = row_op (arr2, m, n, i, j)
// in
// simplex (arr2, m, n)
// end
// else ()
// withtype {m:int,n:int | m > 1, n > 2}
// (float array(n)) array(m) * int(m) * int(n) -> unit
// fun main (A (arr2, m, n)) =
// if m > 1 then
// if n > 2 then simplex (arr2, m, n)
// else abort ("too few columns")
// else abort ("too few rows")
// withtype float array2D -> unit
// *)
// */