src/HOL/Matrix_LP/fspmlp.ML
author haftmann
Fri Oct 10 19:55:32 2014 +0200 (2014-10-10)
changeset 58646 cd63a4b12a33
parent 47455 26315a545e26
child 69597 ff784d5a5bfb
permissions -rw-r--r--
specialized specification: avoid trivial instances
wenzelm@47455
     1
(*  Title:      HOL/Matrix_LP/fspmlp.ML
obua@16784
     2
    Author:     Steven Obua
obua@16784
     3
*)
obua@16784
     4
haftmann@22951
     5
signature FSPMLP =
obua@16784
     6
sig
obua@16784
     7
    type linprog
obua@19404
     8
    type vector = FloatSparseMatrixBuilder.vector
obua@19404
     9
    type matrix = FloatSparseMatrixBuilder.matrix
obua@16784
    10
haftmann@22964
    11
    val y : linprog -> term
haftmann@22964
    12
    val A : linprog -> term * term
haftmann@22964
    13
    val b : linprog -> term
haftmann@22964
    14
    val c : linprog -> term * term
haftmann@22964
    15
    val r12 : linprog -> term * term
obua@16784
    16
obua@16784
    17
    exception Load of string
haftmann@22951
    18
obua@16784
    19
    val load : string -> int -> bool -> linprog
obua@16784
    20
end
obua@16784
    21
haftmann@22964
    22
structure Fspmlp : FSPMLP =
obua@16784
    23
struct
obua@16784
    24
obua@19404
    25
type vector = FloatSparseMatrixBuilder.vector
obua@19404
    26
type matrix = FloatSparseMatrixBuilder.matrix
obua@19404
    27
obua@24302
    28
type linprog = term * (term * term) * term * (term * term) * (term * term)
obua@16784
    29
haftmann@46533
    30
fun y (c1, _, _, _, _) = c1
haftmann@46533
    31
fun A (_, c2, _, _, _) = c2
haftmann@46533
    32
fun b (_, _, c3, _, _) = c3
haftmann@46533
    33
fun c (_, _, _, c4, _) = c4
haftmann@46533
    34
fun r12 (_, _, _, _, c6) = c6
obua@16784
    35
haftmann@22951
    36
structure CplexFloatSparseMatrixConverter =
obua@16784
    37
MAKE_CPLEX_MATRIX_CONVERTER(structure cplex = Cplex and matrix_builder = FloatSparseMatrixBuilder);
obua@16784
    38
obua@16784
    39
datatype bound_type = LOWER | UPPER
obua@16784
    40
wenzelm@24135
    41
fun intbound_ord ((i1: int, b1),(i2,b2)) =
obua@16784
    42
    if i1 < i2 then LESS
haftmann@22951
    43
    else if i1 = i2 then
haftmann@22951
    44
        (if b1 = b2 then EQUAL else if b1=LOWER then LESS else GREATER)
obua@16784
    45
    else GREATER
obua@16784
    46
wenzelm@31971
    47
structure Inttab = Table(type key = int val ord = (rev_order o int_ord));
obua@16784
    48
wenzelm@31971
    49
structure VarGraph = Table(type key = int*bound_type val ord = intbound_ord);
obua@16784
    50
(* key -> (float option) * (int -> (float * (((float * float) * key) list)))) *)
obua@16784
    51
(* dest_key -> (sure_bound * (row_index -> (row_bound * (((coeff_lower * coeff_upper) * src_key) list)))) *)
obua@16784
    52
obua@16784
    53
exception Internal of string;
obua@16784
    54
haftmann@22951
    55
fun add_row_bound g dest_key row_index row_bound =
haftmann@22951
    56
    let
haftmann@22951
    57
        val x =
haftmann@22951
    58
            case VarGraph.lookup g dest_key of
haftmann@22951
    59
                NONE => (NONE, Inttab.update (row_index, (row_bound, [])) Inttab.empty)
haftmann@22951
    60
              | SOME (sure_bound, f) =>
haftmann@22951
    61
                (sure_bound,
haftmann@22951
    62
                 case Inttab.lookup f row_index of
haftmann@22951
    63
                     NONE => Inttab.update (row_index, (row_bound, [])) f
haftmann@22951
    64
                   | SOME _ => raise (Internal "add_row_bound"))
