dReal4
symbolic_variable.h
1 #pragma once
2 
3 #include <cstddef>
4 #include <functional>
5 #include <memory>
6 #include <ostream>
7 #include <string>
8 
9 #include "dreal/symbolic/hash.h"
10 
11 namespace dreal {
12 namespace drake {
13 namespace symbolic {
14 
15 /** Represents a symbolic variable. */
16 class Variable {
17  public:
18  typedef size_t Id;
19 
20  /** Supported types of symbolic variables. */
21  // TODO(soonho-tri): refines the following descriptions.
22  enum class Type {
23  CONTINUOUS, ///< A CONTINUOUS variable takes a `double` value.
24  INTEGER, ///< An INTEGER variable takes an `int` value.
25  BINARY, ///< A BINARY variable takes an integer value from {0, 1}.
26  BOOLEAN, ///< A BOOLEAN variable takes a `bool` value.
27  };
28 
29  Variable(const Variable&) = default;
30  Variable& operator=(const Variable&) = default;
31  Variable(Variable&&) = default;
32  Variable& operator=(Variable&&) = default;
33 
34  /** Default constructor. Constructs a dummy variable of CONTINUOUS type. This
35  * is needed to have Eigen::Matrix<Variable>. The objects created by the
36  * default constructor share the same ID, zero. As a result, they all are
37  * identified as a single variable by equality operator (==). They all have
38  * the same hash value as well.
39  *
40  * It is allowed to construct a dummy variable but it should not be used to
41  * construct a symbolic expression.
42  */
43  Variable() : name_{std::make_shared<std::string>()} {}
44 
45  /** Default destructor. */
46  ~Variable() = default;
47 
48  /** Constructs a variable with a string. If not specified, it has CONTINUOUS
49  * type by default.*/
50  explicit Variable(std::string name, Type type = Type::CONTINUOUS);
51 
52  /** Constructs a variable with @p name and @p type. @p model_variable is
53  * ignored. */
54  [[deprecated("This is only for backward-compatibility.")]] Variable(
55  std::string name, Type type, bool model_variable);
56 
57  /** Checks if this is a dummy variable (ID = 0) which is created by
58  * the default constructor. */
59  bool is_dummy() const { return get_id() == 0; }
60  Id get_id() const;
61  Type get_type() const;
62  size_t get_hash() const { return std::hash<Id>{}(id_); }
63  std::string get_name() const;
64  std::string to_string() const;
65 
66  /// Checks the equality of two variables based on their ID values.
67  bool equal_to(const Variable& v) const { return get_id() == v.get_id(); }
68 
69  /// Compares two variables based on their ID values.
70  bool less(const Variable& v) const { return get_id() < v.get_id(); }
71 
72  friend std::ostream& operator<<(std::ostream& os, const Variable& var);
73 
74  private:
75  // Produces a unique ID for a variable.
76  static Id get_next_id();
77  Id id_{}; // Unique identifier.
78  Type type_{Type::CONTINUOUS};
79 
80  // Variable class has shared_ptr<string> instead of string to be
81  // drake::test::IsMemcpyMovable.
82  // Please check https://github.com/RobotLocomotion/drake/issues/5974
83  // for more information.
84  std::shared_ptr<std::string> name_; // Name of variable.
85 };
86 
87 std::ostream& operator<<(std::ostream& os, Variable::Type type);
88 
89 } // namespace symbolic
90 
91 /** Computes the hash value of a variable. */
92 template <>
93 struct hash_value<symbolic::Variable> {
94  size_t operator()(const symbolic::Variable& v) const { return v.get_hash(); }
95 };
96 
97 } // namespace drake
98 } // namespace dreal
99 
100 namespace std {
101 /* Provides std::less<dreal::drake::symbolic::Variable>. */
102 template <>
103 struct less<dreal::drake::symbolic::Variable> {
104  bool operator()(const dreal::drake::symbolic::Variable& lhs,
105  const dreal::drake::symbolic::Variable& rhs) const {
106  return lhs.less(rhs);
107  }
108 };
109 
110 /* Provides std::equal_to<dreal::drake::symbolic::Variable>. */
111 template <>
112 struct equal_to<dreal::drake::symbolic::Variable> {
113  bool operator()(const dreal::drake::symbolic::Variable& lhs,
114  const dreal::drake::symbolic::Variable& rhs) const {
115  return lhs.equal_to(rhs);
116  }
117 };
118 
119 template <>
120 struct hash<dreal::drake::symbolic::Variable> {
121  size_t operator()(const dreal::drake::symbolic::Variable& v) const {
122  return v.get_hash();
123  }
124 };
125 
126 } // namespace std
A BINARY variable takes an integer value from {0, 1}.
Variable()
Default constructor.
Definition: symbolic_variable.h:43
Sum type of symbolic::Expression and symbolic::Formula.
Definition: api.cc:9
STL namespace.
~Variable()=default
Default destructor.
Type
Supported types of symbolic variables.
Definition: symbolic_variable.h:22
Represents a symbolic variable.
Definition: symbolic_variable.h:16
Computes the hash value of v using std::hash.
Definition: hash.h:35
bool is_dummy() const
Checks if this is a dummy variable (ID = 0) which is created by the default constructor.
Definition: symbolic_variable.h:59
An INTEGER variable takes an int value.
bool less(const Variable &v) const
Compares two variables based on their ID values.
Definition: symbolic_variable.h:70
A CONTINUOUS variable takes a double value.
A BOOLEAN variable takes a bool value.
bool equal_to(const Variable &v) const
Checks the equality of two variables based on their ID values.
Definition: symbolic_variable.h:67