| author | nipkow | 
| Sun, 22 Feb 2009 17:25:28 +0100 | |
| changeset 30056 | 0a35bee25c20 | 
| parent 24630 | 351a308ab58d | 
| child 31971 | 8c1b845ed105 | 
| permissions | -rw-r--r-- | 
| 16784 | 1  | 
(* Title: HOL/Matrix/cplex/fspmlp.ML  | 
2  | 
ID: $Id$  | 
|
3  | 
Author: Steven Obua  | 
|
4  | 
*)  | 
|
5  | 
||
| 22951 | 6  | 
signature FSPMLP =  | 
| 16784 | 7  | 
sig  | 
8  | 
type linprog  | 
|
| 19404 | 9  | 
type vector = FloatSparseMatrixBuilder.vector  | 
10  | 
type matrix = FloatSparseMatrixBuilder.matrix  | 
|
| 16784 | 11  | 
|
| 22964 | 12  | 
val y : linprog -> term  | 
13  | 
val A : linprog -> term * term  | 
|
14  | 
val b : linprog -> term  | 
|
15  | 
val c : linprog -> term * term  | 
|
16  | 
val r12 : linprog -> term * term  | 
|
| 16784 | 17  | 
|
18  | 
exception Load of string  | 
|
| 22951 | 19  | 
|
| 16784 | 20  | 
val load : string -> int -> bool -> linprog  | 
21  | 
end  | 
|
22  | 
||
| 22964 | 23  | 
structure Fspmlp : FSPMLP =  | 
| 16784 | 24  | 
struct  | 
25  | 
||
| 19404 | 26  | 
type vector = FloatSparseMatrixBuilder.vector  | 
27  | 
type matrix = FloatSparseMatrixBuilder.matrix  | 
|
28  | 
||
| 24302 | 29  | 
type linprog = term * (term * term) * term * (term * term) * (term * term)  | 
| 16784 | 30  | 
|
| 24302 | 31  | 
fun y (c1, c2, c3, c4, _) = c1  | 
32  | 
fun A (c1, c2, c3, c4, _) = c2  | 
|
33  | 
fun b (c1, c2, c3, c4, _) = c3  | 
|
34  | 
fun c (c1, c2, c3, c4, _) = c4  | 
|
35  | 
fun r12 (c1, c2, c3, c4, c6) = c6  | 
|
| 16784 | 36  | 
|
| 22951 | 37  | 
structure CplexFloatSparseMatrixConverter =  | 
| 16784 | 38  | 
MAKE_CPLEX_MATRIX_CONVERTER(structure cplex = Cplex and matrix_builder = FloatSparseMatrixBuilder);  | 
39  | 
||
40  | 
datatype bound_type = LOWER | UPPER  | 
|
41  | 
||
| 
24135
 
e7fb7ef2a85e
added int type constraints to accomodate hacked SML/NJ;
 
wenzelm 
parents: 
23665 
diff
changeset
 | 