obua@16784
    65
    in
haftmann@22951
    66
        VarGraph.update (dest_key, x) g
obua@16784
    67
    end
obua@16784
    68
haftmann@22951
    69
fun update_sure_bound g (key as (_, btype)) bound =
haftmann@22951
    70
    let
haftmann@22951
    71
        val x =
haftmann@22951
    72
            case VarGraph.lookup g key of
haftmann@22951
    73
                NONE => (SOME bound, Inttab.empty)
haftmann@22951
    74
              | SOME (NONE, f) => (SOME bound, f)
haftmann@22951
    75
              | SOME (SOME old_bound, f) =>
haftmann@22951
    76
                (SOME ((case btype of
haftmann@22964
    77
                            UPPER => Float.min
haftmann@22964
    78
                          | LOWER => Float.max)
haftmann@22951
    79
                           old_bound bound), f)
haftmann@22951
    80
    in
haftmann@22951
    81
        VarGraph.update (key, x) g
haftmann@22951
    82
    end
haftmann@22951
    83
haftmann@22951
    84
fun get_sure_bound g key =
haftmann@22951
    85
    case VarGraph.lookup g key of
haftmann@22951
    86
        NONE => NONE
obua@16784
    87
      | SOME (sure_bound, _) => sure_bound
obua@16784
    88
haftmann@22951
    89
(*fun get_row_bound g key row_index =
wenzelm@17412
    90
    case VarGraph.lookup g key of
haftmann@22951
    91
        NONE => NONE
obua@16784
    92
      | SOME (sure_bound, f) =>
haftmann@22951
    93
        (case Inttab.lookup f row_index of
haftmann@22951
    94
             NONE => NONE
haftmann@22951
    95
           | SOME (row_bound, _) => (sure_bound, row_bound))*)
haftmann@22951
    96
haftmann@22951
    97
fun add_edge g src_key dest_key row_index coeff =
wenzelm@17412
    98
    case VarGraph.lookup g dest_key of
haftmann@22951
    99
        NONE => raise (Internal "add_edge: dest_key not found")
obua@16784
   100
      | SOME (sure_bound, f) =>
haftmann@22951
   101
        (case Inttab.lookup f row_index of
haftmann@22951
   102
             NONE => raise (Internal "add_edge: row_index not found")
haftmann@22951
   103
           | SOME (row_bound, sources) =>
haftmann@22951
   104
             VarGraph.update (dest_key, (sure_bound, Inttab.update (row_index, (row_bound, (coeff, src_key) :: sources)) f)) g)
obua@16784
   105
haftmann@22951
   106
fun split_graph g =
haftmann@21056
   107
  let
haftmann@21056
   108
    fun split (key, (sure_bound, _)) (r1, r2) = case sure_bound
haftmann@21056
   109
     of NONE => (r1, r2)
haftmann@21056
   110
      | SOME bound =>  (case key
haftmann@21056
   111
         of (u, UPPER) => (r1, Inttab.update (u, bound) r2)
haftmann@21056
   112
          | (u, LOWER) => (Inttab.update (u, bound) r1, r2))
haftmann@21056
   113
  in VarGraph.fold split g (Inttab.empty, Inttab.empty) end
obua@16784
   114
obua@16784
   115
(* If safe is true, termination is guaranteed, but the sure bounds may be not optimal (relative to the algorithm).
obua@16784
   116
   If safe is false, termination is not guaranteed, but on termination the sure bounds are optimal (relative to the algorithm) *)
haftmann@22951
   117
fun propagate_sure_bounds safe names g =
haftmann@22951
   118
    let
haftmann@22951
   119
        (* returns NONE if no new sure bound could be calculated, otherwise the new sure bound is returned *)
haftmann@22951
   120
        fun calc_sure_bound_from_sources g (key as (_, btype)) =
haftmann@22951
   121
            let
haftmann@22951
   122
                fun mult_upper x (lower, upper) =
haftmann@23520
   123
                    if Float.sign x = LESS then
haftmann@22964
   124
                        Float.mult x lower
haftmann@22951
   125
                    else
