dlinear  0.0.1
Delta-complete SMT solver for linear programming
Loading...
Searching...
No Matches
SatSolver.h
1
7#pragma once
8
9#include <cstddef>
10#include <map>
11#include <optional>
12#include <set>
13#include <string>
14#include <vector>
15
16#include "dlinear/symbolic/PlaistedGreenbaumCnfizer.h"
17#include "dlinear/symbolic/PredicateAbstractor.h"
18#include "dlinear/symbolic/literal.h"
19#include "dlinear/symbolic/symbolic.h"
20#include "dlinear/util/Config.h"
21#include "dlinear/util/ScopedUnorderedMap.hpp"
22#include "dlinear/util/ScopedUnorderedSet.hpp"
23#include "dlinear/util/Stats.h"
24
25namespace dlinear {
26
38class SatSolver {
39 public:
51 explicit SatSolver(PredicateAbstractor &predicate_abstractor, const std::string &class_name = "SatSolver");
52 virtual ~SatSolver() = default;
53
54 virtual void Push() = 0;
55 virtual void Pop() = 0;
64 void AddFormula(const Formula &f);
70 void AddFormulas(const std::vector<Formula> &formulas);
78 void AddClause(const Formula &f);
86 void AddClauses(const std::vector<Formula> &formulas);
92 virtual void AddLearnedClause(const LiteralSet &literals) = 0;
98 virtual void AddLearnedClause(const Literal &literals) = 0;
116 virtual void FixedTheoryLiterals(LiteralSet &fixed_literals) = 0;
117
125 void Assume(const LiteralSet &literals);
133 virtual void Assume(const Literal &lit) = 0;
134
141 void MarkAsCnfVariable(const Variable &var);
142
148 virtual std::optional<Model> CheckSat() = 0;
150 [[nodiscard]] const IterationStats &stats() const { return stats_; }
152 [[nodiscard]] const IterationStats &cnfizer_stats() const;
154 [[nodiscard]] const PredicateAbstractor &predicate_abstractor() const { return predicate_abstractor_; }
155
156 [[nodiscard]] std::vector<std::vector<Literal>> clauses() const;
157
158 protected:
165 virtual void AddClauseToSat(const Formula &f) = 0;
175 virtual void MakeSatVar(const Variable &var) = 0;
182 void AddLiteral(const Formula &f);
192 virtual void AddLiteral(const Literal &l, bool learned) = 0;
201 virtual std::set<int> GetMainActiveLiterals() = 0;
212 void GetMainActiveLiterals(std::set<int> &lits) const;
224 void UpdateLookup(int lit);
225
227
228 // Data to help with removing literals that are only required by learned clauses.
229 std::vector<int> main_clauses_copy_;
230 std::map<int, std::set<std::size_t>> main_clause_lookup_;
231 std::size_t cur_clause_start_;
232
237
240
242};
243
244} // namespace dlinear
Simple dataclass used to store the configuration of the program.
Definition Config.h:38
Dataclass collecting statistics about some operation or process.
Definition Stats.h:71
Plaisted-Greenbaum transformation is a method to convert a formula into an equi-satisfiable vector of...
Predicate abstraction is a method to convert a first-order logic formula into a Boolean formula.
Base class for SAT solvers.
Definition SatSolver.h:38
SatSolver(PredicateAbstractor &predicate_abstractor, const std::string &class_name="SatSolver")
Construct a new SatSolver object.
Definition SatSolver.cpp:17
virtual void AddLiteral(const Literal &l, bool learned)=0
Add a literal l to the SAT solver.
virtual std::optional< Model > CheckSat()=0
Check the satisfiability of the current configuration.
IterationStats stats_
Statistics for the solver.
Definition SatSolver.h:241
void AddClauses(const std::vector< Formula > &formulas)
Add a vector of formulas formulas to the solver.
Definition SatSolver.cpp:59
void AddFormula(const Formula &f)
Add a formula f to the solver.
Definition SatSolver.cpp:42
virtual void FixedTheoryLiterals(LiteralSet &fixed_literals)=0
Get the theory literals that are fixed in the current configuration.
ScopedUnorderedSet< Variable::Id > cnf_variables_
Set of temporary Boolean variables introduced by CNF transformations.
Definition SatSolver.h:236
std::size_t cur_clause_start_
Index of the first clause added by the SAT solver.
Definition SatSolver.h:231
ScopedUnorderedMap< int, Variable > sat_to_var_
Map int (Variable type in PicoSat) → symbolic::Variable.
Definition SatSolver.h:234
std::vector< int > main_clauses_copy_
Store the main clauses to restore them after the SAT solver call.
Definition SatSolver.h:229
const PredicateAbstractor & predicate_abstractor() const
Get read-only access to the predicate abstractor of the SAT solver.
Definition SatSolver.h:154
void AddLiteral(const Formula &f)
Add a formula f to the solver.
Definition SatSolver.cpp:71
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.
ScopedUnorderedMap< Variable::Id, int > var_to_sat_
Map symbolic::Variable → int (Variable type in PicoSat).
Definition SatSolver.h:233
void AddClause(const Formula &f)
Add a formula f to the solver.
Definition SatSolver.cpp:63
PlaistedGreenbaumCnfizer cnfizer_
Converts the formula to CNF.
Definition SatSolver.h:238
PredicateAbstractor & predicate_abstractor_
Converts the theory literals to boolean variables.
Definition SatSolver.h:239
const Config & config_
Configuration of the SAT solver.
Definition SatSolver.h:226
const IterationStats & stats() const
Get read-only access to the statistics of the SAT solver.
Definition SatSolver.h:150
void AddFormulas(const std::vector< Formula > &formulas)
Add the formulas to the solver.
Definition SatSolver.cpp:55
LiteralSet FixedTheoryLiterals()
Get the theory literals that are fixed in the current configuration.
Definition SatSolver.cpp:83
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...
Definition SatSolver.cpp:92
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.
Definition SatSolver.cpp:81
void Assume(const LiteralSet &literals)
Assumption a set of literals to be fixed for the next iteration.
Definition SatSolver.cpp:88
const IterationStats & cnfizer_stats() const
Get read-only access to the statistics of the cnfizer of the SAT solver.
Definition SatSolver.cpp:24
virtual void AddLearnedClause(const Literal &literals)=0
Given a clause = {f}, adds a clause (¬f) to the solver.
std::map< int, std::set< std::size_t > > main_clause_lookup_
Lookup table for literals in the main clauses.
Definition SatSolver.h:230
virtual void AddLearnedClause(const LiteralSet &literals)=0
Given a clause = {f₁, ..., fₙ}, adds a clause (¬f₁ ∨ ... ∨ ¬ fₙ) to the solver.
virtual void Assume(const Literal &lit)=0
Assumption a literals to be fixed for the next iteration.
Represents a symbolic form of a first-order logic formula.
Represents a symbolic variable.
Global namespace for the dlinear library.
std::set< Literal > LiteralSet
A set of literals.
Definition literal.h:28
std::pair< std::vector< Literal >, std::vector< Literal > > Model
A model is a pair of two vectors of literals.
Definition literal.h:29
A literal is a variable with an associated truth value, indicating whether it is true or false.
Definition literal.h:24