42  | 
fun intbound_ord ((i1: int, b1),(i2,b2)) =  | 
| 16784 | 43  | 
if i1 < i2 then LESS  | 
| 22951 | 44  | 
else if i1 = i2 then  | 
45  | 
(if b1 = b2 then EQUAL else if b1=LOWER then LESS else GREATER)  | 
|
| 16784 | 46  | 
else GREATER  | 
47  | 
||
48  | 
structure Inttab = TableFun(type key = int val ord = (rev_order o int_ord));  | 
|
49  | 
||
50  | 
structure VarGraph = TableFun(type key = int*bound_type val ord = intbound_ord);  | 
|
51  | 
(* key -> (float option) * (int -> (float * (((float * float) * key) list)))) *)  | 
|
52  | 
(* dest_key -> (sure_bound * (row_index -> (row_bound * (((coeff_lower * coeff_upper) * src_key) list)))) *)  | 
|
53  | 
||
54  | 
exception Internal of string;  | 
|
55  | 
||
| 22951 | 56  | 
fun add_row_bound g dest_key row_index row_bound =  | 
57  | 
let  | 
|
58  | 
val x =  | 
|
59  | 
case VarGraph.lookup g dest_key of  | 
|
60  | 
NONE => (NONE, Inttab.update (row_index, (row_bound, [])) Inttab.empty)  | 
|
61  | 
| SOME (sure_bound, f) =>  | 
|
62  | 
(sure_bound,  | 
|
63  | 
case Inttab.lookup f row_index of  | 
|
64  | 
NONE => Inttab.update (row_index, (row_bound, [])) f  | 
|
65  | 
| SOME _ => raise (Internal "add_row_bound"))  | 
|
| 16784 | 66  | 
in  | 
| 22951 | 67  | 
VarGraph.update (dest_key, x) g  | 
| 16784 | 68  | 
end  | 
69  | 
||
| 22951 | 70  | 
fun update_sure_bound g (key as (_, btype)) bound =  | 
71  | 
let  | 
|
72  | 
val x =  | 
|
73  | 
case VarGraph.lookup g key of  | 
|
74  | 
NONE => (SOME bound, Inttab.empty)  | 
|
75  | 
| SOME (NONE, f) => (SOME bound, f)  | 
|
76  | 
| SOME (SOME old_bound, f) =>  | 
|
77  | 
(SOME ((case btype of  | 
|
| 22964 | 78  | 
UPPER => Float.min  | 
79  | 
| LOWER => Float.max)  | 
|
| 22951 | 80  | 
old_bound bound), f)  | 
81  | 
in  | 
|
82  | 
VarGraph.update (key, x) g  | 
|
83  | 
end  | 
|
84  | 
||
85  | 
fun get_sure_bound g key =  | 
|
86  | 
case VarGraph.lookup g key of  | 
|
87  | 
NONE => NONE  | 
|
| 16784 | 88  | 
| SOME (sure_bound, _) => sure_bound  | 
89  | 
||
| 22951 | 90  | 
(*fun get_row_bound g key row_index =  | 
| 17412 | 91  | 
case VarGraph.lookup g key of  | 
| 22951 | 92  | 
NONE => NONE  | 
| 16784 | 93  | 
| SOME (sure_bound, f) =>  | 
| 22951 | 94  | 
(case Inttab.lookup f row_index of  | 
95  | 
NONE => NONE  | 
|
96  | 
| SOME (row_bound, _) => (sure_bound, row_bound))*)  | 
|
97  | 
||
98  | 
fun add_edge g src_key dest_key row_index coeff =  | 
|
| 17412 | 99  | 
case VarGraph.lookup g dest_key of  | 
| 22951 | 100  | 
NONE => raise (Internal "add_edge: dest_key not found")  | 
| 16784 | 101  | 
| SOME (sure_bound, f) =>  | 
| 22951 | 102  | 
(case Inttab.lookup f row_index of  | 
103  | 
NONE => raise (Internal "add_edge: row_index not found")  | 
|
104  | 
| SOME (row_bound, sources) =>  | 
|
105  | 
VarGraph.update (dest_key, (sure_bound, Inttab.update (row_index, (row_bound, (coeff, src_key) :: sources)) f)) g)  | 
|
| 16784 | 106  | 
|
| 22951 | 107  | 
fun split_graph g =  | 
| 21056 | 108  | 
let  | 
109  | 
fun split (key, (sure_bound, _)) (r1, r2) = case sure_bound  | 
|
110  | 
of NONE => (r1, r2)  | 
|
111  | 
| SOME bound => (case key  | 
|
112  | 
of (u, UPPER) => (r1, Inttab.update (u, bound) r2)  | 
|
113  | 
| (u, LOWER) => (Inttab.update (u, bound) r1, r2))  | 
|
114  | 
in VarGraph.fold split g (Inttab.empty, Inttab.empty) end  | 
|
| 16784 | 115  | 
|
| 21056 | 116  | 
fun it2list t = Inttab.fold cons t [];  | 
| 16784 | 117  | 
|
118  | 
(* If safe is true, termination is guaranteed, but the sure bounds may be not optimal (relative to the algorithm).  | 
|
119  | 
If safe is false, termination is not guaranteed, but on termination the sure bounds are optimal (relative to the algorithm) *)  | 
|
| 22951 | 120  | 
fun propagate_sure_bounds safe names g =  | 
121  | 
let  | 
|
122  | 
(* returns NONE if no new sure bound could be calculated, otherwise the new sure bound is returned *)  | 
|
123  | 
fun calc_sure_bound_from_sources g (key as (_, btype)) =  | 
|
124  | 
let  | 
|
125  | 
fun mult_upper x (lower, upper) =  | 
|
| 23520 | 126  | 
if Float.sign x = LESS then  | 
| 22964 | 127  | 
Float.mult x lower  | 
| 22951 | 128  | 
else  | 
| 22964 | 129  | 
Float.mult x upper  | 
| 16784 | 130  | 
|
| 22951 | 131  | 
fun mult_lower x (lower, upper) =  | 
| 23520 | 132  | 
if Float.sign x = LESS then  | 
| 22964 | 133  | 
Float.mult x upper  | 
| 22951 | 134  | 
else  | 
| 22964 | 135  | 
Float.mult x lower  | 
| 22951 | 136  | 
|
137  | 
val mult_btype = case btype of UPPER => mult_upper | LOWER => mult_lower  | 
|
| 16784 | 138  | 
|
| 22951 | 139  | 
fun calc_sure_bound (row_index, (row_bound, sources)) sure_bound =  | 
140  | 
let  | 
|
141  | 
fun add_src_bound (coeff, src_key) sum =  | 
|
142  | 
case sum of  | 
|
143  | 
NONE => NONE  | 
|
144  | 
| SOME x =>  | 
|
145  | 
(case get_sure_bound g src_key of  | 
|
146  | 
NONE => NONE  | 
|
| 22964 | 147  | 
| SOME src_sure_bound => SOME (Float.add x (mult_btype src_sure_bound coeff)))  | 
| 22951 | 148  | 
in  | 
149  | 
case fold add_src_bound sources (SOME row_bound) of  | 
|
150  | 
NONE => sure_bound  | 
|
151  | 
| new_sure_bound as (SOME new_bound) =>  | 
|
152  | 
(case sure_bound of  | 
|
153  | 
NONE => new_sure_bound  | 
|
154  | 
| SOME old_bound =>  | 
|
155  | 
SOME (case btype of  | 
|
| 22964 | 156  | 
UPPER => Float.min old_bound new_bound  | 
157  | 
| LOWER => Float.max old_bound new_bound))  | 
|
| 22951 | 158  | 
end  | 
159  | 
in  | 
|
160  | 
case VarGraph.lookup g key of  | 
|
161  | 
NONE => NONE  | 
|
162  | 
| SOME (sure_bound, f) =>  | 
|
163  | 
let  | 
|
164  | 
val x = Inttab.fold calc_sure_bound f sure_bound  | 
|
165  | 
in  | 
|
166  | 
if x = sure_bound then NONE else x  | 
|
167  | 
end  | 
|
168  | 
end  | 
|
| 16784 | 169  | 
|
| 22951 | 170  | 
fun propagate (key, _) (g, b) =  | 
171  | 
case calc_sure_bound_from_sources g key of  | 
|
172  | 
NONE => (g,b)  | 
|
173  | 
| SOME bound => (update_sure_bound g key bound,  | 
|
174  | 
if safe then  | 
|
175  | 
case get_sure_bound g key of  | 
|
176  | 
NONE => true  | 
|
177  | 
| _ => b  | 
|
178  | 
else  | 
|
179  | 
true)  | 
|
| 16784 | 180  | 
|
| 22951 | 181  | 
val (g, b) = VarGraph.fold propagate g (g, false)  | 
| 16784 | 182  | 
in  | 
| 22951 | 183  | 
if b then propagate_sure_bounds safe names g else g  | 
184  | 
end  | 
|
185  | 
||
| 16784 | 186  | 
exception Load of string;  | 
187  | 
||
| 
23665
 
