src/HOL/Matrix_LP/Cplex_tools.ML
author wenzelm
Wed, 26 Oct 2016 15:14:17 +0200
changeset 64406 492de9062cd2
parent 62505 9e2a65912111
child 67399 eab6ce8368fa
permissions -rw-r--r--
more specific hardware information: relevant for ultimate Mac OS X version;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
47455
26315a545e26 updated headers;
wenzelm
parents: 46988
diff changeset
     1
(*  Title:      HOL/Matrix_LP/Cplex_tools.ML
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
     2
    Author:     Steven Obua
51940
958d439b3013 decentralized historic settings;
wenzelm
parents: 51930
diff changeset
     3
958d439b3013 decentralized historic settings;
wenzelm
parents: 51930
diff changeset
     4
Relevant Isabelle environment settings:
958d439b3013 decentralized historic settings;
wenzelm
parents: 51930
diff changeset
     5
958d439b3013 decentralized historic settings;
wenzelm
parents: 51930
diff changeset
     6
  # LP_SOLVER is the default solver. It can be changed during runtime via Cplex.set_solver.
958d439b3013 decentralized historic settings;
wenzelm
parents: 51930
diff changeset
     7
  # First option: use the commercial cplex solver
958d439b3013 decentralized historic settings;
wenzelm
parents: 51930
diff changeset
     8
  #LP_SOLVER=CPLEX
958d439b3013 decentralized historic settings;
wenzelm
parents: 51930
diff changeset
     9
  #CPLEX_PATH=cplex
958d439b3013 decentralized historic settings;
wenzelm
parents: 51930
diff changeset
    10
  # Second option: use the open source glpk solver
958d439b3013 decentralized historic settings;
wenzelm
parents: 51930
diff changeset
    11
  #LP_SOLVER=GLPK
958d439b3013 decentralized historic settings;
wenzelm
parents: 51930
diff changeset
    12
  #GLPK_PATH=glpsol
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
    13
*)
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
    14
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
    15
signature CPLEX =
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
    16
sig
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
    17
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
    18
    datatype cplexTerm = cplexVar of string | cplexNum of string | cplexInf
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
    19
                       | cplexNeg of cplexTerm
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
    20
                       | cplexProd of cplexTerm * cplexTerm
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
    21
                       | cplexSum of (cplexTerm list)
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
    22
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
    23
    datatype cplexComp = cplexLe | cplexLeq | cplexEq | cplexGe | cplexGeq
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
    24
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
    25
    datatype cplexGoal = cplexMinimize of cplexTerm
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
    26
               | cplexMaximize of cplexTerm
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
    27
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
    28
    datatype cplexConstr = cplexConstr of cplexComp *
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
    29
                      (cplexTerm * cplexTerm)
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
    30
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
    31
    datatype cplexBounds = cplexBounds of cplexTerm * cplexComp * cplexTerm
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
    32
                      * cplexComp * cplexTerm
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
    33
             | cplexBound of cplexTerm * cplexComp * cplexTerm
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
    34
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
    35
    datatype cplexProg = cplexProg of string
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
    36
                      * cplexGoal
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
    37
                      * ((string option * cplexConstr)
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
    38
                         list)
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
    39
                      * cplexBounds list
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
    40
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
    41
    datatype cplexResult = Unbounded
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
    42
             | Infeasible
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
    43
             | Undefined
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
    44
             | Optimal of string *
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
    45
                      (((* name *) string *
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
    46
                    (* value *) string) list)
16966
37e34f315057 1. changed configuration variables for linear programming (Cplex_tools):
obua
parents: 16873
diff changeset
    47
37e34f315057 1. changed configuration variables for linear programming (Cplex_tools):
obua
parents: 16873
diff changeset
    48
    datatype cplexSolver = SOLVER_DEFAULT | SOLVER_CPLEX | SOLVER_GLPK
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
    49
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
    50
    exception Load_cplexFile of string
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
    51
    exception Load_cplexResult of string
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
    52
    exception Save_cplexFile of string
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
    53
    exception Execute of string
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
    54
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
    55
    val load_cplexFile : string -> cplexProg
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
    56
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
    57
    val save_cplexFile : string -> cplexProg -> unit
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
    58
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
    59
    val elim_nonfree_bounds : cplexProg -> cplexProg
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
    60
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
    61
    val relax_strict_ineqs : cplexProg -> cplexProg
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
    62
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
    63
    val is_normed_cplexProg : cplexProg -> bool
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
    64
16966
37e34f315057 1. changed configuration variables for linear programming (Cplex_tools):
obua
parents: 16873
diff changeset
    65
    val get_solver : unit -> cplexSolver
37e34f315057 1. changed configuration variables for linear programming (Cplex_tools):
obua
parents: 16873
diff changeset
    66
    val set_solver : cplexSolver -> unit
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
    67
    val solve : cplexProg -> cplexResult
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
    68
end;
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
    69
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
    70
structure Cplex  : CPLEX =
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
    71
struct
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
    72
16966
37e34f315057 1. changed configuration variables for linear programming (Cplex_tools):
obua
parents: 16873
diff changeset
    73
datatype cplexSolver = SOLVER_DEFAULT | SOLVER_CPLEX | SOLVER_GLPK
37e34f315057 1. changed configuration variables for linear programming (Cplex_tools):
obua
parents: 16873
diff changeset
    74
32740
9dd0a2f83429 explicit indication of Unsynchronized.ref;
wenzelm
parents: 32491
diff changeset
    75
val cplexsolver = Unsynchronized.ref SOLVER_DEFAULT;
16966
37e34f315057 1. changed configuration variables for linear programming (Cplex_tools):
obua
parents: 16873
diff changeset
    76
fun get_solver () = !cplexsolver;
37e34f315057 1. changed configuration variables for linear programming (Cplex_tools):
obua
parents: 16873
diff changeset
    77
fun set_solver s = (cplexsolver := s);
37e34f315057 1. changed configuration variables for linear programming (Cplex_tools):
obua
parents: 16873
diff changeset
    78
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
    79
exception Load_cplexFile of string;
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
    80
exception Load_cplexResult of string;
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
    81
exception Save_cplexFile of string;
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
    82
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
    83
datatype cplexTerm = cplexVar of string
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
    84
           | cplexNum of string
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
    85
           | cplexInf
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
    86
                   | cplexNeg of cplexTerm
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
    87
                   | cplexProd of cplexTerm * cplexTerm
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
    88
                   | cplexSum of (cplexTerm list)
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
    89
datatype cplexComp = cplexLe | cplexLeq | cplexEq | cplexGe | cplexGeq
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
    90
datatype cplexGoal = cplexMinimize of cplexTerm | cplexMaximize of cplexTerm
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
    91
datatype cplexConstr = cplexConstr of cplexComp * (cplexTerm * cplexTerm)
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
    92
datatype cplexBounds = cplexBounds of cplexTerm * cplexComp * cplexTerm
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
    93
                      * cplexComp * cplexTerm
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
    94
                     | cplexBound of cplexTerm * cplexComp * cplexTerm
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
    95
