solver.ml 27.9 KB
Newer Older
1
(*-----------------------------------------------------------------------
2
 ** Copyright (C) 2001, 2002 - Verimag.
3 4 5 6 7 8 9 10
** This file may only be copied under the terms of the GNU Library General
** Public License 
**-----------------------------------------------------------------------
**
** File: solver.ml
** Main author: jahier@imag.fr
*)

11
open List
12
open Formula
13
open Constraint
14
open Util
15
open Hashtbl
16
open Gne
17
open Value
18
open Rnumsolver
19

20 21
(****************************************************************************)
	  
22
let (lookup: env_in -> subst list -> var_name -> Value.t option) = 
23 24
  fun input pre vn ->  
    try Some(Hashtbl.find input vn)
25
    with Not_found -> 
26 27 28 29 30 31 32 33 34
      try Some(List.assoc vn pre)
      with Not_found -> None

(****************************************************************************)

type comp = SupZero | SupEqZero | EqZero | NeqZero

let rec (formula_to_bdd : env_in -> formula -> Bdd.t * bool) =
  fun input f ->
35 36
    (** Returns the bdd of [f] where input and pre variables
      have been repaced by their values.
37

38 39 40
      Also returns a flag that is true iff the formula depends on
      input and pre vars. If this flag is false, the formula is
      stored (cached) in a global table ([env_state.bdd_tbl_global]);
41
      otherwise, it is stored in a table that is cleared at each new
42
      step ([env_state.bdd_tbl]).  
43
    *)
44
    try (Env_state.bdd f, true)
45
    with Not_found -> 
46
      try (Env_state.bdd_global f, false)
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
      with Not_found -> 
	let (bdd, dep) =
	  match f with 
	      Not(f1) ->
		let (bdd_not, dep) =  (formula_to_bdd input f1) in
		  (Bdd.dnot bdd_not, dep)

	    | Or(f1, f2) ->
		let (bdd1, dep1) = (formula_to_bdd input f1)
		and (bdd2, dep2) = (formula_to_bdd input f2)
		in
		  (Bdd.dor bdd1 bdd2, dep1 || dep2)

	    | And(f1, f2) -> 
		let (bdd1, dep1) = (formula_to_bdd input f1)
		and (bdd2, dep2) = (formula_to_bdd input f2)
		in
		  (Bdd.dand bdd1 bdd2, dep1 || dep2)
65 66 67 68 69 70

	    | EqB(f1, f2) -> 
		let (bdd1, dep1) = (formula_to_bdd input f1)
		and (bdd2, dep2) = (formula_to_bdd input f2)
		in
		  (Bdd.eq bdd1 bdd2, dep1 || dep2)
71 72 73 74 75 76 77 78 79 80

	    | IteB(f1, f2, f3) -> 
		let (bdd1, dep1) = (formula_to_bdd input f1)
		and (bdd2, dep2) = (formula_to_bdd input f2)
		and (bdd3, dep3) = (formula_to_bdd input f3) 
		in
		  ((Bdd.dor (Bdd.dand bdd1 bdd2) 
		      (Bdd.dand (Bdd.dnot bdd1) bdd3)),
		   dep1 || dep2 || dep3 )
		  
81 82
	    | True ->  (Bdd.dtrue  (Env_state.bdd_manager ()), false)
	    | False -> (Bdd.dfalse (Env_state.bdd_manager ()), false)
83
	    | Bvar(vn) ->    
84
		( match (lookup input (Env_state.pre ()) vn) with 
85
		      Some(B(bool)) -> 
86
			if bool
87
			then (Bdd.dtrue  (Env_state.bdd_manager ()), true)
88
			else (Bdd.dfalse (Env_state.bdd_manager ()), true)
89 90 91 92
		    | Some(_) -> 
			print_string (vn ^ " is not a boolean!\n");
			assert false
		    | None ->
93
			if List.mem vn (Env_state.pre_var_names ())
94 95 96 97 98 99
			then failwith 
			  ("*** " ^ vn ^ " is unknown at this stage.\n "
			   ^ "*** Make sure you have not used "
			   ^ "a pre on a output var at the 1st step, \n "
			   ^ "*** or a pre on a input var at the second step in "
			   ^ "your formula in the environment.\n ")
100
			else (Bdd.ithvar (Env_state.bdd_manager ())
101
				(Env_state.linear_constraint_to_index (Bv(vn)) false), 
102
				false)
103 104 105
		)
		
	    | Eq(e1, e2) -> 
106 107
		let gne = expr_to_gne (Diff(e1, e2)) input in 
		  (gne_to_bdd gne EqZero)
108 109
		  
	    | Neq(e1, e2) -> 
110 111
		let gne = expr_to_gne (Diff(e1, e2)) input in
		  (gne_to_bdd gne NeqZero)
112 113
		  
	    | SupEq(e1, e2) ->
