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