solver.ml 24 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 Util
14
open Hashtbl
15
open Gne
16
open Rnumsolver
17

18 19
(****************************************************************************)
	  
20 21 22
let (lookup: env_in -> subst list -> var_name -> var_value option) = 
  fun input pre vn ->  
    try Some(Hashtbl.find input vn)
23
    with Not_found -> 
24 25 26 27 28 29 30 31 32
      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 ->
33 34
    (** Returns the bdd of [f] where input and pre variables
      have been repaced by their values.
35

36 37 38
      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]);
39
      otherwise, it is stored in a table that is cleared at each new
40
      step ([env_state.bdd_tbl]).  
41
    *)
42
    try (Env_state.bdd f, true)
43
    with Not_found -> 
44
      try (Env_state.bdd_global f, false)
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
      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)
63 64 65 66 67 68

	    | 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)
69 70 71 72 73 74 75 76 77 78

	    | 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 )
		  
79 80
	    | True ->  (Bdd.dtrue  (Env_state.bdd_manager ()), false)
	    | False -> (Bdd.dfalse (Env_state.bdd_manager ()), false)
81
	    | Bvar(vn) ->    
82
		( match (lookup input (Env_state.pre ()) vn) with 
83
		      Some(B(bool)) -> 
84
			if bool
85
			then (Bdd.dtrue  (Env_state.bdd_manager ()), true)
86
			else (Bdd.dfalse (Env_state.bdd_manager ()), true)
87 88 89 90
		    | Some(_) -> 
			print_string (vn ^ " is not a boolean!\n");
			assert false
		    | None ->
91
			if List.mem vn (Env_state.pre_var_names ())
92 93 94 95 96 97
			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 ")
98 99 100
			else (Bdd.ithvar (Env_state.bdd_manager ())
				(Env_state.atomic_formula_to_index (Bv(vn)) false), 
				false)
101 102 103
		)
		
	    | Eq(e1, e2) -> 
104 105
		let gne = expr_to_gne (Diff(e1, e2)) input in 
		  (gne_to_bdd gne EqZero)
106 107
		  
	    | Neq(e1, e2) -> 
108 109
		let gne = expr_to_gne (Diff(e1, e2)) input in
		  (gne_to_bdd gne NeqZero)
110 111
		  
	    | SupEq(e1, e2) ->
112 113
		let gne = expr_to_gne (Diff(e1, e2)) input in
		  (gne_to_bdd gne SupEqZero)
114 115
		  
	    | Sup(e1, e2)   ->
116 117
		let gne = expr_to_gne (Diff(e1, e2)) input in
		  (gne_to_bdd gne SupZero)
118 119
		  
	    | InfEq(e1, e2) ->  
120 121
		let gne = expr_to_gne (Diff(e2, e1)) input in
		  (gne_to_bdd gne SupEqZero)
122 123
		  
	    | Inf(e1, e2)   ->  
124 125
		let gne =  expr_to_gne (Diff(e2, e1)) input in
		  (gne_to_bdd gne SupZero)
126 127 128 129
		  
	in
	  if dep
	  then 
130
	    ( Env_state.set_bdd f bdd;	
131 132
	      match f with 
		  Not(nf) -> () (* Already in the tbl thanks to the rec call *)
133
		| _  -> Env_state.set_bdd (Not(f)) (Bdd.dnot bdd) 
134
	    )
135 136
	  else 
	    (* [f] does not depend on pre nor input vars *)
137
	    ( Env_state.set_bdd_global f bdd ;	
138 139 140
	      match f with 
		  Not(nf) -> () (* Already in the table thanks to the rec call *)
		| _  -> 
141
		    Env_state.set_bdd_global (Not(f)) (Bdd.dnot bdd)
142 143 144 145
	    );

	  (bdd, dep)
and
146
  (expr_to_gne: expr -> env_in -> Gne.t) =
147 148 149 150
  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. *)
151
    let gne =
152 153
      match e with  
	  Sum(e1, e2) ->
154 155
	    let gne1 = (expr_to_gne e1 input)
	    and gne2 = (expr_to_gne e2 input) 
156
	    in
157
	      Gne.add  gne1 gne2