114 115
		let gne = expr_to_gne (Diff(e1, e2)) input in
		  (gne_to_bdd gne SupEqZero)
116 117
		  
	    | Sup(e1, e2)   ->
118 119
		let gne = expr_to_gne (Diff(e1, e2)) input in
		  (gne_to_bdd gne SupZero)
120 121
		  
	    | InfEq(e1, e2) ->  
122 123
		let gne = expr_to_gne (Diff(e2, e1)) input in
		  (gne_to_bdd gne SupEqZero)
124 125
		  
	    | Inf(e1, e2)   ->  
126 127
		let gne =  expr_to_gne (Diff(e2, e1)) input in
		  (gne_to_bdd gne SupZero)
128 129 130 131
		  
	in
	  if dep
	  then 
132
	    ( Env_state.set_bdd f bdd;	
133 134
	      match f with 
		  Not(nf) -> () (* Already in the tbl thanks to the rec call *)
135
		| _  -> Env_state.set_bdd (Not(f)) (Bdd.dnot bdd) 
136
	    )
137 138
	  else 
	    (* [f] does not depend on pre nor input vars *)
139
	    ( Env_state.set_bdd_global f bdd ;	
140 141 142
	      match f with 
		  Not(nf) -> () (* Already in the table thanks to the rec call *)
		| _  -> 
143
		    Env_state.set_bdd_global (Not(f)) (Bdd.dnot bdd)
144 145 146 147
	    );

	  (bdd, dep)
and
148
  (expr_to_gne: expr -> env_in -> Gne.t) =
149 150 151 152
  fun e input -> 
    (** Evaluates pre and input vars appearing in [e] and tranlates
      it into a so-called garded normal form. Also returns a flag
      that is true iff [e] depends on pre or input vars. *)
153
    let gne =
154 155
      match e with  
	  Sum(e1, e2) ->
156 157
	    let gne1 = (expr_to_gne e1 input)
	    and gne2 = (expr_to_gne e2 input) 
158
	    in
159
	      Gne.add  gne1 gne2
160 161

	| Diff(e1, e2) -> 
162 163
	    let gne1 = (expr_to_gne e1 input)
	    and gne2 = (expr_to_gne e2 input) 
164
	    in
165
	      Gne.diff gne1 gne2
166 167

	| Prod(e1, e2) -> 
168 169
	    let gne1 = (expr_to_gne e1 input)
	    and gne2 = (expr_to_gne e2 input) 
170
	    in
171
	      Gne.mult gne1 gne2
172 173

	| Quot(e1, e2) -> 
174 175
	    let gne1 = (expr_to_gne e1 input)
	    and gne2 = (expr_to_gne e2 input) 
176
	    in
177
	      Gne.quot gne1 gne2
178 179

	| Mod(e1, e2)  -> 
180 181
	    let gne1 = (expr_to_gne e1 input)
	    and gne2 = (expr_to_gne e2 input) 
182
	    in
183
	      Gne.modulo gne1 gne2
184 185

	| Ivar(str) ->
186
	    ( match (lookup input (Env_state.pre ()) str) with 
187
		  Some(N(I(i))) ->
188 189 190 191
		    (Gne.make 
		       (Ne.make "" (I i)) 
		       (Bdd.dtrue (Env_state.bdd_manager ()))
		       true
192 193
		    )
		| None ->
194 195 196 197
		    (Gne.make 
		       (Ne.make str (I(1)))
		       (Bdd.dtrue (Env_state.bdd_manager ()))
		       false
198
		    )
199 200 201 202 203 204 205 206 207 208 209
		| Some(N(F(f))) -> 
		    print_string ((string_of_float f) 
				  ^ "is a float, but an int is expected.\n");
		    assert false
		| Some(B(f)) -> 
		    print_string ((string_of_bool f) 
				  ^ "is a bool, but an int is expected.\n");
		    assert false
	    )

	| Fvar(str) ->
210
	    ( match (lookup input (Env_state.pre ()) str) with 
211
		  Some(N(F(f))) ->
212 213 214 215
		    ( Gne.make 
			(Ne.make "" (F(f))) 
			(Bdd.dtrue (Env_state.bdd_manager ()))
			true
216 217
		    )
		| None ->
218 219 220 221
		    ( Gne.make 
			(Ne.make str (F(1.))) 
			(Bdd.dtrue (Env_state.bdd_manager ()))
			false
222 223 224 225 226 227 228 229 230 231 232 233
		    )
		| Some(N(I(i))) -> 
		    print_string ((string_of_int i) 
				  ^ "is an int, but a float is expected.\n");
		    assert false
		| Some(B(f)) -> 
		    print_string ((string_of_bool f) 
				  ^ "is a bool, not a float is expected.\n");
		    assert false
	    )

	| Ival(i) ->  
234 235 236 237
	    (Gne.make 
	       (Ne.make "" (I(i))) 
	       (Bdd.dtrue (Env_state.bdd_manager ()))
	       false
238 239 240
	    )

	| Fval(f) -> 
