src/HOL/Matrix/cplex/FloatSparseMatrixBuilder.ML
author obua
Fri, 20 Jul 2007 15:29:25 +0200
changeset 23883 7d5aa704454e
parent 23665 825bea0266db
child 24302 3045683749af
permissions -rw-r--r--
new functions cut_matrix', etc.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
     1
(*  Title:      HOL/Matrix/cplex/FloatSparseMatrixBuilder.ML
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
     2
    ID:         $Id$
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
     3
    Author:     Steven Obua
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
     4
*)
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
     5
22964
2284e0d02e7f reorganized float arithmetic
haftmann
parents: 22951
diff changeset
     6
signature FLOAT_SPARSE_MATIRX_BUILDER =
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
     7
sig
22964
2284e0d02e7f reorganized float arithmetic
haftmann
parents: 22951
diff changeset
     8
  include MATRIX_BUILDER
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
     9
22964
2284e0d02e7f reorganized float arithmetic
haftmann
parents: 22951
diff changeset
    10
  structure cplex : CPLEX
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
    11
22964
2284e0d02e7f reorganized float arithmetic
haftmann
parents: 22951
diff changeset
    12
  type float = Float.float
2284e0d02e7f reorganized float arithmetic
haftmann
parents: 22951
diff changeset
    13
  val approx_value : int -> (float -> float) -> string -> term * term
2284e0d02e7f reorganized float arithmetic
haftmann
parents: 22951
diff changeset
    14
  val approx_vector : int -> (float -> float) -> vector -> term * term
2284e0d02e7f reorganized float arithmetic
haftmann
parents: 22951
diff changeset
    15
  val approx_matrix : int -> (float -> float) -> matrix -> term * term
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
    16
23261
85f27f79232f tuned integers
haftmann
parents: 22964
diff changeset
    17
  val mk_spvec_entry : integer -> float -> term
85f27f79232f tuned integers
haftmann
parents: 22964
diff changeset
    18
  val mk_spmat_entry : integer -> term -> term
22964
2284e0d02e7f reorganized float arithmetic
haftmann
parents: 22951
diff changeset
    19
  val spvecT: typ
2284e0d02e7f reorganized float arithmetic
haftmann
parents: 22951
diff changeset
    20
  val spmatT: typ
2284e0d02e7f reorganized float arithmetic
haftmann
parents: 22951
diff changeset
    21
  
2284e0d02e7f reorganized float arithmetic
haftmann
parents: 22951
diff changeset
    22
  val v_elem_at : vector -> int -> string option
2284e0d02e7f reorganized float arithmetic
haftmann
parents: 22951
diff changeset
    23
  val m_elem_at : matrix -> int -> vector option
2284e0d02e7f reorganized float arithmetic
haftmann
parents: 22951
diff changeset
    24
  val v_only_elem : vector -> int option
2284e0d02e7f reorganized float arithmetic
haftmann
parents: 22951
diff changeset
    25
  val v_fold : (int * string -> 'a -> 'a) -> vector -> 'a -> 'a
2284e0d02e7f reorganized float arithmetic
haftmann
parents: 22951
diff changeset
    26
  val m_fold : (int * vector -> 'a -> 'a) -> matrix -> 'a -> 'a
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 17412
diff changeset
    27
22964
2284e0d02e7f reorganized float arithmetic
haftmann
parents: 22951
diff changeset
    28
  val transpose_matrix : matrix -> matrix
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
    29
22964
2284e0d02e7f reorganized float arithmetic
haftmann
parents: 22951
diff changeset
    30
  val cut_vector : int -> vector -> vector
2284e0d02e7f reorganized float arithmetic
haftmann
parents: 22951
diff changeset
    31
  val cut_matrix : vector -> int option -> matrix -> matrix
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
    32
23883
7d5aa704454e new functions cut_matrix', etc.
obua
parents: 23665
diff changeset
    33
  val delete_matrix : int list -> matrix -> matrix
7d5aa704454e new functions cut_matrix', etc.
obua
parents: 23665
diff changeset
    34
  val cut_matrix' : int list -> matrix -> matrix 
