src/HOL/Matrix_LP/FloatSparseMatrixBuilder.ML
author blanchet
Wed Feb 12 08:35:57 2014 +0100 (2014-02-12)
changeset 55415 05f5fdb8d093
parent 52049 156e12d5cb92
child 59058 a78612c67ec0
permissions -rw-r--r--
renamed 'nat_{case,rec}' to '{case,rec}_nat'
wenzelm@47455
     1
(*  Title:      HOL/Matrix_LP/FloatSparseMatrixBuilder.ML
obua@16784
     2
    Author:     Steven Obua
obua@16784
     3
*)
obua@16784
     4
haftmann@46532
     5
signature FLOAT_SPARSE_MATRIX_BUILDER =
obua@16784
     6
sig
haftmann@22964
     7
  include MATRIX_BUILDER
obua@16784
     8
haftmann@22964
     9
  structure cplex : CPLEX
obua@16784
    10
haftmann@22964
    11
  type float = Float.float
haftmann@22964
    12
  val approx_value : int -> (float -> float) -> string -> term * term
haftmann@22964
    13
  val approx_vector : int -> (float -> float) -> vector -> term * term
haftmann@22964
    14
  val approx_matrix : int -> (float -> float) -> matrix -> term * term
obua@16784
    15
wenzelm@24630
    16
  val mk_spvec_entry : int -> float -> term
wenzelm@24630
    17
  val mk_spvec_entry' : int -> term -> term
wenzelm@24630
    18
  val mk_spmat_entry : int -> 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@46532
    47
structure FloatSparseMatrixBuilder : FLOAT_SPARSE_MATRIX_BUILDER =
obua@16784
    48
struct
obua@16784
    49
haftmann@22964
    50
type float = Float.float
wenzelm@31971
    51