241 242 243 244
	    ( Gne.make 
		(Ne.make "" (F(f))) 
		(Bdd.dtrue (Env_state.bdd_manager ()))
		false
245 246 247
	    )

	| Ite(f, e1, e2) -> 
248 249
	    let (add_formula_to_gne_acc : Bdd.t -> bool -> Ne.t -> 
		   Bdd.t * bool -> Gne.t -> Gne.t) = 
250
	      fun bdd dep1 nexpr (c, dep2) acc -> 
251
		(* Used (by a Gne.fold) to add the condition [c] to every
252 253
		   condition of a garded expression. *)
		let _ = assert (
254
		  try 		    let _ = Gne.find nexpr acc in
255 256 257 258 259 260 261
		      false
		  with Not_found -> true
		) 
		in
		let new_bdd = (Bdd.dand bdd c) in
		  if Bdd.is_false new_bdd
		  then acc
262
		  else Gne.add_ne nexpr new_bdd (dep1 || dep2) acc
263
	    in
264
	    let (bdd, depf) = formula_to_bdd input f in
265
	    let bdd_not = Bdd.dnot bdd
266 267
	    and gne_t = (expr_to_gne e1 input)
	    and gne_e = (expr_to_gne e2 input) in
268 269 270 271
	    let gne1 = 
	      Gne.fold (add_formula_to_gne_acc bdd depf) gne_t (Gne.empty ())
	    in
	    let gne  = Gne.fold (add_formula_to_gne_acc bdd_not depf) gne_e gne1 in
272
	      gne
273
    in
274
      gne
275
	
276
and
277
  (gne_to_bdd : Gne.t -> comp -> Bdd.t * bool) =
278 279
  fun gne cmp -> 
    (** Use [cmp] to compare [gne] with 0 and returns the
280 281 282 283
      corresponding formula.  E.g., if [gne] is bounded to
      [e1 -> c1; e2 -> c2], then [gne_to_bdd gne SupZero] returns
      (the bdd corresponding to) the formula [(c1 and (e1 > 0)) or
      (c2 and (e2 > 0))] *)
284 285
    match cmp with
	SupZero ->
286
	  ( Gne.fold 
287 288 289
	      (fun nexpr (c, dep) (acc, dep_acc) -> 
		 let new_dep = dep || dep_acc 
		 and bdd = 
290
		   if Ne.is_a_constant nexpr
291
		   then 
292
		     let cst = Ne.find "" nexpr in
293 294 295
		       match cst with
			   I(i) -> 
			     if i > 0 
296 297
			     then (Bdd.dtrue (Env_state.bdd_manager ()))
			     else (Bdd.dfalse (Env_state.bdd_manager ()))
298 299
			 | F(f) -> 
			     if f > 0. 
300 301
			     then (Bdd.dtrue (Env_state.bdd_manager ()))
			     else (Bdd.dfalse (Env_state.bdd_manager ()))
302 303 304

		   else 
		     Bdd.ithvar 
305
		       (Env_state.bdd_manager ()) 
306
		       (Env_state.linear_constraint_to_index (GZ(nexpr)) dep) 
307
		 in
308
		   (Bdd.dor (Bdd.dand c bdd) acc, new_dep)
309 310
	      )
	      gne 
311
	      ((Bdd.dfalse (Env_state.bdd_manager ())), false)
312 313
	  )
      | SupEqZero ->
314
	  ( Gne.fold 
315 316 317
	      (fun nexpr (c, dep) (acc, dep_acc) -> 
		 let new_dep = dep || dep_acc 
		 and bdd = 
318
		   if Ne.is_a_constant nexpr
319
		   then 
320
		     let cst = Ne.find "" nexpr in
321 322 323
		       match cst with
			   I(i) -> 
			     if i >= 0 
324 325
			     then (Bdd.dtrue (Env_state.bdd_manager ()))
			     else (Bdd.dfalse (Env_state.bdd_manager ()))
326 327
			 | F(f) -> 
			     if f >= 0. 
328 329
			     then (Bdd.dtrue (Env_state.bdd_manager ()))
			     else (Bdd.dfalse (Env_state.bdd_manager ()))
330 331 332

		   else 
		     Bdd.ithvar 
333
		       (Env_state.bdd_manager ()) 
334
		       (Env_state.linear_constraint_to_index (GeqZ(nexpr)) dep)
335
		 in
336
		   (Bdd.dor (Bdd.dand c bdd) acc, new_dep)
337 338
	      )
	      gne 
339
	      ((Bdd.dfalse (Env_state.bdd_manager ())), false)
340 341
	  )
      | EqZero -> 