825bea0266db
adopted to new computing oracle and fixed bugs introduced by tuning
 
obua 
parents: 
23520 
diff
changeset
 | 
188  | 
val empty_spvec = @{term "Nil :: real spvec"};
 | 
| 
 
825bea0266db
adopted to new computing oracle and fixed bugs introduced by tuning
 
obua 
parents: 
23520 
diff
changeset
 | 
189  | 
fun cons_spvec x xs = @{term "Cons :: nat * real => real spvec => real spvec"} $ x $ xs;
 | 
| 
 
825bea0266db
adopted to new computing oracle and fixed bugs introduced by tuning
 
obua 
parents: 
23520 
diff
changeset
 | 
190  | 
val empty_spmat = @{term "Nil :: real spmat"};
 | 
| 
 
825bea0266db
adopted to new computing oracle and fixed bugs introduced by tuning
 
obua 
parents: 
23520 
diff
changeset
 | 
191  | 
fun cons_spmat x xs = @{term "Cons :: nat * real spvec => real spmat => real spmat"} $ x $ xs;
 | 
| 22964 | 192  | 
|
| 22951 | 193  | 
fun calcr safe_propagation xlen names prec A b =  | 
| 16784 | 194  | 
let  | 
| 22951 | 195  | 
val empty = Inttab.empty  | 
196  | 
||
197  | 
fun instab t i x = Inttab.update (i, x) t  | 
|
198  | 
||
199  | 
fun isnegstr x = String.isPrefix "-" x  | 
|
200  | 
fun negstr x = if isnegstr x then String.extract (x, 1, NONE) else "-"^x  | 
|
| 16784 | 201  | 
|
| 22951 | 202  | 
fun test_1 (lower, upper) =  | 
203  | 
if lower = upper then  | 
|
| 23297 | 204  | 
(if Float.eq (lower, (~1, 0)) then ~1  | 
205  | 
else if Float.eq (lower, (1, 0)) then 1  | 
|
| 22951 | 206  | 
else 0)  | 
207  | 
else 0  | 
|
| 16784 | 208  | 
|
| 22951 | 209  | 
fun calcr (row_index, a) g =  | 
210  | 
let  | 
|
211  | 
val b = FloatSparseMatrixBuilder.v_elem_at b row_index  | 
|
| 22964 | 212  | 
val (_, b2) = FloatArith.approx_decstr_by_bin prec (case b of NONE => "0" | SOME b => b)  | 
| 22951 | 213  | 
val approx_a = FloatSparseMatrixBuilder.v_fold (fn (i, s) => fn l =>  | 
| 22964 | 214  | 
(i, FloatArith.approx_decstr_by_bin prec s)::l) a []  | 
| 16784 | 215  | 
|
| 22951 | 216  | 
fun fold_dest_nodes (dest_index, dest_value) g =  | 
217  | 
let  | 
|
218  | 
val dest_test = test_1 dest_value  | 
|
219  | 
in  | 
|
220  | 
if dest_test = 0 then  | 
|
221  | 
g  | 
|
222  | 
else let  | 
|
223  | 
val (dest_key as (_, dest_btype), row_bound) =  | 
|
224  | 
if dest_test = ~1 then  | 
|
| 22964 | 225  | 
((dest_index, LOWER), Float.neg b2)  | 
| 22951 | 226  | 
else  | 
227  | 
((dest_index, UPPER), b2)  | 
|
| 16784 | 228  | 
|
| 22951 | 229  | 
fun fold_src_nodes (src_index, src_value as (src_lower, src_upper)) g =  | 
230  | 
if src_index = dest_index then g  | 
|
231  | 
else  | 
|
232  | 
let  | 
|
233  | 
val coeff = case dest_btype of  | 
|
| 22964 | 234  | 
UPPER => (Float.neg src_upper, Float.neg src_lower)  | 
| 22951 | 235  | 
| LOWER => src_value  | 
236  | 
in  | 
|
| 23520 | 237  | 
if Float.sign src_lower = LESS then  | 
| 22951 | 238  | 
add_edge g (src_index, UPPER) dest_key row_index coeff  | 
239  | 
else  | 
|
240  | 
add_edge g (src_index, LOWER) dest_key row_index coeff  | 
|
241  | 
end  | 
|
242  | 
in  | 
|
243  | 
fold fold_src_nodes approx_a (add_row_bound g dest_key row_index row_bound)  | 
|
244  | 
end  | 
|
245  | 
end  | 
|
246  | 
in  | 
|
247  | 
case approx_a of  | 
|
248  | 
[] => g  | 
|
249  | 
| [(u, a)] =>  | 
|
250  | 
let  | 
|
251  | 
val atest = test_1 a  | 
|
252  | 
in  | 
|
253  | 
if atest = ~1 then  | 
|
| 22964 | 254  | 
update_sure_bound g (u, LOWER) (Float.neg b2)  | 
| 22951 | 255  | 
else if atest = 1 then  | 
256  | 
update_sure_bound g (u, UPPER) b2  | 
|
257  | 
else  | 
|
258  | 
g  | 
|
259  | 
end  | 
|
260  | 
| _ => fold fold_dest_nodes approx_a g  | 
|
261  | 
end  | 
|
| 16784 | 262  | 
|
| 22951 | 263  | 
val g = FloatSparseMatrixBuilder.m_fold calcr A VarGraph.empty  | 
| 
23665
 
