src/HOL/Matrix/cplex/Cplex_tools.ML
changeset 16966 37e34f315057
parent 16873 9ed940a1bebb
child 17412 e26cb20ef0cc
equal deleted inserted replaced
16965:46697fa536ab 16966:37e34f315057
    33 			 | Infeasible 
    33 			 | Infeasible 
    34 			 | Undefined
    34 			 | Undefined
    35 			 | Optimal of string * 
    35 			 | Optimal of string * 
    36 				      (((* name *) string * 
    36 				      (((* name *) string * 
    37 					(* value *) string) list)
    37 					(* value *) string) list)
       
    38 
       
    39     datatype cplexSolver = SOLVER_DEFAULT | SOLVER_CPLEX | SOLVER_GLPK
    38 						     
    40 						     
    39     exception Load_cplexFile of string
    41     exception Load_cplexFile of string
    40     exception Load_cplexResult of string
    42     exception Load_cplexResult of string
    41     exception Save_cplexFile of string
    43     exception Save_cplexFile of string
    42     exception Execute of string
    44     exception Execute of string
    49 
    51 
    50     val relax_strict_ineqs : cplexProg -> cplexProg
    52     val relax_strict_ineqs : cplexProg -> cplexProg
    51 
    53 
    52     val is_normed_cplexProg : cplexProg -> bool
    54     val is_normed_cplexProg : cplexProg -> bool
    53 
    55 
       
    56     val get_solver : unit -> cplexSolver
       
    57     val set_solver : cplexSolver -> unit
    54     val solve : cplexProg -> cplexResult
    58     val solve : cplexProg -> cplexResult
    55 end;
    59 end;
    56 
    60 
    57 structure Cplex  : CPLEX =
    61 structure Cplex  : CPLEX =
    58 struct
    62 struct
       
    63 
       
    64 datatype cplexSolver = SOLVER_DEFAULT | SOLVER_CPLEX | SOLVER_GLPK
       
    65 
       
    66 val cplexsolver = ref SOLVER_DEFAULT;
       
    67 fun get_solver () = !cplexsolver;
       
    68 fun set_solver s = (cplexsolver := s);
    59 
    69 
    60 exception Load_cplexFile of string;
    70 exception Load_cplexFile of string;
    61 exception Load_cplexResult of string;
    71 exception Load_cplexResult of string;
    62 exception Save_cplexFile of string;
    72 exception Save_cplexFile of string;
    63 	  
    73 	  
  1137     let 
  1147     let 
  1138 	val name = LargeInt.toString (Time.toMicroseconds (Time.now ()))	
  1148 	val name = LargeInt.toString (Time.toMicroseconds (Time.now ()))	
  1139 	val lpname = tmp_file (name^".lp")
  1149 	val lpname = tmp_file (name^".lp")
  1140 	val resultname = tmp_file (name^".txt")
  1150 	val resultname = tmp_file (name^".txt")
  1141 	val _ = save_cplexFile lpname prog		
  1151 	val _ = save_cplexFile lpname prog		
  1142 	val cplex_path = getenv "LP_SOLVER_PATH"
  1152 	val cplex_path = getenv "GLPK_PATH"
  1143 	val cplex = if cplex_path = "" then "glpsol" else cplex_path
  1153 	val cplex = if cplex_path = "" then "glpsol" else cplex_path
  1144 	val command = (wrap cplex)^" --lpt "^(wrap lpname)^" --output "^(wrap resultname)
  1154 	val command = (wrap cplex)^" --lpt "^(wrap lpname)^" --output "^(wrap resultname)
  1145 	val answer = execute command
  1155 	val answer = execute command
  1146     in
  1156     in
  1147 	let
  1157 	let
  1169 	val name = LargeInt.toString (Time.toMicroseconds (Time.now ()))	
  1179 	val name = LargeInt.toString (Time.toMicroseconds (Time.now ()))	
  1170 	val lpname = tmp_file (name^".lp")
  1180 	val lpname = tmp_file (name^".lp")
  1171 	val resultname = tmp_file (name^".txt")
  1181 	val resultname = tmp_file (name^".txt")
  1172 	val scriptname = tmp_file (name^".script")
  1182 	val scriptname = tmp_file (name^".script")
  1173 	val _ = save_cplexFile lpname prog		
  1183 	val _ = save_cplexFile lpname prog		
  1174 	val cplex_path = getenv "LP_SOLVER_PATH"
  1184 	val cplex_path = getenv "CPLEX_PATH"
  1175 	val cplex = if cplex_path = "" then "cplex" else cplex_path
  1185 	val cplex = if cplex_path = "" then "cplex" else cplex_path
  1176 	val _ = write_script scriptname lpname resultname
  1186 	val _ = write_script scriptname lpname resultname
  1177 	val command = (wrap cplex)^" < "^(wrap scriptname)^" > /dev/null"
  1187 	val command = (wrap cplex)^" < "^(wrap scriptname)^" > /dev/null"
  1178 	val answer = "return code "^(Int.toString (system command))
  1188 	val answer = "return code "^(Int.toString (system command))
  1179     in
  1189     in
  1186 	    result
  1196 	    result
  1187 	end 
  1197 	end 
  1188     end
  1198     end
  1189 
  1199 
  1190 fun solve prog = 
  1200 fun solve prog = 
  1191     case getenv "LP_SOLVER_NAME" of
  1201     case get_solver () of
  1192 	"CPLEX" => solve_cplex prog
  1202       SOLVER_DEFAULT => 
  1193       | "GLPK" => solve_glpk prog
  1203         (case getenv "LP_SOLVER" of
  1194       | _ => raise (Execute ("LP_SOLVER_NAME must be set to CPLEX or to GLPK"));
  1204 	   "CPLEX" => solve_cplex prog
       
  1205          | "GLPK" => solve_glpk prog
       
  1206          | _ => raise (Execute ("LP_SOLVER must be set to CPLEX or to GLPK")))
       
  1207     | SOLVER_CPLEX => solve_cplex prog
       
  1208     | SOLVER_GLPK => solve_glpk prog
  1195 		   
  1209 		   
  1196 end;
  1210 end;
  1197 
  1211 
  1198 (*
  1212 (*
  1199 val demofile = "/home/obua/flyspeck/kepler/LP/cplexPent2.lp45"
  1213 val demofile = "/home/obua/flyspeck/kepler/LP/cplexPent2.lp45"