342
	  ( Gne.fold 
343
	      (fun nexpr (c, dep) (acc, dep_acc) -> 
344
		 let bdd1 = 
345
		   if Ne.is_a_constant nexpr
346
		   then 
347
		     let cst = Ne.find "" nexpr in
348 349 350
		       match cst with
			   I(i) -> 
			     if i >= 0 
351 352
			     then (Bdd.dtrue (Env_state.bdd_manager ()))
			     else (Bdd.dfalse (Env_state.bdd_manager ()))
353 354
			 | F(f) -> 
			     if f >= 0. 
355 356
			     then (Bdd.dtrue (Env_state.bdd_manager ()))
			     else (Bdd.dfalse (Env_state.bdd_manager ()))
357 358 359

		   else 
		     Bdd.ithvar 
360
		       (Env_state.bdd_manager ()) 
361
		       (Env_state.linear_constraint_to_index (GeqZ(nexpr)) dep) 
362 363
		 in
		 let bdd2 = 
364
		   if Ne.is_a_constant nexpr
365
		   then 
366
		     let cst = Ne.find "" nexpr in
367 368 369
		       match cst with
			   I(i) -> 
			     if i <= 0 
370 371
			     then (Bdd.dtrue (Env_state.bdd_manager ()))
			     else (Bdd.dfalse (Env_state.bdd_manager ()))
372 373
			 | F(f) -> 
			     if f <= 0. 
374 375
			     then (Bdd.dtrue (Env_state.bdd_manager ()))
			     else (Bdd.dfalse (Env_state.bdd_manager ()))
376 377 378

		   else 
		     Bdd.ithvar 
379
		       (Env_state.bdd_manager ()) 
380
		       (Env_state.linear_constraint_to_index 
381
			  (GeqZ(Ne.neg_nexpr nexpr)) dep) 
382
		 in
383 384
		 let new_dep = dep || dep_acc 
		 and bdd = Bdd.dand bdd1 bdd2 in 
385 386
		   (* We transform [e1 = e2] into [e1 <= e2 ^ e1 >= e2] as the 
		      numeric solver can not handle equalities *)
387
		   (Bdd.dor (Bdd.dand c bdd) acc, new_dep)
388 389
	      )
	      gne 
390
	      ((Bdd.dfalse (Env_state.bdd_manager ())), false)
391 392
	  )
      | NeqZero -> 
393
	  ( Gne.fold 
394
	      (fun nexpr (c, dep) (acc, dep_acc) -> 
395
		 let bdd1 = 
396
		   if Ne.is_a_constant nexpr
397
		   then 
398
		     let cst = Ne.find "" nexpr in
399 400 401
		       match cst with
			   I(i) -> 
			     if i > 0 
402 403
			     then (Bdd.dtrue (Env_state.bdd_manager ()))
			     else (Bdd.dfalse (Env_state.bdd_manager ()))
404 405
			 | F(f) -> 
			     if f > 0. 
406 407
			     then (Bdd.dtrue (Env_state.bdd_manager ()))
			     else (Bdd.dfalse (Env_state.bdd_manager ()))
408 409 410

		   else 
		     Bdd.ithvar 
411
		       (Env_state.bdd_manager ()) 
412
		       (Env_state.linear_constraint_to_index (GZ(nexpr)) dep) 
413 414
		 in
		 let bdd2 = 
415
		   if Ne.is_a_constant nexpr
416
		   then 
417
		     let cst = Ne.find "" nexpr in
418 419 420
		       match cst with
			   I(i) -> 
			     if i < 0 
421 422
			     then (Bdd.dtrue (Env_state.bdd_manager ()))
			     else (Bdd.dfalse (Env_state.bdd_manager ()))
423 424
			 | F(f) -> 
			     if f < 0. 
425 426
			     then (Bdd.dtrue (Env_state.bdd_manager ()))
			     else (Bdd.dfalse (Env_state.bdd_manager ()))
427 428 429

		   else 
		     Bdd.ithvar 
430
		       (Env_state.bdd_manager ()) 
431
		       (Env_state.linear_constraint_to_index 
432
			  (GZ(Ne.neg_nexpr nexpr)) dep)
433
		 in
434 435
		 let new_dep = dep || dep_acc 
		 and bdd = Bdd.dor bdd1 bdd2 in 
436 437
		   (* We transform [e1 <> e2] into [e1 < e2 or e1 > e2] as the 
		      numeric solver can not handle disequalities *)
438
		   (Bdd.dor (Bdd.dand c bdd) acc, new_dep)
439 440
	      )
	      gne 
441
	      ((Bdd.dfalse (Env_state.bdd_manager ())), false)
442
	  )
443

444 445 446 447 448
(****************************************************************************)
(****************************************************************************)


(* Exported *)
449 450
let rec (is_satisfiable: env_in -> formula -> bool) = 
  fun input f -> 
451
    let (bdd, _) = formula_to_bdd input f in
452 453 454
      not (Bdd.is_false bdd) &&
      ( 
	try 
455
	  let (n, m) = Env_state.sol_number bdd in 
456 457 458 459 460 461 462
	    not ((zero_sol, zero_sol) = (n, m))
	with Not_found -> true
      )
      