7d5aa704454e new functions cut_matrix', etc.
obua
parents: 23665
diff changeset
    35
  val delete_vector : int list -> vector -> vector
7d5aa704454e new functions cut_matrix', etc.
obua
parents: 23665
diff changeset
    36
  val cut_vector' : int list -> vector -> vector
7d5aa704454e new functions cut_matrix', etc.
obua
parents: 23665
diff changeset
    37
7d5aa704454e new functions cut_matrix', etc.
obua
parents: 23665
diff changeset
    38
  val indices_of_matrix : matrix -> int list
7d5aa704454e new functions cut_matrix', etc.
obua
parents: 23665
diff changeset
    39
  val indices_of_vector : vector -> int list
7d5aa704454e new functions cut_matrix', etc.
obua
parents: 23665
diff changeset
    40
22964
2284e0d02e7f reorganized float arithmetic
haftmann
parents: 22951
diff changeset
    41
  (* cplexProg c A b *)
2284e0d02e7f reorganized float arithmetic
haftmann
parents: 22951
diff changeset
    42
  val cplexProg : vector -> matrix -> vector -> cplex.cplexProg * (string -> int)
2284e0d02e7f reorganized float arithmetic
haftmann
parents: 22951
diff changeset
    43
  (* dual_cplexProg c A b *)
2284e0d02e7f reorganized float arithmetic
haftmann
parents: 22951
diff changeset
    44
  val dual_cplexProg : vector -> matrix -> vector -> cplex.cplexProg * (string -> int)
2284e0d02e7f reorganized float arithmetic
haftmann
parents: 22951
diff changeset
    45
end;
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
    46
22964
2284e0d02e7f reorganized float arithmetic
haftmann
parents: 22951
diff changeset
    47
structure FloatSparseMatrixBuilder : FLOAT_SPARSE_MATIRX_BUILDER =
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
    48
struct
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
    49
22964
2284e0d02e7f reorganized float arithmetic
haftmann
parents: 22951
diff changeset
    50
type float = Float.float
2284e0d02e7f reorganized float arithmetic
haftmann
parents: 22951
diff changeset
    51
structure Inttab = TableFun(type key = int val ord = rev_order o int_ord);
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
    52
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
    53
type vector = string Inttab.table
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
    54
type matrix = vector Inttab.table
20485
3078fd2eec7b got rid of Numeral.bin type
haftmann
parents: 17412
diff changeset
    55
22964
2284e0d02e7f reorganized float arithmetic
haftmann
parents: 22951
diff changeset
    56
val spvec_elemT = HOLogic.mk_prodT (HOLogic.natT, HOLogic.realT);
2284e0d02e7f reorganized float arithmetic
haftmann
parents: 22951
diff changeset
    57
val spvecT = HOLogic.listT spvec_elemT;
2284e0d02e7f reorganized float arithmetic
haftmann
parents: 22951
diff changeset
    58
val spmat_elemT = HOLogic.mk_prodT (HOLogic.natT, spvecT);
2284e0d02e7f reorganized float arithmetic
haftmann
parents: 22951
diff changeset
    59
val spmatT = HOLogic.listT spmat_elemT;
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
    60
22964
2284e0d02e7f reorganized float arithmetic
haftmann
parents: 22951
diff changeset
    61
fun approx_value prec f =
2284e0d02e7f reorganized float arithmetic
haftmann
parents: 22951
diff changeset
    62
  FloatArith.approx_float prec (fn (x, y) => (f x, f y));
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
    63
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
    64
fun mk_spvec_entry i f =
22964
2284e0d02e7f reorganized float arithmetic
haftmann
parents: 22951
diff changeset
    65
  HOLogic.mk_prod (HOLogic.mk_number HOLogic.natT i, FloatArith.mk_float f);
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
    66
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
    67
fun mk_spmat_entry i e =
22964
2284e0d02e7f reorganized float arithmetic
haftmann
parents: 22951
diff changeset
    68
  HOLogic.mk_prod (HOLogic.mk_number HOLogic.natT i, e);
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
    69
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
    70