datatype cplexProg = cplexProg of string
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
    96
                  * cplexGoal
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
    97
                  * ((string option * cplexConstr) list)
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
    98
                  * cplexBounds list
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
    99
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   100
fun rev_cmp cplexLe = cplexGe
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   101
  | rev_cmp cplexLeq = cplexGeq
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   102
  | rev_cmp cplexGe = cplexLe
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   103
  | rev_cmp cplexGeq = cplexLeq
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   104
  | rev_cmp cplexEq = cplexEq
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   105
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   106
fun the NONE = raise (Load_cplexFile "SOME expected")
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   107
  | the (SOME x) = x;
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   108
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   109
fun modulo_signed is_something (cplexNeg u) = is_something u
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   110
  | modulo_signed is_something u = is_something u
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   111
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   112
fun is_Num (cplexNum _) = true
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   113
  | is_Num _ = false
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   114
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   115
fun is_Inf cplexInf = true
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   116
  | is_Inf _ = false
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   117
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   118
fun is_Var (cplexVar _) = true
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   119
  | is_Var _ = false
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   120
46531
eff798e48efc dropped dead code
haftmann
parents: 43850
diff changeset
   121
fun is_Neg (cplexNeg _) = true
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   122
  | is_Neg _ = false
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   123
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   124
fun is_normed_Prod (cplexProd (t1, t2)) =
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   125
    (is_Num t1) andalso (is_Var t2)
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   126
  | is_normed_Prod x = is_Var x
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   127
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   128
fun is_normed_Sum (cplexSum ts) =
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   129
    (ts <> []) andalso forall (modulo_signed is_normed_Prod) ts
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   130
  | is_normed_Sum x = modulo_signed is_normed_Prod x
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   131
46531
eff798e48efc dropped dead code
haftmann
parents: 43850
diff changeset
   132
fun is_normed_Constr (cplexConstr (_, (t1, t2))) =
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   133
    (is_normed_Sum t1) andalso (modulo_signed is_Num t2)
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   134
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   135
fun is_Num_or_Inf x = is_Inf x orelse is_Num x
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   136
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   137
fun is_normed_Bounds (cplexBounds (t1, c1, t2, c2, t3)) =
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   138
    (c1 = cplexLe orelse c1 = cplexLeq) andalso
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   139
    (c2 = cplexLe orelse c2 = cplexLeq) andalso
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   140
    is_Var t2 andalso
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   141
    modulo_signed is_Num_or_Inf t1 andalso
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   142
    modulo_signed is_Num_or_Inf t3
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   143
  | is_normed_Bounds (cplexBound (t1, c, t2)) =
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   144
    (is_Var t1 andalso (modulo_signed is_Num_or_Inf t2))
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   145
    orelse
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   146
    (c <> cplexEq andalso
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   147
     is_Var t2 andalso (modulo_signed is_Num_or_Inf t1))
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   148
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   149
fun term_of_goal (cplexMinimize x) = x
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   150
  | term_of_goal (cplexMaximize x) = x
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   151
46531
eff798e48efc dropped dead code
haftmann
parents: 43850
diff changeset
   152
