src/HOL/Matrix/CplexMatrixConverter.ML
author wenzelm
Fri Dec 17 17:43:54 2010 +0100 (2010-12-17)
changeset 41229 d797baa3d57c
parent 37788 261c61fabc98
child 46531 eff798e48efc
permissions -rw-r--r--
replaced command 'nonterminals' by slightly modernized version 'nonterminal';
haftmann@37788
     1
(*  Title:      HOL/Matrix/CplexMatrixConverter.ML
obua@16784
     2
    Author:     Steven Obua
obua@16784
     3
*)
obua@16784
     4
obua@16784
     5
signature MATRIX_BUILDER =
obua@16784
     6
sig
obua@16784
     7
    type vector
obua@16784
     8
    type matrix
obua@16784
     9
    
obua@16784
    10
    val empty_vector : vector
obua@16784
    11
    val empty_matrix : matrix
haftmann@22951
    12
obua@16784
    13
    exception Nat_expected of int
haftmann@22951
    14
    val set_elem : vector -> int -> string -> vector
obua@16784
    15
    val set_vector : matrix -> int -> vector -> matrix
obua@16784
    16
end;
obua@16784
    17
obua@16784
    18
signature CPLEX_MATRIX_CONVERTER = 
obua@16784
    19
sig
obua@16784
    20
    structure cplex : CPLEX
obua@16784
    21
    structure matrix_builder : MATRIX_BUILDER 
obua@16784
    22
    type vector = matrix_builder.vector
obua@16784
    23
    type matrix = matrix_builder.matrix
obua@16784
    24
    type naming = int * (int -> string) * (string -> int)
obua@16784
    25
    
obua@16784
    26
    exception Converter of string
obua@16784
    27
obua@16784
    28
    (* program must fulfill is_normed_cplexProg and must be an element of the image of elim_nonfree_bounds *)
obua@16784
    29
    (* convert_prog maximize c A b naming *)
obua@16784
    30
    val convert_prog : cplex.cplexProg -> bool * vector * matrix * vector * naming
obua@16784
    31
obua@16784
    32
    (* results must be optimal, converts_results returns the optimal value as string and the solution as vector *)
obua@16784
    33
    (* convert_results results name2index *)
obua@16784
    34
    val convert_results : cplex.cplexResult -> (string -> int) -> string * vector
obua@16784
    35
end;
obua@16784
    36
obua@16784
    37
functor MAKE_CPLEX_MATRIX_CONVERTER (structure cplex: CPLEX and matrix_builder: MATRIX_BUILDER) : CPLEX_MATRIX_CONVERTER =
obua@16784
    38
struct
obua@16784
    39
obua@16784
    40
structure cplex = cplex
obua@16784
    41
structure matrix_builder = matrix_builder
obua@16784
    42
type matrix = matrix_builder.matrix
haftmann@22951
    43
type vector = matrix_builder.vector
obua@16784
    44
type naming = int * (int -> string) * (string -> int)
haftmann@22951
    45
obua@16784
    46
open matrix_builder 
obua@16784
    47
open cplex
haftmann@22951
    48
obua@16784
    49
exception Converter of string;
obua@16784
    50
obua@16784
    51
fun neg_term (cplexNeg t) = t
obua@16784
    52
  | neg_term (cplexSum ts) = cplexSum (map neg_term ts)
obua@16784
    53
  | neg_term t = cplexNeg t 
haftmann@22951
    54
obua@16784
    55
fun convert_prog (cplexProg (s, goal, constrs, bounds)) = 
haftmann@22951
    56
    let        
haftmann@22951
    57
        fun build_naming index i2s s2i [] = (index, i2s, s2i)
haftmann@22951
    58
          | build_naming index i2s s2i (cplexBounds (cplexNeg cplexInf, cplexLeq, cplexVar v, cplexLeq, cplexInf)::bounds)
haftmann@22951
    59
            = build_naming (index+1) (Inttab.update (index, v) i2s) (Symtab.update_new (v, index) s2i) bounds
haftmann@22951
    60
          | build_naming _ _ _ _ = raise (Converter "nonfree bound")
obua@16784
    61
haftmann@22951
    62
        val (varcount, i2s_tab, s2i_tab) = build_naming 0 Inttab.empty Symtab.empty bounds
obua@16784
    63
haftmann@22951
    64
        fun i2s i = case Inttab.lookup i2s_tab i of NONE => raise (Converter "index not found")
haftmann@22951
    65
                                                     | SOME n => n
haftmann@22951
    66
        fun s2i s = case Symtab.lookup s2i_tab s of NONE => raise (Converter ("name not found: "^s))