825bea0266db
adopted to new computing oracle and fixed bugs introduced by tuning
 
obua 
parents: 
23520 
diff
changeset
 | 
264  | 
|
| 22951 | 265  | 
val g = propagate_sure_bounds safe_propagation names g  | 
266  | 
||
| 
23665
 
825bea0266db
adopted to new computing oracle and fixed bugs introduced by tuning
 
obua 
parents: 
23520 
diff
changeset
 | 
267  | 
val (r1, r2) = split_graph g  | 
| 22951 | 268  | 
|
| 24302 | 269  | 
fun add_row_entry m index f vname value =  | 
| 22951 | 270  | 
let  | 
| 24302 | 271  | 
val v = (case value of  | 
272  | 
SOME value => FloatSparseMatrixBuilder.mk_spvec_entry 0 value  | 
|
273  | 
| NONE => FloatSparseMatrixBuilder.mk_spvec_entry' 0 (f $ (Var ((vname,0), HOLogic.realT))))  | 
|
274  | 
val vec = cons_spvec v empty_spvec  | 
|
| 22951 | 275  | 
in  | 
| 22964 | 276  | 
cons_spmat (FloatSparseMatrixBuilder.mk_spmat_entry index vec) m  | 
| 22951 | 277  | 
end  | 
| 16784 | 278  | 
|
| 22951 | 279  | 
fun abs_estimate i r1 r2 =  | 
280  | 
if i = 0 then  | 
|
| 24302 | 281  | 
let val e = empty_spmat in (e, e) end  | 
| 22951 | 282  | 
else  | 
283  | 
let  | 
|
284  | 
val index = xlen-i  | 
|
| 24302 | 285  | 
val (r12_1, r12_2) = abs_estimate (i-1) r1 r2  | 
286  | 
val b1 = Inttab.lookup r1 index  | 
|
| 
24630
 
351a308ab58d
simplified type int (eliminated IntInf.int, integer);
 
wenzelm 
parents: 
24302 
diff
changeset
 | 
287  | 
val b2 = Inttab.lookup r2 index  | 
| 22951 | 288  | 
in  | 
| 
24630
 
351a308ab58d
simplified type int (eliminated IntInf.int, integer);
 
wenzelm 
parents: 
24302 
diff
changeset
 | 
289  | 
                    (add_row_entry r12_1 index @{term "lbound :: real => real"} ((names index)^"l") b1, 
 | 
| 
 
351a308ab58d
simplified type int (eliminated IntInf.int, integer);
 
wenzelm 
parents: 
24302 
diff
changeset
 | 
290  | 
		     add_row_entry r12_2 index @{term "ubound :: real => real"} ((names index)^"u") b2)
 | 
| 22951 | 291  | 
end  | 
| 
23665
 
825bea0266db
adopted to new computing oracle and fixed bugs introduced by tuning
 
obua 
parents: 
23520 
diff
changeset
 | 
292  | 
|
| 24302 | 293  | 
val (r1, r2) = abs_estimate xlen r1 r2  | 
| 
23665
 
825bea0266db
adopted to new computing oracle and fixed bugs introduced by tuning
 
obua 
parents: 
23520 
diff
changeset
 | 
294  | 
|
| 16784 | 295  | 
in  | 
| 24302 | 296  | 
(r1, r2)  | 
| 16784 | 297  | 
end  | 
| 22951 | 298  | 
|
| 16784 | 299  | 
fun load filename prec safe_propagation =  | 
300  | 
let  | 
|
| 22951 | 301  | 
val prog = Cplex.load_cplexFile filename  | 
302  | 
val prog = Cplex.elim_nonfree_bounds prog  | 
|
303  | 
val prog = Cplex.relax_strict_ineqs prog  | 
|
| 
23665
 
825bea0266db
adopted to new computing oracle and fixed bugs introduced by tuning
 
obua 
parents: 
23520 
diff
changeset
 | 
304  | 
val (maximize, c, A, b, (xlen, names, _)) = CplexFloatSparseMatrixConverter.convert_prog prog  | 
| 24302 | 305  | 
val (r1, r2) = calcr safe_propagation xlen names prec A b  | 
| 22951 | 306  | 
val _ = if maximize then () else raise Load "sorry, cannot handle minimization problems"  | 
307  | 
val (dualprog, indexof) = FloatSparseMatrixBuilder.dual_cplexProg c A b  | 
|
308  | 
val results = Cplex.solve dualprog  | 
|
309  | 
val (optimal,v) = CplexFloatSparseMatrixConverter.convert_results results indexof  | 
|
| 24302 | 310  | 
(*val A = FloatSparseMatrixBuilder.cut_matrix v NONE A*)  | 
| 22951 | 311  | 
fun id x = x  | 
312  | 
val v = FloatSparseMatrixBuilder.set_vector FloatSparseMatrixBuilder.empty_matrix 0 v  | 
|
313  | 
val b = FloatSparseMatrixBuilder.transpose_matrix (FloatSparseMatrixBuilder.set_vector FloatSparseMatrixBuilder.empty_matrix 0 b)  | 
|
314  | 
val c = FloatSparseMatrixBuilder.set_vector FloatSparseMatrixBuilder.empty_matrix 0 c  | 
|
| 22964 | 315  | 
val (y1, _) = FloatSparseMatrixBuilder.approx_matrix prec Float.positive_part v  | 
| 22951 | 316  | 
val A = FloatSparseMatrixBuilder.approx_matrix prec id A  | 
317  | 
val (_,b2) = FloatSparseMatrixBuilder.approx_matrix prec id b  | 
|
318  | 
val c = FloatSparseMatrixBuilder.approx_matrix prec id c  | 
|
| 16784 | 319  | 
in  | 
| 24302 | 320  | 
(y1, A, b2, c, (r1, r2))  | 
| 22951 | 321  | 
    end handle CplexFloatSparseMatrixConverter.Converter s => (raise (Load ("Converter: "^s)))
 | 
| 16784 | 322  | 
|
323  | 
end  |