dReal4
symbolic_environment.h
1 #pragma once
2 
3 #include <initializer_list>
4 #include <ostream>
5 #include <string>
6 #include <unordered_map>
7 
8 #include "dreal/symbolic/symbolic_variable.h"
9 #include "dreal/symbolic/symbolic_variables.h"
10 
11 namespace dreal {
12 namespace drake {
13 namespace symbolic {
14 /** Represents a symbolic environment (mapping from a variable to a value).
15  *
16  * This class is used when we evaluate symbolic expressions or formulas which
17  * include unquantified (free) variables. Here are examples:
18  *
19  * \code{.cpp}
20  * const Variable var_x{"x"};
21  * const Variable var_y{"y"};
22  * const Expression x{var_x};
23  * const Expression y{var_x};
24  * const Expression e1{x + y};
25  * const Expression e2{x - y};
26  * const Formula f{e1 > e2};
27  *
28  * // env maps var_x to 2.0 and var_y to 3.0
29  * const Environment env{{var_x, 2.0}, {var_y, 3.0}};
30  *
31  * const double res1 = e1.Evaluate(env); // x + y => 2.0 + 3.0 => 5.0
32  * const double res2 = e2.Evaluate(env); // x - y => 2.0 - 3.0 => -1.0
33  * const bool res = f.Evaluate(env); // x + y > x - y => 5.0 >= -1.0 => True
34  * \endcode
35  *
36  * Note that it is not allowed to have a dummy variable in an environment. It
37  * throws std::runtime_error for the attempts to create an environment with a
38  * dummy variable, to insert a dummy variable to an existing environment, or to
39  * take a reference to a value mapped to a dummy variable. See the following
40  * examples.
41  *
42  * \code{.cpp}
43  * Variable var_dummy{}; // OK to have a dummy variable
44  * Environment e1{var_dummy}; // throws std::runtime_error exception
45  * Environment e2{{var_dummy, 1.0}}; // throws std::runtime_error exception
46  * Environment e{};
47  * e.insert(var_dummy, 1.0); // throws std::runtime_error exception
48  * e[var_dummy] = 3.0; // throws std::runtime_error exception
49  * \endcode
50  *
51  */
52 class Environment {
53  public:
54  Environment(const Environment&) = default;
55  Environment& operator=(const Environment&) = default;
56  Environment(Environment&&) = default;
57  Environment& operator=(Environment&&) = default;
58 
59  typedef Variable key_type;
60  typedef double mapped_type;
61  typedef
62  typename std::unordered_map<key_type, mapped_type, hash_value<key_type>>
63  map;
64  /** std::pair<key_type, mapped_type> */
65  typedef typename map::value_type value_type;
66  typedef typename map::iterator iterator;
67  typedef typename map::const_iterator const_iterator;
68 
69  /** Default constructor. */
70  Environment() = default;
71 
72  /** Default destructor. */
73  ~Environment() = default;
74 
75  /** List constructor. Constructs an environment from a list of (Variable *
76  * double). */
77  Environment(std::initializer_list<value_type> init);
78 
79  /** List constructor. Constructs an environment from a list of
80  * Variable. Initializes the variables with 0.0. */
81  Environment(std::initializer_list<key_type> vars);
82 
83  /** Constructs an environment from @p m*/
84  explicit Environment(map m);
85 
86  /** Returns an iterator to the beginning. */
87  iterator begin() { return map_.begin(); }
88  /** Returns an iterator to the end. */
89  iterator end() { return map_.end(); }
90  /** Returns a const iterator to the beginning. */
91  const_iterator begin() const { return map_.cbegin(); }
92  /** Returns a const iterator to the end. */
93  const_iterator end() const { return map_.cend(); }
94  /** Returns a const iterator to the beginning. */
95  const_iterator cbegin() const { return map_.cbegin(); }
96  /** Returns a const iterator to the end. */
97  const_iterator cend() const { return map_.cend(); }
98 
99  /** Inserts a pair (@p key, @p elem). */
100  void insert(const key_type& key, const mapped_type& elem);
101  /** Checks whether the container is empty. */
102  bool empty() const { return map_.empty(); }
103  /** Returns the number of elements. */
104  size_t size() const { return map_.size(); }
105 
106  /** Finds element with specific key. */
107  iterator find(const key_type& key) { return map_.find(key); }
108  /** Finds element with specific key. */
109  const_iterator find(const key_type& key) const { return map_.find(key); }
110 
111  /** Returns the domain of this environment. */
112  Variables domain() const;
113 
114  /** Returns string representation. */
115  std::string to_string() const;
116 
117  /** Returns a reference to the value that is mapped to a key equivalent to
118  * @p key, performing an insertion if such key does not already exist.
119  */
120  mapped_type& operator[](const key_type& key);
121 
122  /** As above, but returns a constref and does not perform an insertion
123  * (throwing a runtime error instead) if the key does not exist. */
124  const mapped_type& operator[](const key_type& key) const;
125 
126  friend std::ostream& operator<<(std::ostream& os, const Environment& env);
127 
128  private:
129  map map_;
130 };
131 } // namespace symbolic
132 } // namespace drake
133 } // namespace dreal
mapped_type & operator[](const key_type &key)
Returns a reference to the value that is mapped to a key equivalent to key, performing an insertion i...
Definition: symbolic_environment.cc:83
const_iterator end() const
Returns a const iterator to the end.
Definition: symbolic_environment.h:93
bool empty() const
Checks whether the container is empty.
Definition: symbolic_environment.h:102
Sum type of symbolic::Expression and symbolic::Formula.
Definition: api.cc:9
Variables domain() const
Returns the domain of this environment.
Definition: symbolic_environment.cc:69
void insert(const key_type &key, const mapped_type &elem)
Inserts a pair (key, elem).
Definition: symbolic_environment.cc:63
map::value_type value_type
std::pair<key_type, mapped_type>
Definition: symbolic_environment.h:65
Environment()=default
Default constructor.
Represents a symbolic variable.
Definition: symbolic_variable.h:16
const_iterator find(const key_type &key) const
Finds element with specific key.
Definition: symbolic_environment.h:109
iterator end()
Returns an iterator to the end.
Definition: symbolic_environment.h:89
const_iterator begin() const
Returns a const iterator to the beginning.
Definition: symbolic_environment.h:91
Represents a symbolic environment (mapping from a variable to a value).
Definition: symbolic_environment.h:52
const_iterator cend() const
Returns a const iterator to the end.
Definition: symbolic_environment.h:97
iterator begin()
Returns an iterator to the beginning.
Definition: symbolic_environment.h:87
~Environment()=default
Default destructor.
std::string to_string() const
Returns string representation.
Definition: symbolic_environment.cc:77
Represents a set of variables.
Definition: symbolic_variables.h:25
iterator find(const key_type &key)
Finds element with specific key.
Definition: symbolic_environment.h:107
size_t size() const
Returns the number of elements.
Definition: symbolic_environment.h:104
const_iterator cbegin() const
Returns a const iterator to the beginning.
Definition: symbolic_environment.h:95