diff --git a/src/solvers/sat/satcheck_glucose.cpp b/src/solvers/sat/satcheck_glucose.cpp index fa146d09414..3de729ae0a0 100644 --- a/src/solvers/sat/satcheck_glucose.cpp +++ b/src/solvers/sat/satcheck_glucose.cpp @@ -12,9 +12,9 @@ Author: Daniel Kroening, kroening@kroening.com #include #endif -#include #include +#include #include #include @@ -64,9 +64,19 @@ tvt satcheck_glucose_baset::l_get(literalt a) const template void satcheck_glucose_baset::set_polarity(literalt a, bool value) { - assert(!a.is_constant()); - add_variables(); - solver->setPolarity(a.var_no(), value); + PRECONDITION(!a.is_constant()); + + try + { + add_variables(); + solver->setPolarity(a.var_no(), value); + } + catch(Glucose::OutOfMemoryException) + { + messaget::error() << "SAT checker ran out of memory" << eom; + status = statust::ERROR; + throw std::bad_alloc(); + } } const std::string satcheck_glucose_no_simplifiert::solver_text() @@ -89,32 +99,42 @@ void satcheck_glucose_baset::add_variables() template void satcheck_glucose_baset::lcnf(const bvt &bv) { - add_variables(); - - forall_literals(it, bv) + try { - if(it->is_true()) - return; - else if(!it->is_false()) - assert(it->var_no()<(unsigned)solver->nVars()); - } + add_variables(); - Glucose::vec c; + forall_literals(it, bv) + { + if(it->is_true()) + return; + else if(!it->is_false()) + INVARIANT( + it->var_no() < (unsigned)solver->nVars(), "variable not added yet"); + } + + Glucose::vec c; - convert(bv, c); + convert(bv, c); - // Note the underscore. - // Add a clause to the solver without making superflous internal copy. + // Note the underscore. + // Add a clause to the solver without making superflous internal copy. - solver->addClause_(c); + solver->addClause_(c); - clause_counter++; + clause_counter++; + } + catch(Glucose::OutOfMemoryException) + { + messaget::error() << "SAT checker ran out of memory" << eom; + status = statust::ERROR; + throw std::bad_alloc(); + } } template propt::resultt satcheck_glucose_baset::prop_solve() { - assert(status!=statust::ERROR); + PRECONDITION(status != statust::ERROR); // We start counting at 1, thus there is one variable fewer. { @@ -123,63 +143,79 @@ propt::resultt satcheck_glucose_baset::prop_solve() solver->nClauses() << " clauses" << eom; } - add_variables(); - - if(!solver->okay()) + try { - messaget::status() << - "SAT checker inconsistent: instance is UNSATISFIABLE" << eom; - } - else - { - // if assumptions contains false, we need this to be UNSAT - bool has_false=false; - - forall_literals(it, assumptions) - if(it->is_false()) - has_false=true; + add_variables(); - if(has_false) + if(!solver->okay()) { - messaget::status() << - "got FALSE as assumption: instance is UNSATISFIABLE" << eom; + messaget::status() + << "SAT checker inconsistent: instance is UNSATISFIABLE" << eom; } else { - Glucose::vec solver_assumptions; - convert(assumptions, solver_assumptions); + // if assumptions contains false, we need this to be UNSAT + bool has_false = false; + + forall_literals(it, assumptions) + if(it->is_false()) + has_false = true; - if(solver->solve(solver_assumptions)) + if(has_false) { - messaget::status() << - "SAT checker: instance is SATISFIABLE" << eom; - status=statust::SAT; - return resultt::P_SATISFIABLE; + messaget::status() + << "got FALSE as assumption: instance is UNSATISFIABLE" << eom; } else { - messaget::status() << - "SAT checker: instance is UNSATISFIABLE" << eom; + Glucose::vec solver_assumptions; + convert(assumptions, solver_assumptions); + + if(solver->solve(solver_assumptions)) + { + messaget::status() << "SAT checker: instance is SATISFIABLE" << eom; + status = statust::SAT; + return resultt::P_SATISFIABLE; + } + else + { + messaget::status() << "SAT checker: instance is UNSATISFIABLE" << eom; + } } } - } - status=statust::UNSAT; - return resultt::P_UNSATISFIABLE; + status = statust::UNSAT; + return resultt::P_UNSATISFIABLE; + } + catch(Glucose::OutOfMemoryException) + { + messaget::error() << "SAT checker ran out of memory" << eom; + status = statust::ERROR; + throw std::bad_alloc(); + } } template void satcheck_glucose_baset::set_assignment(literalt a, bool value) { - assert(!a.is_constant()); + PRECONDITION(!a.is_constant()); - unsigned v=a.var_no(); - bool sign=a.sign(); + try + { + unsigned v = a.var_no(); + bool sign = a.sign(); - // MiniSat2 kills the model in case of UNSAT - solver->model.growTo(v+1); - value^=sign; - solver->model[v]=Glucose::lbool(value); + // MiniSat2 kills the model in case of UNSAT + solver->model.growTo(v + 1); + value ^= sign; + solver->model[v] = Glucose::lbool(value); + } + catch(Glucose::OutOfMemoryException) + { + messaget::error() << "SAT checker ran out of memory" << eom; + status = statust::ERROR; + throw std::bad_alloc(); + } } template @@ -218,7 +254,7 @@ void satcheck_glucose_baset::set_assumptions(const bvt &bv) assumptions=bv; forall_literals(it, assumptions) - assert(!it->is_constant()); + INVARIANT(!it->is_constant(), "assumption literals must not be constant"); } satcheck_glucose_no_simplifiert::satcheck_glucose_no_simplifiert(): @@ -233,16 +269,25 @@ satcheck_glucose_simplifiert::satcheck_glucose_simplifiert(): void satcheck_glucose_simplifiert::set_frozen(literalt a) { - if(!a.is_constant()) + try { - add_variables(); - solver->setFrozen(a.var_no(), true); + if(!a.is_constant()) + { + add_variables(); + solver->setFrozen(a.var_no(), true); + } + } + catch(Glucose::OutOfMemoryException) + { + messaget::error() << "SAT checker ran out of memory" << eom; + status = statust::ERROR; + throw std::bad_alloc(); } } bool satcheck_glucose_simplifiert::is_eliminated(literalt a) const { - assert(!a.is_constant()); + PRECONDITION(!a.is_constant()); return solver->isEliminated(a.var_no()); } diff --git a/src/solvers/sat/satcheck_minisat2.cpp b/src/solvers/sat/satcheck_minisat2.cpp index 3f7ecc5c59b..3f50091bbb6 100644 --- a/src/solvers/sat/satcheck_minisat2.cpp +++ b/src/solvers/sat/satcheck_minisat2.cpp @@ -14,12 +14,10 @@ Author: Daniel Kroening, kroening@kroening.com #include #endif -#include #include #include #include -#include #include #include @@ -68,9 +66,19 @@ tvt satcheck_minisat2_baset::l_get(literalt a) const template void satcheck_minisat2_baset::set_polarity(literalt a, bool value) { - assert(!a.is_constant()); - add_variables(); - solver->setPolarity(a.var_no(), value); + PRECONDITION(!a.is_constant()); + + try + { + add_variables(); + solver->setPolarity(a.var_no(), value); + } + catch(Minisat::OutOfMemoryException) + { + messaget::error() << "SAT checker ran out of memory" << eom; + status = statust::ERROR; + throw std::bad_alloc(); + } } const std::string satcheck_minisat_no_simplifiert::solver_text() @@ -93,26 +101,38 @@ void satcheck_minisat2_baset::add_variables() template void satcheck_minisat2_baset::lcnf(const bvt &bv) { - add_variables(); - - forall_literals(it, bv) + try { - if(it->is_true()) - return; - else if(!it->is_false()) - assert(it->var_no()<(unsigned)solver->nVars()); - } + add_variables(); - Minisat::vec c; + forall_literals(it, bv) + { + if(it->is_true()) + return; + else if(!it->is_false()) + { + INVARIANT( + it->var_no() < (unsigned)solver->nVars(), "variable not added yet"); + } + } - convert(bv, c); + Minisat::vec c; - // Note the underscore. - // Add a clause to the solver without making superflous internal copy. + convert(bv, c); - solver->addClause_(c); + // Note the underscore. + // Add a clause to the solver without making superflous internal copy. - clause_counter++; + solver->addClause_(c); + + clause_counter++; + } + catch(Minisat::OutOfMemoryException) + { + messaget::error() << "SAT checker ran out of memory" << eom; + status = statust::ERROR; + throw std::bad_alloc(); + } } #ifndef _WIN32 @@ -129,7 +149,7 @@ static void interrupt_solver(int signum) template propt::resultt satcheck_minisat2_baset::prop_solve() { - assert(status!=statust::ERROR); + PRECONDITION(status != statust::ERROR); { messaget::status() << @@ -241,15 +261,24 @@ propt::resultt satcheck_minisat2_baset::prop_solve() template void satcheck_minisat2_baset::set_assignment(literalt a, bool value) { - assert(!a.is_constant()); + PRECONDITION(!a.is_constant()); - unsigned v=a.var_no(); - bool sign=a.sign(); + try + { + unsigned v = a.var_no(); + bool sign = a.sign(); - // MiniSat2 kills the model in case of UNSAT - solver->model.growTo(v+1); - value^=sign; - solver->model[v]=Minisat::lbool(value); + // MiniSat2 kills the model in case of UNSAT + solver->model.growTo(v + 1); + value ^= sign; + solver->model[v] = Minisat::lbool(value); + } + catch(Minisat::OutOfMemoryException) + { + messaget::error() << "SAT checker ran out of memory" << eom; + status = statust::ERROR; + throw std::bad_alloc(); + } } template @@ -307,16 +336,25 @@ satcheck_minisat_simplifiert::satcheck_minisat_simplifiert(): void satcheck_minisat_simplifiert::set_frozen(literalt a) { - if(!a.is_constant()) + try { - add_variables(); - solver->setFrozen(a.var_no(), true); + if(!a.is_constant()) + { + add_variables(); + solver->setFrozen(a.var_no(), true); + } + } + catch(Minisat::OutOfMemoryException) + { + messaget::error() << "SAT checker ran out of memory" << eom; + status = statust::ERROR; + throw std::bad_alloc(); } } bool satcheck_minisat_simplifiert::is_eliminated(literalt a) const { - assert(!a.is_constant()); + PRECONDITION(!a.is_constant()); return solver->isEliminated(a.var_no()); }