src/HOL/Matrix_LP/fspmlp.ML
author haftmann
Sat, 06 Sep 2014 20:12:32 +0200
changeset 58195 1fee63e0377d
parent 47455 26315a545e26
child 69597 ff784d5a5bfb
permissions -rw-r--r--
added various facts
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
47455
26315a545e26 updated headers;
wenzelm
parents: 46988
diff changeset
     1
(*  Title:      HOL/Matrix_LP/fspmlp.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
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 21056
diff changeset
     5
signature FSPMLP =
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
     6
sig
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
     7
    type linprog
19404
9bf2cdc9e8e8 Moved stuff from Ring_and_Field to Matrix
obua
parents: 17412
diff changeset
     8
    type vector = FloatSparseMatrixBuilder.vector
9bf2cdc9e8e8 Moved stuff from Ring_and_Field to Matrix
obua
parents: 17412
diff changeset
     9
    type matrix = FloatSparseMatrixBuilder.matrix
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
    10
22964
2284e0d02e7f reorganized float arithmetic
haftmann
parents: 22951
diff changeset
    11
    val y : linprog -> term
2284e0d02e7f reorganized float arithmetic
haftmann
parents: 22951
diff changeset
    12
    val A : linprog -> term * term
2284e0d02e7f reorganized float arithmetic
haftmann
parents: 22951
diff changeset
    13
    val b : linprog -> term
2284e0d02e7f reorganized float arithmetic
haftmann
parents: 22951
diff changeset
    14
    val c : linprog -> term * term
2284e0d02e7f reorganized float arithmetic
haftmann
parents: 22951
diff changeset
    15
    val r12 : linprog -> term * term
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
    16
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
    17
    exception Load of string
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 21056
diff changeset
    18
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
    19
    val load : string -> int -> bool -> linprog
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
    20
end
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
    21
22964
2284e0d02e7f reorganized float arithmetic
haftmann
parents: 22951
diff changeset
    22
structure Fspmlp : FSPMLP =
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
    23
struct
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
    24
19404
9bf2cdc9e8e8 Moved stuff from Ring_and_Field to Matrix
obua
parents: 17412
diff changeset
    25
type vector = FloatSparseMatrixBuilder.vector
9bf2cdc9e8e8 Moved stuff from Ring_and_Field to Matrix
obua
parents: 17412
diff changeset
    26
type matrix = FloatSparseMatrixBuilder.matrix
9bf2cdc9e8e8 Moved stuff from Ring_and_Field to Matrix
obua
parents: 17412
diff changeset
    27
24302
obua
parents: 24135
diff changeset
    28
type linprog = term * (term * term) * term * (term * term) * (term * term)
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
    29
46533
faf233c4a404 dropped dead code
haftmann
parents: 37788
diff changeset
    30
fun y (c1, _, _, _, _) = c1
faf233c4a404 dropped dead code
haftmann
parents: 37788
diff changeset
    31
fun A (_, c2, _, _, _) = c2
faf233c4a404 dropped dead code
haftmann
parents: 37788
diff changeset
    32
fun b (_, _, c3, _, _) = c3
faf233c4a404 dropped dead code
haftmann
parents: 37788
diff changeset
    33
fun c (_, _, _, c4, _) = c4
faf233c4a404 dropped dead code
haftmann
parents: 37788
diff changeset
    34
fun r12 (_, _, _, _, c6) = c6
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
    35
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 21056
diff changeset
    36
structure CplexFloatSparseMatrixConverter =
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
    37
MAKE_CPLEX_MATRIX_CONVERTER(structure cplex = Cplex and matrix_builder = FloatSparseMatrixBuilder);
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
    38
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
    39
datatype bound_type = LOWER | UPPER
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
    40
24135
e7fb7ef2a85e added int type constraints to accomodate hacked SML/NJ;
wenzelm
parents: 23665
diff changeset
    41
fun intbound_ord ((i1: int, b1),(i2,b2)) =
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
    42
    if i1 < i2 then LESS
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 21056
diff changeset
    43
    else if i1 = i2 then
dfafcd6223ad whitespace tuned
haftmann
parents: 21056
diff changeset
    44
        (if b1 = b2 then EQUAL else if b1=LOWER then LESS else GREATER)
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
    45
    else GREATER
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
    46
31971
8c1b845ed105 renamed functor TableFun to Table, and GraphFun to Graph;
wenzelm
parents: 24630
diff changeset
    47
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
    48
31971
8c1b845ed105 renamed functor TableFun to Table, and GraphFun to Graph;
wenzelm
parents: 24630
diff changeset
    49
structure VarGraph = Table(type key = int*bound_type val ord = intbound_ord);
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
    50
(* key -> (float option) * (int -> (float * (((float * float) * key) list)))) *)
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
    51
