dReal4
box.h
1 #pragma once
2 
3 #include <iostream>
4 #include <memory>
5 #include <unordered_map>
6 #include <utility>
7 #include <vector>
8 
9 #include "./ibex.h"
10 
12 
13 namespace dreal {
14 
15 /// Represents a n-dimensional interval vector. This is a wrapper of
16 /// ibex::IntervalVector.
17 class Box {
18  public:
19  using Interval = ibex::Interval;
20  using IntervalVector = ibex::IntervalVector;
21 
22  /// Constructs a zero-dimensional box.
23  Box();
24 
25  /// Constructs a box from @p variables.
26  explicit Box(const std::vector<Variable>& variables);
27 
28  /// Default copy constructor.
29  Box(const Box&) = default;
30 
31  /// Default move constructor.
32  Box(Box&&) = default;
33 
34  /// Default copy assign operator.
35  Box& operator=(const Box&) = default;
36 
37  /// Default move assign operator.
38  Box& operator=(Box&&) = default;
39 
40  /// Default destructor.
41  ~Box() = default;
42 
43  /// Adds @p v to the box.
44  void Add(const Variable& v);
45 
46  /// Adds @p v to the box and sets its domain using @p lb and @p ub.
47  void Add(const Variable& v, double lb, double ub);
48 
49  /// Checks if this box is empty.
50  bool empty() const;
51 
52  /// Make this box empty.
53  void set_empty();
54 
55  /// Returns the size of the box.
56  int size() const;
57 
58  /// Returns @p i -th interval in the box.
59  Interval& operator[](int i);
60 
61  /// Returns an interval associated with @p var.
62  Interval& operator[](const Variable& var);
63 
64  /// Returns @p i -th interval in the box.
65  const Interval& operator[](int i) const;
66 
67  /// Returns an interval associated with @p var.
68  const Interval& operator[](const Variable& var) const;
69 
70  /// Returns the variables in the box.
71  const std::vector<Variable>& variables() const;
72 
73  /// Returns i-th variable in the box.
74  const Variable& variable(int i) const;
75 
76  /// Checks if this box has @p var.
77  bool has_variable(const Variable& var) const;
78 
79  /// Returns the interval vector of the box.
80  const IntervalVector& interval_vector() const;
81 
82  /// Returns the interval vector of the box.
83  IntervalVector& mutable_interval_vector();
84 
85  /// Returns the index associated with @p var.
86  int index(const Variable& var) const;
87 
88  /// Returns the max diameter of the box and the associated index .
89  std::pair<double, int> MaxDiam() const;
90 
91  /// Bisects the box at @p i -th dimension.
92  /// @throws std::runtime if @p i -th dimension is not bisectable.
93  std::pair<Box, Box> bisect(int i) const;
94 
95  /// Bisects the box at @p the dimension represented by @p var.
96  /// @throws std::runtime if @p i -th dimension is not bisectable.
97  std::pair<Box, Box> bisect(const Variable& var) const;
98 
99  /// Updates the current box by taking union with @p b.
100  ///
101  /// @pre variables() == b.variables().
102  Box& InplaceUnion(const Box& b);
103 
104  private:
105  /// Bisects the box at @p i -th dimension.
106  /// @pre i-th variable is bisectable.
107  /// @pre i-th variable is of integer type.
108  std::pair<Box, Box> bisect_int(int i) const;
109 
110  /// Bisects the box at @p i -th dimension.
111  /// @pre i-th variable is bisectable.
112  /// @pre i-th variable is of continuous type.
113  std::pair<Box, Box> bisect_continuous(int i) const;
114 
115  std::shared_ptr<std::vector<Variable>> variables_;
116 
117  ibex::IntervalVector values_;
118 
119  std::shared_ptr<std::unordered_map<Variable, int, hash_value<Variable>>>
120  var_to_idx_;
121 
122  std::shared_ptr<std::unordered_map<int, Variable>> idx_to_var_;
123 
124  friend std::ostream& operator<<(std::ostream& os, const Box& box);
125 };
126 
127 std::ostream& operator<<(std::ostream& os, const Box& box);
128 
129 bool operator==(const Box& b1, const Box& b2);
130 
131 bool operator!=(const Box& b1, const Box& b2);
132 
133 std::ostream& DisplayDiff(std::ostream& os,
134  const std::vector<Variable>& variables,
135  const Box::IntervalVector& old_iv,
136  const Box::IntervalVector& new_iv);
137 
138 } // namespace dreal
Sum type of symbolic::Expression and symbolic::Formula.
Definition: api.cc:9
int index(const Variable &var) const
Returns the index associated with var.
Definition: box.cc:129
Box & InplaceUnion(const Box &b)
Updates the current box by taking union with b.
Definition: box.cc:208
const IntervalVector & interval_vector() const
Returns the interval vector of the box.
Definition: box.cc:131
Box()
Constructs a zero-dimensional box.
Definition: box.cc:29
Box & operator=(const Box &)=default
Default copy assign operator.
Represents a symbolic variable.
Definition: symbolic_variable.h:16
Interval & operator[](int i)
Returns i -th interval in the box.
Definition: box.cc:106
std::pair< Box, Box > bisect(int i) const
Bisects the box at i -th dimension.
Definition: box.cc:147
const Variable & variable(int i) const
Returns i-th variable in the box.
Definition: box.cc:123
bool empty() const
Checks if this box is empty.
Definition: box.cc:100
std::pair< double, int > MaxDiam() const
Returns the max diameter of the box and the associated index .
Definition: box.cc:134
void set_empty()
Make this box empty.
Definition: box.cc:102
void Add(const Variable &v)
Adds v to the box.
Definition: box.cc:51
Represents a n-dimensional interval vector.
Definition: box.h:17
bool has_variable(const Variable &var) const
Checks if this box has var.
Definition: box.cc:125
int size() const
Returns the size of the box.
Definition: box.cc:104
~Box()=default
Default destructor.
This is the header file that we consolidate Drake&#39;s symbolic classes and expose them inside of dreal ...
IntervalVector & mutable_interval_vector()
Returns the interval vector of the box.
Definition: box.cc:132
const std::vector< Variable > & variables() const
Returns the variables in the box.
Definition: box.cc:121