158 159

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

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

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

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

	| Ivar(str) ->
184
	    ( match (lookup input (Env_state.pre ()) str) with 
185
		  Some(N(I(i))) ->
186
		    (GneMap.add 
187 188 189
		       (NeMap.add "" (I(i)) NeMap.empty) 
		       ((Bdd.dtrue (Env_state.bdd_manager ())), true)
		       GneMap.empty
190 191
		    )
		| None ->
192
		    (GneMap.add 
193 194 195
		       (NeMap.add str (I(1)) NeMap.empty)
		       ((Bdd.dtrue (Env_state.bdd_manager ())), false)
		       GneMap.empty
196
		    )
197 198 199 200 201 202 203 204 205 206 207
		| 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) ->
208
	    ( match (lookup input (Env_state.pre ()) str) with 
209
		  Some(N(F(f))) ->
210 211 212 213
		    ( GneMap.add 
			(NeMap.add "" (F(f)) NeMap.empty) 
			((Bdd.dtrue (Env_state.bdd_manager ())), true)
			GneMap.empty
214 215
		    )
		| None ->
216 217 218 219
		    ( GneMap.add 
			(NeMap.add str (F(1.)) NeMap.empty) 
			((Bdd.dtrue (Env_state.bdd_manager ())), false)
			GneMap.empty
220 221 222 223 224 225 226 227 228 229 230 231
		    )
		| 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) ->  
232 233 234 235
	    (GneMap.add 
	       (NeMap.add "" (I(i)) NeMap.empty) 
	       ((Bdd.dtrue (Env_state.bdd_manager ())), false)
	       GneMap.empty
236 237 238
	    )

	| Fval(f) -> 
239 240 241 242
	    ( GneMap.add 
		(NeMap.add "" (F(f)) NeMap.empty) 
		((Bdd.dtrue (Env_state.bdd_manager ())), false)
		GneMap.empty
243 244 245
	    )

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

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

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

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

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

		   else 
		     Bdd.ithvar 
407
		       (Env_state.bdd_manager ()) 
408
		       (Env_state.atomic_formula_to_index (GZ(nexpr)) dep) 
409 410 411 412 413 414 415 416
		 in
		 let bdd2 = 
		   if is_n_expr_a_constant nexpr
		   then 
		     let cst = NeMap.find "" nexpr in
		       match cst with
			   I(i) -> 
			     if i < 0 
417 418
			     then (Bdd.dtrue (Env_state.bdd_manager ()))
			     else (Bdd.dfalse (Env_state.bdd_manager ()))
419 420
			 | F(f) -> 
			     if f < 0. 
421 422
			     then (Bdd.dtrue (Env_state.bdd_manager ()))
			     else (Bdd.dfalse (Env_state.bdd_manager ()))
423 424 425

		   else 
		     Bdd.ithvar 
426
		       (Env_state.bdd_manager ()) 
427
		       (Env_state.atomic_formula_to_index (SZ(nexpr)) dep) 
428
		 in
429 430
		 let new_dep = dep || dep_acc 
		 and bdd = Bdd.dor bdd1 bdd2 in 
431 432
		   (* We transform [e1 <> e2] into [e1 < e2 or e1 > e2] as the 
		      numeric solver can not handle disequalities *)
433
		   (Bdd.dor (Bdd.dand c bdd) acc, new_dep)
434 435
	      )
	      gne 
436
	      ((Bdd.dfalse (Env_state.bdd_manager ())), false)
437
	  )
438

439 440 441 442 443
(****************************************************************************)
(****************************************************************************)


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


(****************************************************************************)
458 459 460 461 462 463 464 465 466 467 468 469 470 471 472
(****************************************************************************)

type var = int

(** 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.
*)



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
473 474 475 476 477 478
      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].  
479
    *)
480 481 482
    let _ = assert (not (Bdd.is_cst bdd) 
		    && (Bdd.topvar comb) = (Bdd.topvar bdd)) 
    in
483 484 485
    let bdd_not = (Bdd.dnot bdd) in
    let (sol_nb, sol_nb_not) =
      try
486 487
	let (nt, ne) = Env_state.sol_number bdd 
	and (not_nt, not_ne) = Env_state.sol_number bdd_not in