(****************************************************************************)
463 464 465 466 467 468 469 470 471
(****************************************************************************)


(** In the following, we call a comb the bdd of a conjunction of
 litterals (var). They provide the ordering in which litterals
 appear in the bdds we manipulate.
*)


472
type var = int
473 474 475 476 477

let rec (build_sol_nb_table: Bdd.t -> Bdd.t -> sol_nb * sol_nb) =
  fun bdd comb -> 
    (** Returns the relative (to which bbd points to it) number of
      solutions of [bdd] and the one of its negation. Also udpates
478 479 480 481 482 483
      the solution number table for [bdd] and its negation, and
      recursively for all its sub-bdds.

      [comb] is a positive cube that ougth to contain the indexes of
      boolean vars that are still to be generated, and the numerical
      indexes that appears in [bdd].  
484
    *)
485 486 487
    let _ = assert (not (Bdd.is_cst bdd) 
		    && (Bdd.topvar comb) = (Bdd.topvar bdd)) 
    in
488 489 490
    let bdd_not = (Bdd.dnot bdd) in
    let (sol_nb, sol_nb_not) =
      try
491 492
	let (nt, ne) = Env_state.sol_number bdd 
	and (not_nt, not_ne) = Env_state.sol_number bdd_not in
493 494
	  (* solutions numbers in the table are absolute *)
	  ((add_sol_nb nt ne), (add_sol_nb not_nt not_ne))
495
      with Not_found ->
496 497
	let (nt, not_nt) = compute_absolute_sol_nb (Bdd.dthen bdd) comb in
	let (ne, not_ne) = compute_absolute_sol_nb (Bdd.delse bdd) comb in
498 499
	  Env_state.set_sol_number bdd (nt, ne) ;
	  Env_state.set_sol_number bdd_not (not_nt, not_ne) ;
500 501 502 503 504 505
	  ((add_sol_nb nt ne), (add_sol_nb not_nt not_ne))
    in
      (sol_nb, sol_nb_not)
and 
  (compute_absolute_sol_nb: Bdd.t -> Bdd.t -> sol_nb * sol_nb) =
  fun sub_bdd comb -> 
506
    (* Returns the absolute number of solutions of [sub_bdd] (and its
507
       negation) w.r.t. [comb], where [comb] is the comb of the
508 509 510 511 512 513 514 515 516 517
       father of [sub_bdd].

       The [comb] is used to know which output boolean variables are
       unconstraint along a path in the bdd. Indeed, the comb is made
       of all the boolean output var indexes plus the num contraints
       indexes that appears in the bdd; hence, if the topvar of the
       bdd is different from the topvar of the comb, it means that
       the topvar of the comb is unsconstraint and we need to
       multiply the number of solution of the branch by 2.
    *)
518
    if Bdd.is_cst sub_bdd 
519
    then
520
      let sol_nb = 
521
	if Bdd.is_true comb
522
	then one_sol
523
	else (two_power_of (List.length (Bdd.list_of_support (Bdd.dthen comb)))) 
524
      in
525 526 527 528 529
	if Bdd.is_true sub_bdd
	then (sol_nb, zero_sol) 
	else (zero_sol, sol_nb)
    else 
      let topvar = Bdd.topvar sub_bdd in
530 531 532 533 534 535 536 537 538 539 540 541
      let rec
	(count_missing_vars: Bdd.t -> var -> int -> Bdd.t * int) =
	fun comb var cpt -> 
	  (* Returns [cpt] + the number of variables occurring in [comb]
	     before reaching [var] ([var] excluded). Also returns the comb
	     whch topvar is [var]. *)
	  let _ = assert (not (Bdd.is_cst comb)) in
	  let combvar = Bdd.topvar comb in
	    if var = combvar
	    then (comb, cpt)
	    else count_missing_vars (Bdd.dthen comb) var (cpt+1)
      in
542
      let (sub_comb, missing_vars_nb) = 
543
	count_missing_vars (Bdd.dthen comb) topvar 0
544 545 546 547 548
      in
      let (n0, not_n0) = build_sol_nb_table sub_bdd sub_comb in
      let factor = (two_power_of missing_vars_nb) in
	(mult_sol_nb n0 factor, mult_sol_nb not_n0 factor)
	
549

550

551 552 553
(****************************************************************************)
(****************************************************************************)

554
(* Raised during the toss if no solution is found in the branch  *)
555
exception No_numeric_solution
556

557 558 559 560 561 562 563 564 565 566
let (toss_up_one_var: var_name -> subst) =
  fun var -> 
   (* *)
    let ran = Random.float 1. in
      if (ran < 0.5) 
      then (var, B(true)) 
      else (var, B(false))


let (toss_up_one_var_index: var -> subst option) =
567
  fun var -> 