fun approx_vector prec pprt vector =
22964
2284e0d02e7f reorganized float arithmetic
haftmann
parents: 22951
diff changeset
    71
  let
2284e0d02e7f reorganized float arithmetic
haftmann
parents: 22951
diff changeset
    72
    fun app (index, s) (lower, upper) =
2284e0d02e7f reorganized float arithmetic
haftmann
parents: 22951
diff changeset
    73
      let
2284e0d02e7f reorganized float arithmetic
haftmann
parents: 22951
diff changeset
    74
        val (flower, fupper) = approx_value prec pprt s
23261
85f27f79232f tuned integers
haftmann
parents: 22964
diff changeset
    75
        val index = HOLogic.mk_number HOLogic.natT (Integer.int index)
22964
2284e0d02e7f reorganized float arithmetic
haftmann
parents: 22951
diff changeset
    76
        val elower = HOLogic.mk_prod (index, flower)
2284e0d02e7f reorganized float arithmetic
haftmann
parents: 22951
diff changeset
    77
        val eupper = HOLogic.mk_prod (index, fupper)
2284e0d02e7f reorganized float arithmetic
haftmann
parents: 22951
diff changeset
    78
      in (elower :: lower, eupper :: upper) end;
2284e0d02e7f reorganized float arithmetic
haftmann
parents: 22951
diff changeset
    79
  in
23665
825bea0266db adopted to new computing oracle and fixed bugs introduced by tuning
obua
parents: 23261
diff changeset
    80
    pairself (HOLogic.mk_list spvec_elemT) (Inttab.fold app vector ([], []))
22964
2284e0d02e7f reorganized float arithmetic
haftmann
parents: 22951
diff changeset
    81
  end;
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
    82
22964
2284e0d02e7f reorganized float arithmetic
haftmann
parents: 22951
diff changeset
    83
fun approx_matrix prec pprt vector =
2284e0d02e7f reorganized float arithmetic
haftmann
parents: 22951
diff changeset
    84
  let
2284e0d02e7f reorganized float arithmetic
haftmann
parents: 22951
diff changeset
    85
    fun app (index, v) (lower, upper) =
2284e0d02e7f reorganized float arithmetic
haftmann
parents: 22951
diff changeset
    86
      let
2284e0d02e7f reorganized float arithmetic
haftmann
parents: 22951
diff changeset
    87
        val (flower, fupper) = approx_vector prec pprt v
23261
85f27f79232f tuned integers
haftmann
parents: 22964
diff changeset
    88
        val index = HOLogic.mk_number HOLogic.natT (Integer.int index)
22964
2284e0d02e7f reorganized float arithmetic
haftmann
parents: 22951
diff changeset
    89
        val elower = HOLogic.mk_prod (index, flower)
2284e0d02e7f reorganized float arithmetic
haftmann
parents: 22951
diff changeset
    90
        val eupper = HOLogic.mk_prod (index, fupper)
2284e0d02e7f reorganized float arithmetic
haftmann
parents: 22951
diff changeset
    91
      in (elower :: lower, eupper :: upper) end;
2284e0d02e7f reorganized float arithmetic
haftmann
parents: 22951
diff changeset
    92
  in
23665
825bea0266db adopted to new computing oracle and fixed bugs introduced by tuning
obua
parents: 23261
diff changeset
    93
    pairself (HOLogic.mk_list spmat_elemT) (Inttab.fold app vector ([], []))
22964
2284e0d02e7f reorganized float arithmetic
haftmann
parents: 22951
diff changeset
    94
  end;
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
    95
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
    96
exception Nat_expected of int;
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
    97
22964
2284e0d02e7f reorganized float arithmetic
haftmann
parents: 22951
diff changeset
    98
val zero_interval = approx_value 1 I "0"
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
    99
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   100
fun set_elem vector index str =
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   101
    if index < 0 then
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   102
        raise (Nat_expected index)