fun is_normed_cplexProg (cplexProg (_, goal, constraints, bounds)) =
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   153
    is_normed_Sum (term_of_goal goal) andalso
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   154
    forall (fn (_,x) => is_normed_Constr x) constraints andalso
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   155
    forall is_normed_Bounds bounds
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   156
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   157
fun is_NL s = s = "\n"
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   158
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   159
fun is_blank s = forall (fn c => c <> #"\n" andalso Char.isSpace c) (String.explode s)
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   160
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   161
fun is_num a =
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   162
    let
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   163
    val b = String.explode a
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   164
    fun num4 cs = forall Char.isDigit cs
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   165
    fun num3 [] = true
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   166
      | num3 (ds as (c::cs)) =
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   167
        if c = #"+" orelse c = #"-" then
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   168
        num4 cs
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   169
        else
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   170
        num4 ds
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   171
    fun num2 [] = true
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   172
      | num2 (c::cs) =
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   173
        if c = #"e" orelse c = #"E" then num3 cs
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   174
        else (Char.isDigit c) andalso num2 cs
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   175
    fun num1 [] = true
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   176
      | num1 (c::cs) =
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   177
        if c = #"." then num2 cs
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   178
        else if c = #"e" orelse c = #"E" then num3 cs
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   179
        else (Char.isDigit c) andalso num1 cs
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   180
    fun num [] = true
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   181
      | num (c::cs) =
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   182
        if c = #"." then num2 cs
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   183
        else (Char.isDigit c) andalso num1 cs
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   184
    in
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   185
    num b
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   186
    end
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   187
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   188
fun is_delimiter s = s = "+" orelse s = "-" orelse s = ":"
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   189
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   190
fun is_cmp s = s = "<" orelse s = ">" orelse s = "<="
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   191
             orelse s = ">=" orelse s = "="
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   192
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   193
fun is_symbol a =
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   194
    let
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   195
    val symbol_char = String.explode "!\"#$%&()/,.;?@_`'{}|~"
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   196
    fun is_symbol_char c = Char.isAlphaNum c orelse
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   197
                   exists (fn d => d=c) symbol_char
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   198
    fun is_symbol_start c = is_symbol_char c andalso
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   199
                not (Char.isDigit c) andalso
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   200
                not (c= #".")
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   201
    val b = String.explode a
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   202
    in
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   203
    b <> [] andalso is_symbol_start (hd b) andalso
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   204
    forall is_symbol_char b
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   205
    end
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   206
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   207
fun to_upper s = String.implode (map Char.toUpper (String.explode s))
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   208
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   209
fun keyword x =
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   210
    let
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   211
    val a = to_upper x
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   212
    in
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   213
    if a = "BOUNDS" orelse a = "BOUND" then
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   214
        SOME "BOUNDS"
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   215
    else if a = "MINIMIZE" orelse a = "MINIMUM" orelse a = "MIN" then
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   216
        SOME "MINIMIZE"
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   217
    else if a = "MAXIMIZE" orelse a = "MAXIMUM" orelse a = "MAX" then
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   218
        SOME "MAXIMIZE"
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   219
    else if a = "ST" orelse a = "S.T." orelse a = "ST." then
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   220
        SOME "ST"
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   221
    else if a = "FREE" orelse a = "END" then
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   222
        SOME a
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   223
    else if a = "GENERAL" orelse a = "GENERALS" orelse a = "GEN" then
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   224
        SOME "GENERAL"
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   225
    else if a = "INTEGER" orelse a = "INTEGERS" orelse a = "INT" then
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   226
        SOME "INTEGER"
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   227
    else if a = "BINARY" orelse a = "BINARIES" orelse a = "BIN" then
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   228
        SOME "BINARY"
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   229
    else if a = "INF" orelse a = "INFINITY" then
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   230
        SOME "INF"
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   231
    else
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   232
        NONE
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   233
    end
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   234
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   235
val TOKEN_ERROR = ~1
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   236
val TOKEN_BLANK = 0
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   237
val TOKEN_NUM = 1
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   238
val TOKEN_DELIMITER = 2
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   239
val TOKEN_SYMBOL = 3
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   240
val TOKEN_LABEL = 4
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   241
val TOKEN_CMP = 5
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   242
val TOKEN_KEYWORD = 6
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   243
val TOKEN_NL = 7
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   244
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   245
(* tokenize takes a list of chars as argument and returns a list of
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   246
   int * string pairs, each string representing a "cplex token",
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   247
   and each int being one of TOKEN_NUM, TOKEN_DELIMITER, TOKEN_CMP
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   248
   or TOKEN_SYMBOL *)
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   249
fun tokenize s =
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   250
    let
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   251
    val flist = [(is_NL, TOKEN_NL),
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   252
             (is_blank, TOKEN_BLANK),
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   253
             (is_num, TOKEN_NUM),
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   254
                     (is_delimiter, TOKEN_DELIMITER),
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   255
             (is_cmp, TOKEN_CMP),
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   256
             (is_symbol, TOKEN_SYMBOL)]
46531
eff798e48efc dropped dead code
haftmann
parents: 43850
diff changeset
   257
    fun match_helper [] s = (fn _ => false, TOKEN_ERROR)
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   258
      | match_helper (f::fs) s =
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   259
        if ((fst f) s) then f else match_helper fs s
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   260
    fun match s = match_helper flist s
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   261
    fun tok s =
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   262
        if s = "" then [] else
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   263
        let
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   264
        val h = String.substring (s,0,1)
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   265
        val (f, j) = match h
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   266
        fun len i =
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   267
            if size s = i then i
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   268
            else if f (String.substring (s,0,i+1)) then
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   269
            len (i+1)
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   270
            else i
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   271
        in
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   272
        if j < 0 then
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   273
            (if h = "\\" then []
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   274
             else raise (Load_cplexFile ("token expected, found: "
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   275
                         ^s)))
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   276
        else
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   277
            let
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   278
            val l = len 1
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   279
            val u = String.substring (s,0,l)
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   280
            val v = String.extract (s,l,NONE)
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   281
            in
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   282
            if j = 0 then tok v else (j, u) :: tok v
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   283
            end
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   284
        end
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   285
    in
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   286
    tok s
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   287
    end
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   288
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   289
exception Tokenize of string;
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   290
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   291
fun tokenize_general flist s =
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   292
    let
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   293
    fun match_helper [] s = raise (Tokenize s)
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   294
      | match_helper (f::fs) s =
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   295
        if ((fst f) s) then f else match_helper fs s
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   296
    fun match s = match_helper flist s
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   297
    fun tok s =
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   298
        if s = "" then [] else
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   299
        let
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   300
        val h = String.substring (s,0,1)
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   301
        val (f, j) = match h
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   302
        fun len i =
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   303
            if size s = i then i
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   304
            else if f (String.substring (s,0,i+1)) then
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   305
            len (i+1)
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   306
            else i
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   307
        val l = len 1
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   308
        in
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   309
        (j, String.substring (s,0,l)) :: tok (String.extract (s,l,NONE))
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   310
        end
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   311
    in
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   312
    tok s
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   313
    end
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   314
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   315
fun load_cplexFile name =
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   316
    let
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   317
    val f = TextIO.openIn name
32740
9dd0a2f83429 explicit indication of Unsynchronized.ref;
wenzelm
parents: 32491
diff changeset
   318
        val ignore_NL = Unsynchronized.ref true
9dd0a2f83429 explicit indication of Unsynchronized.ref;
wenzelm
parents: 32491
diff changeset
   319
    val rest = Unsynchronized.ref []
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   320
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   321
    fun is_symbol s c = (fst c) = TOKEN_SYMBOL andalso (to_upper (snd c)) = s
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   322
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   323
    fun readToken_helper () =
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   324
        if length (!rest) > 0 then
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   325
        let val u = hd (!rest) in
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   326
            (
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   327
             rest := tl (!rest);
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   328
             SOME u
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   329
            )
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   330
        end
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   331
        else
23139
aa899bce7c3b TextIO.inputLine: use present SML B library version;
wenzelm
parents: 22951
diff changeset
   332
          (case TextIO.inputLine f of
aa899bce7c3b TextIO.inputLine: use present SML B library version;
wenzelm
parents: 22951
diff changeset
   333
            NONE => NONE
aa899bce7c3b TextIO.inputLine: use present SML B library version;
wenzelm
parents: 22951
diff changeset
   334
          | SOME s =>
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   335
            let val t = tokenize s in
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   336
            if (length t >= 2 andalso
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   337
                snd(hd (tl t)) = ":")
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   338
            then
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   339
                rest := (TOKEN_LABEL, snd (hd t)) :: (tl (tl t))
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   340
            else if (length t >= 2) andalso is_symbol "SUBJECT" (hd (t))
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   341
                andalso is_symbol "TO" (hd (tl t))
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   342
            then
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   343
                rest := (TOKEN_SYMBOL, "ST") :: (tl (tl t))
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   344
            else
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   345
                rest := t;
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   346
            readToken_helper ()
23139
aa899bce7c3b TextIO.inputLine: use present SML B library version;
wenzelm
parents: 22951
diff changeset
   347
            end)
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   348
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   349
    fun readToken_helper2 () =
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   350
        let val c = readToken_helper () in
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   351
            if c = NONE then NONE
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   352
                    else if !ignore_NL andalso fst (the c) = TOKEN_NL then
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   353
            readToken_helper2 ()
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   354
            else if fst (the c) = TOKEN_SYMBOL
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   355
                andalso keyword (snd (the c)) <> NONE
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   356
            then SOME (TOKEN_KEYWORD, the (keyword (snd (the c))))
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   357
            else c
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   358
        end
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   359
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   360
    fun readToken () = readToken_helper2 ()
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   361
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   362
    fun pushToken a = rest := (a::(!rest))
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   363
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   364
    fun is_value token =
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   365
        fst token = TOKEN_NUM orelse (fst token = TOKEN_KEYWORD
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   366
                      andalso snd token = "INF")
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   367
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   368
        fun get_value token =
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   369
        if fst token = TOKEN_NUM then
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   370
        cplexNum (snd token)
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   371
        else if fst token = TOKEN_KEYWORD andalso snd token = "INF"
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   372
        then
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   373
        cplexInf
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   374
        else
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   375
        raise (Load_cplexFile "num expected")
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   376
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   377
    fun readTerm_Product only_num =
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   378
        let val c = readToken () in
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   379
        if c = NONE then NONE
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   380
        else if fst (the c) = TOKEN_SYMBOL
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   381
        then (
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   382
            if only_num then (pushToken (the c); NONE)
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   383
            else SOME (cplexVar (snd (the c)))
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   384
            )
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   385
        else if only_num andalso is_value (the c) then
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   386
            SOME (get_value (the c))
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   387
        else if is_value (the c) then
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   388
            let val t1 = get_value (the c)
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   389
            val d = readToken ()
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   390
            in
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   391
            if d = NONE then SOME t1
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   392
            else if fst (the d) = TOKEN_SYMBOL then
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   393
                SOME (cplexProd (t1, cplexVar (snd (the d))))
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   394
            else
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   395
                (pushToken (the d); SOME t1)
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   396
            end
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   397
        else (pushToken (the c); NONE)
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   398
        end
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   399
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   400
    fun readTerm_Signed only_signed only_num =
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   401
        let
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   402
        val c = readToken ()
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   403
        in
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   404
        if c = NONE then NONE
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   405
        else
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   406
            let val d = the c in
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   407
            if d = (TOKEN_DELIMITER, "+") then
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   408
                readTerm_Product only_num
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   409
             else if d = (TOKEN_DELIMITER, "-") then
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   410
                 SOME (cplexNeg (the (readTerm_Product
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   411
                              only_num)))
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   412
             else (pushToken d;
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   413
                   if only_signed then NONE
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   414
                   else readTerm_Product only_num)
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   415
            end
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   416
        end
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   417
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   418
    fun readTerm_Sum first_signed =
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   419
        let val c = readTerm_Signed first_signed false in
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   420
        if c = NONE then [] else (the c)::(readTerm_Sum true)
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   421
        end
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   422
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   423
    fun readTerm () =
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   424
        let val c = readTerm_Sum false in
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   425
        if c = [] then NONE
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   426
        else if tl c = [] then SOME (hd c)
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   427
        else SOME (cplexSum c)
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   428
        end
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   429
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   430
    fun readLabeledTerm () =
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   431
        let val c = readToken () in
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   432
        if c = NONE then (NONE, NONE)
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   433
        else if fst (the c) = TOKEN_LABEL then
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   434
            let val t = readTerm () in
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   435
            if t = NONE then
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   436
                raise (Load_cplexFile ("term after label "^
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   437
                           (snd (the c))^
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   438
                           " expected"))
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   439
            else (SOME (snd (the c)), t)
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   440
            end
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   441
        else (pushToken (the c); (NONE, readTerm ()))
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   442
        end
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   443
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   444
    fun readGoal () =
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   445
        let
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   446
        val g = readToken ()
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   447
        in
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   448
            if g = SOME (TOKEN_KEYWORD, "MAXIMIZE") then
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   449
            cplexMaximize (the (snd (readLabeledTerm ())))
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   450
        else if g = SOME (TOKEN_KEYWORD, "MINIMIZE") then
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   451
            cplexMinimize (the (snd (readLabeledTerm ())))
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   452
        else raise (Load_cplexFile "MAXIMIZE or MINIMIZE expected")
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   453
        end
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   454
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   455
    fun str2cmp b =
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   456
        (case b of
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   457
         "<" => cplexLe
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   458
           | "<=" => cplexLeq
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   459
           | ">" => cplexGe
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   460
           | ">=" => cplexGeq
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   461
               | "=" => cplexEq
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   462
           | _ => raise (Load_cplexFile (b^" is no TOKEN_CMP")))
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   463
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   464
    fun readConstraint () =
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   465
            let
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   466
        val t = readLabeledTerm ()
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   467
        fun make_constraint b t1 t2 =
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   468
                    cplexConstr
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   469
            (str2cmp b,
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   470
             (t1, t2))
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   471
        in
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   472
        if snd t = NONE then NONE
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   473
        else
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   474
            let val c = readToken () in
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   475
            if c = NONE orelse fst (the c) <> TOKEN_CMP
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   476
            then raise (Load_cplexFile "TOKEN_CMP expected")
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   477
            else
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   478
                let val n = readTerm_Signed false true in
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   479
                if n = NONE then
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   480
                    raise (Load_cplexFile "num expected")
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   481
                else
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   482
                    SOME (fst t,
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   483
                      make_constraint (snd (the c))
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   484
                              (the (snd t))
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   485
                              (the n))
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   486
                end
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   487
            end
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   488
        end
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   489
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   490
        fun readST () =
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   491
        let
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   492
        fun readbody () =
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   493
            let val t = readConstraint () in
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   494
            if t = NONE then []
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   495
            else if (is_normed_Constr (snd (the t))) then
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   496
                (the t)::(readbody ())
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   497
            else if (fst (the t) <> NONE) then
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   498
                raise (Load_cplexFile
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   499
                       ("constraint '"^(the (fst (the t)))^
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   500
                    "'is not normed"))
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   501
            else
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   502
                raise (Load_cplexFile
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   503
                       "constraint is not normed")
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   504
            end
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   505
        in
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   506
        if readToken () = SOME (TOKEN_KEYWORD, "ST")
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   507
        then
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   508
            readbody ()
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   509
        else
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   510
            raise (Load_cplexFile "ST expected")
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   511
        end
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   512
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   513
    fun readCmp () =
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   514
        let val c = readToken () in
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   515
        if c = NONE then NONE
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   516
        else if fst (the c) = TOKEN_CMP then
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   517
            SOME (str2cmp (snd (the c)))
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   518
        else (pushToken (the c); NONE)
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   519
        end
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   520
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   521
    fun skip_NL () =
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   522
        let val c = readToken () in
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   523
        if c <> NONE andalso fst (the c) = TOKEN_NL then
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   524
            skip_NL ()
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   525
        else
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   526
            (pushToken (the c); ())
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   527
        end
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   528
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   529
    fun make_bounds c t1 t2 =
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   530
        cplexBound (t1, c, t2)
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   531
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   532
    fun readBound () =
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   533
        let
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   534
        val _ = skip_NL ()
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   535
        val t1 = readTerm ()
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   536
        in
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   537
        if t1 = NONE then NONE
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   538
        else
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   539
            let
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   540
            val c1 = readCmp ()
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   541
            in
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   542
            if c1 = NONE then
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   543
                let
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   544
                val c = readToken ()
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   545
                in
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   546
                if c = SOME (TOKEN_KEYWORD, "FREE") then
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   547
                    SOME (
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   548
                    cplexBounds (cplexNeg cplexInf,
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   549
                         cplexLeq,
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   550
                         the t1,
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   551
                         cplexLeq,
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   552
                         cplexInf))
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   553
                else
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   554
                    raise (Load_cplexFile "FREE expected")
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   555
                end
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   556
            else
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   557
                let
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   558
                val t2 = readTerm ()
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   559
                in
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   560
                if t2 = NONE then
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   561
                    raise (Load_cplexFile "term expected")
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   562
                else
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   563
                    let val c2 = readCmp () in
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   564
                    if c2 = NONE then
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   565
                        SOME (make_bounds (the c1)
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   566
                                  (the t1)
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   567
                                  (the t2))
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   568
                    else
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   569
                        SOME (
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   570
                        cplexBounds (the t1,
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   571
                             the c1,
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   572
                             the t2,
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   573
                             the c2,
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   574
                             the (readTerm())))
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   575
                    end
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   576
                end
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   577
            end
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   578
        end
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   579
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   580
    fun readBounds () =
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   581
        let
46531
eff798e48efc dropped dead code
haftmann
parents: 43850
diff changeset
   582
        fun makestring _ = "?"
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   583
        fun readbody () =
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   584
            let
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   585
            val b = readBound ()
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   586
            in
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   587
            if b = NONE then []
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   588
            else if (is_normed_Bounds (the b)) then
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   589
                (the b)::(readbody())
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   590
            else (
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   591
                raise (Load_cplexFile
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   592
                       ("bounds are not normed in: "^
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   593
                    (makestring (the b)))))
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   594
            end
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   595
        in
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   596
        if readToken () = SOME (TOKEN_KEYWORD, "BOUNDS") then
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   597
            readbody ()
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   598
        else raise (Load_cplexFile "BOUNDS expected")
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   599
        end
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   600
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   601
        fun readEnd () =
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   602
        if readToken () = SOME (TOKEN_KEYWORD, "END") then ()
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   603
        else raise (Load_cplexFile "END expected")
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   604
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   605
    val result_Goal = readGoal ()
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   606
    val result_ST = readST ()
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   607
    val _ =    ignore_NL := false
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   608
        val result_Bounds = readBounds ()
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   609
        val _ = ignore_NL := true
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   610
        val _ = readEnd ()
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   611
    val _ = TextIO.closeIn f
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   612
    in
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   613
    cplexProg (name, result_Goal, result_ST, result_Bounds)
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   614
    end
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   615
46531
eff798e48efc dropped dead code
haftmann
parents: 43850
diff changeset
   616
fun save_cplexFile filename (cplexProg (_, goal, constraints, bounds)) =
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   617
    let
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   618
    val f = TextIO.openOut filename
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   619
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   620
    fun basic_write s = TextIO.output(f, s)
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   621
32740
9dd0a2f83429 explicit indication of Unsynchronized.ref;
wenzelm
parents: 32491
diff changeset
   622
    val linebuf = Unsynchronized.ref ""
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   623
    fun buf_flushline s =
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   624
        (basic_write (!linebuf);
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   625
         basic_write "\n";
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   626
         linebuf := s)
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   627
    fun buf_add s = linebuf := (!linebuf) ^ s
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   628
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   629
    fun write s =
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   630
        if (String.size s) + (String.size (!linebuf)) >= 250 then
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   631
        buf_flushline ("    "^s)
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   632
        else
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   633
        buf_add s
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   634
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   635
        fun writeln s = (buf_add s; buf_flushline "")
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   636
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   637
    fun write_term (cplexVar x) = write x
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   638
      | write_term (cplexNum x) = write x
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   639
      | write_term cplexInf = write "inf"
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   640
      | write_term (cplexProd (cplexNum "1", b)) = write_term b
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   641
      | write_term (cplexProd (a, b)) =
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   642
        (write_term a; write " "; write_term b)
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   643
          | write_term (cplexNeg x) = (write " - "; write_term x)
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   644
          | write_term (cplexSum ts) = write_terms ts
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   645
    and write_terms [] = ()
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   646
      | write_terms (t::ts) =
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   647
        (if (not (is_Neg t)) then write " + " else ();
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   648
         write_term t; write_terms ts)
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   649
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   650
    fun write_goal (cplexMaximize term) =
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   651
        (writeln "MAXIMIZE"; write_term term; writeln "")
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   652
      | write_goal (cplexMinimize term) =
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   653
        (writeln "MINIMIZE"; write_term term; writeln "")
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   654
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   655
    fun write_cmp cplexLe = write "<"
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   656
      | write_cmp cplexLeq = write "<="
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   657
      | write_cmp cplexEq = write "="
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   658
      | write_cmp cplexGe = write ">"
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   659
      | write_cmp cplexGeq = write ">="
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   660
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   661
    fun write_constr (cplexConstr (cmp, (a,b))) =
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   662
        (write_term a;
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   663
         write " ";
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   664
         write_cmp cmp;
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   665
         write " ";
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   666
         write_term b)
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   667
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   668
    fun write_constraints [] = ()
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   669
      | write_constraints (c::cs) =
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   670
        (if (fst c <> NONE)
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   671
         then
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   672
         (write (the (fst c)); write ": ")
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   673
         else
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   674
         ();
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   675
         write_constr (snd c);
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   676
         writeln "";
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   677
         write_constraints cs)
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   678
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   679
    fun write_bounds [] = ()
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   680
      | write_bounds ((cplexBounds (t1,c1,t2,c2,t3))::bs) =
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   681
        ((if t1 = cplexNeg cplexInf andalso t3 = cplexInf
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   682
         andalso (c1 = cplexLeq orelse c1 = cplexLe)
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   683
         andalso (c2 = cplexLeq orelse c2 = cplexLe)
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   684
          then
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   685
          (write_term t2; write " free")
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   686
          else
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   687
          (write_term t1; write " "; write_cmp c1; write " ";
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   688
           write_term t2; write " "; write_cmp c2; write " ";
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   689
           write_term t3)
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   690
         ); writeln ""; write_bounds bs)
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   691
      | write_bounds ((cplexBound (t1, c, t2)) :: bs) =
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   692
        (write_term t1; write " ";
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   693
         write_cmp c; write " ";
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   694
         write_term t2; writeln ""; write_bounds bs)
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   695
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   696
    val _ = write_goal goal
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   697
        val _ = (writeln ""; writeln "ST")
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   698
    val _ = write_constraints constraints
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   699
        val _ = (writeln ""; writeln "BOUNDS")
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   700
    val _ = write_bounds bounds
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   701
        val _ = (writeln ""; writeln "END")
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   702
        val _ = TextIO.closeOut f
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   703
    in
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   704
    ()
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   705
    end
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   706
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   707
fun norm_Constr (constr as cplexConstr (c, (t1, t2))) =
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   708
    if not (modulo_signed is_Num t2) andalso
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   709
       modulo_signed is_Num t1
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   710
    then
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   711
    [cplexConstr (rev_cmp c, (t2, t1))]
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   712
    else if (c = cplexLe orelse c = cplexLeq) andalso
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   713
        (t1 = (cplexNeg cplexInf) orelse t2 = cplexInf)
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   714
    then
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   715
    []
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   716
    else if (c = cplexGe orelse c = cplexGeq) andalso
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   717
        (t1 = cplexInf orelse t2 = cplexNeg cplexInf)
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   718
    then
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   719
    []
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   720
    else
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   721
    [constr]
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   722
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   723
fun bound2constr (cplexBounds (t1,c1,t2,c2,t3)) =
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   724
    (norm_Constr(cplexConstr (c1, (t1, t2))))
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   725
    @ (norm_Constr(cplexConstr (c2, (t2, t3))))
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   726
  | bound2constr (cplexBound (t1, cplexEq, t2)) =
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   727
    (norm_Constr(cplexConstr (cplexLeq, (t1, t2))))
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   728
    @ (norm_Constr(cplexConstr (cplexLeq, (t2, t1))))
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   729
  | bound2constr (cplexBound (t1, c1, t2)) =
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   730
    norm_Constr(cplexConstr (c1, (t1,t2)))
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   731
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   732
val emptyset = Symtab.empty
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   733
17412
e26cb20ef0cc TableFun/Symtab: curried lookup and update;
wenzelm
parents: 16966
diff changeset
   734
fun singleton v = Symtab.update (v, ()) emptyset
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   735
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   736
fun merge a b = Symtab.merge (op =) (a, b)
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   737
21056
2cfe839e8d58 Symtab.foldl replaced by Symtab.fold
haftmann
parents: 17521
diff changeset
   738
fun mergemap f ts = fold (fn x => fn table => merge table (f x)) ts Symtab.empty
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   739
37116
e32cc5958282 tuned -- avoid catch-all exception pattern;
wenzelm
parents: 35010
diff changeset
   740
fun diff a b = Symtab.fold (Symtab.delete_safe o fst) b a
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   741
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   742
fun collect_vars (cplexVar v) = singleton v
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   743
  | collect_vars (cplexNeg t) = collect_vars t
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   744
  | collect_vars (cplexProd (t1, t2)) =
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   745
    merge (collect_vars t1) (collect_vars t2)
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   746
  | collect_vars (cplexSum ts) = mergemap collect_vars ts
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   747
  | collect_vars _ = emptyset
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   748
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   749
(* Eliminates all nonfree bounds from the linear program and produces an
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   750
   equivalent program with only free bounds
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   751
   IF for the input program P holds: is_normed_cplexProg P *)
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   752
fun elim_nonfree_bounds (cplexProg (name, goal, constraints, bounds)) =
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   753
    let
46531
eff798e48efc dropped dead code
haftmann
parents: 43850
diff changeset
   754
    fun collect_constr_vars (_, cplexConstr (_, (t1,_))) =
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   755
        (collect_vars t1)
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   756
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   757
    val cvars = merge (collect_vars (term_of_goal goal))
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   758
              (mergemap collect_constr_vars constraints)
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   759
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   760
    fun collect_lower_bounded_vars
46531
eff798e48efc dropped dead code
haftmann
parents: 43850
diff changeset
   761
        (cplexBounds (_, _, cplexVar v, _, _)) =
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   762
        singleton v
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   763
      |  collect_lower_bounded_vars
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   764
         (cplexBound (_, cplexLe, cplexVar v)) =
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   765
         singleton v
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   766
      |  collect_lower_bounded_vars
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   767
         (cplexBound (_, cplexLeq, cplexVar v)) =
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   768
         singleton v
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   769
      |  collect_lower_bounded_vars
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   770
         (cplexBound (cplexVar v, cplexGe,_)) =
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   771
         singleton v
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   772
      |  collect_lower_bounded_vars
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   773
         (cplexBound (cplexVar v, cplexGeq, _)) =
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   774
         singleton v
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   775
      | collect_lower_bounded_vars
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   776
        (cplexBound (cplexVar v, cplexEq, _)) =
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   777
        singleton v
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   778
      |  collect_lower_bounded_vars _ = emptyset
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   779
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   780
    val lvars = mergemap collect_lower_bounded_vars bounds
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   781
    val positive_vars = diff cvars lvars
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   782
    val zero = cplexNum "0"
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   783
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   784
    fun make_pos_constr v =
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   785
        (NONE, cplexConstr (cplexGeq, ((cplexVar v), zero)))
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   786
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   787
    fun make_free_bound v =
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   788
        cplexBounds (cplexNeg cplexInf, cplexLeq,
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   789
             cplexVar v, cplexLeq,
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   790
             cplexInf)
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   791
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   792
    val pos_constrs = rev (Symtab.fold
46531
eff798e48efc dropped dead code
haftmann
parents: 43850
diff changeset
   793
                  (fn (k, _) => cons (make_pos_constr k))
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   794
                  positive_vars [])
21056
2cfe839e8d58 Symtab.foldl replaced by Symtab.fold
haftmann
parents: 17521
diff changeset
   795
        val bound_constrs = map (pair NONE)
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   796
                (maps bound2constr bounds)
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   797
    val constraints' = constraints @ pos_constrs @ bound_constrs
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   798
    val bounds' = rev (Symtab.fold (fn (v, _) => cons (make_free_bound v)) cvars []);
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   799
    in
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   800
    cplexProg (name, goal, constraints', bounds')
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   801
    end
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   802
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   803
fun relax_strict_ineqs (cplexProg (name, goals, constrs, bounds)) =
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   804
    let
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   805
    fun relax cplexLe = cplexLeq
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   806
      | relax cplexGe = cplexGeq
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   807
      | relax x = x
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   808
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   809
    fun relax_constr (n, cplexConstr(c, (t1, t2))) =
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   810
        (n, cplexConstr(relax c, (t1, t2)))
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   811
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   812
    fun relax_bounds (cplexBounds (t1, c1, t2, c2, t3)) =
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   813
        cplexBounds (t1, relax c1, t2, relax c2, t3)
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   814
      | relax_bounds (cplexBound (t1, c, t2)) =
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   815
        cplexBound (t1, relax c, t2)
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   816
    in
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   817
    cplexProg (name,
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   818
           goals,
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   819
           map relax_constr constrs,
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   820
           map relax_bounds bounds)
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   821
    end
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   822
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   823
datatype cplexResult = Unbounded
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   824
             | Infeasible
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   825
             | Undefined
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   826
             | Optimal of string * ((string * string) list)
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   827
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   828
fun is_separator x = forall (fn c => c = #"-") (String.explode x)
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   829
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   830
fun is_sign x = (x = "+" orelse x = "-")
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   831
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   832
fun is_colon x = (x = ":")
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   833
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   834
fun is_resultsymbol a =
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   835
    let
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   836
    val symbol_char = String.explode "!\"#$%&()/,.;?@_`'{}|~-"
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   837
    fun is_symbol_char c = Char.isAlphaNum c orelse
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   838
                   exists (fn d => d=c) symbol_char
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   839
    fun is_symbol_start c = is_symbol_char c andalso
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   840
                not (Char.isDigit c) andalso
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   841
                not (c= #".") andalso
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   842
                not (c= #"-")
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   843
    val b = String.explode a
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   844
    in
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   845
    b <> [] andalso is_symbol_start (hd b) andalso
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   846
    forall is_symbol_char b
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   847
    end
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   848
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   849
val TOKEN_SIGN = 100
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   850
val TOKEN_COLON = 101
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   851
val TOKEN_SEPARATOR = 102
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   852
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   853
fun load_glpkResult name =
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   854
    let
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   855
    val flist = [(is_NL, TOKEN_NL),
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   856
             (is_blank, TOKEN_BLANK),
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   857
             (is_num, TOKEN_NUM),
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   858
             (is_sign, TOKEN_SIGN),
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   859
                     (is_colon, TOKEN_COLON),
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   860
             (is_cmp, TOKEN_CMP),
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   861
             (is_resultsymbol, TOKEN_SYMBOL),
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   862
             (is_separator, TOKEN_SEPARATOR)]
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   863
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   864
    val tokenize = tokenize_general flist
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   865
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   866
    val f = TextIO.openIn name
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   867
32740
9dd0a2f83429 explicit indication of Unsynchronized.ref;
wenzelm
parents: 32491
diff changeset
   868
    val rest = Unsynchronized.ref []
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   869
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   870
    fun readToken_helper () =
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   871
        if length (!rest) > 0 then
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   872
        let val u = hd (!rest) in
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   873
            (
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   874
             rest := tl (!rest);
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   875
             SOME u
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   876
            )
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   877
        end
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   878
        else
23139
aa899bce7c3b TextIO.inputLine: use present SML B library version;
wenzelm
parents: 22951
diff changeset
   879
        (case TextIO.inputLine f of
aa899bce7c3b TextIO.inputLine: use present SML B library version;
wenzelm
parents: 22951
diff changeset
   880
          NONE => NONE
aa899bce7c3b TextIO.inputLine: use present SML B library version;
wenzelm
parents: 22951
diff changeset
   881
        | SOME s => (rest := tokenize s; readToken_helper()))
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   882
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   883
    fun is_tt tok ty = (tok <> NONE andalso (fst (the tok)) = ty)
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   884
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   885
    fun pushToken a = if a = NONE then () else (rest := ((the a)::(!rest)))
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   886
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   887
    fun readToken () =
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   888
        let val t = readToken_helper () in
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   889
        if is_tt t TOKEN_BLANK then
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   890
            readToken ()
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   891
        else if is_tt t TOKEN_NL then
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   892
            let val t2 = readToken_helper () in
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   893
            if is_tt t2 TOKEN_SIGN then
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   894
                (pushToken (SOME (TOKEN_SEPARATOR, "-")); t)
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   895
            else
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   896
                (pushToken t2; t)
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   897
            end
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   898
        else if is_tt t TOKEN_SIGN then
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   899
            let val t2 = readToken_helper () in
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   900
            if is_tt t2 TOKEN_NUM then
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   901
                (SOME (TOKEN_NUM, (snd (the t))^(snd (the t2))))
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   902
            else
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   903
                (pushToken t2; t)
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   904
            end
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   905
        else
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   906
            t
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   907
        end
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   908
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   909
        fun readRestOfLine P =
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   910
        let
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   911
        val t = readToken ()
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   912
        in
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   913
        if is_tt t TOKEN_NL orelse t = NONE
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   914
        then P
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   915
        else readRestOfLine P
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   916
        end
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   917
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   918
    fun readHeader () =
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   919
        let
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   920
        fun readStatus () = readRestOfLine ("STATUS", snd (the (readToken ())))
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   921
        fun readObjective () = readRestOfLine ("OBJECTIVE", snd (the (readToken (); readToken (); readToken ())))
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   922
        val t1 = readToken ()
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   923
        val t2 = readToken ()
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   924
        in
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   925
        if is_tt t1 TOKEN_SYMBOL andalso is_tt t2 TOKEN_COLON
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   926
        then
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   927
            case to_upper (snd (the t1)) of
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   928
            "STATUS" => (readStatus ())::(readHeader ())
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   929
              | "OBJECTIVE" => (readObjective())::(readHeader ())
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   930
              | _ => (readRestOfLine (); readHeader ())
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   931
        else
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   932
            (pushToken t2; pushToken t1; [])
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   933
        end
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   934
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   935
    fun skip_until_sep () =
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   936
        let val x = readToken () in
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   937
        if is_tt x TOKEN_SEPARATOR then
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   938
            readRestOfLine ()
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   939
        else
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   940
            skip_until_sep ()
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   941
        end
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   942
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   943
    fun load_value () =
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   944
        let
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   945
        val t1 = readToken ()
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   946
        val t2 = readToken ()
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   947
        in
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   948
        if is_tt t1 TOKEN_NUM andalso is_tt t2 TOKEN_SYMBOL then
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   949
            let
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   950
            val t = readToken ()
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   951
            val state = if is_tt t TOKEN_NL then readToken () else t
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   952
            val _ = if is_tt state TOKEN_SYMBOL then () else raise (Load_cplexResult "state expected")
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   953
            val k = readToken ()
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   954
            in
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   955
            if is_tt k TOKEN_NUM then
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   956
                readRestOfLine (SOME (snd (the t2), snd (the k)))
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   957
            else
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   958
                raise (Load_cplexResult "number expected")
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   959
            end
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   960
        else
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   961
            (pushToken t2; pushToken t1; NONE)
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   962
        end
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   963
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   964
    fun load_values () =
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   965
        let val v = load_value () in
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   966
        if v = NONE then [] else (the v)::(load_values ())
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   967
        end
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   968
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   969
    val header = readHeader ()
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   970
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   971
    val result =
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   972
        case AList.lookup (op =) header "STATUS" of
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   973
        SOME "INFEASIBLE" => Infeasible
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   974
          | SOME "UNBOUNDED" => Unbounded
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   975
          | SOME "OPTIMAL" => Optimal (the (AList.lookup (op =) header "OBJECTIVE"),
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   976
                       (skip_until_sep ();
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   977
                        skip_until_sep ();
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   978
                        load_values ()))
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   979
          | _ => Undefined
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   980
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   981
    val _ = TextIO.closeIn f
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   982
    in
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   983
    result
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   984
    end
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   985
    handle (Tokenize s) => raise (Load_cplexResult ("Tokenize: "^s))
51930
52fd62618631 prefer explicitly qualified exceptions, which is particular important for robust handlers;
wenzelm
parents: 50902
diff changeset
   986
     | Option.Option => raise (Load_cplexResult "Option")
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   987
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   988
fun load_cplexResult name =
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   989
    let
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   990
    val flist = [(is_NL, TOKEN_NL),
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   991
             (is_blank, TOKEN_BLANK),
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   992
             (is_num, TOKEN_NUM),
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   993
             (is_sign, TOKEN_SIGN),
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   994
                     (is_colon, TOKEN_COLON),
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   995
             (is_cmp, TOKEN_CMP),
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   996
             (is_resultsymbol, TOKEN_SYMBOL)]
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   997
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   998
    val tokenize = tokenize_general flist
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
   999
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
  1000
    val f = TextIO.openIn name
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
  1001
32740
9dd0a2f83429 explicit indication of Unsynchronized.ref;
wenzelm
parents: 32491
diff changeset
  1002
    val rest = Unsynchronized.ref []
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
  1003
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
  1004
    fun readToken_helper () =
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
  1005
        if length (!rest) > 0 then
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
  1006
        let val u = hd (!rest) in
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
  1007
            (
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
  1008
             rest := tl (!rest);
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
  1009
             SOME u
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
  1010
            )
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
  1011
        end
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
  1012
        else
23139
aa899bce7c3b TextIO.inputLine: use present SML B library version;
wenzelm
parents: 22951
diff changeset
  1013
        (case TextIO.inputLine f of
aa899bce7c3b TextIO.inputLine: use present SML B library version;
wenzelm
parents: 22951
diff changeset
  1014
          NONE => NONE
aa899bce7c3b TextIO.inputLine: use present SML B library version;
wenzelm
parents: 22951
diff changeset
  1015
        | SOME s => (rest := tokenize s; readToken_helper()))
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
  1016
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
  1017
    fun is_tt tok ty = (tok <> NONE andalso (fst (the tok)) = ty)
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
  1018
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
  1019
    fun pushToken a = if a = NONE then () else (rest := ((the a)::(!rest)))
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
  1020
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
  1021
    fun readToken () =
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
  1022
        let val t = readToken_helper () in
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
  1023
        if is_tt t TOKEN_BLANK then
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
  1024
            readToken ()
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
  1025
        else if is_tt t TOKEN_SIGN then
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
  1026
            let val t2 = readToken_helper () in
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
  1027
            if is_tt t2 TOKEN_NUM then
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
  1028
                (SOME (TOKEN_NUM, (snd (the t))^(snd (the t2))))
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
  1029
            else
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
  1030
                (pushToken t2; t)
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
  1031
            end
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
  1032
        else
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
  1033
            t
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
  1034
        end
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
  1035
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
  1036
        fun readRestOfLine P =
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
  1037
        let
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
  1038
        val t = readToken ()
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
  1039
        in
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
  1040
        if is_tt t TOKEN_NL orelse t = NONE
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
  1041
        then P
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
  1042
        else readRestOfLine P
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
  1043
        end
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
  1044
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
  1045
    fun readHeader () =
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
  1046
        let
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
  1047
        fun readStatus () = readRestOfLine ("STATUS", snd (the (readToken ())))
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
  1048
        fun readObjective () =
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
  1049
            let
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
  1050
            val t = readToken ()
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
  1051
            in
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
  1052
            if is_tt t TOKEN_SYMBOL andalso to_upper (snd (the t)) = "VALUE" then
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
  1053
                readRestOfLine ("OBJECTIVE", snd (the (readToken())))
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
  1054
            else
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
  1055
                readRestOfLine ("OBJECTIVE_NAME", snd (the t))
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
  1056
            end
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
  1057
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
  1058
        val t = readToken ()
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
  1059
        in
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
  1060
        if is_tt t TOKEN_SYMBOL then
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
  1061
            case to_upper (snd (the t)) of
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
  1062
            "STATUS" => (readStatus ())::(readHeader ())
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
  1063
              | "OBJECTIVE" => (readObjective ())::(readHeader ())
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
  1064
              | "SECTION" => (pushToken t; [])
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
  1065
              | _ => (readRestOfLine (); readHeader ())
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
  1066
        else
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
  1067
            (readRestOfLine (); readHeader ())
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
  1068
        end
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
  1069
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
  1070
    fun skip_nls () =
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
  1071
        let val x = readToken () in
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
  1072
        if is_tt x TOKEN_NL then
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
  1073
            skip_nls ()
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
  1074
        else
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
  1075
            (pushToken x; ())
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
  1076
        end
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
  1077
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
  1078
    fun skip_paragraph () =
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
  1079
        if is_tt (readToken ()) TOKEN_NL then
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
  1080
        (if is_tt (readToken ()) TOKEN_NL then
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
  1081
             skip_nls ()
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
  1082
         else
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
  1083
             skip_paragraph ())
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
  1084
        else
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
  1085
        skip_paragraph ()
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
  1086
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
  1087
    fun load_value () =
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
  1088
        let
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
  1089
        val t1 = readToken ()
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
  1090
        val t1 = if is_tt t1 TOKEN_SYMBOL andalso snd (the t1) = "A" then readToken () else t1
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
  1091
        in
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
  1092
        if is_tt t1 TOKEN_NUM then
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
  1093
            let
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
  1094
            val name = readToken ()
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
  1095
            val status = readToken ()
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
  1096
            val value = readToken ()
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
  1097
            in
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
  1098
            if is_tt name TOKEN_SYMBOL andalso
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
  1099
               is_tt status TOKEN_SYMBOL andalso
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
  1100
               is_tt value TOKEN_NUM
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
  1101
            then
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
  1102
                readRestOfLine (SOME (snd (the name), snd (the value)))
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
  1103
            else
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
  1104
                raise (Load_cplexResult "column line expected")
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
  1105
            end
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
  1106
        else
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
  1107
            (pushToken t1; NONE)
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
  1108
        end
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
  1109
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
  1110
    fun load_values () =
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
  1111
        let val v = load_value () in
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
  1112
        if v = NONE then [] else (the v)::(load_values ())
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
  1113
        end
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
  1114
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
  1115
    val header = readHeader ()
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
  1116
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
  1117
    val result =
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
  1118
        case AList.lookup (op =) header "STATUS" of
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
  1119
        SOME "INFEASIBLE" => Infeasible
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
  1120
          | SOME "NONOPTIMAL" => Unbounded
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
  1121
          | SOME "OPTIMAL" => Optimal (the (AList.lookup (op =) header "OBJECTIVE"),
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
  1122
                       (skip_paragraph ();
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
  1123
                        skip_paragraph ();
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
  1124
                        skip_paragraph ();
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
  1125
                        skip_paragraph ();
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
  1126
                        skip_paragraph ();
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
  1127
                        load_values ()))
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
  1128
          | _ => Undefined
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
  1129
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
  1130
    val _ = TextIO.closeIn f
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
  1131
    in
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
  1132
    result
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
  1133
    end
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
  1134
    handle (Tokenize s) => raise (Load_cplexResult ("Tokenize: "^s))
51930
52fd62618631 prefer explicitly qualified exceptions, which is particular important for robust handlers;
wenzelm
parents: 50902
diff changeset
  1135
     | Option.Option => raise (Load_cplexResult "Option")
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
  1136
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
  1137
exception Execute of string;
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
  1138
43602
8c89a1fb30f2 standardized use of Path operations;
wenzelm
parents: 41491
diff changeset
  1139
fun tmp_file s = Path.implode (Path.expand (File.tmp_path (Path.basic s)));
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
  1140
fun wrap s = "\""^s^"\"";
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
  1141
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
  1142
fun solve_glpk prog =
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
  1143
    let
41491
a2ad5b824051 eliminated Int.toString;
wenzelm
parents: 41490
diff changeset
  1144
    val name = string_of_int (Time.toMicroseconds (Time.now ()))
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
  1145
    val lpname = tmp_file (name^".lp")
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
  1146
    val resultname = tmp_file (name^".txt")
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
  1147
    val _ = save_cplexFile lpname prog
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
  1148
    val cplex_path = getenv "GLPK_PATH"
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
  1149
    val cplex = if cplex_path = "" then "glpsol" else cplex_path
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
  1150
    val command = (wrap cplex)^" --lpt "^(wrap lpname)^" --output "^(wrap resultname)
43850
7f2cbc713344 moved bash operations to Isabelle_System (cf. Scala version);
wenzelm
parents: 43602
diff changeset
  1151
    val answer = #1 (Isabelle_System.bash_output command)
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
  1152
    in
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
  1153
    let
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
  1154
        val result = load_glpkResult resultname
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
  1155
        val _ = OS.FileSys.remove lpname
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
  1156
        val _ = OS.FileSys.remove resultname
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
  1157
    in
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
  1158
        result
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
  1159
    end
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
  1160
    handle (Load_cplexResult s) => raise (Execute ("Load_cplexResult: "^s^"\nExecute: "^answer))
62505
9e2a65912111 clarified modules;
wenzelm
parents: 51940
diff changeset
  1161
         | exn => if Exn.is_interrupt exn then Exn.reraise exn else raise (Execute answer)
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
  1162
    end
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
  1163
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
  1164
fun solve_cplex prog =
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
  1165
    let
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
  1166
    fun write_script s lp r =
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
  1167
        let
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
  1168
        val f = TextIO.openOut s
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
  1169
        val _ = TextIO.output (f, "read\n"^lp^"\noptimize\nwrite\n"^r^"\nquit")
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
  1170
        val _ = TextIO.closeOut f
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
  1171
        in
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
  1172
        ()
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
  1173
        end
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
  1174
41491
a2ad5b824051 eliminated Int.toString;
wenzelm
parents: 41490
diff changeset
  1175
    val name = string_of_int (Time.toMicroseconds (Time.now ()))
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
  1176
    val lpname = tmp_file (name^".lp")
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
  1177
    val resultname = tmp_file (name^".txt")
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
  1178
    val scriptname = tmp_file (name^".script")
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
  1179
    val _ = save_cplexFile lpname prog
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
  1180
    val _ = write_script scriptname lpname resultname
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
  1181
    in
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
  1182
    let
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
  1183
        val result = load_cplexResult resultname
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
  1184
        val _ = OS.FileSys.remove lpname
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
  1185
        val _ = OS.FileSys.remove resultname
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
  1186
        val _ = OS.FileSys.remove scriptname
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
  1187
    in
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
  1188
        result
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
  1189
    end
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
  1190
    end
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
  1191
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
  1192
fun solve prog =
16966
37e34f315057 1. changed configuration variables for linear programming (Cplex_tools):
obua
parents: 16873
diff changeset
  1193
    case get_solver () of
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
  1194
      SOLVER_DEFAULT =>
16966
37e34f315057 1. changed configuration variables for linear programming (Cplex_tools):
obua
parents: 16873
diff changeset
  1195
        (case getenv "LP_SOLVER" of
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
  1196
       "CPLEX" => solve_cplex prog
16966
37e34f315057 1. changed configuration variables for linear programming (Cplex_tools):
obua
parents: 16873
diff changeset
  1197
         | "GLPK" => solve_glpk prog
37e34f315057 1. changed configuration variables for linear programming (Cplex_tools):
obua
parents: 16873
diff changeset
  1198
         | _ => raise (Execute ("LP_SOLVER must be set to CPLEX or to GLPK")))
37e34f315057 1. changed configuration variables for linear programming (Cplex_tools):
obua
parents: 16873
diff changeset
  1199
    | SOLVER_CPLEX => solve_cplex prog
37e34f315057 1. changed configuration variables for linear programming (Cplex_tools):
obua
parents: 16873
diff changeset
  1200
    | SOLVER_GLPK => solve_glpk prog
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 21858
diff changeset
  1201
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
  1202
end;