haftmann@22964
   126
                        Float.mult x upper
obua@16784
   127
haftmann@22951
   128
                fun mult_lower x (lower, upper) =
haftmann@23520
   129
                    if Float.sign x = LESS then
haftmann@22964
   130
                        Float.mult x upper
haftmann@22951
   131
                    else
haftmann@22964
   132
                        Float.mult x lower
haftmann@22951
   133
haftmann@22951
   134
                val mult_btype = case btype of UPPER => mult_upper | LOWER => mult_lower
obua@16784
   135
haftmann@46533
   136
                fun calc_sure_bound (_, (row_bound, sources)) sure_bound =
haftmann@22951
   137
                    let
haftmann@22951
   138
                        fun add_src_bound (coeff, src_key) sum =
haftmann@22951
   139
                            case sum of
haftmann@22951
   140
                                NONE => NONE
haftmann@22951
   141
                              | SOME x =>
haftmann@22951
   142
                                (case get_sure_bound g src_key of
haftmann@22951
   143
                                     NONE => NONE
haftmann@22964
   144
                                   | SOME src_sure_bound => SOME (Float.add x (mult_btype src_sure_bound coeff)))
haftmann@22951
   145
                    in
haftmann@22951
   146
                        case fold add_src_bound sources (SOME row_bound) of
haftmann@22951
   147
                            NONE => sure_bound
haftmann@22951
   148
                          | new_sure_bound as (SOME new_bound) =>
haftmann@22951
   149
                            (case sure_bound of
haftmann@22951
   150
                                 NONE => new_sure_bound
haftmann@22951
   151
                               | SOME old_bound =>
haftmann@22951
   152
                                 SOME (case btype of
haftmann@22964
   153
                                           UPPER => Float.min old_bound new_bound
haftmann@22964
   154
                                         | LOWER => Float.max old_bound new_bound))
haftmann@22951
   155
                    end
haftmann@22951
   156
            in
haftmann@22951
   157
                case VarGraph.lookup g key of
haftmann@22951
   158
                    NONE => NONE
haftmann@22951
   159
                  | SOME (sure_bound, f) =>
haftmann@22951
   160
                    let
haftmann@22951
   161
                        val x = Inttab.fold calc_sure_bound f sure_bound
haftmann@22951
   162
                    in
haftmann@22951
   163
                        if x = sure_bound then NONE else x
haftmann@22951
   164
                    end
haftmann@22951
   165
                end
obua@16784
   166
haftmann@22951
   167
        fun propagate (key, _) (g, b) =
haftmann@22951
   168
            case calc_sure_bound_from_sources g key of
haftmann@22951
   169
                NONE => (g,b)
haftmann@22951
   170
              | SOME bound => (update_sure_bound g key bound,
haftmann@22951
   171
                               if safe then
haftmann@22951
   172
                                   case get_sure_bound g key of
haftmann@22951
   173
                                       NONE => true
haftmann@22951
   174
                                     | _ => b
haftmann@22951
   175
                               else
haftmann@22951
   176
                                   true)
obua@16784
   177
haftmann@22951
   178
        val (g, b) = VarGraph.fold propagate g (g, false)
obua@16784
   179
    in
haftmann@22951
   180
        if b then propagate_sure_bounds safe names g else g
haftmann@22951
   181
    end
haftmann@22951
   182
obua@16784
   183
exception Load of string;
obua@16784
   184
obua@23665
   185
val empty_spvec = @{term "Nil :: real spvec"};
obua@23665
   186
fun cons_spvec x xs = @{term "Cons :: nat * real => real spvec => real spvec"} $ x $ xs;
obua@23665
   187
val empty_spmat = @{term "Nil :: real spmat"};
obua@23665
   188
fun cons_spmat x xs = @{term "Cons :: nat * real spvec => real spmat => real spmat"} $ x $ xs;
haftmann@22964
   189
haftmann@22951
   190
fun calcr safe_propagation xlen names prec A b =
obua@16784
   191
    let
haftmann@22951
   192
        fun test_1 (lower, upper) =
haftmann@22951
   193
            if lower = upper then