488 489
	  (* solutions numbers in the table are absolute *)
	  ((add_sol_nb nt ne), (add_sol_nb not_nt not_ne))
490
      with Not_found ->
491 492
	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
493 494
	  Env_state.set_sol_number bdd (nt, ne) ;
	  Env_state.set_sol_number bdd_not (not_nt, not_ne) ;
495 496 497 498 499 500
	  ((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 -> 
501
    (* Returns the absolute number of solutions of [sub_bdd] (and its
502
       negation) w.r.t. [comb], where [comb] is the comb of the
503 504 505 506 507 508 509 510 511 512
       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.
    *)
513
    if Bdd.is_cst sub_bdd 
514
    then
515
      let sol_nb = 
516
	if Bdd.is_true comb
517
	then one_sol
518
	else (two_power_of (List.length (Bdd.list_of_support (Bdd.dthen comb)))) 
519
      in
520 521 522 523 524
	if Bdd.is_true sub_bdd
	then (sol_nb, zero_sol) 
	else (zero_sol, sol_nb)
    else 
      let topvar = Bdd.topvar sub_bdd in
525 526 527 528 529 530 531 532 533 534 535 536
      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
537
      let (sub_comb, missing_vars_nb) = 
538
	count_missing_vars (Bdd.dthen comb) topvar 0
539 540 541 542 543
      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)
	
544 545 546 547

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

548

549
let (toss_up_one_var: var -> subst option) =
550
  fun var -> 
551 552 553 554 555 556 557 558 559
    (* 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.
    *)
560 561
    let af = Env_state.index_to_atomic_formula var in
      match af with 
562 563 564 565 566 567 568
          Bv(vn) -> 
	    let ran = Random.float 1. in
	      if (ran < 0.5) 
	      then Some(vn, Formula.B(true)) 
	      else Some(vn, Formula.B(false))
	| _  -> None
     
569

570 571 572 573 574 575 576 577
let (is_a_numeric_constraint : atomic_formula -> bool) =
  fun af -> 
    match af with
	Bv(_) -> false
      | GZ(_)   -> true 
      | GeqZ(_) -> true
      | SZ(_)   -> true
      | SeqZ(_) -> true
578 579 580 581 582


(* exported *)
exception No_numeric_solution

583 584
let rec (draw_in_bdd: subst list * store -> Bdd.t -> Bdd.t -> 
	   subst list * store) = 
585 586 587 588 589 590
  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.
591

592 593 594
      Raises the [No_numeric_solution] exception whenever no valid
      path in [bdd] leads to a satisfiable set of numeric
      constraints.  
595
    *)
596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635

    if Bdd.is_true bdd
    then
      (* Toss the remaining bool vars. *)
      ( (List.append sl
	   (Util.list_map_option toss_up_one_var (Bdd.list_of_support comb))),
	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
      let af = (Env_state.index_to_atomic_formula bddvar) in 
      let top_var_is_numeric = is_a_numeric_constraint af in
	if
	  bddvar <> (Bdd.topvar comb) &&
	  not top_var_is_numeric
	then
	  let new_sl =
	    match toss_up_one_var (Bdd.topvar comb) with
		Some(s) -> s::sl
	      | None -> sl
	  in
	    draw_in_bdd (new_sl, store) bdd (Bdd.dthen comb)
	else 
	  (* bddvar = combvar xor top_var_is_numeric *) 
	  (* nb: I handle those two cases alltogether to avoid code
	     duplication (i.e., retrieving sol numbers, performing the
	     toss, the recursive call, handling the base case where a
	     dtrue bdd is reached, etc).  It makes the code a little
	     bit more obscur, but ...  *)
	  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_af, store_plus_not_af) = 
	    (* A first trick to avoid code dup (cf nb above) *)
	    if top_var_is_numeric
	    then 
	      split_store store af
636
	    else 