structure Inttab = Table(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
obua@24302
    67
fun mk_spvec_entry' i x =
obua@24302
    68
  HOLogic.mk_prod (HOLogic.mk_number HOLogic.natT i, x);
obua@24302
    69
haftmann@22951
    70
fun mk_spmat_entry i e =
haftmann@22964
    71
  HOLogic.mk_prod (HOLogic.mk_number HOLogic.natT i, e);
obua@16784
    72
obua@16784
    73
fun approx_vector prec pprt vector =
haftmann@22964
    74
  let
haftmann@22964
    75
    fun app (index, s) (lower, upper) =
haftmann@22964
    76
      let
haftmann@22964
    77
        val (flower, fupper) = approx_value prec pprt s
wenzelm@24630
    78
        val index = HOLogic.mk_number HOLogic.natT index
haftmann@22964
    79
        val elower = HOLogic.mk_prod (index, flower)
haftmann@22964
    80
        val eupper = HOLogic.mk_prod (index, fupper)
haftmann@22964
    81
      in (elower :: lower, eupper :: upper) end;
haftmann@22964
    82
  in
obua@23665
    83
    pairself (HOLogic.mk_list spvec_elemT) (Inttab.fold app vector ([], []))
haftmann@22964
    84
  end;
obua@16784
    85
haftmann@22964
    86
fun approx_matrix prec pprt vector =
haftmann@22964
    87
  let
haftmann@22964
    88
    fun app (index, v) (lower, upper) =
haftmann@22964
    89
      let
haftmann@22964
    90
        val (flower, fupper) = approx_vector prec pprt v
wenzelm@24630
    91
        val index = HOLogic.mk_number HOLogic.natT index
haftmann@22964
    92
        val elower = HOLogic.mk_prod (index, flower)
haftmann@22964
    93
        val eupper = HOLogic.mk_prod (index, fupper)
haftmann@22964
    94
      in (elower :: lower, eupper :: upper) end;
haftmann@22964
    95
  in
obua@23665
    96
    pairself (HOLogic.mk_list spmat_elemT) (Inttab.fold app vector ([], []))
haftmann@22964
    97
  end;
obua@16784
    98
obua@16784
    99
exception Nat_expected of int;
obua@16784
   100
haftmann@22964
   101
val zero_interval = approx_value 1 I "0"
obua@16784
   102
haftmann@22951
   103
fun set_elem vector index str =
haftmann@22951
   104
    if index < 0 then
haftmann@22951
   105
        raise (Nat_expected index)
haftmann@22964
   106
    else if (approx_value 1 I str) = zero_interval then
haftmann@22951
   107
        vector
haftmann@22951
   108
    else
haftmann@22951
   109
        Inttab.update (index, str) vector
obua@16784
   110
haftmann@22951
   111
fun set_vector matrix index vector =
obua@16784
   112
    if index < 0 then
haftmann@22951
   113
        raise (Nat_expected index)
obua@16784
   114
    else if Inttab.is_empty vector then
haftmann@22951
   115
        matrix
obua@16784
   116
    else
haftmann@22951
   117
        Inttab.update (index, vector) matrix
obua@16784
   118
obua@16784
   119
val empty_matrix = Inttab.empty
obua@16784
   120
val empty_vector = Inttab.empty
obua@16784
   121
obua@16784
   122
(* dual stuff *)
obua@16784
   123
obua@16784
   124
structure cplex = Cplex
obua@16784
   125
haftmann@22951
   126
fun transpose_matrix matrix =
haftmann@21056
   127
  let
haftmann@21056
   128
    fun upd j (i, s) =
haftmann@21056
   129
      Inttab.map_default (i, Inttab.empty) (Inttab.update (j, s));
haftmann@21056
   130
    fun updm (j, v) = Inttab.fold (upd j) v;
haftmann@21056
   131
  in Inttab.fold updm matrix empty_matrix end;
obua@16784
   132
obua@16784
   133
exception No_name of string;
obua@16784
   134
obua@16784
   135
exception Superfluous_constr_right_hand_sides
obua@16784
   136
haftmann@22951
   137
fun cplexProg c A b =
obua@16784
   138
    let
wenzelm@32740
   139
        val ytable = Unsynchronized.ref Inttab.empty
haftmann@22951
   140
        fun indexof s =
haftmann@22951
   141
            if String.size s = 0 then raise (No_name s)
haftmann@22951
   142
            else case Int.fromString (String.extract(s, 1, NONE)) of
haftmann@22951
   143
                     SOME i => i | NONE => raise (No_name s)
obua@16784
   144
haftmann@22951
   145
        fun nameof i =
haftmann@22951
   146
            let
wenzelm@41491
   147
                val s = "x" ^ string_of_int i
wenzelm@32740
   148
                val _ = Unsynchronized.change ytable (Inttab.update (i, s))
haftmann@22951
   149
            in
haftmann@22951
   150
                s
haftmann@22951
   151
            end
haftmann@22951
   152
haftmann@22951
   153
        fun split_numstr s =
haftmann@22951
   154
            if String.isPrefix "-" s then (false,String.extract(s, 1, NONE))
haftmann@22951
   155
            else if String.isPrefix "+" s then (true, String.extract(s, 1, NONE))
haftmann@22951
   156
            else (true, s)
haftmann@22951
   157
haftmann@22951
   158
        fun mk_term index s =
haftmann@22951
   159
            let
haftmann@22951
   160
                val (p, s) = split_numstr s
haftmann@22951
   161
                val prod = cplex.cplexProd (cplex.cplexNum s, cplex.cplexVar (nameof index))
haftmann@22951
   162
            in
haftmann@22951
   163
                if p then prod else cplex.cplexNeg prod
haftmann@22951
   164
            end
obua@16784
   165
haftmann@22951
   166
        fun vec2sum vector =
haftmann@22951
   167
            cplex.cplexSum (Inttab.fold (fn (index, s) => fn list => (mk_term index s) :: list) vector [])
obua@16784
   168
haftmann@22951
   169
        fun mk_constr index vector c =
haftmann@22951
   170
            let
haftmann@22951
   171
                val s = case Inttab.lookup c index of SOME s => s | NONE => "0"
haftmann@22951
   172
                val (p, s) = split_numstr s
haftmann@22951
   173
                val num = if p then cplex.cplexNum s else cplex.cplexNeg (cplex.cplexNum s)
haftmann@22951
   174
            in
haftmann@22951
   175
                (NONE, cplex.cplexConstr (cplex.cplexLeq, (vec2sum vector, num)))
haftmann@22951
   176
            end
haftmann@22951
   177
haftmann@22951
   178
        fun delete index c = Inttab.delete index c handle Inttab.UNDEF _ => c
obua@16784
   179
haftmann@22951
   180
        val (list, b) = Inttab.fold
haftmann@22951
   181
                            (fn (index, v) => fn (list, c) => ((mk_constr index v c)::list, delete index c))
haftmann@22951
   182
                            A ([], b)
haftmann@22951
   183
        val _ = if Inttab.is_empty b then () else raise Superfluous_constr_right_hand_sides
obua@16784
   184
haftmann@22951
   185
        fun mk_free y = cplex.cplexBounds (cplex.cplexNeg cplex.cplexInf, cplex.cplexLeq,
haftmann@22951
   186
                                           cplex.cplexVar y, cplex.cplexLeq,
haftmann@22951
   187
                                           cplex.cplexInf)
obua@16784
   188
haftmann@46531
   189
        val yvars = Inttab.fold (fn (_, y) => fn l => (mk_free y)::l) (!ytable) []
obua@16784
   190
haftmann@22951
   191
        val prog = cplex.cplexProg ("original", cplex.cplexMaximize (vec2sum c), list, yvars)
obua@16784
   192
    in
haftmann@22951
   193
        (prog, indexof)
obua@16784
   194
    end
obua@16784
   195
obua@16784
   196
haftmann@22951
   197
fun dual_cplexProg c A b =
obua@16784
   198
    let
haftmann@22951
   199
        fun indexof s =
haftmann@22951
   200
            if String.size s = 0 then raise (No_name s)
haftmann@22951
   201
            else case Int.fromString (String.extract(s, 1, NONE)) of
haftmann@22951
   202
                     SOME i => i | NONE => raise (No_name s)
haftmann@22951
   203
wenzelm@41491
   204
        fun nameof i = "y" ^ string_of_int i
obua@16784
   205
haftmann@22951
   206
        fun split_numstr s =
haftmann@22951
   207
            if String.isPrefix "-" s then (false,String.extract(s, 1, NONE))
haftmann@22951
   208
            else if String.isPrefix "+" s then (true, String.extract(s, 1, NONE))
haftmann@22951
   209
            else (true, s)
haftmann@22951
   210
haftmann@22951
   211
        fun mk_term index s =
haftmann@22951
   212
            let
haftmann@22951
   213
                val (p, s) = split_numstr s
haftmann@22951
   214
                val prod = cplex.cplexProd (cplex.cplexNum s, cplex.cplexVar (nameof index))
haftmann@22951
   215
            in
haftmann@22951
   216
                if p then prod else cplex.cplexNeg prod
haftmann@22951
   217
            end
obua@16784
   218
haftmann@22951
   219
        fun vec2sum vector =
haftmann@22951
   220
            cplex.cplexSum (Inttab.fold (fn (index, s) => fn list => (mk_term index s)::list) vector [])
obua@16784
   221
haftmann@22951
   222
        fun mk_constr index vector c =
haftmann@22951
   223
            let
haftmann@22951
   224
                val s = case Inttab.lookup c index of SOME s => s | NONE => "0"
haftmann@22951
   225
                val (p, s) = split_numstr s
haftmann@22951
   226
                val num = if p then cplex.cplexNum s else cplex.cplexNeg (cplex.cplexNum s)
haftmann@22951
   227
            in
haftmann@22951
   228
                (NONE, cplex.cplexConstr (cplex.cplexEq, (vec2sum vector, num)))
haftmann@22951
   229
            end
obua@16784
   230
haftmann@22951
   231
        fun delete index c = Inttab.delete index c handle Inttab.UNDEF _ => c
obua@16784
   232
haftmann@22951
   233
        val (list, c) = Inttab.fold
haftmann@22951
   234
                            (fn (index, v) => fn (list, c) => ((mk_constr index v c)::list, delete index c))
haftmann@22951
   235
                            (transpose_matrix A) ([], c)
haftmann@22951
   236
        val _ = if Inttab.is_empty c then () else raise Superfluous_constr_right_hand_sides
haftmann@22951
   237
haftmann@22951
   238
        val prog = cplex.cplexProg ("dual", cplex.cplexMinimize (vec2sum b), list, [])
obua@16784
   239
    in
haftmann@22951
   240
        (prog, indexof)
obua@16784
   241
    end
obua@16784
   242
haftmann@22951
   243
fun cut_vector size v =
haftmann@21056
   244
  let
wenzelm@32740
   245
    val count = Unsynchronized.ref 0;
haftmann@21056
   246
    fun app (i, s) =  if (!count < size) then
haftmann@21056
   247
        (count := !count +1 ; Inttab.update (i, s))
haftmann@21056
   248
      else I
haftmann@21056
   249
  in
haftmann@21056
   250
    Inttab.fold app v empty_vector
haftmann@21056
   251
  end
obua@16784
   252
haftmann@22951
   253
fun cut_matrix vfilter vsize m =
haftmann@21056
   254
  let
haftmann@22951
   255
    fun app (i, v) =
haftmann@21056
   256
      if is_none (Inttab.lookup vfilter i) then I
haftmann@21056
   257
      else case vsize
haftmann@21056
   258
       of NONE => Inttab.update (i, v)
haftmann@21056
   259
        | SOME s => Inttab.update (i, cut_vector s v)
haftmann@21056
   260
  in Inttab.fold app m empty_matrix end
haftmann@21056
   261
wenzelm@17412
   262
fun v_elem_at v i = Inttab.lookup v i
wenzelm@17412
   263
fun m_elem_at m i = Inttab.lookup m i
obua@16784
   264
haftmann@22951
   265
fun v_only_elem v =
wenzelm@52049
   266
    case Inttab.min v of
haftmann@22951
   267
        NONE => NONE
wenzelm@52049
   268
      | SOME (vmin, _) => (case Inttab.max v of
haftmann@22951
   269
                          NONE => SOME vmin
wenzelm@52049
   270
                        | SOME (vmax, _) => if vmin = vmax then SOME vmin else NONE)
obua@16784
   271
haftmann@21056
   272
fun v_fold f = Inttab.fold f;
haftmann@21056
   273
fun m_fold f = Inttab.fold f;
obua@16784
   274
obua@23883
   275
fun indices_of_vector v = Inttab.keys v
obua@23883
   276
fun indices_of_matrix m = Inttab.keys m
obua@23883
   277
fun delete_vector indices v = fold Inttab.delete indices v
obua@23883
   278
fun delete_matrix indices m = fold Inttab.delete indices m
haftmann@46531
   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
haftmann@46531
   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
obua@23883
   281
obua@23883
   282
obua@23883
   283
obua@16784
   284
end;