wenzelm@23297
   194
                (if Float.eq (lower, (~1, 0)) then ~1
wenzelm@23297
   195
                 else if Float.eq (lower, (1, 0)) then 1
haftmann@22951
   196
                 else 0)
haftmann@22951
   197
            else 0
obua@16784
   198
haftmann@22951
   199
        fun calcr (row_index, a) g =
haftmann@22951
   200
            let
haftmann@22951
   201
                val b =  FloatSparseMatrixBuilder.v_elem_at b row_index
haftmann@22964
   202
                val (_, b2) = FloatArith.approx_decstr_by_bin prec (case b of NONE => "0" | SOME b => b)
haftmann@22951
   203
                val approx_a = FloatSparseMatrixBuilder.v_fold (fn (i, s) => fn l =>
haftmann@22964
   204
                                                                   (i, FloatArith.approx_decstr_by_bin prec s)::l) a []
obua@16784
   205
haftmann@22951
   206
                fun fold_dest_nodes (dest_index, dest_value) g =
haftmann@22951
   207
                    let
haftmann@22951
   208
                        val dest_test = test_1 dest_value
haftmann@22951
   209
                    in
haftmann@22951
   210
                        if dest_test = 0 then
haftmann@22951
   211
                            g
haftmann@22951
   212
                        else let
haftmann@22951
   213
                                val (dest_key as (_, dest_btype), row_bound) =
haftmann@22951
   214
                                    if dest_test = ~1 then
haftmann@22964
   215
                                        ((dest_index, LOWER), Float.neg b2)
haftmann@22951
   216
                                    else
haftmann@22951
   217
                                        ((dest_index, UPPER), b2)
obua@16784
   218
haftmann@22951
   219
                                fun fold_src_nodes (src_index, src_value as (src_lower, src_upper)) g =
haftmann@22951
   220
                                    if src_index = dest_index then g
haftmann@22951
   221
                                    else
haftmann@22951
   222
                                        let
haftmann@22951
   223
                                            val coeff = case dest_btype of
haftmann@22964
   224
                                                            UPPER => (Float.neg src_upper, Float.neg src_lower)
haftmann@22951
   225
                                                          | LOWER => src_value
haftmann@22951
   226
                                        in
haftmann@23520
   227
                                            if Float.sign src_lower = LESS then
haftmann@22951
   228
                                                add_edge g (src_index, UPPER) dest_key row_index coeff
haftmann@22951
   229
                                            else
haftmann@22951
   230
                                                add_edge g (src_index, LOWER) dest_key row_index coeff
haftmann@22951
   231
                                        end
haftmann@22951
   232
                            in
haftmann@22951
   233
                                fold fold_src_nodes approx_a (add_row_bound g dest_key row_index row_bound)
haftmann@22951
   234
                            end
haftmann@22951
   235
                    end
haftmann@22951
   236
            in
haftmann@22951
   237
                case approx_a of
haftmann@22951
   238
                    [] => g
haftmann@22951
   239
                  | [(u, a)] =>
haftmann@22951
   240
                    let
haftmann@22951
   241
                        val atest = test_1 a
haftmann@22951
   242
                    in
haftmann@22951
   243
                        if atest = ~1 then
haftmann@22964
   244
                            update_sure_bound g (u, LOWER) (Float.neg b2)
haftmann@22951
   245
                        else if atest = 1 then
haftmann@22951
   246
                            update_sure_bound g (u, UPPER) b2
haftmann@22951
   247
                        else
haftmann@22951
   248
                            g
haftmann@22951
   249
                    end
haftmann@22951
   250
                  | _ => fold fold_dest_nodes approx_a g
haftmann@22951
   251
            end
obua@16784
   252
haftmann@22951
   253
        val g = FloatSparseMatrixBuilder.m_fold calcr A VarGraph.empty
obua@23665
   254
haftmann@22951
   255
        val g = propagate_sure_bounds safe_propagation names g
haftmann@22951
   256
wenzelm@32960
   257
        val (r1, r2) = split_graph g
haftmann@22951
   258
obua@24302
   259
        fun add_row_entry m index f vname value =
haftmann@22951
   260
            let
