| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
type var = int |
| 16 |
type id = int |
| 17 |
|
| 18 |
module Itv = |
| 19 |
struct |
| 20 |
let debug = false |
| 21 |
|
| 22 |
type t = int * int |
| 23 |
|
| 24 |
exception Empty |
| 25 |
|
| 26 |
let output o (lb,ub) = |
| 27 |
Printf.fprintf o "[%i,%i]" lb ub |
| 28 |
|
| 29 |
let wf o (lb,ub) = |
| 30 |
if lb <= ub then () |
| 31 |
else Printf.fprintf o "error %a\n" output (lb,ub) |
| 32 |
|
| 33 |
|
| 34 |
let mul_cst c (lb,ub) = (c * lb, c * ub) |
| 35 |
|
| 36 |
|
| 37 |
let opp (lb,ub) = (-ub,-lb) |
| 38 |
|
| 39 |
let opp i1 = |
| 40 |
let i = opp i1 in |
| 41 |
if debug then Printf.printf "opp %a -> %a\n" output i1 output i; |
| 42 |
i |
| 43 |
|
| 44 |
|
| 45 |
let div (lb,ub) c = |
| 46 |
let lb = lb /c + (if lb mod c = 0 then 0 else 1) in |
| 47 |
let ub = ub / c in |
| 48 |
if lb <= ub then (lb,ub) else raise Empty |
| 49 |
|
| 50 |
let div i c = |
| 51 |
try let r = div i c in |
| 52 |
if debug then Printf.printf "%a div %i -> %a\n" output i c output r; |
| 53 |
r |
| 54 |
with Empty -> |
| 55 |
if debug then Printf.printf "%a div %i -> Empty \n" output i c; |
| 56 |
raise Empty |
| 57 |
|
| 58 |
let add (lb1,ub1) (lb2,ub2) = |
| 59 |
(lb1+lb2,ub1+ub2) |
| 60 |
|
| 61 |
let add i1 i2 = |
| 62 |
let i = add i1 i2 in |
| 63 |
if debug then Printf.printf "%a add %a -> %a\n" output i1 output i2 output i; |
| 64 |
i |
| 65 |
|
| 66 |
let inter : t -> t -> t = |
| 67 |
fun (lb1,ub1) (lb2,ub2) -> |
| 68 |
let ub = max lb1 lb2 in |
| 69 |
let lb = min ub1 ub2 in |
| 70 |
if ub <= lb |
| 71 |
then (ub,lb) |
| 72 |
else raise Empty |
| 73 |
|
| 74 |
let inter i1 i2 = |
| 75 |
try |
| 76 |
let i = inter i1 i2 in |
| 77 |
if debug then Printf.printf "%a inter %a -> %a\n" output i1 output i2 output i; |
| 78 |
i |
| 79 |
with Empty -> |
| 80 |
if debug then Printf.printf "%a inter %a -> Empty\n" output i1 output i2 ; |
| 81 |
raise Empty |
| 82 |
|
| 83 |
|
| 84 |
let enum (lb,ub) = |
| 85 |
match Int.compare lb ub with |
| 86 |
| 0 -> (lb,None) |
| 87 |
| _ -> (lb,Some (lb+1,ub)) |
| 88 |
|
| 89 |
let range (lb,ub) = ub - lb + 1 |
| 90 |
|
| 91 |
let top = (min_int,max_int) |
| 92 |
|
| 93 |
let lt i1 i2 = |
| 94 |
range i1 < range i2 |
| 95 |
|
| 96 |
end |
| 97 |
|
| 98 |
module ItvMap = |
| 99 |
struct |
| 100 |
|
| 101 |
module M = Map.Make(Int) |
| 102 |
|
| 103 |
include M |
| 104 |
|
| 105 |
let refine_with v i m = |
| 106 |
try |
| 107 |
let i0 = M.find v m in |
| 108 |
let i' = Itv.inter i i0 in |
| 109 |
(Itv.lt i' i0,i',M.add v i' m) |
| 110 |
with Not_found -> |
| 111 |
(true, i, M.add v i m) |
| 112 |
|
| 113 |
let pick m = |
| 114 |
let (x,i,r) = |
| 115 |
fold (fun v i (v',i',r') -> |
| 116 |
let r = Itv.range i in |
| 117 |
if r < r' then (v,i,r) else (v',i',r')) m (min_int,Itv.top,max_int) in |
| 118 |
if x = min_int then raise Not_found else (x,i) |
| 119 |
|
| 120 |
let output o m = |
| 121 |
Printf.fprintf o "["; |
| 122 |
iter (fun k (lb,ub) -> Printf.fprintf o "x%i -> [%i,%i] " k lb ub) m; |
| 123 |
Printf.fprintf o "]"; |
| 124 |
|
| 125 |
|
| 126 |
end |
| 127 |
|
| 128 |
exception Unsat |
| 129 |
|
| 130 |
module Eqn = |
| 131 |
struct |
| 132 |
type t = (var * int) list * int |
| 133 |
|
| 134 |
let empty = ([],0) |
| 135 |
|
| 136 |
let rec output_lin o l = |
| 137 |
match l with |
| 138 |
| [] -> Printf.fprintf o "0" |
| 139 |
| [x,v] -> Printf.fprintf o "%i.x%i" v x |
| 140 |
| (x,v)::l' -> Printf.fprintf o "%i.x%i + %a" v x output_lin l' |
| 141 |
|
| 142 |
let normalise (l,c) = |
| 143 |
match l with |
| 144 |
| [] -> if c = 0 then None else raise Unsat |
| 145 |
| _ -> Some(l,c) |
| 146 |
|
| 147 |
|
| 148 |
let rec no_dup l = |
| 149 |
match l with |
| 150 |
| [] -> true |
| 151 |
| (x,v)::l -> try |
| 152 |
let _ = List.assoc x l in |
| 153 |
false |
| 154 |
with Not_found -> no_dup l |
| 155 |
|
| 156 |
|
| 157 |
let add (l1,c1) (l2,c2) = |
| 158 |
(l1@l2,c1+c2) |
| 159 |
|
| 160 |
let add e1 e2 = |
| 161 |
let r = add e1 e2 in |
| 162 |
if no_dup (fst r) then () |
| 163 |
else Printf.printf "add(duplicate)%a %a" output_lin (fst e1) output_lin (fst e2) ; |
| 164 |
r |
| 165 |
|
| 166 |
|
| 167 |
let itv_of_ax m (var,coe) = |
| 168 |
Itv.mul_cst coe (ItvMap.find var m) |
| 169 |
|
| 170 |
let itv_list m l = |
| 171 |
List.fold_left (fun i (var,coe) -> |
| 172 |
Itv.add i (itv_of_ax m (var,coe))) (0,0) l |
| 173 |
|
| 174 |
let get_remove x l = |
| 175 |
let l' = List.remove_assoc x l in |
| 176 |
let c = try List.assoc x l with Not_found -> 0 in |
| 177 |
(c,l') |
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
end |
| 182 |
type eqn = Eqn.t |
| 183 |
|
| 184 |
open Eqn |
| 185 |
|
| 186 |
let debug = false |
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
let init_bound m (v,c) = |
| 192 |
match v with |
| 193 |
| [] -> if c = 0 then m else raise Unsat |
| 194 |
| [x,v] -> let (_,_,m) = ItvMap.refine_with x (Itv.div (c,c) v) m in m |
| 195 |
| _ -> |
| 196 |
List.fold_left (fun m (var,coe) -> |
| 197 |
let (_,_,m) = ItvMap.refine_with var (0,c / coe) m in |
| 198 |
m) m v |
| 199 |
|
| 200 |
let init_bounds sys = |
| 201 |
List.fold_left init_bound ItvMap.empty sys |
| 202 |
|
| 203 |
let init_bounds sys = |
| 204 |
let m = init_bounds sys in |
| 205 |
if debug then Printf.printf "init_bound : %a\n" ItvMap.output m; |
| 206 |
m |
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
let rec refine_bound p m acc (v,c) = |
| 215 |
Itv.wf stdout acc; |
| 216 |
match v with |
| 217 |
| [] -> (m,p) |
| 218 |
| (var,coe)::v' -> |
| 219 |
if debug then Printf.printf "Refining %i.x%i + %a + %a = %i\n" coe var |
| 220 |
Itv.output acc output_lin v' c; |
| 221 |
let itv_acc_l = Itv.inter (0,c) (Itv.add acc (itv_list m v')) in |
| 222 |
let itv_coe_var = Itv.add (c,c) (Itv.opp itv_acc_l) in |
| 223 |
let i = Itv.div itv_coe_var coe in |
| 224 |
let (b,i',m) = ItvMap.refine_with var i m in |
| 225 |
refine_bound (p || b) |
| 226 |
m (Itv.add (Itv.mul_cst coe i') acc) (v',c) |
| 227 |
|
| 228 |
let refine_bounds p m l = |
| 229 |
List.fold_left (fun (m,p) eqn -> |
| 230 |
refine_bound p m (0,0) eqn) (m,p) l |
| 231 |
|
| 232 |
let refine_until_fix m l = |
| 233 |
let rec iter_refine m = |
| 234 |
let (m',b) = refine_bounds false m l in |
| 235 |
if b then iter_refine m' else m' in |
| 236 |
iter_refine m |
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
let subst x a l = |
| 242 |
|
| 243 |
let subst_eqn acc (v,c) = |
| 244 |
let (coe,v') = Eqn.get_remove x v in |
| 245 |
let (v',c') = (v', c - coe * a) in |
| 246 |
match v' with |
| 247 |
| [] -> if c' = 0 then acc |
| 248 |
else raise Unsat |
| 249 |
| _ -> (v',c')::acc in |
| 250 |
|
| 251 |
List.fold_left subst_eqn [] l |
| 252 |
|
| 253 |
let output_list elt o l = |
| 254 |
Printf.fprintf o "["; |
| 255 |
List.iter (fun e -> Printf.fprintf o "%a; " elt e) l; |
| 256 |
Printf.fprintf o "]" |
| 257 |
|
| 258 |
let output_equations o l = |
| 259 |
let output_equation o (l,c) = |
| 260 |
Printf.fprintf o "%a = %i" output_lin l c in |
| 261 |
output_list output_equation o l |
| 262 |
|
| 263 |
let output_intervals o m = |
| 264 |
ItvMap.iter (fun k v -> Printf.fprintf o "x%i:%a " k Itv.output v) m |
| 265 |
|
| 266 |
|
| 267 |
type solution = (var * int) list |
| 268 |
|
| 269 |
let solve_system l = |
| 270 |
|
| 271 |
let rec solve m l = |
| 272 |
if debug then Printf.printf "Solve %a\n" output_equations l; |
| 273 |
|
| 274 |
match l with |
| 275 |
| [] -> [m] |
| 276 |
| _ -> |
| 277 |
try |
| 278 |
let m' = refine_until_fix m l in |
| 279 |
try |
| 280 |
if debug then Printf.printf "Refined %a\n" ItvMap.output m' ; |
| 281 |
let (k,i) = ItvMap.pick m' in |
| 282 |
let (v,itv') = Itv.enum i in |
| 283 |
|
| 284 |
let sol1 = |
| 285 |
List.map (ItvMap.add k (v,v)) |
| 286 |
(solve (ItvMap.remove k m) (subst k v l)) in |
| 287 |
let sol2 = |
| 288 |
match itv' with |
| 289 |
| None -> [] |
| 290 |
| Some itv' -> |
| 291 |
|
| 292 |
solve (ItvMap.add k itv' m) l in |
| 293 |
sol1 @ sol2 |
| 294 |
with | Not_found -> Printf.printf "NOT FOUND %a %a\n" output_equations l output_intervals m'; raise Not_found |
| 295 |
with (Unsat | Itv.Empty) as e -> |
| 296 |
begin |
| 297 |
if debug then Printf.printf "Unsat detected %s\n" (Printexc.to_string e); |
| 298 |
[] end |
| 299 |
in |
| 300 |
|
| 301 |
try |
| 302 |
let l = CList.map_filter Eqn.normalise l in |
| 303 |
solve (init_bounds l) l |
| 304 |
with Itv.Empty | Unsat -> [] |
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
let enum_sol m = |
| 309 |
let rec augment_sols x (lb,ub) s = |
| 310 |
let slb = if lb = 0 then s |
| 311 |
else List.rev_map (fun s -> (x,lb)::s) s in |
| 312 |
if lb = ub then slb |
| 313 |
else let sl = augment_sols x (lb+1,ub) s in |
| 314 |
List.rev_append slb sl in |
| 315 |
|
| 316 |
ItvMap.fold augment_sols m [[]] |
| 317 |
|
| 318 |
let enum_sols l = |
| 319 |
List.fold_left (fun s m -> List.rev_append (enum_sol m) s) [] l |
| 320 |
|
| 321 |
|
| 322 |
let solve_and_enum l = enum_sols (solve_system l) |
| 323 |
|
| 324 |
|
| 325 |
let output_solution o s = |
| 326 |
let output_var_coef o (x,v) = |
| 327 |
Printf.fprintf o "x%i:%i" x v in |
| 328 |
output_list output_var_coef o s ; |
| 329 |
Printf.fprintf o "\n" |
| 330 |
|
| 331 |
let output_solutions o l = output_list output_solution o l |
| 332 |
|
| 333 |
|
| 334 |
open Mutils |
| 335 |
|
| 336 |
type system = Eqn.t IMap.t |
| 337 |
|
| 338 |
let empty : system = IMap.empty |
| 339 |
|
| 340 |
let set_constant (idx:int) (c:int) (s:system) : Eqn.t = |
| 341 |
let e = try IMap.find idx s with |
| 342 |
|Not_found -> Eqn.empty in |
| 343 |
(fst e,c) |
| 344 |
|
| 345 |
let make_mon (idx:int) (v:var) (c:int) (s:system) : system = |
| 346 |
IMap.add idx ([v,c],0) s |
| 347 |
|
| 348 |
let merge (s1:system) (s2:system) : system = |
| 349 |
IMap.merge (fun k e1 e2 -> |
| 350 |
match e1 , e2 with |
| 351 |
| None , None -> None |
| 352 |
| None , Some e | Some e , None -> Some e |
| 353 |
| Some e1, Some e2 -> Some (Eqn.add e1 e2)) s1 s2 |