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