568 569 570 571 572 573 574 575 576
    (* if [var] is a index that corresponds to a boolean variable,
       this fonction performs a toss and returns a substitution for
       the corresponding boolean variable. It returns [None]
       otherwise.

       Indeed, if it happens that a numerical constraint does not
       appear along a path, we simply ignore it and hence it will not
       be added to the store.
    *)
577 578 579
    let cstr = Env_state.index_to_linear_constraint var in
      match cstr with 
          Bv(vn) -> Some(toss_up_one_var vn)
580
	| _  -> None
581

582

583

584
let (is_a_numeric_constraint : Constraint.t -> bool) =
585 586
  fun cstr -> 
    match cstr with
587 588 589
	Bv(_) -> false
      | GZ(_)   -> true 
      | GeqZ(_) -> true
590
      | EqZ(_)  -> true
591 592


593 594
let rec (draw_in_bdd: subst list * store -> Bdd.t -> Bdd.t -> 
	   subst list * store) = 
595 596 597 598 599 600
  fun (sl, store) bdd comb ->
    (** Returns [sl] appended to a draw of all the boolean variables
      bigger than the topvar of [bdd] according to the ordering
      induced by the comb [comb]. Also returns the (non empty) store
      obtained by adding to [store] all the numeric constraints that
      were encountered during this draw.
601

602 603 604
      Raises the [No_numeric_solution] exception whenever no valid
      path in [bdd] leads to a satisfiable set of numeric
      constraints.  
605
    *)
606

607 608
    if 
      Bdd.is_true bdd
609 610 611
    then
      (* Toss the remaining bool vars. *)
      ( (List.append sl
612
	   (Util.list_map_option toss_up_one_var_index (Bdd.list_of_support comb))),
613 614 615 616 617
	store )
    else
      let _ = assert (not (Bdd.is_false bdd)) in 
      let _ = assert (Env_state.sol_number_exists bdd) in
      let bddvar  = Bdd.topvar bdd in
618 619
      let cstr = (Env_state.index_to_linear_constraint bddvar) in 
	match cstr with
620 621 622 623 624
	    Bv(var) -> 
	      draw_in_bdd_bool var  (sl, store) bdd comb
	  | EqZ(e)  -> draw_in_bdd_eq   e    (sl, store) bdd comb
	  | _       -> 
	      draw_in_bdd_ineq cstr (sl, store) bdd comb
625 626 627 628

and (draw_in_bdd_bool: string -> subst list * store -> Bdd.t -> Bdd.t -> 
       subst list * store) = 
  fun var (sl, store) bdd comb ->
629
    let bddvar = Bdd.topvar bdd in
630 631 632 633 634 635 636
    let topvar_comb  = Bdd.topvar comb in
      
    if
      bddvar <> topvar_comb 
    then
      (* that condition means that topvar_comb is an unconstraint
	 boolean var; hence we toss it up. *)
637 638 639 640 641
      let new_sl = 
	match toss_up_one_var_index topvar_comb with
	    Some(s) -> s::sl
	  | None -> sl
      in
642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696
	draw_in_bdd (new_sl, store) bdd (Bdd.dthen comb) 
    else 
      (* bddvar = combvar *) 
      let (n, m) = Env_state.sol_number bdd in
      let _ =
	if ((eq_sol_nb n zero_sol) && (eq_sol_nb m zero_sol))
	then raise No_numeric_solution ;
      in
      let (
	bdd1, bool1, sol_nb1,
	bdd2, bool2, sol_nb2
      ) =
	let ran = Random.float 1. 
	and sol_nb_ratio = 
	  ((float_of_sol_nb n) /. (float_of_sol_nb (add_sol_nb n m))) 
	in
	  if 
	    ran < sol_nb_ratio
	      (* 
		 Depending on the result of a toss (based on the number
		 of solution in each branch), we try the [then] or the
		 [else] branch first.  
	      *)
	  then
	    ((Bdd.dthen bdd), true, n,
	     (Bdd.delse bdd), false, m )
	  else 
	    ((Bdd.delse bdd), false, m,
	     (Bdd.dthen bdd), true, n )
      in
      let (sl1, sl2, new_comb) = (
	((var, B(bool1))::sl), 
	((var, B(bool2))::sl),
	(if Bdd.is_true comb then comb else Bdd.dthen comb) 
      )
	    
      in
	(* 
	   A solution will be found in this branch iff there exists
	   at least one path in the bdd that leads to a satisfiable
	   set of numeric constraints. If it is not the case,
	   [res_opt] is bound to [None]. 
	*)
	try 
	  draw_in_bdd (sl1, store) bdd1 new_comb
	with No_numeric_solution -> 
	  if not (eq_sol_nb sol_nb2 zero_sol)
	  then draw_in_bdd (sl2, store) bdd2 new_comb
	    (* 
	       The second branch is now tried because no path in
	       the first bdd leaded to a satisfiable set of
	       numeric constraints. 
	    *) 
	  else raise No_numeric_solution
  
