dReal4
sat_solver.h
1 #pragma once
2 
3 #include <memory>
4 #include <set>
5 #include <utility>
6 #include <vector>
7 
8 #include "./picosat.h"
9 
10 #include "dreal/solver/config.h"
12 #include "dreal/util/optional.h"
13 #include "dreal/util/predicate_abstractor.h"
14 #include "dreal/util/scoped_unordered_map.h"
15 #include "dreal/util/scoped_unordered_set.h"
16 #include "dreal/util/tseitin_cnfizer.h"
17 
18 namespace dreal {
19 
20 class SatSolver {
21  public:
22  using Literal = std::pair<Variable, bool>;
23 
24  // Boolean model + Theory model.
25  using Model = std::pair<std::vector<Literal>, std::vector<Literal>>;
26 
27  /// Constructs a SatSolver.
28  explicit SatSolver(const Config& config);
29 
30  /// Constructs a SatSolver while asserting @p clauses.
31  SatSolver(const Config& config, const std::vector<Formula>& clauses);
32 
33  /// Deleted copy constructor.
34  SatSolver(const SatSolver&) = delete;
35 
36  /// Deleted move constructor.
37  SatSolver(SatSolver&&) = delete;
38 
39  /// Deleted copy-assignment operator.
40  SatSolver& operator=(const SatSolver&) = delete;
41 
42  /// Deleted move-assignment operator.
43  SatSolver& operator=(SatSolver&&) = delete;
44 
45  ~SatSolver();
46 
47  /// Adds a formula @p f to the solver.
48  ///
49  /// @note If @p f is a clause, please use AddClause function. This
50  /// function does not assume anything about @p f and perform
51  /// pre-processings (CNFize and PredicateAbstraction).
52  void AddFormula(const Formula& f);
53 
54  /// Adds formulas @p formulas to the solver.
55  void AddFormulas(const std::vector<Formula>& formulas);
56 
57  /// Given a @p formulas = {f₁, ..., fₙ}, adds a clause (¬f₁ ∨ ... ∨ ¬ fₙ) to
58  /// the solver.
59  void AddLearnedClause(const std::set<Formula>& formulas);
60 
61  /// Checks the satisfiability of the current configuration.
62  ///
63  /// @returns a witness, satisfying model if the problem is satisfiable.
64  /// @returns nullopt if UNSAT.
65  optional<Model> CheckSat();
66 
67  // TODO(soonho): Push/Pop cnfizer and predicate_abstractor?
68  void Pop();
69 
70  void Push();
71 
72  Formula theory_literal(const Variable& var) const {
73  return predicate_abstractor_[var];
74  }
75 
76  private:
77  // Adds a formula @p f to the solver.
78  //
79  // @pre @p f is a clause. That is, it is either a literal (b or ¬b)
80  // or a disjunction of literals (l₁ ∨ ... ∨ lₙ).
81  void AddClause(const Formula& f);
82 
83  // Adds a vector of formulas @p formulas to the solver.
84  //
85  // @pre Each formula fᵢ ∈ formulas is a clause.
86  void AddClauses(const std::vector<Formula>& formulas);
87 
88  // Returns a corresponding literal ID of @p var. It maintains two
89  // maps `lit_to_var_` and `var_to_lit_` to keep track of the
90  // relationship between Variable ⇔ Literal (in SAT).
91  void MakeSatVar(const Variable& var);
92 
93  // Add a symbolic formula @p f to @p clause.
94  //
95  // @pre @p f is either a Boolean variable or a negation of Boolean
96  // variable.
97  void AddLiteral(const Formula& f);
98 
99  // Add a clause @p f to sat solver.
100  void DoAddClause(const Formula& f);
101 
102  // Member variables
103  // ----------------
104  // Pointer to the PicoSat solver.
105  PicoSAT* const sat_{};
106  TseitinCnfizer cnfizer_;
107  PredicateAbstractor predicate_abstractor_;
108 
109  // Map symbolic::Variable → int (Variable type in PicoSat).
111 
112  // Map int (Variable type in PicoSat) → symbolic::Variable.
114 
115  /// Set of temporary Boolean variables introduced by Tseitin
116  /// transformations.
117  ScopedUnorderedSet<Variable::Id> tseitin_variables_;
118 
119  /// @note We found an issue when picosat_deref_partial is used with
120  /// picosat_pop. When this variable is true, we use `picosat_deref`
121  /// instead.
122  ///
123  /// TODO(soonho): Remove this hack when it's not needed.
124  bool has_picosat_pop_used_{false};
125 };
126 
127 } // namespace dreal
void AddFormula(const Formula &f)
Adds a formula f to the solver.
Definition: sat_solver.cc:39
void AddFormulas(const std::vector< Formula > &formulas)
Adds formulas formulas to the solver.
Definition: sat_solver.cc:52
Sum type of symbolic::Expression and symbolic::Formula.
Definition: api.cc:9
optional< Model > CheckSat()
Checks the satisfiability of the current configuration.
Definition: sat_solver.cc:105
Represents a symbolic variable.
Definition: symbolic_variable.h:16
SatSolver & operator=(const SatSolver &)=delete
Deleted copy-assignment operator.
Transforms a symbolic formula f into an equi-satisfiable CNF formula by introducing extra Boolean var...
Definition: tseitin_cnfizer.h:13
Definition: predicate_abstractor.h:11
void AddLearnedClause(const std::set< Formula > &formulas)
Given a formulas = {f₁, ..., fₙ}, adds a clause (¬f₁ ∨ ...
Definition: sat_solver.cc:58
Definition: config.h:12
This is the header file that we consolidate Drake&#39;s symbolic classes and expose them inside of dreal ...
SatSolver(const Config &config)
Constructs a SatSolver.
Definition: sat_solver.cc:18
Definition: sat_solver.h:20
Represents a symbolic form of a first-order logic formula.
Definition: symbolic_formula.h:101