224 |
224 |
225 fun transpose_matrix matrix = |
225 fun transpose_matrix matrix = |
226 let |
226 let |
227 fun upd m j i x = |
227 fun upd m j i x = |
228 case Inttab.lookup (m, j) of |
228 case Inttab.lookup (m, j) of |
229 Some v => Inttab.update ((j, Inttab.update ((i, x), v)), m) |
229 SOME v => Inttab.update ((j, Inttab.update ((i, x), v)), m) |
230 | None => Inttab.update ((j, Inttab.update ((i, x), Inttab.empty)), m) |
230 | NONE => Inttab.update ((j, Inttab.update ((i, x), Inttab.empty)), m) |
231 |
231 |
232 fun updv j (m, (i, s)) = upd m i j s |
232 fun updv j (m, (i, s)) = upd m i j s |
233 |
233 |
234 fun updm (m, (j, v)) = Inttab.foldl (updv j) (m, v) |
234 fun updm (m, (j, v)) = Inttab.foldl (updv j) (m, v) |
235 in |
235 in |
272 fun vec2sum vector = |
272 fun vec2sum vector = |
273 cplex.cplexSum (Inttab.foldl (fn (list, (index, s)) => (mk_term index s)::list) ([], vector)) |
273 cplex.cplexSum (Inttab.foldl (fn (list, (index, s)) => (mk_term index s)::list) ([], vector)) |
274 |
274 |
275 fun mk_constr index vector c = |
275 fun mk_constr index vector c = |
276 let |
276 let |
277 val s = case Inttab.lookup (c, index) of Some s => s | None => "0" |
277 val s = case Inttab.lookup (c, index) of SOME s => s | NONE => "0" |
278 val (p, s) = split_numstr s |
278 val (p, s) = split_numstr s |
279 val num = if p then cplex.cplexNum s else cplex.cplexNeg (cplex.cplexNum s) |
279 val num = if p then cplex.cplexNum s else cplex.cplexNeg (cplex.cplexNum s) |
280 in |
280 in |
281 (None, cplex.cplexConstr (cplex.cplexLeq, (vec2sum vector, num))) |
281 (NONE, cplex.cplexConstr (cplex.cplexLeq, (vec2sum vector, num))) |
282 end |
282 end |
283 |
283 |
284 fun delete index c = Inttab.delete index c handle Inttab.UNDEF _ => c |
284 fun delete index c = Inttab.delete index c handle Inttab.UNDEF _ => c |
285 |
285 |
286 val (list, b) = Inttab.foldl |
286 val (list, b) = Inttab.foldl |
325 fun vec2sum vector = |
325 fun vec2sum vector = |
326 cplex.cplexSum (Inttab.foldl (fn (list, (index, s)) => (mk_term index s)::list) ([], vector)) |
326 cplex.cplexSum (Inttab.foldl (fn (list, (index, s)) => (mk_term index s)::list) ([], vector)) |
327 |
327 |
328 fun mk_constr index vector c = |
328 fun mk_constr index vector c = |
329 let |
329 let |
330 val s = case Inttab.lookup (c, index) of Some s => s | None => "0" |
330 val s = case Inttab.lookup (c, index) of SOME s => s | NONE => "0" |
331 val (p, s) = split_numstr s |
331 val (p, s) = split_numstr s |
332 val num = if p then cplex.cplexNum s else cplex.cplexNeg (cplex.cplexNum s) |
332 val num = if p then cplex.cplexNum s else cplex.cplexNeg (cplex.cplexNum s) |
333 in |
333 in |
334 (None, cplex.cplexConstr (cplex.cplexEq, (vec2sum vector, num))) |
334 (NONE, cplex.cplexConstr (cplex.cplexEq, (vec2sum vector, num))) |
335 end |
335 end |
336 |
336 |
337 fun delete index c = Inttab.delete index c handle Inttab.UNDEF _ => c |
337 fun delete index c = Inttab.delete index c handle Inttab.UNDEF _ => c |
338 |
338 |
339 val (list, c) = Inttab.foldl |
339 val (list, c) = Inttab.foldl |
359 end |
359 end |
360 |
360 |
361 fun cut_matrix vfilter vsize m = |
361 fun cut_matrix vfilter vsize m = |
362 let |
362 let |
363 fun app (m, (i, v)) = |
363 fun app (m, (i, v)) = |
364 if (Inttab.lookup (vfilter, i) = None) then |
364 if (Inttab.lookup (vfilter, i) = NONE) then |
365 m |
365 m |
366 else |
366 else |
367 case vsize of |
367 case vsize of |
368 None => Inttab.update ((i,v), m) |
368 NONE => Inttab.update ((i,v), m) |
369 | Some s => Inttab.update((i, cut_vector s v),m) |
369 | SOME s => Inttab.update((i, cut_vector s v),m) |
370 in |
370 in |
371 Inttab.foldl app (empty_matrix, m) |
371 Inttab.foldl app (empty_matrix, m) |
372 end |
372 end |
373 |
373 |
374 fun v_elem_at v i = Inttab.lookup (v,i) |
374 fun v_elem_at v i = Inttab.lookup (v,i) |
375 fun m_elem_at m i = Inttab.lookup (m,i) |
375 fun m_elem_at m i = Inttab.lookup (m,i) |
376 |
376 |
377 fun v_only_elem v = |
377 fun v_only_elem v = |
378 case Inttab.min_key v of |
378 case Inttab.min_key v of |
379 None => None |
379 NONE => NONE |
380 | Some vmin => (case Inttab.max_key v of |
380 | SOME vmin => (case Inttab.max_key v of |
381 None => Some vmin |
381 NONE => SOME vmin |
382 | Some vmax => if vmin = vmax then Some vmin else None) |
382 | SOME vmax => if vmin = vmax then SOME vmin else NONE) |
383 |
383 |
384 fun v_fold f a v = Inttab.foldl f (a,v) |
384 fun v_fold f a v = Inttab.foldl f (a,v) |
385 |
385 |
386 fun m_fold f a m = Inttab.foldl f (a,m) |
386 fun m_fold f a m = Inttab.foldl f (a,m) |
387 |
387 |