dReal4
ibex_converter.h
1 #pragma once
2 
3 #include <memory>
4 #include <unordered_map>
5 #include <vector>
6 
7 #include "./ibex.h"
8 
10 #include "dreal/util/box.h"
11 
12 namespace dreal {
13 /// Visitor class which converts a symbolic Formula into
14 /// ibex::ExprCtr.
16  public:
17  /// Delete the default constructor.
18  IbexConverter() = delete;
19 
20  /// Constructs a converter from @p variables.
21  explicit IbexConverter(const std::vector<Variable>& variables);
22 
23  /// Constructs a converter from @p box.
24  explicit IbexConverter(const Box& box);
25 
26  ///@{ Delete copy/move constructors and copy/move assign operations.
27  IbexConverter(const IbexConverter&) = delete;
28  IbexConverter& operator=(const IbexConverter&) = delete;
29  IbexConverter(IbexConverter&&) = delete;
30  IbexConverter& operator=(IbexConverter&&) = delete;
31  ///@}
32 
33  /// Destructor. If `need_to_delete_variables_` is set, delete
34  /// `ibex::ExprSymbol*` in `symbolic_var_to_ibex_var_`.
36 
37  /// Convert @p f into the corresponding IBEX data structure,
38  /// ibex::ExprCtr*.
39  ///
40  /// @note It is caller's responsibility to delete the return value.
41  /// @note So far, it is always the case that the returned value is
42  /// used to construct an `ibex::Function` object. Simply deleting
43  /// `ibex::ExprCtr` does not destruct the included `ibex::ExprNode`
44  /// objects. However, `ibex::Function`'s destructor does the job. As
45  /// a result, we should not call `ibex::cleanup` function explicitly
46  /// with the return value of this method.
47  const ibex::ExprCtr* Convert(const Formula& f);
48 
49  const ibex::Array<const ibex::ExprSymbol>& variables() const;
50 
51  void set_need_to_delete_variables(bool value);
52 
53  private:
54  // Convert @p e into the corresponding IBEX data structure,
55  // ibex::ExprNode*.
56  //
57  // @note See the above note in `Convert(const Formula& f)`.
58  const ibex::ExprNode* Convert(const Expression& e);
59 
60  // Visits @p e and converts it into ibex::ExprNode.
61  const ibex::ExprNode* Visit(const Expression& e);
62  const ibex::ExprNode* VisitVariable(const Expression& e);
63  const ibex::ExprNode* VisitConstant(const Expression& e);
64  const ibex::ExprNode* VisitRealConstant(const Expression& e);
65  const ibex::ExprNode* VisitAddition(const Expression& e);
66  const ibex::ExprNode* VisitMultiplication(const Expression& e);
67  const ibex::ExprNode* VisitDivision(const Expression& e);
68  const ibex::ExprNode* VisitLog(const Expression& e);
69  const ibex::ExprNode* VisitAbs(const Expression& e);
70  const ibex::ExprNode* VisitExp(const Expression& e);
71  const ibex::ExprNode* VisitSqrt(const Expression& e);
72  const ibex::ExprNode* ProcessPow(const Expression& base,
73  const Expression& exponent);
74  const ibex::ExprNode* VisitPow(const Expression& e);
75  const ibex::ExprNode* VisitSin(const Expression& e);
76  const ibex::ExprNode* VisitCos(const Expression& e);
77  const ibex::ExprNode* VisitTan(const Expression& e);
78  const ibex::ExprNode* VisitAsin(const Expression& e);
79  const ibex::ExprNode* VisitAcos(const Expression& e);
80  const ibex::ExprNode* VisitAtan(const Expression& e);
81  const ibex::ExprNode* VisitAtan2(const Expression& e);
82  const ibex::ExprNode* VisitSinh(const Expression& e);
83  const ibex::ExprNode* VisitCosh(const Expression& e);
84  const ibex::ExprNode* VisitTanh(const Expression& e);
85  const ibex::ExprNode* VisitMin(const Expression& e);
86  const ibex::ExprNode* VisitMax(const Expression& e);
87  const ibex::ExprNode* VisitIfThenElse(const Expression&);
88  const ibex::ExprNode* VisitUninterpretedFunction(const Expression&);
89 
90  // Visits @p e and converts it into ibex::ibex::ExprNode.
91  const ibex::ExprCtr* Visit(const Formula& f, bool polarity);
92  const ibex::ExprCtr* VisitFalse(const Formula&, bool);
93  const ibex::ExprCtr* VisitTrue(const Formula&, bool);
94  const ibex::ExprCtr* VisitVariable(const Formula&, bool);
95  const ibex::ExprCtr* VisitEqualTo(const Formula& f, bool polarity);
96  const ibex::ExprCtr* VisitNotEqualTo(const Formula& f, bool polarity);
97  const ibex::ExprCtr* VisitGreaterThan(const Formula& f, bool polarity);
98  const ibex::ExprCtr* VisitGreaterThanOrEqualTo(const Formula& f,
99  bool polarity);
100  const ibex::ExprCtr* VisitLessThan(const Formula& f, bool polarity);
101  const ibex::ExprCtr* VisitLessThanOrEqualTo(const Formula& f, bool polarity);
102  const ibex::ExprCtr* VisitConjunction(const Formula&, bool);
103  const ibex::ExprCtr* VisitDisjunction(const Formula&, bool);
104  const ibex::ExprCtr* VisitNegation(const Formula& f, bool polarity);
105  const ibex::ExprCtr* VisitForall(const Formula&, bool);
106 
107  // ---------------
108  // Member fields
109  // ---------------
110  // Set of domain variables.
111  const std::vector<Variable>& vars_;
112 
113  // True if we need to delete the variables (ibex::ExprSymbol
114  // objects) in the destructor. At initialization, this is true. But
115  // when `Convert()` method returns a non-null pointer, it changes to
116  // false assuming that the caller will delete the variables.
117  bool need_to_delete_variables_{true};
118 
119  // Variable → ibex::ExprSymbol*.
120  std::unordered_map<Variable::Id, const ibex::ExprSymbol*>
121  symbolic_var_to_ibex_var_;
122 
123  ibex::Array<const ibex::ExprSymbol> var_array_;
124 
125  // Represents the value `0.0`. We use this to avoid possible
126  // memory-leak caused by IBEX code: See
127  // https://github.com/ibex-team/ibex-lib/blob/af48e38847414818913b6954e1b1b3050aa14593/src/symbolic/ibex_ExprCtr.h#L53-L55
128  const ibex::ExprNode* zero_;
129 
130  // Makes VisitFormula a friend of this class so that it can use private
131  // operator()s.
132  friend const ibex::ExprCtr*
133  drake::symbolic::VisitFormula<const ibex::ExprCtr*>(IbexConverter*,
134  const Formula&,
135  const bool&);
136  // Makes VisitExpression a friend of this class so that it can use private
137  // operator()s.
138  friend const ibex::ExprNode*
139  drake::symbolic::VisitExpression<const ibex::ExprNode*>(IbexConverter*,
140  const Expression&);
141 };
142 
143 } // namespace dreal
const ibex::ExprCtr * Convert(const Formula &f)
Convert f into the corresponding IBEX data structure, ibex::ExprCtr*.
Definition: ibex_converter.cc:82
Visitor class which converts a symbolic Formula into ibex::ExprCtr.
Definition: ibex_converter.h:15
Sum type of symbolic::Expression and symbolic::Formula.
Definition: api.cc:9
~IbexConverter()
Destructor.
Definition: ibex_converter.cc:72
Represents a n-dimensional interval vector.
Definition: box.h:17
IbexConverter()=delete
Delete the default constructor.
This is the header file that we consolidate Drake&#39;s symbolic classes and expose them inside of dreal ...
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