dReal4
context_impl.h
1 #pragma once
2 
3 #include <string>
4 #include <unordered_map>
5 #include <unordered_set>
6 #include <vector>
7 
8 #include "dreal/solver/context.h"
9 #include "dreal/solver/sat_solver.h"
10 #include "dreal/solver/theory_solver.h"
11 #include "dreal/util/optional.h"
12 #include "dreal/util/scoped_vector.h"
13 
14 namespace dreal {
15 
16 // The actual implementation.
18  public:
19  Impl();
20  explicit Impl(Config config);
21  Impl(const Impl&) = delete;
22  Impl(Impl&&) = delete;
23  Impl& operator=(const Impl&) = delete;
24  Impl& operator=(Impl&&) = delete;
25  ~Impl() = default;
26 
27  void Assert(const Formula& f);
28  optional<Box> CheckSat();
29  void DeclareVariable(const Variable& v, bool is_model_variable);
30  void SetDomain(const Variable& v, const Expression& lb, const Expression& ub);
31  void Minimize(const std::vector<Expression>& functions);
32  void Pop();
33  void Push();
34  void SetInfo(const std::string& key, double val);
35  void SetInfo(const std::string& key, const std::string& val);
36  void SetInterval(const Variable& v, double lb, double ub);
37  void SetLogic(const Logic& logic);
38  void SetOption(const std::string& key, double val);
39  void SetOption(const std::string& key, const std::string& val);
40  optional<std::string> GetOption(const std::string& key) const;
41  const Config& config() const { return config_; }
42  Config& mutable_config() { return config_; }
43  const ScopedVector<Formula>& assertions() const;
44  Box& box() { return boxes_.last(); }
45  const Box& get_model() { return model_; }
46 
47  private:
48  // Add the variable @p v to the current box. This is used to
49  // introduce a non-model variable to solver. For a model variable,
50  // `DeclareVariable` should be used. But `DeclareVariable` should be
51  // called from outside of this class. Any methods in this class
52  // should not call it directly.
53  void AddToBox(const Variable& v);
54 
55  // Returns the current box in the stack.
56  optional<Box> CheckSatCore(const ScopedVector<Formula>& stack, Box box,
57  SatSolver* sat_solver);
58 
59  // Marks variable @p v as a model variable
60  void mark_model_variable(const Variable& v);
61 
62  // Checks if the variable @p v is a model variable or not.
63  bool is_model_variable(const Variable& v) const;
64 
65  // Extracts a model from the @p box. Note that @p box might include
66  // non-model variables (i.e. variables introduced by if-then-else
67  // elimination). This function creates a new box which is free of
68  // those non-model variables.
69  Box ExtractModel(const Box& box) const;
70 
71  Config config_;
72  optional<Logic> logic_{};
73  std::unordered_map<std::string, std::string> info_;
74  std::unordered_map<std::string, std::string> option_;
75 
76  // Stack of boxes. The top one is the current box.
77  ScopedVector<Box> boxes_;
78  // Stack of asserted formulas.
79  ScopedVector<Formula> stack_;
80  SatSolver sat_solver_;
81  std::unordered_set<Variable::Id> model_variables_;
82  TheorySolver theory_solver_;
83 
84  // Stores the result of the latest checksat.
85  // Note that if the checksat result was UNSAT, this box holds an empty box.
86  Box model_;
87 };
88 
89 } // namespace dreal
Definition: context_impl.h:17
Sum type of symbolic::Expression and symbolic::Formula.
Definition: api.cc:9
Theory solver for nonlinear theory over the Reals.
Definition: theory_solver.h:19
Represents a symbolic variable.
Definition: symbolic_variable.h:16
Definition: scoped_vector.h:16
Represents a n-dimensional interval vector.
Definition: box.h:17
Definition: config.h:12
Definition: sat_solver.h:20
Represents a symbolic form of an expression.
Definition: symbolic_expression.h:164
Represents a symbolic form of a first-order logic formula.
Definition: symbolic_formula.h:101