haftmann@22951
    67
                                                     | SOME i => i
haftmann@22951
    68
        fun num2str positive (cplexNeg t) = num2str (not positive) t
haftmann@22951
    69
          | num2str positive (cplexNum num) = if positive then num else "-"^num                        
haftmann@22951
    70
          | num2str _ _ = raise (Converter "term is not a (possibly signed) number")
obua@16784
    71
haftmann@22951
    72
        fun setprod vec positive (cplexNeg t) = setprod vec (not positive) t  
haftmann@22951
    73
          | setprod vec positive (cplexVar v) = set_elem vec (s2i v) (if positive then "1" else "-1")
haftmann@22951
    74
          | setprod vec positive (cplexProd (cplexNum num, cplexVar v)) = 
haftmann@22951
    75
            set_elem vec (s2i v) (if positive then num else "-"^num)
haftmann@22951
    76
          | setprod _ _ _ = raise (Converter "term is not a normed product")        
obua@16784
    77
wenzelm@33245
    78
        fun sum2vec (cplexSum ts) = fold (fn t => fn vec => setprod vec true t) ts empty_vector
haftmann@22951
    79
          | sum2vec t = setprod empty_vector true t                                                
obua@16784
    80
haftmann@22951
    81
        fun constrs2Ab j A b [] = (A, b)
haftmann@22951
    82
          | constrs2Ab j A b ((_, cplexConstr (cplexLeq, (t1,t2)))::cs) = 
haftmann@22951
    83
            constrs2Ab (j+1) (set_vector A j (sum2vec t1)) (set_elem b j (num2str true t2)) cs
haftmann@22951
    84
          | constrs2Ab j A b ((_, cplexConstr (cplexGeq, (t1,t2)))::cs) = 
haftmann@22951
    85
            constrs2Ab (j+1) (set_vector A j (sum2vec (neg_term t1))) (set_elem b j (num2str true (neg_term t2))) cs
haftmann@22951
    86
          | constrs2Ab j A b ((_, cplexConstr (cplexEq, (t1,t2)))::cs) =
haftmann@22951
    87
            constrs2Ab j A b ((NONE, cplexConstr (cplexLeq, (t1,t2)))::
haftmann@22951
    88
                              (NONE, cplexConstr (cplexGeq, (t1, t2)))::cs)
haftmann@22951
    89
          | constrs2Ab _ _ _ _ = raise (Converter "no strict constraints allowed")
obua@16784
    90
haftmann@22951
    91
        val (A, b) = constrs2Ab 0 empty_matrix empty_vector constrs
haftmann@22951
    92
                                                                 
haftmann@22951
    93
        val (goal_maximize, goal_term) = 
haftmann@22951
    94
            case goal of
haftmann@22951
    95
                (cplexMaximize t) => (true, t)
haftmann@22951
    96
              | (cplexMinimize t) => (false, t)                                     
haftmann@22951
    97
    in          
haftmann@22951
    98
        (goal_maximize, sum2vec goal_term, A, b, (varcount, i2s, s2i))
obua@16784
    99
    end
obua@16784
   100
obua@16784
   101
fun convert_results (cplex.Optimal (opt, entries)) name2index =
obua@16784
   102
    let
wenzelm@33245
   103
        fun setv (name, value) v = matrix_builder.set_elem v (name2index name) value
obua@16784
   104
    in
wenzelm@33245
   105
        (opt, fold setv entries (matrix_builder.empty_vector))
obua@16784
   106
    end
obua@16784
   107
  | convert_results _ _ = raise (Converter "No optimal result")
obua@16784
   108
obua@16784
   109
end;
obua@16784
   110
obua@16784
   111
structure SimpleMatrixBuilder : MATRIX_BUILDER = 
obua@16784
   112
struct
obua@16784
   113
type vector = (int * string) list
obua@16784
   114
type matrix = (int * vector) list
obua@16784
   115
obua@16784
   116
val empty_matrix = []
obua@16784
   117
val empty_vector = []
obua@16784
   118
obua@16784
   119
exception Nat_expected of int;
obua@16784
   120
obua@16784
   121
fun set_elem v i s = v @ [(i, s)] 
obua@16784
   122
obua@16784
   123
fun set_vector m i v = m @ [(i, v)]
obua@16784
   124
haftmann@22951
   125
end;
obua@16784
   126
haftmann@22951
   127
structure SimpleCplexMatrixConverter =
haftmann@22951
   128
  MAKE_CPLEX_MATRIX_CONVERTER(structure cplex = Cplex and matrix_builder = SimpleMatrixBuilder);