wenzelm@32960
   261
                val v = (case value of 
wenzelm@32960
   262
                             SOME value => FloatSparseMatrixBuilder.mk_spvec_entry 0 value
wenzelm@32960
   263
                           | NONE => FloatSparseMatrixBuilder.mk_spvec_entry' 0 (f $ (Var ((vname,0), HOLogic.realT))))
obua@24302
   264
                val vec = cons_spvec v empty_spvec
haftmann@22951
   265
            in
haftmann@22964
   266
                cons_spmat (FloatSparseMatrixBuilder.mk_spmat_entry index vec) m
haftmann@22951
   267
            end
obua@16784
   268
haftmann@22951
   269
        fun abs_estimate i r1 r2 =
haftmann@22951
   270
            if i = 0 then
obua@24302
   271
                let val e = empty_spmat in (e, e) end
haftmann@22951
   272
            else
haftmann@22951
   273
                let
haftmann@22951
   274
                    val index = xlen-i
obua@24302
   275
                    val (r12_1, r12_2) = abs_estimate (i-1) r1 r2
obua@24302
   276
                    val b1 = Inttab.lookup r1 index
wenzelm@24630
   277
                    val b2 = Inttab.lookup r2 index
haftmann@22951
   278
                in
wenzelm@24630
   279
                    (add_row_entry r12_1 index @{term "lbound :: real => real"} ((names index)^"l") b1, 
wenzelm@32960
   280
                     add_row_entry r12_2 index @{term "ubound :: real => real"} ((names index)^"u") b2)
haftmann@22951
   281
                end
obua@23665
   282
obua@24302
   283
        val (r1, r2) = abs_estimate xlen r1 r2
obua@23665
   284
obua@16784
   285
    in
obua@24302
   286
        (r1, r2)
obua@16784
   287
    end
haftmann@22951
   288
obua@16784
   289
fun load filename prec safe_propagation =
obua@16784
   290
    let
haftmann@22951
   291
        val prog = Cplex.load_cplexFile filename
haftmann@22951
   292
        val prog = Cplex.elim_nonfree_bounds prog
haftmann@22951
   293
        val prog = Cplex.relax_strict_ineqs prog
wenzelm@32960
   294
        val (maximize, c, A, b, (xlen, names, _)) = CplexFloatSparseMatrixConverter.convert_prog prog                       
obua@24302
   295
        val (r1, r2) = calcr safe_propagation xlen names prec A b
haftmann@22951
   296
        val _ = if maximize then () else raise Load "sorry, cannot handle minimization problems"
haftmann@22951
   297
        val (dualprog, indexof) = FloatSparseMatrixBuilder.dual_cplexProg c A b
haftmann@22951
   298
        val results = Cplex.solve dualprog
haftmann@46533
   299
        val (_, v) = CplexFloatSparseMatrixConverter.convert_results results indexof
obua@24302
   300
        (*val A = FloatSparseMatrixBuilder.cut_matrix v NONE A*)
haftmann@22951
   301
        fun id x = x
haftmann@22951
   302
        val v = FloatSparseMatrixBuilder.set_vector FloatSparseMatrixBuilder.empty_matrix 0 v
haftmann@22951
   303
        val b = FloatSparseMatrixBuilder.transpose_matrix (FloatSparseMatrixBuilder.set_vector FloatSparseMatrixBuilder.empty_matrix 0 b)
haftmann@22951
   304
        val c = FloatSparseMatrixBuilder.set_vector FloatSparseMatrixBuilder.empty_matrix 0 c
haftmann@22964
   305
        val (y1, _) = FloatSparseMatrixBuilder.approx_matrix prec Float.positive_part v
haftmann@22951
   306
        val A = FloatSparseMatrixBuilder.approx_matrix prec id A
haftmann@22951
   307
        val (_,b2) = FloatSparseMatrixBuilder.approx_matrix prec id b
haftmann@22951
   308
        val c = FloatSparseMatrixBuilder.approx_matrix prec id c
obua@16784
   309
    in
obua@24302
   310
        (y1, A, b2, c, (r1, r2))
haftmann@22951
   311
    end handle CplexFloatSparseMatrixConverter.Converter s => (raise (Load ("Converter: "^s)))
obua@16784
   312
obua@16784
   313
end