15178
|
1 |
signature CPLEX =
|
|
2 |
sig
|
|
3 |
|
|
4 |
datatype cplexTerm = cplexVar of string | cplexNum of string | cplexInf
|
|
5 |
| cplexNeg of cplexTerm
|
|
6 |
| cplexProd of cplexTerm * cplexTerm
|
|
7 |
| cplexSum of (cplexTerm list)
|
|
8 |
|
|
9 |
datatype cplexComp = cplexLe | cplexLeq | cplexEq | cplexGe | cplexGeq
|
|
10 |
|
|
11 |
datatype cplexGoal = cplexMinimize of cplexTerm
|
|
12 |
| cplexMaximize of cplexTerm
|
|
13 |
|
|
14 |
datatype cplexConstr = cplexConstr of cplexComp *
|
|
15 |
(cplexTerm * cplexTerm)
|
|
16 |
|
|
17 |
datatype cplexBounds = cplexBounds of cplexTerm * cplexComp * cplexTerm
|
|
18 |
* cplexComp * cplexTerm
|
|
19 |
| cplexBound of cplexTerm * cplexComp * cplexTerm
|
|
20 |
|
|
21 |
datatype cplexProg = cplexProg of string
|
|
22 |
* cplexGoal
|
|
23 |
* ((string option * cplexConstr)
|
|
24 |
list)
|
|
25 |
* cplexBounds list
|
|
26 |
|
|
27 |
datatype cplexResult = Unbounded
|
|
28 |
| Infeasible
|
|
29 |
| Undefined
|
|
30 |
| Optimal of string *
|
|
31 |
(((* name *) string *
|
|
32 |
(* value *) string) list)
|
|
33 |
|
|
34 |
exception Load_cplexFile of string
|
|
35 |
exception Load_cplexResult of string
|
|
36 |
exception Save_cplexFile of string
|
|
37 |
exception Execute of string
|
|
38 |
|
|
39 |
val load_cplexFile : string -> cplexProg
|
|
40 |
|
|
41 |
val save_cplexFile : string -> cplexProg -> unit
|
|
42 |
|
|
43 |
val elim_nonfree_bounds : cplexProg -> cplexProg
|
|
44 |
|
|
45 |
val relax_strict_ineqs : cplexProg -> cplexProg
|
|
46 |
|
|
47 |
val is_normed_cplexProg : cplexProg -> bool
|
|
48 |
|
|
49 |
val load_cplexResult : string -> cplexResult
|
|
50 |
|
|
51 |
val solve : cplexProg -> cplexResult
|
|
52 |
end;
|
|
53 |
|
|
54 |
structure Cplex : CPLEX =
|
|
55 |
struct
|
|
56 |
|
|
57 |
exception Load_cplexFile of string;
|
|
58 |
exception Load_cplexResult of string;
|
|
59 |
exception Save_cplexFile of string;
|
|
60 |
|
|
61 |
datatype cplexTerm = cplexVar of string
|
|
62 |
| cplexNum of string
|
|
63 |
| cplexInf
|
|
64 |
| cplexNeg of cplexTerm
|
|
65 |
| cplexProd of cplexTerm * cplexTerm
|
|
66 |
| cplexSum of (cplexTerm list)
|
|
67 |
datatype cplexComp = cplexLe | cplexLeq | cplexEq | cplexGe | cplexGeq
|
|
68 |
datatype cplexGoal = cplexMinimize of cplexTerm | cplexMaximize of cplexTerm
|
|
69 |
datatype cplexConstr = cplexConstr of cplexComp * (cplexTerm * cplexTerm)
|
|
70 |
datatype cplexBounds = cplexBounds of cplexTerm * cplexComp * cplexTerm
|
|
71 |
* cplexComp * cplexTerm
|
|
72 |
| cplexBound of cplexTerm * cplexComp * cplexTerm
|
|
73 |
datatype cplexProg = cplexProg of string
|
|
74 |
* cplexGoal
|
|
75 |
* ((string option * cplexConstr) list)
|
|
76 |
* cplexBounds list
|
|
77 |
|
|
78 |
fun rev_cmp cplexLe = cplexGe
|
|
79 |
| rev_cmp cplexLeq = cplexGeq
|
|
80 |
| rev_cmp cplexGe = cplexLe
|
|
81 |
| rev_cmp cplexGeq = cplexLeq
|
|
82 |
| rev_cmp cplexEq = cplexEq
|
|
83 |
|
15531
|
84 |
fun the NONE = raise (Load_cplexFile "SOME expected")
|
|
85 |
| the (SOME x) = x;
|
15178
|
86 |
|
|
87 |
fun modulo_signed is_something (cplexNeg u) = is_something u
|
|
88 |
| modulo_signed is_something u = is_something u
|
|
89 |
|
|
90 |
fun is_Num (cplexNum _) = true
|
|
91 |
| is_Num _ = false
|
|
92 |
|
|
93 |
fun is_Inf cplexInf = true
|
|
94 |
| is_Inf _ = false
|
|
95 |
|
|
96 |
fun is_Var (cplexVar _) = true
|
|
97 |
| is_Var _ = false
|
|
98 |
|
|
99 |
fun is_Neg (cplexNeg x ) = true
|
|
100 |
| is_Neg _ = false
|
|
101 |
|
|
102 |
fun is_normed_Prod (cplexProd (t1, t2)) =
|
|
103 |
(is_Num t1) andalso (is_Var t2)
|
|
104 |
| is_normed_Prod x = is_Var x
|
|
105 |
|
|
106 |
fun is_normed_Sum (cplexSum ts) =
|
|
107 |
(ts <> []) andalso forall (modulo_signed is_normed_Prod) ts
|
|
108 |
| is_normed_Sum x = modulo_signed is_normed_Prod x
|
|
109 |
|
|
110 |
fun is_normed_Constr (cplexConstr (c, (t1, t2))) =
|
|
111 |
(is_normed_Sum t1) andalso (modulo_signed is_Num t2)
|
|
112 |
|
|
113 |
fun is_Num_or_Inf x = is_Inf x orelse is_Num x
|
|
114 |
|
|
115 |
fun is_normed_Bounds (cplexBounds (t1, c1, t2, c2, t3)) =
|
|
116 |
(c1 = cplexLe orelse c1 = cplexLeq) andalso
|
|
117 |
(c2 = cplexLe orelse c2 = cplexLeq) andalso
|
|
118 |
is_Var t2 andalso
|
|
119 |
modulo_signed is_Num_or_Inf t1 andalso
|
|
120 |
modulo_signed is_Num_or_Inf t3
|
|
121 |
| is_normed_Bounds (cplexBound (t1, c, t2)) =
|
|
122 |
(is_Var t1 andalso (modulo_signed is_Num_or_Inf t2))
|
|
123 |
orelse
|
|
124 |
(c <> cplexEq andalso
|
|
125 |
is_Var t2 andalso (modulo_signed is_Num_or_Inf t1))
|
|
126 |
|
|
127 |
fun term_of_goal (cplexMinimize x) = x
|
|
128 |
| term_of_goal (cplexMaximize x) = x
|
|
129 |
|
|
130 |
fun is_normed_cplexProg (cplexProg (name, goal, constraints, bounds)) =
|
|
131 |
is_normed_Sum (term_of_goal goal) andalso
|
|
132 |
forall (fn (_,x) => is_normed_Constr x) constraints andalso
|
|
133 |
forall is_normed_Bounds bounds
|
|
134 |
|
|
135 |
fun is_NL s = s = "\n"
|
|
136 |
|
|
137 |
fun is_blank s = forall (fn c => c <> #"\n" andalso Char.isSpace c) (String.explode s)
|
|
138 |
|
|
139 |
fun is_num a =
|
|
140 |
let
|
|
141 |
val b = String.explode a
|
|
142 |
fun num4 cs = forall Char.isDigit cs
|
|
143 |
fun num3 [] = true
|
|
144 |
| num3 (ds as (c::cs)) =
|
|
145 |
if c = #"+" orelse c = #"-" then
|
|
146 |
num4 cs
|
|
147 |
else
|
|
148 |
num4 ds
|
|
149 |
fun num2 [] = true
|
|
150 |
| num2 (c::cs) =
|
|
151 |
if c = #"e" orelse c = #"E" then num3 cs
|
|
152 |
else (Char.isDigit c) andalso num2 cs
|
|
153 |
fun num1 [] = true
|
|
154 |
| num1 (c::cs) =
|
|
155 |
if c = #"." then num2 cs
|
|
156 |
else if c = #"e" orelse c = #"E" then num3 cs
|
|
157 |
else (Char.isDigit c) andalso num1 cs
|
|
158 |
fun num [] = true
|
|
159 |
| num (c::cs) =
|
|
160 |
if c = #"." then num2 cs
|
|
161 |
else (Char.isDigit c) andalso num1 cs
|
|
162 |
in
|
|
163 |
num b
|
|
164 |
end
|
|
165 |
|
|
166 |
fun is_delimiter s = s = "+" orelse s = "-" orelse s = ":"
|
|
167 |
|
|
168 |
fun is_cmp s = s = "<" orelse s = ">" orelse s = "<="
|
|
169 |
orelse s = ">=" orelse s = "="
|
|
170 |
|
|
171 |
fun is_symbol a =
|
|
172 |
let
|
|
173 |
val symbol_char = String.explode "!\"#$%&()/,.;?@_`'{}|~"
|
|
174 |
fun is_symbol_char c = Char.isAlphaNum c orelse
|
|
175 |
exists (fn d => d=c) symbol_char
|
|
176 |
fun is_symbol_start c = is_symbol_char c andalso
|
|
177 |
not (Char.isDigit c) andalso
|
|
178 |
not (c= #".")
|
|
179 |
val b = String.explode a
|
|
180 |
in
|
|
181 |
b <> [] andalso is_symbol_start (hd b) andalso
|
|
182 |
forall is_symbol_char b
|
|
183 |
end
|
|
184 |
|
|
185 |
fun to_upper s = String.implode (map Char.toUpper (String.explode s))
|
|
186 |
|
|
187 |
fun keyword x =
|
|
188 |
let
|
|
189 |
val a = to_upper x
|
|
190 |
in
|
|
191 |
if a = "BOUNDS" orelse a = "BOUND" then
|
15531
|
192 |
SOME "BOUNDS"
|
15178
|
193 |
else if a = "MINIMIZE" orelse a = "MINIMUM" orelse a = "MIN" then
|
15531
|
194 |
SOME "MINIMIZE"
|
15178
|
195 |
else if a = "MAXIMIZE" orelse a = "MAXIMUM" orelse a = "MAX" then
|
15531
|
196 |
SOME "MAXIMIZE"
|
15178
|
197 |
else if a = "ST" orelse a = "S.T." orelse a = "ST." then
|
15531
|
198 |
SOME "ST"
|
15178
|
199 |
else if a = "FREE" orelse a = "END" then
|
15531
|
200 |
SOME a
|
15178
|
201 |
else if a = "GENERAL" orelse a = "GENERALS" orelse a = "GEN" then
|
15531
|
202 |
SOME "GENERAL"
|
15178
|
203 |
else if a = "INTEGER" orelse a = "INTEGERS" orelse a = "INT" then
|
15531
|
204 |
SOME "INTEGER"
|
15178
|
205 |
else if a = "BINARY" orelse a = "BINARIES" orelse a = "BIN" then
|
15531
|
206 |
SOME "BINARY"
|
15178
|
207 |
else if a = "INF" orelse a = "INFINITY" then
|
15531
|
208 |
SOME "INF"
|
15178
|
209 |
else
|
15531
|
210 |
NONE
|
15178
|
211 |
end
|
|
212 |
|
|
213 |
val TOKEN_ERROR = ~1
|
|
214 |
val TOKEN_BLANK = 0
|
|
215 |
val TOKEN_NUM = 1
|
|
216 |
val TOKEN_DELIMITER = 2
|
|
217 |
val TOKEN_SYMBOL = 3
|
|
218 |
val TOKEN_LABEL = 4
|
|
219 |
val TOKEN_CMP = 5
|
|
220 |
val TOKEN_KEYWORD = 6
|
|
221 |
val TOKEN_NL = 7
|
|
222 |
|
|
223 |
(* tokenize takes a list of chars as argument and returns a list of
|
|
224 |
int * string pairs, each string representing a "cplex token",
|
|
225 |
and each int being one of TOKEN_NUM, TOKEN_DELIMITER, TOKEN_CMP
|
|
226 |
or TOKEN_SYMBOL *)
|
|
227 |
fun tokenize s =
|
|
228 |
let
|
|
229 |
val flist = [(is_NL, TOKEN_NL),
|
|
230 |
(is_blank, TOKEN_BLANK),
|
|
231 |
(is_num, TOKEN_NUM),
|
|
232 |
(is_delimiter, TOKEN_DELIMITER),
|
|
233 |
(is_cmp, TOKEN_CMP),
|
|
234 |
(is_symbol, TOKEN_SYMBOL)]
|
|
235 |
fun match_helper [] s = (fn x => false, TOKEN_ERROR)
|
|
236 |
| match_helper (f::fs) s =
|
|
237 |
if ((fst f) s) then f else match_helper fs s
|
|
238 |
fun match s = match_helper flist s
|
|
239 |
fun tok s =
|
|
240 |
if s = "" then [] else
|
|
241 |
let
|
|
242 |
val h = String.substring (s,0,1)
|
|
243 |
val (f, j) = match h
|
|
244 |
fun len i =
|
|
245 |
if size s = i then i
|
|
246 |
else if f (String.substring (s,0,i+1)) then
|
|
247 |
len (i+1)
|
|
248 |
else i
|
|
249 |
in
|
|
250 |
if j < 0 then
|
|
251 |
(if h = "\\" then []
|
|
252 |
else raise (Load_cplexFile ("token expected, found: "
|
|
253 |
^s)))
|
|
254 |
else
|
|
255 |
let
|
|
256 |
val l = len 1
|
|
257 |
val u = String.substring (s,0,l)
|
|
258 |
val v = String.extract (s,l,NONE)
|
|
259 |
in
|
|
260 |
if j = 0 then tok v else (j, u) :: tok v
|
|
261 |
end
|
|
262 |
end
|
|
263 |
in
|
|
264 |
tok s
|
|
265 |
end
|
|
266 |
|
|
267 |
exception Tokenize of string;
|
|
268 |
|
|
269 |
fun tokenize_general flist s =
|
|
270 |
let
|
|
271 |
fun match_helper [] s = raise (Tokenize s)
|
|
272 |
| match_helper (f::fs) s =
|
|
273 |
if ((fst f) s) then f else match_helper fs s
|
|
274 |
fun match s = match_helper flist s
|
|
275 |
fun tok s =
|
|
276 |
if s = "" then [] else
|
|
277 |
let
|
|
278 |
val h = String.substring (s,0,1)
|
|
279 |
val (f, j) = match h
|
|
280 |
fun len i =
|
|
281 |
if size s = i then i
|
|
282 |
else if f (String.substring (s,0,i+1)) then
|
|
283 |
len (i+1)
|
|
284 |
else i
|
|
285 |
val l = len 1
|
|
286 |
in
|
|
287 |
(j, String.substring (s,0,l)) :: tok (String.extract (s,l,NONE))
|
|
288 |
end
|
|
289 |
in
|
|
290 |
tok s
|
|
291 |
end
|
|
292 |
|
|
293 |
fun load_cplexFile name =
|
|
294 |
let
|
|
295 |
val f = TextIO.openIn name
|
|
296 |
val ignore_NL = ref true
|
|
297 |
val rest = ref []
|
|
298 |
|
|
299 |
fun is_symbol s c = (fst c) = TOKEN_SYMBOL andalso (to_upper (snd c)) = s
|
|
300 |
|
|
301 |
fun readToken_helper () =
|
|
302 |
if length (!rest) > 0 then
|
|
303 |
let val u = hd (!rest) in
|
|
304 |
(
|
|
305 |
rest := tl (!rest);
|
15531
|
306 |
SOME u
|
15178
|
307 |
)
|
|
308 |
end
|
|
309 |
else
|
|
310 |
let val s = TextIO.inputLine f in
|
15531
|
311 |
if s = "" then NONE else
|
15178
|
312 |
let val t = tokenize s in
|
|
313 |
if (length t >= 2 andalso
|
|
314 |
snd(hd (tl t)) = ":")
|
|
315 |
then
|
|
316 |
rest := (TOKEN_LABEL, snd (hd t)) :: (tl (tl t))
|
|
317 |
else if (length t >= 2) andalso is_symbol "SUBJECT" (hd (t))
|
|
318 |
andalso is_symbol "TO" (hd (tl t))
|
|
319 |
then
|
|
320 |
rest := (TOKEN_SYMBOL, "ST") :: (tl (tl t))
|
|
321 |
else
|
|
322 |
rest := t;
|
|
323 |
readToken_helper ()
|
|
324 |
end
|
|
325 |
end
|
|
326 |
|
|
327 |
fun readToken_helper2 () =
|
|
328 |
let val c = readToken_helper () in
|
15531
|
329 |
if c = NONE then NONE
|
15178
|
330 |
else if !ignore_NL andalso fst (the c) = TOKEN_NL then
|
|
331 |
readToken_helper2 ()
|
|
332 |
else if fst (the c) = TOKEN_SYMBOL
|
15531
|
333 |
andalso keyword (snd (the c)) <> NONE
|
|
334 |
then SOME (TOKEN_KEYWORD, the (keyword (snd (the c))))
|
15178
|
335 |
else c
|
|
336 |
end
|
|
337 |
|
|
338 |
fun readToken () = readToken_helper2 ()
|
|
339 |
|
|
340 |
fun pushToken a = rest := (a::(!rest))
|
|
341 |
|
|
342 |
fun is_value token =
|
|
343 |
fst token = TOKEN_NUM orelse (fst token = TOKEN_KEYWORD
|
|
344 |
andalso snd token = "INF")
|
|
345 |
|
|
346 |
fun get_value token =
|
|
347 |
if fst token = TOKEN_NUM then
|
|
348 |
cplexNum (snd token)
|
|
349 |
else if fst token = TOKEN_KEYWORD andalso snd token = "INF"
|
|
350 |
then
|
|
351 |
cplexInf
|
|
352 |
else
|
|
353 |
raise (Load_cplexFile "num expected")
|
|
354 |
|
|
355 |
fun readTerm_Product only_num =
|
|
356 |
let val c = readToken () in
|
15531
|
357 |
if c = NONE then NONE
|
15178
|
358 |
else if fst (the c) = TOKEN_SYMBOL
|
|
359 |
then (
|
15531
|
360 |
if only_num then (pushToken (the c); NONE)
|
|
361 |
else SOME (cplexVar (snd (the c)))
|
15178
|
362 |
)
|
|
363 |
else if only_num andalso is_value (the c) then
|
15531
|
364 |
SOME (get_value (the c))
|
15178
|
365 |
else if is_value (the c) then
|
|
366 |
let val t1 = get_value (the c)
|
|
367 |
val d = readToken ()
|
|
368 |
in
|
15531
|
369 |
if d = NONE then SOME t1
|
15178
|
370 |
else if fst (the d) = TOKEN_SYMBOL then
|
15531
|
371 |
SOME (cplexProd (t1, cplexVar (snd (the d))))
|
15178
|
372 |
else
|
15531
|
373 |
(pushToken (the d); SOME t1)
|
15178
|
374 |
end
|
15531
|
375 |
else (pushToken (the c); NONE)
|
15178
|
376 |
end
|
|
377 |
|
|
378 |
fun readTerm_Signed only_signed only_num =
|
|
379 |
let
|
|
380 |
val c = readToken ()
|
|
381 |
in
|
15531
|
382 |
if c = NONE then NONE
|
15178
|
383 |
else
|
|
384 |
let val d = the c in
|
|
385 |
if d = (TOKEN_DELIMITER, "+") then
|
|
386 |
readTerm_Product only_num
|
|
387 |
else if d = (TOKEN_DELIMITER, "-") then
|
15531
|
388 |
SOME (cplexNeg (the (readTerm_Product
|
15178
|
389 |
only_num)))
|
|
390 |
else (pushToken d;
|
15531
|
391 |
if only_signed then NONE
|
15178
|
392 |
else readTerm_Product only_num)
|
|
393 |
end
|
|
394 |
end
|
|
395 |
|
|
396 |
fun readTerm_Sum first_signed =
|
|
397 |
let val c = readTerm_Signed first_signed false in
|
15531
|
398 |
if c = NONE then [] else (the c)::(readTerm_Sum true)
|
15178
|
399 |
end
|
|
400 |
|
|
401 |
fun readTerm () =
|
|
402 |
let val c = readTerm_Sum false in
|
15531
|
403 |
if c = [] then NONE
|
|
404 |
else if tl c = [] then SOME (hd c)
|
|
405 |
else SOME (cplexSum c)
|
15178
|
406 |
end
|
|
407 |
|
|
408 |
fun readLabeledTerm () =
|
|
409 |
let val c = readToken () in
|
15531
|
410 |
if c = NONE then (NONE, NONE)
|
15178
|
411 |
else if fst (the c) = TOKEN_LABEL then
|
|
412 |
let val t = readTerm () in
|
15531
|
413 |
if t = NONE then
|
15178
|
414 |
raise (Load_cplexFile ("term after label "^
|
|
415 |
(snd (the c))^
|
|
416 |
" expected"))
|
15531
|
417 |
else (SOME (snd (the c)), t)
|
15178
|
418 |
end
|
15531
|
419 |
else (pushToken (the c); (NONE, readTerm ()))
|
15178
|
420 |
end
|
|
421 |
|
|
422 |
fun readGoal () =
|
|
423 |
let
|
|
424 |
val g = readToken ()
|
|
425 |
in
|
15531
|
426 |
if g = SOME (TOKEN_KEYWORD, "MAXIMIZE") then
|
15178
|
427 |
cplexMaximize (the (snd (readLabeledTerm ())))
|
15531
|
428 |
else if g = SOME (TOKEN_KEYWORD, "MINIMIZE") then
|
15178
|
429 |
cplexMinimize (the (snd (readLabeledTerm ())))
|
|
430 |
else raise (Load_cplexFile "MAXIMIZE or MINIMIZE expected")
|
|
431 |
end
|
|
432 |
|
|
433 |
fun str2cmp b =
|
|
434 |
(case b of
|
|
435 |
"<" => cplexLe
|
|
436 |
| "<=" => cplexLeq
|
|
437 |
| ">" => cplexGe
|
|
438 |
| ">=" => cplexGeq
|
|
439 |
| "=" => cplexEq
|
|
440 |
| _ => raise (Load_cplexFile (b^" is no TOKEN_CMP")))
|
|
441 |
|
|
442 |
fun readConstraint () =
|
|
443 |
let
|
|
444 |
val t = readLabeledTerm ()
|
|
445 |
fun make_constraint b t1 t2 =
|
|
446 |
cplexConstr
|
|
447 |
(str2cmp b,
|
|
448 |
(t1, t2))
|
|
449 |
in
|
15531
|
450 |
if snd t = NONE then NONE
|
15178
|
451 |
else
|
|
452 |
let val c = readToken () in
|
15531
|
453 |
if c = NONE orelse fst (the c) <> TOKEN_CMP
|
15178
|
454 |
then raise (Load_cplexFile "TOKEN_CMP expected")
|
|
455 |
else
|
|
456 |
let val n = readTerm_Signed false true in
|
15531
|
457 |
if n = NONE then
|
15178
|
458 |
raise (Load_cplexFile "num expected")
|
|
459 |
else
|
15531
|
460 |
SOME (fst t,
|
15178
|
461 |
make_constraint (snd (the c))
|
|
462 |
(the (snd t))
|
|
463 |
(the n))
|
|
464 |
end
|
|
465 |
end
|
|
466 |
end
|
|
467 |
|
|
468 |
fun readST () =
|
|
469 |
let
|
|
470 |
fun readbody () =
|
|
471 |
let val t = readConstraint () in
|
15531
|
472 |
if t = NONE then []
|
15178
|
473 |
else if (is_normed_Constr (snd (the t))) then
|
|
474 |
(the t)::(readbody ())
|
15531
|
475 |
else if (fst (the t) <> NONE) then
|
15178
|
476 |
raise (Load_cplexFile
|
|
477 |
("constraint '"^(the (fst (the t)))^
|
|
478 |
"'is not normed"))
|
|
479 |
else
|
|
480 |
raise (Load_cplexFile
|
|
481 |
"constraint is not normed")
|
|
482 |
end
|
|
483 |
in
|
15531
|
484 |
if readToken () = SOME (TOKEN_KEYWORD, "ST")
|
15178
|
485 |
then
|
|
486 |
readbody ()
|
|
487 |
else
|
|
488 |
raise (Load_cplexFile "ST expected")
|
|
489 |
end
|
|
490 |
|
|
491 |
fun readCmp () =
|
|
492 |
let val c = readToken () in
|
15531
|
493 |
if c = NONE then NONE
|
15178
|
494 |
else if fst (the c) = TOKEN_CMP then
|
15531
|
495 |
SOME (str2cmp (snd (the c)))
|
|
496 |
else (pushToken (the c); NONE)
|
15178
|
497 |
end
|
|
498 |
|
|
499 |
fun skip_NL () =
|
|
500 |
let val c = readToken () in
|
15531
|
501 |
if c <> NONE andalso fst (the c) = TOKEN_NL then
|
15178
|
502 |
skip_NL ()
|
|
503 |
else
|
|
504 |
(pushToken (the c); ())
|
|
505 |
end
|
|
506 |
|
|
507 |
fun is_var (cplexVar _) = true
|
|
508 |
| is_var _ = false
|
|
509 |
|
|
510 |
fun make_bounds c t1 t2 =
|
|
511 |
cplexBound (t1, c, t2)
|
|
512 |
|
|
513 |
fun readBound () =
|
|
514 |
let
|
|
515 |
val _ = skip_NL ()
|
|
516 |
val t1 = readTerm ()
|
|
517 |
in
|
15531
|
518 |
if t1 = NONE then NONE
|
15178
|
519 |
else
|
|
520 |
let
|
|
521 |
val c1 = readCmp ()
|
|
522 |
in
|
15531
|
523 |
if c1 = NONE then
|
15178
|
524 |
let
|
|
525 |
val c = readToken ()
|
|
526 |
in
|
15531
|
527 |
if c = SOME (TOKEN_KEYWORD, "FREE") then
|
|
528 |
SOME (
|
15178
|
529 |
cplexBounds (cplexNeg cplexInf,
|
|
530 |
cplexLeq,
|
|
531 |
the t1,
|
|
532 |
cplexLeq,
|
|
533 |
cplexInf))
|
|
534 |
else
|
|
535 |
raise (Load_cplexFile "FREE expected")
|
|
536 |
end
|
|
537 |
else
|
|
538 |
let
|
|
539 |
val t2 = readTerm ()
|
|
540 |
in
|
15531
|
541 |
if t2 = NONE then
|
15178
|
542 |
raise (Load_cplexFile "term expected")
|
|
543 |
else
|
|
544 |
let val c2 = readCmp () in
|
15531
|
545 |
if c2 = NONE then
|
|
546 |
SOME (make_bounds (the c1)
|
15178
|
547 |
(the t1)
|
|
548 |
(the t2))
|
|
549 |
else
|
15531
|
550 |
SOME (
|
15178
|
551 |
cplexBounds (the t1,
|
|
552 |
the c1,
|
|
553 |
the t2,
|
|
554 |
the c2,
|
|
555 |
the (readTerm())))
|
|
556 |
end
|
|
557 |
end
|
|
558 |
end
|
|
559 |
end
|
|
560 |
|
|
561 |
fun readBounds () =
|
|
562 |
let
|
|
563 |
fun makestring b = "?"
|
|
564 |
fun readbody () =
|
|
565 |
let
|
|
566 |
val b = readBound ()
|
|
567 |
in
|
15531
|
568 |
if b = NONE then []
|
15178
|
569 |
else if (is_normed_Bounds (the b)) then
|
|
570 |
(the b)::(readbody())
|
|
571 |
else (
|
|
572 |
raise (Load_cplexFile
|
|
573 |
("bounds are not normed in: "^
|
|
574 |
(makestring (the b)))))
|
|
575 |
end
|
|
576 |
in
|
15531
|
577 |
if readToken () = SOME (TOKEN_KEYWORD, "BOUNDS") then
|
15178
|
578 |
readbody ()
|
|
579 |
else raise (Load_cplexFile "BOUNDS expected")
|
|
580 |
end
|
|
581 |
|
|
582 |
fun readEnd () =
|
15531
|
583 |
if readToken () = SOME (TOKEN_KEYWORD, "END") then ()
|
15178
|
584 |
else raise (Load_cplexFile "END expected")
|
|
585 |
|
|
586 |
val result_Goal = readGoal ()
|
|
587 |
val result_ST = readST ()
|
|
588 |
val _ = ignore_NL := false
|
|
589 |
val result_Bounds = readBounds ()
|
|
590 |
val _ = ignore_NL := true
|
|
591 |
val _ = readEnd ()
|
|
592 |
val _ = TextIO.closeIn f
|
|
593 |
in
|
|
594 |
cplexProg (name, result_Goal, result_ST, result_Bounds)
|
|
595 |
end
|
|
596 |
|
|
597 |
fun save_cplexFile filename (cplexProg (name, goal, constraints, bounds)) =
|
|
598 |
let
|
|
599 |
val f = TextIO.openOut filename
|
|
600 |
|
|
601 |
fun basic_write s = TextIO.output(f, s)
|
|
602 |
|
|
603 |
val linebuf = ref ""
|
|
604 |
fun buf_flushline s =
|
|
605 |
(basic_write (!linebuf);
|
|
606 |
basic_write "\n";
|
|
607 |
linebuf := s)
|
|
608 |
fun buf_add s = linebuf := (!linebuf) ^ s
|
|
609 |
|
|
610 |
fun write s =
|
|
611 |
if (String.size s) + (String.size (!linebuf)) >= 250 then
|
|
612 |
buf_flushline (" "^s)
|
|
613 |
else
|
|
614 |
buf_add s
|
|
615 |
|
|
616 |
fun writeln s = (buf_add s; buf_flushline "")
|
|
617 |
|
|
618 |
fun write_term (cplexVar x) = write x
|
|
619 |
| write_term (cplexNum x) = write x
|
|
620 |
| write_term cplexInf = write "inf"
|
|
621 |
| write_term (cplexProd (cplexNum "1", b)) = write_term b
|
|
622 |
| write_term (cplexProd (a, b)) =
|
|
623 |
(write_term a; write " "; write_term b)
|
|
624 |
| write_term (cplexNeg x) = (write " - "; write_term x)
|
|
625 |
| write_term (cplexSum ts) = write_terms ts
|
|
626 |
and write_terms [] = ()
|
|
627 |
| write_terms (t::ts) =
|
|
628 |
(if (not (is_Neg t)) then write " + " else ();
|
|
629 |
write_term t; write_terms ts)
|
|
630 |
|
|
631 |
fun write_goal (cplexMaximize term) =
|
|
632 |
(writeln "MAXIMIZE"; write_term term; writeln "")
|
|
633 |
| write_goal (cplexMinimize term) =
|
|
634 |
(writeln "MINIMIZE"; write_term term; writeln "")
|
|
635 |
|
|
636 |
fun write_cmp cplexLe = write "<"
|
|
637 |
| write_cmp cplexLeq = write "<="
|
|
638 |
| write_cmp cplexEq = write "="
|
|
639 |
| write_cmp cplexGe = write ">"
|
|
640 |
| write_cmp cplexGeq = write ">="
|
|
641 |
|
|
642 |
fun write_constr (cplexConstr (cmp, (a,b))) =
|
|
643 |
(write_term a;
|
|
644 |
write " ";
|
|
645 |
write_cmp cmp;
|
|
646 |
write " ";
|
|
647 |
write_term b)
|
|
648 |
|
|
649 |
fun write_constraints [] = ()
|
|
650 |
| write_constraints (c::cs) =
|
15531
|
651 |
(if (fst c <> NONE)
|
15178
|
652 |
then
|
|
653 |
(write (the (fst c)); write ": ")
|
|
654 |
else
|
|
655 |
();
|
|
656 |
write_constr (snd c);
|
|
657 |
writeln "";
|
|
658 |
write_constraints cs)
|
|
659 |
|
|
660 |
fun write_bounds [] = ()
|
|
661 |
| write_bounds ((cplexBounds (t1,c1,t2,c2,t3))::bs) =
|
|
662 |
((if t1 = cplexNeg cplexInf andalso t3 = cplexInf
|
|
663 |
andalso (c1 = cplexLeq orelse c1 = cplexLe)
|
|
664 |
andalso (c2 = cplexLeq orelse c2 = cplexLe)
|
|
665 |
then
|
|
666 |
(write_term t2; write " free")
|
|
667 |
else
|
|
668 |
(write_term t1; write " "; write_cmp c1; write " ";
|
|
669 |
write_term t2; write " "; write_cmp c2; write " ";
|
|
670 |
write_term t3)
|
|
671 |
); writeln ""; write_bounds bs)
|
|
672 |
| write_bounds ((cplexBound (t1, c, t2)) :: bs) =
|
|
673 |
(write_term t1; write " ";
|
|
674 |
write_cmp c; write " ";
|
|
675 |
write_term t2; writeln ""; write_bounds bs)
|
|
676 |
|
|
677 |
val _ = write_goal goal
|
|
678 |
val _ = (writeln ""; writeln "ST")
|
|
679 |
val _ = write_constraints constraints
|
|
680 |
val _ = (writeln ""; writeln "BOUNDS")
|
|
681 |
val _ = write_bounds bounds
|
|
682 |
val _ = (writeln ""; writeln "END")
|
|
683 |
val _ = TextIO.closeOut f
|
|
684 |
in
|
|
685 |
()
|
|
686 |
end
|
|
687 |
|
|
688 |
fun norm_Constr (constr as cplexConstr (c, (t1, t2))) =
|
|
689 |
if not (modulo_signed is_Num t2) andalso
|
|
690 |
modulo_signed is_Num t1
|
|
691 |
then
|
|
692 |
[cplexConstr (rev_cmp c, (t2, t1))]
|
|
693 |
else if (c = cplexLe orelse c = cplexLeq) andalso
|
|
694 |
(t1 = (cplexNeg cplexInf) orelse t2 = cplexInf)
|
|
695 |
then
|
|
696 |
[]
|
|
697 |
else if (c = cplexGe orelse c = cplexGeq) andalso
|
|
698 |
(t1 = cplexInf orelse t2 = cplexNeg cplexInf)
|
|
699 |
then
|
|
700 |
[]
|
|
701 |
else
|
|
702 |
[constr]
|
|
703 |
|
|
704 |
fun bound2constr (cplexBounds (t1,c1,t2,c2,t3)) =
|
|
705 |
(norm_Constr(cplexConstr (c1, (t1, t2))))
|
|
706 |
@ (norm_Constr(cplexConstr (c2, (t2, t3))))
|
|
707 |
| bound2constr (cplexBound (t1, cplexEq, t2)) =
|
|
708 |
(norm_Constr(cplexConstr (cplexLeq, (t1, t2))))
|
|
709 |
@ (norm_Constr(cplexConstr (cplexLeq, (t2, t1))))
|
|
710 |
| bound2constr (cplexBound (t1, c1, t2)) =
|
|
711 |
norm_Constr(cplexConstr (c1, (t1,t2)))
|
|
712 |
|
|
713 |
val emptyset = Symtab.empty
|
|
714 |
|
|
715 |
fun singleton v = Symtab.update ((v, ()), emptyset)
|
|
716 |
|
|
717 |
fun merge a b = Symtab.merge (op =) (a, b)
|
|
718 |
|
|
719 |
fun mergemap f ts = foldl
|
|
720 |
(fn (table, x) => merge table (f x))
|
|
721 |
(Symtab.empty, ts)
|
|
722 |
|
|
723 |
fun diff a b = Symtab.foldl (fn (a, (k,v)) =>
|
|
724 |
(Symtab.delete k a) handle UNDEF => a)
|
|
725 |
(a, b)
|
|
726 |
|
|
727 |
fun collect_vars (cplexVar v) = singleton v
|
|
728 |
| collect_vars (cplexNeg t) = collect_vars t
|
|
729 |
| collect_vars (cplexProd (t1, t2)) =
|
|
730 |
merge (collect_vars t1) (collect_vars t2)
|
|
731 |
| collect_vars (cplexSum ts) = mergemap collect_vars ts
|
|
732 |
| collect_vars _ = emptyset
|
|
733 |
|
|
734 |
(* Eliminates all nonfree bounds from the linear program and produces an
|
|
735 |
equivalent program with only free bounds
|
|
736 |
IF for the input program P holds: is_normed_cplexProg P *)
|
|
737 |
fun elim_nonfree_bounds (cplexProg (name, goal, constraints, bounds)) =
|
|
738 |
let
|
|
739 |
fun collect_constr_vars (_, cplexConstr (c, (t1,_))) =
|
|
740 |
(collect_vars t1)
|
|
741 |
|
|
742 |
val cvars = merge (collect_vars (term_of_goal goal))
|
|
743 |
(mergemap collect_constr_vars constraints)
|
|
744 |
|
|
745 |
fun collect_lower_bounded_vars
|
|
746 |
(cplexBounds (t1, c1, cplexVar v, c2, t3)) =
|
|
747 |
singleton v
|
|
748 |
| collect_lower_bounded_vars
|
|
749 |
(cplexBound (_, cplexLe, cplexVar v)) =
|
|
750 |
singleton v
|
|
751 |
| collect_lower_bounded_vars
|
|
752 |
(cplexBound (_, cplexLeq, cplexVar v)) =
|
|
753 |
singleton v
|
|
754 |
| collect_lower_bounded_vars
|
|
755 |
(cplexBound (cplexVar v, cplexGe,_)) =
|
|
756 |
singleton v
|
|
757 |
| collect_lower_bounded_vars
|
|
758 |
(cplexBound (cplexVar v, cplexGeq, _)) =
|
|
759 |
singleton v
|
|
760 |
| collect_lower_bounded_vars
|
|
761 |
(cplexBound (cplexVar v, cplexEq, _)) =
|
|
762 |
singleton v
|
|
763 |
| collect_lower_bounded_vars _ = emptyset
|
|
764 |
|
|
765 |
val lvars = mergemap collect_lower_bounded_vars bounds
|
|
766 |
val positive_vars = diff cvars lvars
|
|
767 |
val zero = cplexNum "0"
|
|
768 |
|
|
769 |
fun make_pos_constr v =
|
15531
|
770 |
(NONE, cplexConstr (cplexGeq, ((cplexVar v), zero)))
|
15178
|
771 |
|
|
772 |
fun make_free_bound v =
|
|
773 |
cplexBounds (cplexNeg cplexInf, cplexLeq,
|
|
774 |
cplexVar v, cplexLeq,
|
|
775 |
cplexInf)
|
|
776 |
|
|
777 |
val pos_constrs = rev(Symtab.foldl
|
|
778 |
(fn (l, (k,v)) => (make_pos_constr k) :: l)
|
|
779 |
([], positive_vars))
|
15531
|
780 |
val bound_constrs = map (fn x => (NONE, x))
|
15178
|
781 |
(flat (map bound2constr bounds))
|
|
782 |
val constraints' = constraints @ pos_constrs @ bound_constrs
|
|
783 |
val bounds' = rev(Symtab.foldl (fn (l, (v,_)) =>
|
|
784 |
(make_free_bound v)::l)
|
|
785 |
([], cvars))
|
|
786 |
in
|
|
787 |
cplexProg (name, goal, constraints', bounds')
|
|
788 |
end
|
|
789 |
|
|
790 |
fun relax_strict_ineqs (cplexProg (name, goals, constrs, bounds)) =
|
|
791 |
let
|
|
792 |
fun relax cplexLe = cplexLeq
|
|
793 |
| relax cplexGe = cplexGeq
|
|
794 |
| relax x = x
|
|
795 |
|
|
796 |
fun relax_constr (n, cplexConstr(c, (t1, t2))) =
|
|
797 |
(n, cplexConstr(relax c, (t1, t2)))
|
|
798 |
|
|
799 |
fun relax_bounds (cplexBounds (t1, c1, t2, c2, t3)) =
|
|
800 |
cplexBounds (t1, relax c1, t2, relax c2, t3)
|
|
801 |
| relax_bounds (cplexBound (t1, c, t2)) =
|
|
802 |
cplexBound (t1, relax c, t2)
|
|
803 |
in
|
|
804 |
cplexProg (name,
|
|
805 |
goals,
|
|
806 |
map relax_constr constrs,
|
|
807 |
map relax_bounds bounds)
|
|
808 |
end
|
|
809 |
|
|
810 |
datatype cplexResult = Unbounded
|
|
811 |
| Infeasible
|
|
812 |
| Undefined
|
|
813 |
| Optimal of string * ((string * string) list)
|
|
814 |
|
|
815 |
fun is_separator x = forall (fn c => c = #"-") (String.explode x)
|
|
816 |
|
|
817 |
fun is_sign x = (x = "+" orelse x = "-")
|
|
818 |
|
|
819 |
fun is_colon x = (x = ":")
|
|
820 |
|
|
821 |
fun is_resultsymbol a =
|
|
822 |
let
|
|
823 |
val symbol_char = String.explode "!\"#$%&()/,.;?@_`'{}|~-"
|
|
824 |
fun is_symbol_char c = Char.isAlphaNum c orelse
|
|
825 |
exists (fn d => d=c) symbol_char
|
|
826 |
fun is_symbol_start c = is_symbol_char c andalso
|
|
827 |
not (Char.isDigit c) andalso
|
|
828 |
not (c= #".") andalso
|
|
829 |
not (c= #"-")
|
|
830 |
val b = String.explode a
|
|
831 |
in
|
|
832 |
b <> [] andalso is_symbol_start (hd b) andalso
|
|
833 |
forall is_symbol_char b
|
|
834 |
end
|
|
835 |
|
|
836 |
val TOKEN_SIGN = 100
|
|
837 |
val TOKEN_COLON = 101
|
|
838 |
val TOKEN_SEPARATOR = 102
|
|
839 |
|
|
840 |
fun load_cplexResult name =
|
|
841 |
let
|
|
842 |
val flist = [(is_NL, TOKEN_NL),
|
|
843 |
(is_blank, TOKEN_BLANK),
|
|
844 |
(is_num, TOKEN_NUM),
|
|
845 |
(is_sign, TOKEN_SIGN),
|
|
846 |
(is_colon, TOKEN_COLON),
|
|
847 |
(is_cmp, TOKEN_CMP),
|
|
848 |
(is_resultsymbol, TOKEN_SYMBOL),
|
|
849 |
(is_separator, TOKEN_SEPARATOR)]
|
|
850 |
|
|
851 |
val tokenize = tokenize_general flist
|
|
852 |
|
|
853 |
val f = TextIO.openIn name
|
|
854 |
|
|
855 |
val rest = ref []
|
|
856 |
|
|
857 |
fun readToken_helper () =
|
|
858 |
if length (!rest) > 0 then
|
|
859 |
let val u = hd (!rest) in
|
|
860 |
(
|
|
861 |
rest := tl (!rest);
|
15531
|
862 |
SOME u
|
15178
|
863 |
)
|
|
864 |
end
|
|
865 |
else
|
|
866 |
let val s = TextIO.inputLine f in
|
15531
|
867 |
if s = "" then NONE else (rest := tokenize s; readToken_helper())
|
15178
|
868 |
end
|
|
869 |
|
15531
|
870 |
fun is_tt tok ty = (tok <> NONE andalso (fst (the tok)) = ty)
|
15178
|
871 |
|
15531
|
872 |
fun pushToken a = if a = NONE then () else (rest := ((the a)::(!rest)))
|
15178
|
873 |
|
|
874 |
fun readToken () =
|
|
875 |
let val t = readToken_helper () in
|
|
876 |
if is_tt t TOKEN_BLANK then
|
|
877 |
readToken ()
|
|
878 |
else if is_tt t TOKEN_NL then
|
|
879 |
let val t2 = readToken_helper () in
|
|
880 |
if is_tt t2 TOKEN_SIGN then
|
15531
|
881 |
(pushToken (SOME (TOKEN_SEPARATOR, "-")); t)
|
15178
|
882 |
else
|
|
883 |
(pushToken t2; t)
|
|
884 |
end
|
|
885 |
else if is_tt t TOKEN_SIGN then
|
|
886 |
let val t2 = readToken_helper () in
|
|
887 |
if is_tt t2 TOKEN_NUM then
|
15531
|
888 |
(SOME (TOKEN_NUM, (snd (the t))^(snd (the t2))))
|
15178
|
889 |
else
|
|
890 |
(pushToken t2; t)
|
|
891 |
end
|
|
892 |
else
|
|
893 |
t
|
|
894 |
end
|
|
895 |
|
|
896 |
fun readRestOfLine P =
|
|
897 |
let
|
|
898 |
val t = readToken ()
|
|
899 |
in
|
15531
|
900 |
if is_tt t TOKEN_NL orelse t = NONE
|
15178
|
901 |
then P
|
|
902 |
else readRestOfLine P
|
|
903 |
end
|
|
904 |
|
|
905 |
fun readHeader () =
|
|
906 |
let
|
|
907 |
fun readStatus () = readRestOfLine ("STATUS", snd (the (readToken ())))
|
|
908 |
fun readObjective () = readRestOfLine ("OBJECTIVE", snd (the (readToken (); readToken (); readToken ())))
|
|
909 |
val t1 = readToken ()
|
|
910 |
val t2 = readToken ()
|
|
911 |
in
|
|
912 |
if is_tt t1 TOKEN_SYMBOL andalso is_tt t2 TOKEN_COLON
|
|
913 |
then
|
|
914 |
case to_upper (snd (the t1)) of
|
|
915 |
"STATUS" => (readStatus ())::(readHeader ())
|
|
916 |
| "OBJECTIVE" => (readObjective())::(readHeader ())
|
|
917 |
| _ => (readRestOfLine (); readHeader ())
|
|
918 |
else
|
|
919 |
(pushToken t2; pushToken t1; [])
|
|
920 |
end
|
|
921 |
|
|
922 |
fun skip_until_sep () =
|
|
923 |
let val x = readToken () in
|
|
924 |
if is_tt x TOKEN_SEPARATOR then
|
|
925 |
readRestOfLine ()
|
|
926 |
else
|
|
927 |
skip_until_sep ()
|
|
928 |
end
|
|
929 |
|
|
930 |
fun load_value () =
|
|
931 |
let
|
|
932 |
val t1 = readToken ()
|
|
933 |
val t2 = readToken ()
|
|
934 |
in
|
|
935 |
if is_tt t1 TOKEN_NUM andalso is_tt t2 TOKEN_SYMBOL then
|
|
936 |
let
|
|
937 |
val t = readToken ()
|
|
938 |
val state = if is_tt t TOKEN_NL then readToken () else t
|
|
939 |
val _ = if is_tt state TOKEN_SYMBOL then () else raise (Load_cplexResult "state expected")
|
|
940 |
val k = readToken ()
|
|
941 |
in
|
|
942 |
if is_tt k TOKEN_NUM then
|
15531
|
943 |
readRestOfLine (SOME (snd (the t2), snd (the k)))
|
15178
|
944 |
else
|
|
945 |
raise (Load_cplexResult "number expected")
|
|
946 |
end
|
|
947 |
else
|
15531
|
948 |
(pushToken t2; pushToken t1; NONE)
|
15178
|
949 |
end
|
|
950 |
|
|
951 |
fun load_values () =
|
|
952 |
let val v = load_value () in
|
15531
|
953 |
if v = NONE then [] else (the v)::(load_values ())
|
15178
|
954 |
end
|
|
955 |
|
|
956 |
val header = readHeader ()
|
|
957 |
|
|
958 |
val result =
|
|
959 |
case assoc (header, "STATUS") of
|
15531
|
960 |
SOME "INFEASIBLE" => Infeasible
|
|
961 |
| SOME "UNBOUNDED" => Unbounded
|
|
962 |
| SOME "OPTIMAL" => Optimal (the (assoc (header, "OBJECTIVE")),
|
15178
|
963 |
(skip_until_sep ();
|
|
964 |
skip_until_sep ();
|
|
965 |
load_values ()))
|
|
966 |
| _ => Undefined
|
|
967 |
|
|
968 |
val _ = TextIO.closeIn f
|
|
969 |
in
|
|
970 |
result
|
|
971 |
end
|
|
972 |
handle (Tokenize s) => raise (Load_cplexResult ("Tokenize: "^s))
|
|
973 |
| OPTION => raise (Load_cplexResult "OPTION")
|
|
974 |
| x => raise x
|
|
975 |
|
|
976 |
exception Execute of string;
|
|
977 |
|
|
978 |
fun execute_cplex lpname resultname =
|
|
979 |
let
|
|
980 |
fun wrap s = "\""^s^"\""
|
|
981 |
val cplex_path = getenv "CPLEX"
|
|
982 |
val cplex = if cplex_path = "" then "glpsol" else cplex_path
|
|
983 |
val command = (wrap cplex)^" --lpt "^(wrap lpname)^" --output "^(wrap resultname)
|
|
984 |
in
|
|
985 |
execute command
|
|
986 |
end
|
|
987 |
|
|
988 |
fun tmp_file s = Path.pack (Path.expand (File.tmp_path (Path.make [s])))
|
|
989 |
|
|
990 |
fun solve prog =
|
|
991 |
let
|
|
992 |
val name = LargeInt.toString (Time.toMicroseconds (Time.now ()))
|
|
993 |
val lpname = tmp_file (name^".lp")
|
|
994 |
val resultname = tmp_file (name^".result")
|
|
995 |
val _ = save_cplexFile lpname prog
|
|
996 |
val answer = execute_cplex lpname resultname
|
|
997 |
in
|
|
998 |
let
|
|
999 |
val result = load_cplexResult resultname
|
|
1000 |
val _ = OS.FileSys.remove lpname
|
|
1001 |
val _ = OS.FileSys.remove resultname
|
|
1002 |
in
|
|
1003 |
result
|
|
1004 |
end
|
|
1005 |
handle (Load_cplexResult s) => raise (Execute ("Load_cplexResult: "^s^"\nExecute: "^answer))
|
|
1006 |
| _ => raise (Execute answer)
|
|
1007 |
end
|
|
1008 |
end;
|
|
1009 |
|
|
1010 |
val demofile = "/home/obua/flyspeck/kepler/LP/cplexPent2.lp45"
|
|
1011 |
val demoout = "/home/obua/flyspeck/kepler/LP/test.out"
|
|
1012 |
val demoresult = "/home/obua/flyspeck/kepler/LP/try/test2.sol"
|
|
1013 |
|
|
1014 |
fun loadcplex () = Cplex.relax_strict_ineqs
|
|
1015 |
(Cplex.load_cplexFile demofile)
|
|
1016 |
|
|
1017 |
fun writecplex lp = Cplex.save_cplexFile demoout lp
|
|
1018 |
|
|
1019 |
fun test () =
|
|
1020 |
let
|
|
1021 |
val lp = loadcplex ()
|
|
1022 |
val lp2 = Cplex.elim_nonfree_bounds lp
|
|
1023 |
in
|
|
1024 |
writecplex lp2
|
|
1025 |
end
|
|
1026 |
|
|
1027 |
fun loadresult () = Cplex.load_cplexResult demoresult;
|