22964
2284e0d02e7f reorganized float arithmetic
haftmann
parents: 22951
diff changeset
   103
    else if (approx_value 1 I str) = zero_interval then
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   104
        vector
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   105
    else
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   106
        Inttab.update (index, str) vector
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   107
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   108
fun set_vector matrix index vector =
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   109
    if index < 0 then
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   110
        raise (Nat_expected index)
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   111
    else if Inttab.is_empty vector then
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   112
        matrix
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   113
    else
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   114
        Inttab.update (index, vector) matrix
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   115
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   116
val empty_matrix = Inttab.empty
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   117
val empty_vector = Inttab.empty
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   118
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   119
(* dual stuff *)
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   120
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   121
structure cplex = Cplex
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   122
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   123
fun transpose_matrix matrix =
21056
2cfe839e8d58 Symtab.foldl replaced by Symtab.fold
haftmann
parents: 20485
diff changeset
   124
  let
2cfe839e8d58 Symtab.foldl replaced by Symtab.fold
haftmann
parents: 20485
diff changeset
   125
    fun upd j (i, s) =
2cfe839e8d58 Symtab.foldl replaced by Symtab.fold
haftmann
parents: 20485
diff changeset
   126
      Inttab.map_default (i, Inttab.empty) (Inttab.update (j, s));
2cfe839e8d58 Symtab.foldl replaced by Symtab.fold
haftmann
parents: 20485
diff changeset
   127
    fun updm (j, v) = Inttab.fold (upd j) v;