(* dest_key -> (sure_bound * (row_index -> (row_bound * (((coeff_lower * coeff_upper) * src_key) list)))) *)
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
    52
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
    53
exception Internal of string;
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
    54
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 21056
diff changeset
    55
fun add_row_bound g dest_key row_index row_bound =
dfafcd6223ad whitespace tuned
haftmann
parents: 21056
diff changeset
    56
    let
dfafcd6223ad whitespace tuned
haftmann
parents: 21056
diff changeset
    57
        val x =
dfafcd6223ad whitespace tuned
haftmann
parents: 21056
diff changeset
    58
            case VarGraph.lookup g dest_key of
dfafcd6223ad whitespace tuned
haftmann
parents: 21056
diff changeset
    59
                NONE => (NONE, Inttab.update (row_index, (row_bound, [])) Inttab.empty)
dfafcd6223ad whitespace tuned
haftmann
parents: 21056
diff changeset
    60
              | SOME (sure_bound, f) =>
dfafcd6223ad whitespace tuned
haftmann
parents: 21056
diff changeset
    61
                (sure_bound,
dfafcd6223ad whitespace tuned
haftmann
parents: 21056
diff changeset
    62
                 case Inttab.lookup f row_index of
dfafcd6223ad whitespace tuned
haftmann
parents: 21056
diff changeset
    63
                     NONE => Inttab.update (row_index, (row_bound, [])) f
dfafcd6223ad whitespace tuned
haftmann
parents: 21056
diff changeset
    64
                   | SOME _ => raise (Internal "add_row_bound"))
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
    65
    in
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 21056
diff changeset
    66
        VarGraph.update (dest_key, x) g
16784
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
    67
    end
92ff7c903585 - added cplex package to HOL/Matrix
obua
parents:
diff changeset
    68
22951
dfafcd6223ad whitespace tuned
haftmann
parents: 21056
diff changeset
    69
fun update_sure_bound g (key as (_, btype)) bound =
dfafcd6223ad whitespace tuned
haftmann
parents: 21056
diff changeset
    70
    let
dfafcd6223ad whitespace tuned
haftmann
parents: 21056
diff changeset
    71
        val x =
dfafcd6223ad whitespace tuned
haftmann
parents: 21056
diff changeset
    72
            case VarGraph.lookup g key of
dfafcd6223ad whitespace tuned
haftmann
parents: 21056
diff changeset
    73
                NONE => (SOME bound, Inttab.empty)
dfafcd6223ad whitespace tuned
haftmann
parents: 21056
diff changeset
    74
              | SOME (NONE, f) => (SOME bound, f)
dfafcd6223ad whitespace tuned
haftmann
parents: 21056
diff changeset
    75
              | SOME (SOME old_bound, f) =>
dfafcd6223ad whitespace tuned
haftmann
parents: 21056
diff changeset
    76
                (SOME ((case btype of
22964
2284e0d02e7f reorganized float arithmetic
haftmann
parents: 22951
diff changeset
    77
                            UPPER => Float.min
2284e0d02e7f reorganized float arithmetic
haftmann
parents: 22951
diff changeset
    78
                          | LOWER => Float.max)
22951
dfafcd6223ad whitespace tuned
haftmann