src/HOL/Matrix_LP/FloatSparseMatrixBuilder.ML
author Christian Sternagel
Thu, 30 Aug 2012 15:44:03 +0900
changeset 49093 fdc301f592c4
parent 47455 26315a545e26
child 52049 156e12d5cb92
permissions -rw-r--r--
forgot to add lemmas
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
47455
26315a545e26 updated headers;
wenzelm
parents: 46988
diff changeset
     1
(*  Title:      HOL/Matrix_LP/FloatSparseMatrixBuilder.ML
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
     2
    Author:     Steven Obua
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
     3
*)
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
     4
46532
89970ca96284 corrected typo
haftmann
parents: 46531
diff changeset
     5
signature FLOAT_SPARSE_MATRIX_BUILDER =
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
     6
sig
22964
2284e0d02e7f reorganized float arithmetic
haftmann
parents: 22951
diff changeset
     7
  include MATRIX_BUILDER
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
     8
22964
2284e0d02e7f reorganized float arithmetic
haftmann
parents: 22951
diff changeset
     9
  structure cplex : CPLEX
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
    10
22964
2284e0d02e7f reorganized float arithmetic
haftmann
parents: 22951
diff changeset
    11
  type float = Float.float
2284e0d02e7f reorganized float arithmetic
haftmann
parents: 22951
diff changeset
    12
  val approx_value : int -> (float -> float) -> string -> term * term
2284e0d02e7f reorganized float arithmetic
haftmann
parents: 22951
diff changeset
    13
  val approx_vector : int -> (float -> float) -> vector -> term * term
2284e0d02e7f reorganized float arithmetic
haftmann
parents: 22951
diff changeset
    14
  val approx_matrix : int -> (float -> float) -> matrix -> term * term
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
    15
24630
351a308ab58d simplified type int (eliminated IntInf.int, integer);
wenzelm
parents: 24302
diff changeset
    16
  val mk_spvec_entry : int -> float -> term
351a308ab58d simplified type int (eliminated IntInf.int, integer);
wenzelm
parents: 24302
diff changeset
    17
  val mk_spvec_entry' : int -> term -> term
351a308ab58d simplified type int (eliminated IntInf.int, integer);
wenzelm
parents: 24302
diff changeset
    18
  val mk_spmat_entry : int -> 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
46532
89970ca96284 corrected typo
haftmann
parents: 46531
diff changeset
    47
structure FloatSparseMatrixBuilder : FLOAT_SPARSE_MATRIX_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
31971
8c1b845ed105 renamed functor TableFun to Table, and GraphFun to Graph;
wenzelm
parents: 24630
diff changeset
    51
