solver.ml 11.9 KB
 Erwan Jahier committed Mar 17, 2010 1 2 3 4 5 6 7 8 9 10 ``````(*----------------------------------------------------------------------- ** Copyright (C) 2001 - Verimag. ** This file may only be copied under the terms of the GNU Library General ** Public License **----------------------------------------------------------------------- ** ** File: solver.ml ** Main author: jahier@imag.fr *) `````` Erwan Jahier committed Mar 17, 2010 11 ``````open List `````` Erwan Jahier committed Mar 17, 2010 12 ``````open Formula `````` Erwan Jahier committed Mar 17, 2010 13 ``````open Env_state `````` Erwan Jahier committed Mar 17, 2010 14 ``````open Util `````` Erwan Jahier committed Mar 17, 2010 15 `````` `````` Erwan Jahier committed Mar 17, 2010 16 17 18 ``````(****************************************************************************) let (formula_list_to_conj: formula list -> formula) = `````` Erwan Jahier committed Mar 17, 2010 19 `````` fun fl -> `````` Erwan Jahier committed Mar 17, 2010 20 21 22 23 24 25 26 27 28 `````` (** Transform a (non-empty) list of formula to the conjunction made of those formula. *) match fl with [] -> assert false | f::[] -> f | f1::f2::tail -> List.fold_left (fun x y -> And(x, y)) (And(f1, f2)) tail `````` Erwan Jahier committed Mar 17, 2010 29 `````` `````` Erwan Jahier committed Mar 17, 2010 30 31 32 33 34 35 ``````let rec (formula_to_bdd : formula -> Bdd.t) = fun f -> (** Transform the formula [f] into a bdd. Also tabulates the result in the [bdd_tbl] field of [env_state] because the translation is very expensive. *) `````` Erwan Jahier committed Mar 17, 2010 36 37 `````` try Hashtbl.find env_state.bdd_tbl f with Not_found -> `````` Erwan Jahier committed Mar 17, 2010 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 `````` let bdd = match f with Not(f) -> Bdd.dnot (formula_to_bdd f) | Or(f1, f2) -> Bdd.dor (formula_to_bdd f1) (formula_to_bdd f2) | And(f1, f2) -> Bdd.dand (formula_to_bdd f1) (formula_to_bdd f2) | True -> Bdd.dtrue () | False -> Bdd.dfalse () | Bvar(vn) -> Bdd.var (Env_state.vn_to_index vn) | Eq(e1, e2) -> assert false (* XXX FIX US !!! *) | Ge(e1, e2) -> assert false (* XXX FIX US !!! *) | G(e1, e2) -> assert false (* XXX FIX US !!! *) in let _ = match f with Not(nf) -> Hashtbl.add env_state.bdd_tbl nf (Bdd.dnot bdd) | _ -> Hashtbl.add env_state.bdd_tbl (Not(f)) (Bdd.dnot bdd) in `````` Erwan Jahier committed Mar 17, 2010 56 57 ``````(* print_string ("\$\$\$ building the bdd of " ^ (formula_to_string f) ^ "\n") ; *) (* flush stdout ; *) `````` Erwan Jahier committed Mar 17, 2010 58 59 `````` Hashtbl.add env_state.bdd_tbl f bdd; bdd `````` Erwan Jahier committed Mar 17, 2010 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 `````` (****************************************************************************) 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 (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 which 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) 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 the solution number table [env_state.snt] for [bdd] and its negation, and recursively for all its sub-bdds. *) let bdd_not = (Bdd.dnot bdd) in let (sol_nb, sol_nb_not) = try let (nt, ne) = Hashtbl.find env_state.snt bdd and (not_nt, not_ne) = Hashtbl.find env_state.snt bdd_not in (* solutions numbers in the table are absolute *) ((add_sol_nb nt ne), (add_sol_nb not_nt not_ne)) with Not_found -> let _ = assert (not (Bdd.is_cst bdd)) in let _ = assert ((Bdd.topvar bdd) = (Bdd.topvar comb)) in 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 let _ = assert ( (* XXX Debugging stuff *) let card_sol_t = (Bdd.cardinal (List.length (Bdd.int_of_support (Bdd.dthen comb))) (Bdd.dthen bdd)) in let card_sol_e = (Bdd.cardinal (List.length (Bdd.int_of_support (Bdd.dthen comb))) (Bdd.delse bdd)) in let nt_float = float_of_sol_nb nt in let ne_float = float_of_sol_nb ne in let card_sol_t_not = (Bdd.cardinal (List.length (Bdd.int_of_support (Bdd.dthen comb))) (Bdd.dthen bdd_not)) in let card_sol_e_not = (Bdd.cardinal (List.length (Bdd.int_of_support (Bdd.dthen comb))) (Bdd.delse bdd_not)) in let nt_float_not = float_of_sol_nb not_nt in let ne_float_not = float_of_sol_nb not_ne in if (card_sol_t <> nt_float) || (card_sol_e <> ne_float) || (card_sol_t_not <> nt_float_not) || (card_sol_e_not <> ne_float_not) then begin print_string "Lurette computes: "; print_string (string_of_sol_nb (add_sol_nb nt ne)); print_string " = ";print_string (string_of_sol_nb nt); print_string " + ";print_string (string_of_sol_nb ne); print_string "\nBdd.cardinal computes: "; print_float (Bdd.cardinal (List.length (Bdd.int_of_support comb)) bdd); print_string " = "; print_float card_sol_t; print_string " + "; print_float card_sol_e ; print_string "\n\n"; flush stdout ; `````` Erwan Jahier committed Mar 17, 2010 128 `````` Print.bdd_with_dot bdd (Env_state.index_to_vn) "bdd" ; `````` Erwan Jahier committed Mar 17, 2010 129 130 131 132 133 134 135 136 137 `````` print_string "(Not) Lurette computes: "; print_string (string_of_sol_nb (add_sol_nb not_nt not_ne)); print_string " = ";print_string (string_of_sol_nb not_nt); print_string " + ";print_string (string_of_sol_nb not_ne); print_string "\nBdd.cardinal computes: "; print_float (Bdd.cardinal (List.length (Bdd.int_of_support comb)) bdd_not); print_string " = "; print_float card_sol_t_not; print_string " + "; print_float card_sol_e_not ; print_string "\n\n"; flush stdout ; `````` Erwan Jahier committed Mar 17, 2010 138 `````` Print.bdd_with_dot bdd_not (Env_state.index_to_vn) "bdd_not" ; `````` Erwan Jahier committed Mar 17, 2010 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 `````` Util.gv "bdd.ps"; Util.gv "bdd_not.ps"; false end else true ) in Hashtbl.add env_state.snt bdd (nt, ne) ; Hashtbl.add env_state.snt bdd_not (not_nt, not_ne) ; ((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 -> (* returns the absolute number of solutions of [sub_bdd] (and its negation) w.r.t. [comb], where [comb] is the comb of the father of [sub_bdd]. *) if Bdd.is_cst sub_bdd then let sol_nb = (two_power_of (List.length (Bdd.int_of_support (Bdd.dthen comb)))) in if Bdd.is_true sub_bdd then (sol_nb, zero_sol) else (zero_sol, sol_nb) else let topvar = Bdd.topvar sub_bdd in let (sub_comb, missing_vars_nb) = count_missing_vars (Bdd.dthen comb) topvar 0 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) `````` Erwan Jahier committed Mar 17, 2010 174 175 176 177 178 179 180 181 `````` (****************************************************************************) (****************************************************************************) let (toss_up_one_var: var -> var * bool) = fun var -> let ran = Random.float 1. in if (ran < 0.5) then (var, true) else (var, false) `````` Erwan Jahier committed Mar 17, 2010 182 183 `````` `````` Erwan Jahier committed Mar 17, 2010 184 185 ``````let rec (draw_in_bdd: Bdd.t -> Bdd.t -> (var * bool) list) = fun bdd comb -> `````` Erwan Jahier committed Mar 17, 2010 186 `````` (** Returns a draw of the variables from the topvar of [bdd] to the end `````` Erwan Jahier committed Mar 17, 2010 187 `````` (according to the ordering of the comb). `````` Erwan Jahier committed Mar 17, 2010 188 189 `````` *) let _ = assert (not (Bdd.is_cst bdd)) in `````` Erwan Jahier committed Mar 17, 2010 190 `````` let bddvar = Bdd.topvar bdd in `````` Erwan Jahier committed Mar 17, 2010 191 `````` let combvar = Bdd.topvar comb in `````` Erwan Jahier committed Mar 17, 2010 192 `````` if `````` Erwan Jahier committed Mar 17, 2010 193 `````` bddvar = combvar `````` Erwan Jahier committed Mar 17, 2010 194 `````` then `````` Erwan Jahier committed Mar 17, 2010 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 `````` let _ = assert (Hashtbl.mem env_state.snt bdd) in let (n, m) = try Hashtbl.find env_state.snt bdd with Not_found -> (* XXX Debugging stuff *) (* Print.bdd_with_dot bdd (Env_state.index_to_vn) ("bbd"^ "_" ^ (string_of_int (Hashtbl.hash bdd))) ; *) (* Print.snt env_state.snt; *) (* print_int (Util.hashtbl_size env_state.snt); *) (* flush stdout; *) assert false in let _ = assert (not ((eq_sol_nb n zero_sol) && (eq_sol_nb m zero_sol))) in let t = add_sol_nb n m in let _ = assert ( (* XXX Debugging stuff *) let card_sol_t = (Bdd.cardinal (List.length (Bdd.int_of_support (Bdd.dthen comb))) (Bdd.dthen bdd)) and card_sol_e = (Bdd.cardinal (List.length (Bdd.int_of_support (Bdd.dthen comb))) (Bdd.delse bdd)) and n_float = float_of_sol_nb n and m_float = float_of_sol_nb m in if ( (card_sol_t <> n_float) || (card_sol_e <> m_float) ) && (card_sol_t < 10. ** 12.) (* Bdd.cardinal ougth to do rounding errors after 10^12 *) && (card_sol_e < 10. ** 12.) then begin print_string "Lurette computes: "; print_string (string_of_sol_nb t); print_string " = ";print_string (string_of_sol_nb n); print_string " + ";print_string (string_of_sol_nb m); print_string "\nBdd.cardinal computes: "; print_float (Bdd.cardinal (List.length (Bdd.int_of_support comb)) bdd); print_string " = "; print_float card_sol_t; print_string " + "; print_float card_sol_e ; print_string "\n\n"; flush stdout ; Print.bdd_with_dot bdd (Env_state.index_to_vn) "bbd" ; Util.gv "bdd.ps"; false end else true ) in `````` Erwan Jahier committed Mar 17, 2010 244 `````` let (bool, newbdd) = `````` Erwan Jahier committed Mar 17, 2010 245 246 247 `````` (* we draw [true] with the probability [n / (n + m)]. *) if (eq_sol_nb m zero_sol) then (true, (Bdd.dthen bdd)) else if (eq_sol_nb n zero_sol) then (false, (Bdd.delse bdd)) `````` Erwan Jahier committed Mar 17, 2010 248 249 `````` else let ran = Random.float 1. in `````` Erwan Jahier committed Mar 17, 2010 250 `````` if ran < ((float_of_sol_nb n) /. (float_of_sol_nb t)) `````` Erwan Jahier committed Mar 17, 2010 251 252 253 `````` then (true, (Bdd.dthen bdd)) else (false, (Bdd.delse bdd)) in `````` Erwan Jahier committed Mar 17, 2010 254 255 256 257 258 259 `````` let _ = assert (not (Bdd.is_false newbdd)) (* a branch with no solution should not have been drawn ! *) in if Bdd.is_true newbdd then (bddvar, bool)::(List.map toss_up_one_var (Bdd.int_of_support (Bdd.dthen comb))) `````` Erwan Jahier committed Mar 17, 2010 260 `````` else `````` Erwan Jahier committed Mar 17, 2010 261 `````` (bddvar, bool)::(draw_in_bdd newbdd (Bdd.dthen comb)) `````` Erwan Jahier committed Mar 17, 2010 262 `````` else `````` Erwan Jahier committed Mar 17, 2010 263 `````` (* Combvar <> topvar *) `````` Erwan Jahier committed Mar 17, 2010 264 265 `````` (toss_up_one_var combvar)::(draw_in_bdd bdd (Bdd.dthen comb)) `````` Erwan Jahier committed Mar 17, 2010 266 267 268 269 270 271 272 273 274 275 276 277 278 279 `````` (****************************************************************************) (****************************************************************************) (* Exported *) let rec (is_satisfiable: formula list -> bool) = (* As a side effect, also updates the [bdd_tbl] field of [env_state] with the bbd of [fl] (so that the expensive formula to bdd transcrition can be reused in [solve_formula]). *) fun fl -> let f = formula_list_to_conj fl in `````` Erwan Jahier committed Mar 17, 2010 280 `````` let bdd = formula_to_bdd (formula_list_to_conj fl) in `````` Erwan Jahier committed Mar 17, 2010 281 `````` not (Bdd.is_false bdd) `````` Erwan Jahier committed Mar 17, 2010 282 `````` `````` Erwan Jahier committed Mar 17, 2010 283 284 285 `````` (* Exported *) let (solve_formula: int -> formula list -> var_name list -> (subst list * subst list) list) = `````` Erwan Jahier committed Mar 17, 2010 286 `````` fun p fl vars_to_gen -> `````` Erwan Jahier committed Mar 17, 2010 287 288 289 290 291 292 293 `````` let f = formula_list_to_conj fl in let bdd = Hashtbl.find env_state.bdd_tbl f in let vars_to_gen_f = List.fold_left (fun acc vn -> (And(Bvar(vn), acc))) True vars_to_gen `````` Erwan Jahier committed Mar 17, 2010 294 `````` in `````` Erwan Jahier committed Mar 17, 2010 295 296 297 298 299 `````` let comb = formula_to_bdd vars_to_gen_f in let (draw_and_split : Bdd.t -> subst list * subst list) = fun bdd -> (* Draw values in the bdd *) let var_index_bool_l = draw_in_bdd bdd comb in `````` Erwan Jahier committed Mar 17, 2010 300 301 302 303 304 305 306 307 308 309 310 311 312 `````` let subst_l = List.map (fun (i, b) -> (* Replace the indexes by the corresponding var names, and booleans by atomic_expr. *) ((Env_state.index_to_vn i), Formula.Bool(b))) var_index_bool_l in let _ = assert ( (* Checks that the generated variables are indeed the ones that should have been generated... *) let (gen_vars, _) = split subst_l in (sort (compare) gen_vars) = (sort (compare) vars_to_gen) ) `````` Erwan Jahier committed Mar 17, 2010 313 `````` in `````` Erwan Jahier committed Mar 17, 2010 314 `````` let (out_vars, _) = List.split (env_state.output_var_names) in `````` Erwan Jahier committed Mar 17, 2010 315 `````` (* Splits output and local vars. *) `````` Erwan Jahier committed Mar 17, 2010 316 317 `````` List.partition (fun (vn, _) -> List.mem vn out_vars) subst_l in `````` Erwan Jahier committed Mar 17, 2010 318 319 320 `````` (* XXX Recompute the solution number everytime as long as the bdds interface sucks *) `````` Erwan Jahier committed Mar 17, 2010 321 322 323 324 325 326 327 328 329 330 331 332 `````` let _ = begin Hashtbl.clear env_state.snt ; Hashtbl.add env_state.snt (Bdd.dtrue ()) (one_sol, zero_sol); Hashtbl.add env_state.snt (Bdd.dfalse ()) (zero_sol, one_sol) end in let _ = if not (Hashtbl.mem env_state.snt bdd) then `````` Erwan Jahier committed Mar 17, 2010 333 334 335 336 337 338 339 340 341 342 `````` let rec skip_var comb v = if v = (Bdd.topvar comb) then comb else skip_var (Bdd.dthen comb) v in let comb2 = skip_var comb (Bdd.topvar bdd) in build_sol_nb_table bdd comb2 else (zero_sol, zero_sol) in Util.unfold (draw_and_split) bdd p `````` Erwan Jahier committed Mar 17, 2010 343