dReal4
context.h
1 #pragma once
2 
3 #include <memory>
4 #include <string>
5 #include <unordered_map>
6 #include <utility>
7 #include <vector>
8 
9 #include "dreal/smt2/logic.h"
10 #include "dreal/solver/config.h"
12 #include "dreal/util/box.h"
13 #include "dreal/util/optional.h"
14 #include "dreal/util/scoped_vector.h"
15 #include "dreal/version.h"
16 
17 namespace dreal {
18 
19 /// Context class that holds a set of constraints and provide
20 /// Assert/Push/Pop/CheckSat functionalities.
21 ///
22 /// @note The implementation details are in context_impl.h file.
23 class Context {
24  public:
25  /// Constructs a context with an empty configuration.
26  Context();
27 
28  /// Deleted copy constructor.
29  Context(const Context& context) = delete;
30 
31  /// Move constructor.
32  Context(Context&& context) noexcept;
33 
34  /// Destructor (Defaulted in .cc file. Needed here for compilation).
35  ~Context();
36 
37  /// Deleted copy-assign.
38  Context& operator=(const Context&) = delete;
39 
40  /// Deleted move-assign.
41  Context& operator=(Context&&) = delete;
42 
43  /// Constructs a context with @p config.
44  explicit Context(const Config& config);
45 
46  /// Asserts a formula @p f.
47  void Assert(const Formula& f);
48 
49  /// Checks the satisfiability of the asserted formulas.
50  optional<Box> CheckSat();
51 
52  /// Declare a variable @p v. By default @p v is considered as a
53  /// model variable. If @p is_model_variable is false, it is declared as
54  /// a non-model variable and will not appear in the model.
55  void DeclareVariable(const Variable& v, bool is_model_variable = true);
56 
57  /// Declare a variable @p v which is bounded by an interval `[lb,
58  /// ub]`. By default @p v is considered as a model variable. If @p
59  /// is_model_variable is false, it is declared as a non-model variable
60  /// and will not appear in the model.
61  void DeclareVariable(const Variable& v, const Expression& lb,
62  const Expression& ub, bool is_model_variable = true);
63 
64  /// Closes the context.
65  static void Exit();
66 
67  /// Asserts a formula minimizing a cost function @p f.
68  void Minimize(const Expression& f);
69 
70  /// Asserts a formula encoding Pareto optimality with a given set of
71  /// objective functions.
72  void Minimize(const std::vector<Expression>& functions);
73 
74  /// Asserts a formula maximizing a cost function @p f.
75  void Maximize(const Expression& f);
76 
77  /// Pops @p n stacks.
78  void Pop(int n);
79 
80  /// Pushes @p n stacks.
81  void Push(int n);
82 
83  /// Sets an info @p key with a value @p val.
84  void SetInfo(const std::string& key, double val);
85 
86  /// Sets an info @p key with a value @p val.
87  void SetInfo(const std::string& key, const std::string& val);
88 
89  /// Sets the interval of @p v in the current box (top one in boxes_).
90  void SetInterval(const Variable& v, double lb, double ub);
91 
92  /// Sets the current logic to be @p logic.
93  void SetLogic(const Logic& logic);
94 
95  /// Sets an option @p key with a value @p val.
96  void SetOption(const std::string& key, double val);
97 
98  /// Sets an option @p key with a value @p val.
99  void SetOption(const std::string& key, const std::string& val);
100 
101  /// Gets the associated value for @p key.
102  optional<std::string> GetOption(const std::string& key) const;
103 
104  /// Returns a const reference of configuration.
105  const Config& config() const;
106 
107  /// Returns a mutable reference of configuration.
109 
110  /// Returns the version string.
111  static std::string version();
112 
113  /// Returns the const reference to the asserted formulas.
114  ///
115  /// @note that the returned vector can be a proper subset of the
116  /// asserted formulas. For example, when `x <= 5` is asserted, box()
117  /// is updated to have this information (x <= 5) and this formula is
118  /// thrown away.
119  const ScopedVector<Formula>& assertions() const;
120 
121  /// Returns the const reference to the top box.
122  const Box& box() const;
123 
124  /// Returns a representation of a model computed by the solver in
125  /// response to an invocation of the check-sat.
126  const Box& get_model() const;
127 
128  private:
129  // This header is exposed to external users as a part of API. We use
130  // PIMPL idiom to hide internals and to reduce number of '#includes' in this
131  // file.
132  class Impl;
133 
134  std::unique_ptr<Impl> impl_;
135 };
136 } // namespace dreal
Context class that holds a set of constraints and provide Assert/Push/Pop/CheckSat functionalities...
Definition: context.h:23
const Box & box() const
Returns the const reference to the top box.
Definition: context.cc:103
Definition: context_impl.h:17
Sum type of symbolic::Expression and symbolic::Formula.
Definition: api.cc:9
optional< Box > CheckSat()
Checks the satisfiability of the asserted formulas.
Definition: context.cc:27
const ScopedVector< Formula > & assertions() const
Returns the const reference to the asserted formulas.
Definition: context.cc:107
Context & operator=(const Context &)=delete
Deleted copy-assign.
Represents a symbolic variable.
Definition: symbolic_variable.h:16
Context()
Constructs a context with an empty configuration.
Definition: context.cc:16
~Context()
Destructor (Defaulted in .cc file. Needed here for compilation).
const Config & config() const
Returns a const reference of configuration.
Definition: context.cc:98
void Maximize(const Expression &f)
Asserts a formula maximizing a cost function f.
Definition: context.cc:48
void DeclareVariable(const Variable &v, bool is_model_variable=true)
Declare a variable v.
Definition: context.cc:29
void Minimize(const Expression &f)
Asserts a formula minimizing a cost function f.
Definition: context.cc:42
optional< std::string > GetOption(const std::string &key) const
Gets the associated value for key.
Definition: context.cc:94
const Box & get_model() const
Returns a representation of a model computed by the solver in response to an invocation of the check-...
Definition: context.cc:105
void SetInfo(const std::string &key, double val)
Sets an info key with a value val.
Definition: context.cc:72
void Assert(const Formula &f)
Asserts a formula f.
Definition: context.cc:25
Definition: scoped_vector.h:16
Represents a n-dimensional interval vector.
Definition: box.h:17
Definition: config.h:12
void SetLogic(const Logic &logic)
Sets the current logic to be logic.
Definition: context.cc:84
void Push(int n)
Pushes n stacks.
Definition: context.cc:61
static void Exit()
Closes the context.
Definition: context.cc:40
void Pop(int n)
Pops n stacks.
Definition: context.cc:50
Config & mutable_config()
Returns a mutable reference of configuration.
Definition: context.cc:99
This is the header file that we consolidate Drake&#39;s symbolic classes and expose them inside of dreal ...
void SetOption(const std::string &key, double val)
Sets an option key with a value val.
Definition: context.cc:86
static std::string version()
Returns the version string.
Definition: context.cc:101
Represents a symbolic form of an expression.
Definition: symbolic_expression.h:164
void SetInterval(const Variable &v, double lb, double ub)
Sets the interval of v in the current box (top one in boxes_).
Definition: context.cc:80
Represents a symbolic form of a first-order logic formula.
Definition: symbolic_formula.h:101