697
and (draw_in_bdd_eq: Ne.t -> subst list * store -> Bdd.t -> Bdd.t -> 
698
       subst list * store) = 
699
  fun ne (sl, store) bdd comb ->
700 701 702 703 704 705 706
    
    let (n, m) = Env_state.sol_number bdd in
    let _ =
      if ((eq_sol_nb n zero_sol) && (eq_sol_nb m zero_sol))
      then raise No_numeric_solution ;
    in
    let (store_plus_cstr, store_plus_not_cstr, store_plus_not_cstr2) =  
707
      split_store_eq store ne  
708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725
    in
    let (
      store1, bdd1, sol_nb1, 
      store2, bdd2, sol_nb2,
      store3, bdd3, sol_nb3
    ) =
      let ran0 = Random.float 1. 
      and ran  = Random.float 1. 
      and sol_nb_ratio = 
	((float_of_sol_nb n) /. (float_of_sol_nb (add_sol_nb n m))) 
      in
	if 
	  ran0 < 0.5
	    (* 
	       When taking the negation of an equality, we can
	       either try > or <. Here, We toss which one we
	       will try first.  
	    *)
726
	then
727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813
	  if 
	    ran < sol_nb_ratio
	      (* 
		 Depending on the result of a toss (based on the number
		 of solution in each branch), we try the [then] or the
		 [else] branch first.  
	      *)
	  then
	    (store_plus_cstr,      (Bdd.dthen bdd), n,
	     store_plus_not_cstr,  (Bdd.delse bdd), m,
	     store_plus_not_cstr2, (Bdd.delse bdd), m)
	  else 
	    (store_plus_not_cstr, (Bdd.delse bdd), m,
	     store_plus_cstr, (Bdd.dthen bdd), n,
	     store_plus_not_cstr2, (Bdd.delse bdd), m )
	else
	  if
	    ran < sol_nb_ratio
	      (* Ditto *)
	  then
	    (store_plus_cstr, (Bdd.dthen bdd), n,
	     store_plus_not_cstr2, (Bdd.delse bdd), m,
	     store_plus_not_cstr, (Bdd.delse bdd), m)
	  else 
	    (store_plus_not_cstr2, (Bdd.delse bdd), m,
	     store_plus_cstr, (Bdd.dthen bdd), n,
	     store_plus_not_cstr, (Bdd.delse bdd), m)
    in
    let call_choice_point3 _ =
      (* 
	 The third possibility is tried if no path is found in the 2
	 previous ones. Note that there only is a third one if the
	 current constraint is an equality.  
      *)
      if 
	not (eq_sol_nb sol_nb3 zero_sol)
      then
	if is_store_satisfiable store3
	then draw_in_bdd (sl, store3) bdd3 comb
	else raise No_numeric_solution
      else
	raise No_numeric_solution
    in
    let call_choice_point2 _ =
      (* 
	 The second branch is tried if no path in the first bdd
	 leaded to a satisfiable set of numeric constraints.  
      *)
      if 
	not (eq_sol_nb sol_nb2 zero_sol)
      then
	if is_store_satisfiable store2
	then draw_in_bdd (sl, store2) bdd2 comb
	else call_choice_point3 ()
      else
	call_choice_point3 ()
    in
      (* 
	 A solution will be found in this branch iff there exists
	 at least one path in the bdd that leads to a satisfiable
	 set of numeric constraints. 
      *)
      if 
	is_store_satisfiable store1
      then 
	try draw_in_bdd (sl, store1) bdd1 comb
	with No_numeric_solution -> call_choice_point2 ()
      else
	call_choice_point2 ()
    


and (draw_in_bdd_ineq: Constraint.t -> subst list * store -> Bdd.t -> Bdd.t -> 
	   subst list * store) = 
  fun cstr (sl, store) bdd comb ->
    let (n, m) = Env_state.sol_number bdd in
    let _ =
      if ((eq_sol_nb n zero_sol) && (eq_sol_nb m zero_sol))
      then raise No_numeric_solution ;
    in
    let (store_plus_cstr, store_plus_not_cstr) = split_store store cstr in
    let (store1, bdd1, sol_nb1,  store2, bdd2, sol_nb2) =
      let ran = Random.float 1. in
	if 
	  ran < ((float_of_sol_nb n) /. (float_of_sol_nb (add_sol_nb n m)))
	    (* 
	       Depending on the result of a toss (based on the number
814
	       of solution in each branch), we try the [then] or the
815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853
	       [else] branch first.  
	    *)
	then
	  (store_plus_cstr,      (Bdd.dthen bdd), n,
	   store_plus_not_cstr,  (Bdd.delse bdd), m)
	else 
	  (store_plus_not_cstr, (Bdd.delse bdd), m,
	   store_plus_cstr, (Bdd.dthen bdd), n )
    in
    let call_choice_point _ =
      (* 
	 The second branch is tried if no path in the first bdd leaded
	 to a satisfiable set of numeric constraints.  
      *)
      if 
	not (eq_sol_nb sol_nb2 zero_sol)
      then
	if 
	  is_store_satisfiable store2
	then 
	  draw_in_bdd (sl, store2) bdd2 comb
	else
	  raise No_numeric_solution
      else
	raise No_numeric_solution
    in
      (* A solution will be found in this branch iff there exists
	 at least one path in the bdd that leads to a satisfiable
	 set of numeric constraints. If it is not the case,
	 [res_opt] is bound to [None]. *)
      if 
	is_store_satisfiable store1
      then 
	try draw_in_bdd (sl, store1) bdd1 comb 
	with No_numeric_solution -> call_choice_point ()
      else
	call_choice_point ()
    	
		
