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