9#include <unordered_map>
12#include "dlinear/util/exception.h"
13#include "dlinear/util/logging.h"
18 : config_{predicate_abstractor.config()},
20 cnfizer_{predicate_abstractor.config()},
21 predicate_abstractor_{predicate_abstractor},
22 stats_{config_.with_timings(), class_name,
"Total time spent in CheckSat",
"Total # of CheckSat"} {}
26std::vector<std::vector<Literal>> SatSolver::clauses()
const {
27 std::vector<std::vector<Literal>> clauses{std::vector<Literal>{}};
30 if (!clauses.back().empty()) clauses.emplace_back();
33 const Variable &var =
sat_to_var_[std::abs(sat_lit)];
36 clauses.back().emplace_back(var, sat_lit > 0);
43 DLINEAR_DEBUG_FMT(
"SatSolver::AddFormula({})", f);
64 DLINEAR_DEBUG_FMT(
"ContextImpl::AddClause({})", f);
72 DLINEAR_ASSERT_FMT(is_variable(f) || (is_negation(f) && is_variable(get_operand(f))),
73 "f must be a variable or negation of a variable. Found {}", f);
77 AddLiteral({get_variable(get_operand(f)),
false},
false);
86 return fixed_literals;
98 for (
auto it = lits.begin(); it != lits.end();) {
100 bool required =
false;
104 for (
size_t c : c_it->second) {
109 if (lits.find(k) != lits.end()) ++count;
112 DLINEAR_ASSERT(count > 0,
"should contain at least 'i'");
140 const Variable &var{it_var->second};
142 if (var_to_formula_map.contains(var)) {
143 DLINEAR_TRACE_FMT(
"SatSolver::CheckSat: Add theory literal {}{} to Model", i > 0 ?
"" :
"¬", var);
144 model.second.emplace_back(var, i > 0);
146 DLINEAR_TRACE_FMT(
"SatSolver::CheckSat: Add Boolean literal {}{} to Model ", i > 0 ?
"" :
"¬", var);
147 model.first.emplace_back(var, i > 0);
149 DLINEAR_TRACE_FMT(
"SatSolver::CheckSat: Skip {}{} which is a temporary variable.", i > 0 ?
"" :
"¬", var);
152 DLINEAR_DEBUG(
"SatSolver::CheckSat() Found a model.");
Dataclass collecting statistics about some operation or process.
Predicate abstraction is a method to convert a first-order logic formula into a Boolean formula.
Formula Process(const Formula &f)
Convert a first-order logic formula f into a Boolean formula by predicate abstraction.
const std::unordered_map< Variable, Formula > & var_to_formula_map() const
Get read-only access to the map of previously converted formulae to variable of the PredicateAbstract...
SatSolver(PredicateAbstractor &predicate_abstractor, const std::string &class_name="SatSolver")
Construct a new SatSolver object.
void AddClauses(const std::vector< Formula > &formulas)
Add a vector of formulas formulas to the solver.
void AddFormula(const Formula &f)
Add a formula f to the solver.
ScopedUnorderedSet< Variable::Id > cnf_variables_
Set of temporary Boolean variables introduced by CNF transformations.
std::size_t cur_clause_start_
Index of the first clause added by the SAT solver.
ScopedUnorderedMap< int, Variable > sat_to_var_
Map int (Variable type in PicoSat) → symbolic::Variable.
std::vector< int > main_clauses_copy_
Store the main clauses to restore them after the SAT solver call.
void AddLiteral(const Formula &f)
Add a formula f to the solver.
virtual void AddClauseToSat(const Formula &f)=0
Add a clause f to the internal SAT solver.
Model OnSatResult()
If the SAT solver returns SAT, this function can be used to obtain a model for the formula.
void AddClause(const Formula &f)
Add a formula f to the solver.
PlaistedGreenbaumCnfizer cnfizer_
Converts the formula to CNF.
PredicateAbstractor & predicate_abstractor_
Converts the theory literals to boolean variables.
void AddFormulas(const std::vector< Formula > &formulas)
Add the formulas to the solver.
LiteralSet FixedTheoryLiterals()
Get the theory literals that are fixed in the current configuration.
virtual void MakeSatVar(const Variable &var)=0
Add a variable to the internal SAT solver.
void UpdateLookup(int lit)
Update data structures used to minimize the number of assigned literals the theory solver has to veri...
virtual std::set< int > GetMainActiveLiterals()=0
Get a set of the currently active literals in the clauses, ignoring those required by learned clauses...
void MarkAsCnfVariable(const Variable &var)
Mark var as a CNF variable.
void Assume(const LiteralSet &literals)
Assumption a set of literals to be fixed for the next iteration.
const IterationStats & cnfizer_stats() const
Get read-only access to the statistics of the cnfizer of the SAT solver.
std::map< int, std::set< std::size_t > > main_clause_lookup_
Lookup table for literals in the main clauses.
Represents a symbolic variable.
Global namespace for the dlinear library.
std::set< Literal > LiteralSet
A set of literals.
std::pair< std::vector< Literal >, std::vector< Literal > > Model
A model is a pair of two vectors of literals.
A literal is a variable with an associated truth value, indicating whether it is true or false.