854

855 856 857 858

(* exported *)
let (draw : vn list -> vnt list -> Bdd.t -> Bdd.t -> subst list * subst list) =
  fun bool_vars_to_gen num_vnt_to_gen comb bdd ->
859 860
    (** Draw the output and local vars to be generated by the environnent. *)
    let (bool_subst_l, store) = 
861
      draw_in_bdd ([], (new_store num_vnt_to_gen)) bdd comb
862
    in
863 864 865 866 867 868
    let num_subst_l = 
      match Env_state.draw_mode () with
	  Env_state.Verteces -> draw_verteces store
	| Env_state.Edges    -> draw_edges store
	| Env_state.Inside   -> draw_inside store 
    in
869
    let subst_l = append bool_subst_l num_subst_l in
870
    let (out_vars, _) = List.split (Env_state.output_var_names ()) 
871 872 873
    in
      assert ( 
	(*  Checks that we generated all variables. *)
874 875
	let (gen_vars, _) = List.split subst_l in
	let (num_vars_to_gen, _) = List.split num_vnt_to_gen in
876
	let vars_to_gen = append bool_vars_to_gen num_vars_to_gen in
877 878 879 880 881 882 883 884 885 886
          if (sort (compare) gen_vars) = (sort (compare) vars_to_gen) 
	  then true
	  else
	    (
	      output_string stderr " \ngen vars :";
              List.iter (fun vn -> output_string stderr (vn ^ " ")) gen_vars;
	      output_string stderr " \nvar to gen:";
	      List.iter (fun vn -> output_string stderr (vn ^ " ")) vars_to_gen;
	      false
	    )
887 888 889 890 891
      );
      (* Splits output and local vars. *)
      List.partition 
	(fun (vn, _) -> List.mem vn out_vars) 
	subst_l
892

893

894 895 896 897 898

(****************************************************************************)
(****************************************************************************)


899
(* Exported *)
900 901 902
let (solve_formula: env_in -> int -> formula -> formula -> vnt list ->
       (subst list * subst list) list option) =
  fun input p f bool_vars_to_gen_f num_vars_to_gen ->
903
    let bdd = 
904
      (* The bdd of f has necessarily been computed (by is_satisfiable) *)
905 906
      try Env_state.bdd f
      with Not_found -> Env_state.bdd_global f
907
    in
908 909 910 911 912 913 914 915 916 917 918 919 920
    let (comb0, _) = formula_to_bdd input bool_vars_to_gen_f in
    let comb = 
      (* All boolean vars should appear in the comb so that when we
	 find that such a var is missing along a bdd path, we can
	 perform a (fair) toss for it. On the contrary, if a
	 numerical contraint disappear from a bdd (eg, consider [(f
	 && false) || true]), it is not important; fairly tossing a
	 (boolean) value for a num constaint [nc] and performing a
	 fair toss in the resulting domain is equivalent to directly
	 perform the toss in the (unconstraint wrt [nc]) initial
	 domain.  
      *)
      Bdd.dand (Bdd.support bdd) comb0 
921 922
    in	
    let bool_vars_to_gen = Formula.support bool_vars_to_gen_f in
923
    let _ =
924
      if not (Env_state.sol_number_exists bdd)
925
      then
926 927 928 929 930
	let rec skip_unconstraint_bool_var_at_top comb v =
	  (* [build_sol_nb_table] supposes that the bdd and its comb 
	     have the same top var. 
	  *)
	  if Bdd.is_true comb then comb
931 932
	  else 
	    let topvar = (Bdd.topvar comb) in
933 934
	      if v = topvar then comb 
	      else skip_unconstraint_bool_var_at_top (Bdd.dthen comb) v
935
	in
936
	let comb2 = skip_unconstraint_bool_var_at_top comb (Bdd.topvar bdd) in 
937 938
	let _ = build_sol_nb_table bdd comb2 in
	  ()
939
    in
940
      try 	
941
	Some(Util.unfold (draw bool_vars_to_gen num_vars_to_gen comb) bdd p)
942
      with No_numeric_solution -> 
943
	Env_state.set_sol_number bdd (zero_sol, zero_sol);
944
	None
945