637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662
	      (store, store)
	  in
	  let (swap, store1, bdd1, bool1, sol_nb1, store2, bdd2, bool2, sol_nb2) =
	    (* Depending on the result of a toss (based on the number
	       of solution in each branch), we try the [else] or the
	       [then] branch first. [swap] indicates whether or not the
	       [else] part is put before the [then] one. *)
	    let ran = Random.float 1. in
	      if ran < ((float_of_sol_nb n) /. (float_of_sol_nb (add_sol_nb n m)))
	      then
		(false, store_plus_af, (Bdd.dthen bdd), true, n,
		 store_plus_not_af, (Bdd.delse bdd), false, m )
	      else 
		(true, store_plus_not_af, (Bdd.delse bdd), false, m,
		 store_plus_af, (Bdd.dthen bdd), true, n )
	  in
	  let (sl1, sl2, new_comb) = (
	    (* A second trick to avoid code dup (cf nb above) *)
	    match af with 
		Bv(vn) -> 
		  (((vn, Formula.B(bool1))::sl), 
		   ((vn, Formula.B(bool2))::sl), 
		   (if Bdd.is_true comb then comb else Bdd.dthen comb) )
	      | _ -> 
		  (* top_var_is_numeric *)
		  (sl, sl, comb)
663
	  )
664 665 666 667 668 669 670 671 672 673 674 675 676 677
	  in
	  let res_opt =
	    (* 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 not (is_empty store1)
	    then 
	      try 
		let tail_draw1 = draw_in_bdd (sl1, store1) bdd1 new_comb in
		  Some(tail_draw1)
	      with No_numeric_solution -> 
		None
	    else
678
	      None
679 680 681 682 683 684 685 686 687 688 689
	  in
	    match res_opt with 
		Some(res) -> res
	      | None -> 
		  (* The second branch is now tried because 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 not (is_empty store2)
		    then draw_in_bdd (sl2, store2) bdd2 new_comb 
690
		    else
691
		      raise No_numeric_solution
692
		  else
693
		    raise No_numeric_solution
694

695

696 697
let (draw : formula -> vnt list -> Bdd.t -> Bdd.t -> subst list * subst list) =
  fun bool_vars_to_gen_f num_vnt_to_gen comb bdd ->
698 699
    (** Draw the output and local vars to be generated by the environnent. *)
    let (bool_subst_l, store) = 
700
      draw_in_bdd ([], (new_store num_vnt_to_gen)) bdd comb 
701
    in
702 703 704 705 706 707
    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
708
    let subst_l = append bool_subst_l num_subst_l in
709
    let (out_vars, _) = List.split (Env_state.output_var_names ()) 
710 711 712
    in
      assert ( 
	(*  Checks that we generated all variables. *)
713 714
	let (gen_vars, _) = List.split subst_l in
	let (num_vars_to_gen, _) = List.split num_vnt_to_gen in
715
	let bool_vars_to_gen = Formula.support bool_vars_to_gen_f in
716
	let vars_to_gen = append bool_vars_to_gen num_vars_to_gen in
717 718 719 720 721 722 723 724 725 726
          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
	    )
727 728 729 730 731
      );
      (* Splits output and local vars. *)
      List.partition 
	(fun (vn, _) -> List.mem vn out_vars) 
	subst_l
732

733

734
  
735
(* Exported *)
736 737 738
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 ->
739
    let bdd = 
740
      (* The bdd of f has necessarily been computed (by is_satisfiable) *)
741 742
      try Env_state.bdd f
      with Not_found -> Env_state.bdd_global f
743
    in
744 745 746 747 748 749 750 751 752 753 754 755 756 757
    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 
    in
758
    let _ =
759
      if not (Env_state.sol_number_exists bdd)
760
      then
761 762 763 764 765
	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
766 767
	  else 
	    let topvar = (Bdd.topvar comb) in
768 769
	      if v = topvar then comb 
	      else skip_unconstraint_bool_var_at_top (Bdd.dthen comb) v
770
	in
771
	let comb2 = skip_unconstraint_bool_var_at_top comb (Bdd.topvar bdd) in 
772 773
	let _ = build_sol_nb_table bdd comb2 in
	  ()
774
    in
775
      try 	
776
	Some(Util.unfold (draw bool_vars_to_gen_f num_vars_to_gen comb) bdd p)
777
      with No_numeric_solution -> 
778
	Env_state.set_sol_number bdd (zero_sol, zero_sol);
779
	None
780