structure Inttab = Table(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
24302
obua
parents: 23883
diff changeset
    67
fun mk_spvec_entry' i x =
obua
parents: 23883
diff changeset
    68
  HOLogic.mk_prod (HOLogic.mk_number HOLogic.natT i, x);
obua
parents: 23883
diff changeset
    69
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
    70
fun mk_spmat_entry i e =
22964
2284e0d02e7f reorganized float arithmetic
haftmann
parents: 22951
diff changeset
    71
  HOLogic.mk_prod (HOLogic.mk_number HOLogic.natT i, e);
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
    72
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
    73
fun approx_vector prec pprt vector =
22964
2284e0d02e7f reorganized float arithmetic
haftmann
parents: 22951
diff changeset
    74
  let
2284e0d02e7f reorganized float arithmetic
haftmann
parents: 22951
diff changeset
    75
    fun app (index, s) (lower, upper) =
2284e0d02e7f reorganized float arithmetic
haftmann
parents: 22951
diff changeset
    76
      let
2284e0d02e7f reorganized float arithmetic
haftmann
parents: 22951
diff changeset
    77
        val (flower, fupper) = approx_value prec pprt s
24630
351a308ab58d simplified type int (eliminated IntInf.int, integer);
wenzelm
parents: 24302
diff changeset
    78
        val index = HOLogic.mk_number HOLogic.natT index
22964
2284e0d02e7f reorganized float arithmetic
haftmann
parents: 22951
diff changeset
    79
        val elower = HOLogic.mk_prod (index, flower)
2284e0d02e7f reorganized float arithmetic
haftmann
parents: 22951
diff changeset
    80
        val eupper = HOLogic.mk_prod (index, fupper)
2284e0d02e7f reorganized float arithmetic
haftmann
parents: 22951
diff changeset
    81
      in (elower :: lower, eupper :: upper) end;
2284e0d02e7f reorganized float arithmetic
haftmann
parents: 22951
diff changeset
    82
  in
23665
825bea0266db adopted to new computing oracle and fixed bugs introduced by tuning
obua
parents: 23261
diff changeset
    83
    pairself (HOLogic.mk_list spvec_elemT) (Inttab.fold app vector ([], []))
22964
2284e0d02e7f reorganized float arithmetic
haftmann
parents: 22951
diff changeset
    84
  end;
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
    85
22964
2284e0d02e7f reorganized float arithmetic
haftmann
parents: 22951
diff changeset
    86
fun approx_matrix prec pprt vector =
2284e0d02e7f reorganized float arithmetic
haftmann
parents: 22951
diff changeset
    87
  let
2284e0d02e7f reorganized float arithmetic
haftmann
parents: 22951
diff changeset
    88
    fun app (index, v) (lower, upper) =
2284e0d02e7f reorganized float arithmetic
haftmann
parents: 22951
diff changeset
    89
      let
2284e0d02e7f reorganized float arithmetic
haftmann
parents: 22951
diff changeset
    90
        val (flower, fupper) = approx_vector prec pprt v
24630
351a308ab58d simplified type int (eliminated IntInf.int, integer);
wenzelm
parents: 24302
diff changeset
    91
        val index = HOLogic.mk_number HOLogic.natT index
22964
2284e0d02e7f reorganized float arithmetic
haftmann
parents: 22951
diff changeset
    92
        val elower = HOLogic.mk_prod (index, flower)
2284e0d02e7f reorganized float arithmetic
haftmann
parents: 22951
diff changeset
    93
        val eupper = HOLogic.mk_prod (index, fupper)
2284e0d02e7f reorganized float arithmetic
haftmann
parents: 22951
diff changeset
    94
      in (elower :: lower, eupper :: upper) end;
2284e0d02e7f reorganized float arithmetic
haftmann
parents: 22951
diff changeset
    95
  in
23665
825bea0266db adopted to new computing oracle and fixed bugs introduced by tuning
obua
parents: 23261
diff changeset
    96
    pairself (HOLogic.mk_list spmat_elemT) (Inttab.fold app vector ([], []))
22964
2284e0d02e7f reorganized float arithmetic
haftmann
parents: 22951
diff changeset
    97
  end;
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
    98
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
    99
exception Nat_expected of int;
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   100
22964
2284e0d02e7f reorganized float arithmetic
haftmann
parents: 22951
diff changeset
   101
val zero_interval = approx_value 1 I "0"
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   102
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   103
fun set_elem vector index str =
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   104
    if index < 0 then
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   105
        raise (Nat_expected index)
22964
2284e0d02e7f reorganized float arithmetic
haftmann
parents: 22951
diff changeset
   106
    else if (approx_value 1 I str) = zero_interval then
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   107
        vector
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   108
    else
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   109
        Inttab.update (index, str) vector
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   110
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   111
fun set_vector matrix index vector =
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   112
    if index < 0 then
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   113
        raise (Nat_expected index)
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   114
    else if Inttab.is_empty vector then
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   115
        matrix
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   116
    else
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   117
        Inttab.update (index, vector) matrix
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   118
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   119
val empty_matrix = Inttab.empty
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   120
val empty_vector = Inttab.empty
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   121
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   122
(* dual stuff *)
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   123
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   124
structure cplex = Cplex
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   125
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   126
fun transpose_matrix matrix =
21056
2cfe839e8d58 Symtab.foldl replaced by Symtab.fold
haftmann
parents: 20485
diff changeset
   127
  let
2cfe839e8d58 Symtab.foldl replaced by Symtab.fold
haftmann
parents: 20485
diff changeset
   128
    fun upd j (i, s) =
2cfe839e8d58 Symtab.foldl replaced by Symtab.fold
haftmann
parents: 20485
diff changeset
   129
      Inttab.map_default (i, Inttab.empty) (Inttab.update (j, s));
2cfe839e8d58 Symtab.foldl replaced by Symtab.fold
haftmann
parents: 20485
diff changeset
   130
    fun updm (j, v) = Inttab.fold (upd j) v;
2cfe839e8d58 Symtab.foldl replaced by Symtab.fold
haftmann
parents: 20485
diff changeset
   131
  in Inttab.fold updm matrix empty_matrix end;
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   132
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   133
exception No_name of string;
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   134
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   135
exception Superfluous_constr_right_hand_sides
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   136
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   137
fun cplexProg c A b =
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   138
    let
32740
9dd0a2f83429 explicit indication of Unsynchronized.ref;
wenzelm
parents: 31971
diff changeset
   139
        val ytable = Unsynchronized.ref Inttab.empty
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   140
        fun indexof s =
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   141
            if String.size s = 0 then raise (No_name s)
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   142
            else case Int.fromString (String.extract(s, 1, NONE)) of
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   143
                     SOME i => i | NONE => raise (No_name s)
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   144
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   145
        fun nameof i =
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   146
            let
41491
a2ad5b824051 eliminated Int.toString;
wenzelm
parents: 37788
diff changeset
   147
                val s = "x" ^ string_of_int i
32740
9dd0a2f83429 explicit indication of Unsynchronized.ref;
wenzelm
parents: 31971
diff changeset
   148
                val _ = Unsynchronized.change ytable (Inttab.update (i, s))
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   149
            in
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   150
                s
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   151
            end
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   152
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   153
        fun split_numstr s =
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   154
            if String.isPrefix "-" s then (false,String.extract(s, 1, NONE))
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   155
            else if String.isPrefix "+" s then (true, String.extract(s, 1, NONE))
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   156
            else (true, s)
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   157
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   158
        fun mk_term index s =
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   159
            let
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   160
                val (p, s) = split_numstr s
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   161
                val prod = cplex.cplexProd (cplex.cplexNum s, cplex.cplexVar (nameof index))
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   162
            in
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   163
                if p then prod else cplex.cplexNeg prod
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   164
            end
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   165
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   166
        fun vec2sum vector =
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   167
            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
   168
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   169
        fun mk_constr index vector c =
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   170
            let
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   171
                val s = case Inttab.lookup c index of SOME s => s | NONE => "0"
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   172
                val (p, s) = split_numstr s
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   173
                val num = if p then cplex.cplexNum s else cplex.cplexNeg (cplex.cplexNum s)
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   174
            in
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   175
                (NONE, cplex.cplexConstr (cplex.cplexLeq, (vec2sum vector, num)))
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   176
            end
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   177
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   178
        fun delete index c = Inttab.delete index c handle Inttab.UNDEF _ => c
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   179
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   180
        val (list, b) = Inttab.fold
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   181
                            (fn (index, v) => fn (list, c) => ((mk_constr index v c)::list, delete index c))
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   182
                            A ([], b)
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   183
        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
   184
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   185
        fun mk_free y = cplex.cplexBounds (cplex.cplexNeg cplex.cplexInf, cplex.cplexLeq,
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   186
                                           cplex.cplexVar y, cplex.cplexLeq,
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   187
                                           cplex.cplexInf)
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   188
46531
eff798e48efc dropped dead code
haftmann
parents: 41491
diff changeset
   189
        val yvars = Inttab.fold (fn (_, y) => fn l => (mk_free y)::l) (!ytable) []
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   190
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   191
        val prog = cplex.cplexProg ("original", cplex.cplexMaximize (vec2sum c), list, yvars)
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   192
    in
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   193
        (prog, indexof)
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   194
    end
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   195
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   196
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   197
fun dual_cplexProg c A b =
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   198
    let
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   199
        fun indexof s =
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   200
            if String.size s = 0 then raise (No_name s)
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   201
            else case Int.fromString (String.extract(s, 1, NONE)) of
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   202
                     SOME i => i | NONE => raise (No_name s)
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   203
41491
a2ad5b824051 eliminated Int.toString;
wenzelm
parents: 37788
diff changeset
   204
        fun nameof i = "y" ^ string_of_int i
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   205
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   206
        fun split_numstr s =
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   207
            if String.isPrefix "-" s then (false,String.extract(s, 1, NONE))
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   208
            else if String.isPrefix "+" s then (true, String.extract(s, 1, NONE))
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   209
            else (true, s)
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   210
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   211
        fun mk_term index s =
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   212
            let
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   213
                val (p, s) = split_numstr s
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   214
                val prod = cplex.cplexProd (cplex.cplexNum s, cplex.cplexVar (nameof index))
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   215
            in
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   216
                if p then prod else cplex.cplexNeg prod
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   217
            end
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   218
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   219
        fun vec2sum vector =
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   220
            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
   221
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   222
        fun mk_constr index vector c =
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   223
            let
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   224
                val s = case Inttab.lookup c index of SOME s => s | NONE => "0"
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   225
                val (p, s) = split_numstr s
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   226
                val num = if p then cplex.cplexNum s else cplex.cplexNeg (cplex.cplexNum s)
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   227
            in
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   228
                (NONE, cplex.cplexConstr (cplex.cplexEq, (vec2sum vector, num)))
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   229
            end
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   230
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   231
        fun delete index c = Inttab.delete index c handle Inttab.UNDEF _ => c
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   232
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   233
        val (list, c) = Inttab.fold
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   234
                            (fn (index, v) => fn (list, c) => ((mk_constr index v c)::list, delete index c))
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   235
                            (transpose_matrix A) ([], c)
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   236
        val _ = if Inttab.is_empty c then () else raise Superfluous_constr_right_hand_sides
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   237
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   238
        val prog = cplex.cplexProg ("dual", cplex.cplexMinimize (vec2sum b), list, [])
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   239
    in
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   240
        (prog, indexof)
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   241
    end
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   242
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   243
fun cut_vector size v =
21056
2cfe839e8d58 Symtab.foldl replaced by Symtab.fold
haftmann
parents: 20485
diff changeset
   244
  let
32740
9dd0a2f83429 explicit indication of Unsynchronized.ref;
wenzelm
parents: 31971
diff changeset
   245
    val count = Unsynchronized.ref 0;
21056
2cfe839e8d58 Symtab.foldl replaced by Symtab.fold
haftmann
parents: 20485
diff changeset
   246
    fun app (i, s) =  if (!count < size) then
2cfe839e8d58 Symtab.foldl replaced by Symtab.fold
haftmann
parents: 20485
diff changeset
   247
        (count := !count +1 ; Inttab.update (i, s))
2cfe839e8d58 Symtab.foldl replaced by Symtab.fold
haftmann
parents: 20485
diff changeset
   248
      else I
2cfe839e8d58 Symtab.foldl replaced by Symtab.fold
haftmann
parents: 20485
diff changeset
   249
  in
2cfe839e8d58 Symtab.foldl replaced by Symtab.fold
haftmann
parents: 20485
diff changeset
   250
    Inttab.fold app v empty_vector
2cfe839e8d58 Symtab.foldl replaced by Symtab.fold
haftmann
parents: 20485
diff changeset
   251
  end
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   252
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   253
fun cut_matrix vfilter vsize m =
21056
2cfe839e8d58 Symtab.foldl replaced by Symtab.fold
haftmann
parents: 20485
diff changeset
   254
  let
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   255
    fun app (i, v) =
21056
2cfe839e8d58 Symtab.foldl replaced by Symtab.fold
haftmann
parents: 20485
diff changeset
   256
      if is_none (Inttab.lookup vfilter i) then I
2cfe839e8d58 Symtab.foldl replaced by Symtab.fold
haftmann
parents: 20485
diff changeset
   257
      else case vsize
2cfe839e8d58 Symtab.foldl replaced by Symtab.fold
haftmann
parents: 20485
diff changeset
   258
       of NONE => Inttab.update (i, v)
2cfe839e8d58 Symtab.foldl replaced by Symtab.fold
haftmann
parents: 20485
diff changeset
   259
        | SOME s => Inttab.update (i, cut_vector s v)
2cfe839e8d58 Symtab.foldl replaced by Symtab.fold
haftmann
parents: 20485
diff changeset
   260
  in Inttab.fold app m empty_matrix end
2cfe839e8d58 Symtab.foldl replaced by Symtab.fold
haftmann
parents: 20485
diff changeset
   261
17412
e26cb20ef0cc TableFun/Symtab: curried lookup and update;
wenzelm
parents: 17261
diff changeset
   262
fun v_elem_at v i = Inttab.lookup v i
e26cb20ef0cc TableFun/Symtab: curried lookup and update;
wenzelm
parents: 17261
diff changeset
   263
fun m_elem_at m i = Inttab.lookup m i
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   264
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   265
fun v_only_elem v =
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   266
    case Inttab.min_key v of
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   267
        NONE => NONE
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   268
      | SOME vmin => (case Inttab.max_key v of
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   269
                          NONE => SOME vmin
dfafcd6223ad whitespace tuned
haftmann
parents: 22578
diff changeset
   270
                        | SOME vmax => if vmin = vmax then SOME vmin else NONE)
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   271
21056
2cfe839e8d58 Symtab.foldl replaced by Symtab.fold
haftmann
parents: 20485
diff changeset
   272
fun v_fold f = Inttab.fold f;
2cfe839e8d58 Symtab.foldl replaced by Symtab.fold
haftmann
parents: 20485
diff changeset
   273
fun m_fold f = Inttab.fold f;
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   274
23883
7d5aa704454e new functions cut_matrix', etc.
obua
parents: 23665
diff changeset
   275
fun indices_of_vector v = Inttab.keys v
7d5aa704454e new functions cut_matrix', etc.
obua
parents: 23665
diff changeset
   276
fun indices_of_matrix m = Inttab.keys m
7d5aa704454e new functions cut_matrix', etc.
obua
parents: 23665
diff changeset
   277
fun delete_vector indices v = fold Inttab.delete indices v
7d5aa704454e new functions cut_matrix', etc.
obua
parents: 23665
diff changeset
   278
fun delete_matrix indices m = fold Inttab.delete indices m
46531
eff798e48efc dropped dead code
haftmann
parents: 41491
diff changeset
   279
fun cut_matrix' indices _ = fold (fn i => fn m => (case Inttab.lookup m i of NONE => m | SOME v => Inttab.update (i, v) m)) indices Inttab.empty
eff798e48efc dropped dead code
haftmann
parents: 41491
diff changeset
   280
fun cut_vector' indices _ = fold (fn i => fn v => (case Inttab.lookup v i of NONE => v | SOME x => Inttab.update (i, x) v)) indices Inttab.empty
23883
7d5aa704454e new functions cut_matrix', etc.
obua
parents: 23665
diff changeset
   281
7d5aa704454e new functions cut_matrix', etc.
obua
parents: 23665
diff changeset
   282
7d5aa704454e new functions cut_matrix', etc.
obua
parents: 23665
diff changeset
   283
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
   284
end;