2cfe839e8d58 Symtab.foldl replaced by Symtab.fold
haftmann
parents: 20485
diff changeset
   128
  in Inttab.fold updm matrix empty_matrix end;
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   129
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   130
exception No_name of string;
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   131
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   132
exception Superfluous_constr_right_hand_sides
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   133
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   134
fun cplexProg c A b =
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   135
    let
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   136
        val ytable = ref Inttab.empty
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   137
        fun indexof s =
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   138
            if String.size s = 0 then raise (No_name s)
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   139
            else case Int.fromString (String.extract(s, 1, NONE)) of
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   140
                     SOME i => i | NONE => raise (No_name s)
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   141
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   142
        fun nameof i =
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   143
            let
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   144
                val s = "x"^(Int.toString i)
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   145
                val _ = change ytable (Inttab.update (i, s))
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   146
            in
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   147
                s
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   148
            end
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   149
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   150
        fun split_numstr s =
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   151
            if String.isPrefix "-" s then (false,String.extract(s, 1, NONE))
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   152
            else if String.isPrefix "+" s then (true, String.extract(s, 1, NONE))
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   153
            else (true, s)
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   154
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   155
        fun mk_term index s =
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   156
            let
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   157
                val (p, s) = split_numstr s
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   158
                val prod = cplex.cplexProd (cplex.cplexNum s, cplex.cplexVar (nameof index))
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   159
            in
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   160
                if p then prod else cplex.cplexNeg prod
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   161
            end
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   162
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   163
        fun vec2sum vector =
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   164
            cplex.cplexSum (Inttab.fold (fn (index, s) => fn list => (mk_term index s) :: list) vector [])
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   165
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   166
        fun mk_constr index vector c =
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   167
            let
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   168
                val s = case Inttab.lookup c index of SOME s => s | NONE => "0"
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   169
                val (p, s) = split_numstr s
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   170
                val num = if p then cplex.cplexNum s else cplex.cplexNeg (cplex.cplexNum s)
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   171
            in
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   172
                (NONE, cplex.cplexConstr (cplex.cplexLeq, (vec2sum vector, num)))
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   173
            end
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   174
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   175
        fun delete index c = Inttab.delete index c handle Inttab.UNDEF _ => c
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   176
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   177
        val (list, b) = Inttab.fold
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   178
                            (fn (index, v) => fn (list, c) => ((mk_constr index v c)::list, delete index c))
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   179
                            A ([], b)
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   180
        val _ = if Inttab.is_empty b then () else raise Superfluous_constr_right_hand_sides
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   181
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   182
        fun mk_free y = cplex.cplexBounds (cplex.cplexNeg cplex.cplexInf, cplex.cplexLeq,
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   183
                                           cplex.cplexVar y, cplex.cplexLeq,
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   184
                                           cplex.cplexInf)
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   185
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   186
        val yvars = Inttab.fold (fn (i, y) => fn l => (mk_free y)::l) (!ytable) []
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   187
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   188
        val prog = cplex.cplexProg ("original", cplex.cplexMaximize (vec2sum c), list, yvars)
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   189
    in
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   190
        (prog, indexof)
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   191
    end
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   192
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   193
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   194
fun dual_cplexProg c A b =
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   195
    let
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   196
        fun indexof s =
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   197
            if String.size s = 0 then raise (No_name s)
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   198
            else case Int.fromString (String.extract(s, 1, NONE)) of
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   199
                     SOME i => i | NONE => raise (No_name s)
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   200
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   201
        fun nameof i = "y"^(Int.toString i)
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   202
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   203
        fun split_numstr s =
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   204
            if String.isPrefix "-" s then (false,String.extract(s, 1, NONE))
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   205
            else if String.isPrefix "+" s then (true, String.extract(s, 1, NONE))
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   206
            else (true, s)
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   207
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   208
        fun mk_term index s =
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   209
            let
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   210
                val (p, s) = split_numstr s
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   211
                val prod = cplex.cplexProd (cplex.cplexNum s, cplex.cplexVar (nameof index))
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   212
            in
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   213
                if p then prod else cplex.cplexNeg prod
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   214
            end
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   215
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   216
        fun vec2sum vector =
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   217
            cplex.cplexSum (Inttab.fold (fn (index, s) => fn list => (mk_term index s)::list) vector [])
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   218
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   219
        fun mk_constr index vector c =
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   220
            let
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   221
                val s = case Inttab.lookup c index of SOME s => s | NONE => "0"
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   222
                val (p, s) = split_numstr s
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   223
                val num = if p then cplex.cplexNum s else cplex.cplexNeg (cplex.cplexNum s)
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   224
            in
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   225
                (NONE, cplex.cplexConstr (cplex.cplexEq, (vec2sum vector, num)))
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   226
            end
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   227
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   228
        fun delete index c = Inttab.delete index c handle Inttab.UNDEF _ => c
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   229
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   230
        val (list, c) = Inttab.fold
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   231
                            (fn (index, v) => fn (list, c) => ((mk_constr index v c)::list, delete index c))
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   232
                            (transpose_matrix A) ([], c)
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   233
        val _ = if Inttab.is_empty c then () else raise Superfluous_constr_right_hand_sides
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   234
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   235
        val prog = cplex.cplexProg ("dual", cplex.cplexMinimize (vec2sum b), list, [])
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   236
    in
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   237
        (prog, indexof)
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   238
    end
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   239
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   240
fun cut_vector size v =
21056
2cfe839e8d58 Symtab.foldl replaced by Symtab.fold
haftmann
parents: 20485
diff changeset
   241
  let
2cfe839e8d58 Symtab.foldl replaced by Symtab.fold
haftmann
parents: 20485
diff changeset
   242
    val count = ref 0;
2cfe839e8d58 Symtab.foldl replaced by Symtab.fold
haftmann
parents: 20485
diff changeset
   243
    fun app (i, s) =  if (!count < size) then
2cfe839e8d58 Symtab.foldl replaced by Symtab.fold
haftmann
parents: 20485
diff changeset
   244
        (count := !count +1 ; Inttab.update (i, s))
2cfe839e8d58 Symtab.foldl replaced by Symtab.fold
haftmann
parents: 20485
diff changeset
   245
      else I
2cfe839e8d58 Symtab.foldl replaced by Symtab.fold
haftmann
parents: 20485
diff changeset
   246
  in
2cfe839e8d58 Symtab.foldl replaced by Symtab.fold
haftmann
parents: 20485
diff changeset
   247
    Inttab.fold app v empty_vector
