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