2cfe839e8d58 Symtab.foldl replaced by Symtab.fold
haftmann
parents: 20485
diff changeset
   248
  end
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   249
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   250
fun cut_matrix vfilter vsize m =
21056
2cfe839e8d58 Symtab.foldl replaced by Symtab.fold
haftmann
parents: 20485
diff changeset
   251
  let
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   252
    fun app (i, v) =
21056
2cfe839e8d58 Symtab.foldl replaced by Symtab.fold
haftmann
parents: 20485
diff changeset
   253
      if is_none (Inttab.lookup vfilter i) then I
2cfe839e8d58 Symtab.foldl replaced by Symtab.fold
haftmann
parents: 20485
diff changeset
   254
      else case vsize
2cfe839e8d58 Symtab.foldl replaced by Symtab.fold
haftmann
parents: 20485
diff changeset
   255
       of NONE => Inttab.update (i, v)
2cfe839e8d58 Symtab.foldl replaced by Symtab.fold
haftmann
parents: 20485
diff changeset
   256
        | SOME s => Inttab.update (i, cut_vector s v)
2cfe839e8d58 Symtab.foldl replaced by Symtab.fold
haftmann
parents: 20485
diff changeset
   257
  in Inttab.fold app m empty_matrix end
2cfe839e8d58 Symtab.foldl replaced by Symtab.fold
haftmann
parents: 20485
diff changeset
   258
17412
e26cb20ef0cc TableFun/Symtab: curried lookup and update;
wenzelm
parents: 17261
diff changeset
   259
fun v_elem_at v i = Inttab.lookup v i
e26cb20ef0cc TableFun/Symtab: curried lookup and update;
wenzelm
parents: 17261
diff changeset
   260
fun m_elem_at m i = Inttab.lookup m i
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   261
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   262
fun v_only_elem v =
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   263
    case Inttab.min_key v of
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   264
        NONE => NONE
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   265
      | SOME vmin => (case Inttab.max_key v of
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   266
                          NONE => SOME vmin
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   267
                        | SOME vmax => if vmin = vmax then SOME vmin else NONE)
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   268
21056
2cfe839e8d58 Symtab.foldl replaced by Symtab.fold
haftmann
parents: 20485
diff changeset
   269
fun v_fold f = Inttab.fold f;
2cfe839e8d58 Symtab.foldl replaced by Symtab.fold
haftmann
parents: 20485
diff changeset
   270
fun m_fold f = Inttab.fold f;
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   271
23883
7d5aa704454e new functions cut_matrix', etc.
obua
parents: 23665
diff changeset
   272
fun indices_of_vector v = Inttab.keys v
7d5aa704454e new functions cut_matrix', etc.
obua
parents: 23665
diff changeset
   273
fun indices_of_matrix m = Inttab.keys m
7d5aa704454e new functions cut_matrix', etc.
obua
parents: 23665
diff changeset
   274
fun delete_vector indices v = fold Inttab.delete indices v
7d5aa704454e new functions cut_matrix', etc.
obua
parents: 23665
diff changeset
   275
fun delete_matrix indices m = fold Inttab.delete indices m
7d5aa704454e new functions cut_matrix', etc.
obua
parents: 23665
diff changeset
   276
fun cut_matrix' indices m = fold (fn i => fn m => (case Inttab.lookup m i of NONE => m | SOME v => Inttab.update (i, v) m))  indices Inttab.empty
7d5aa704454e new functions cut_matrix', etc.
obua
parents: 23665
diff changeset
   277
fun cut_vector' indices v = fold (fn i => fn v => (case Inttab.lookup v i of NONE => v | SOME x => Inttab.update (i, x) v))  indices Inttab.empty
7d5aa704454e new functions cut_matrix', etc.
obua
parents: 23665
diff changeset
   278
7d5aa704454e new functions cut_matrix', etc.
obua
parents: 23665
diff changeset
   279
7d5aa704454e new functions cut_matrix', etc.
obua
parents: 23665
diff changeset
   280
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   281
end;