ZonoOpt 2.2.0
Loading...
Searching...
No Matches
Box.hpp
Go to the documentation of this file.
1#ifndef ZONOOPT_BOX_HPP_
2#define ZONOOPT_BOX_HPP_
3
15#include "Interval.hpp"
16
17namespace ZonoOpt
18{
19 using namespace detail;
20
24 class Box
25 {
26 public:
27 // constructors
28
32 Box() = default;
33
38 explicit Box(const size_t size);
39
44 explicit Box(const std::vector<Interval>& vals);
45
50 explicit Box(const Eigen::Vector<Interval, -1>& vals);
51
57 Box(const Eigen::Vector<zono_float, -1>& x_lb, const Eigen::Vector<zono_float, -1>& x_ub);
58
64 std::vector<Interval> to_array() const;
65
66 // virtual destructor
70 virtual ~Box() = default;
71
72 // copy assignment
78 Box& operator=(const Box& other);
79
80 // copy constructor
85 Box(const Box& other);
86
87 // element-wise assignment, access
88
94 Interval get_element(int i) const;
95
96
102 void set_element(int i, const Interval& val);
103
108 size_t size() const;
109
110 // project onto box
111
116 virtual void project(Eigen::Ref<Eigen::Vector<zono_float, -1>> x) const;
117
122 virtual Box* clone() const;
123
124 // access bounds
129 const Eigen::Vector<zono_float, -1>& lower() const { return x_lb; }
130
135 const Eigen::Vector<zono_float, -1>& upper() const { return x_ub; }
136
137 // width of box
144 zono_float width() const;
145
152 Box radius() const;
153
158 Eigen::Vector<zono_float, -1> center() const;
159
166 Box intersect(const Box& other) const;
167
174 Box interval_hull(const Box& other) const;
175
182 bool contains(const Eigen::Vector<zono_float, -1>& v);
183
190 bool contains_set(const Box& other) const;
191
192 // operator overloading
193
199 Box operator+(const Box& other) const;
200
205 void operator+=(const Box& other);
206
213 Box operator+(const Eigen::Vector<zono_float, -1>& v) const;
214
215
220 void operator+=(const Eigen::Vector<zono_float, -1>& v);
221
228 friend Box operator+(const Eigen::Vector<zono_float, -1>& v, const Box& box);
229
235 Box operator-(const Box& other) const;
236
241 void operator-=(const Box& other);
242
248 Box operator-(const Eigen::Vector<zono_float, -1>& v) const;
249
254 void operator-=(const Eigen::Vector<zono_float, -1>& v);
255
262 friend Box operator-(const Eigen::Vector<zono_float, -1>& v, const Box& box);
263
269 Box operator*(const Box& other) const;
270
275 void operator*=(const Box& other);
276
282 Box operator*(zono_float alpha) const;
283
288 void operator*=(zono_float alpha);
289
296 friend Box operator*(zono_float alpha, const Box& box);
297
303 Box operator*(const Eigen::Vector<zono_float, -1>& v) const;
304
309 void operator*=(const Eigen::Vector<zono_float, -1>& v);
310
317 friend Box operator*(const Eigen::Vector<zono_float, -1>& v, const Box& box);
318
324 Box operator*(const Interval& interval) const;
325
330 void operator*=(const Interval& interval);
331
338 friend Box operator*(const Interval& interval, const Box& box);
339
345 friend Box operator*(const Eigen::SparseMatrix<zono_float, Eigen::RowMajor>& A, const Box& box);
346
352 friend Box operator*(const Eigen::Matrix<zono_float, -1, -1>& A, const Box& box);
353
359 Box operator/(const Box& other) const;
360
365 void operator/=(const Box& other);
366
372 Box operator/(zono_float alpha) const;
373
378 void operator/=(zono_float alpha);
379
386 friend Box operator/(zono_float alpha, const Box& box);
387
393 Box operator/(const Interval& interval) const;
394
399 void operator/=(const Interval& interval);
400
407 friend Box operator/(const Interval& interval, const Box& box);
408
413 Box operator-() const;
414
421 Box operator&(const Box& other) const;
422
429 Box operator|(const Box& other) const;
430
437 bool operator<=(const Box& other) const;
438
445 bool operator>=(const Box& other) const;
446
453 bool operator==(const Box& other) const;
454
455 // interval contractors
456
468 bool contract(const Eigen::SparseMatrix<zono_float, Eigen::RowMajor>& A, const Eigen::Vector<zono_float, -1>& b,
469 int iter);
470
484 bool contract_subset(const Eigen::SparseMatrix<zono_float, Eigen::RowMajor>& A_rm,
485 const Eigen::Vector<zono_float, -1>& b, int iter,
486 const Eigen::SparseMatrix<zono_float>& A, const std::set<int>& inds,
487 int tree_search_depth);
488
494 Box linear_map(const Eigen::Matrix<zono_float, -1, -1>& A) const;
495
501 Box linear_map(const Eigen::SparseMatrix<zono_float, Eigen::RowMajor>& A) const;
502
508 Interval dot(const Eigen::Vector<zono_float, -1>& x) const;
509
514 void permute(const Eigen::PermutationMatrix<Eigen::Dynamic, Eigen::Dynamic>& P);
515
520 std::string print() const;
521
528 friend std::ostream& operator<<(std::ostream& os, const Box& box);
529
530 protected:
531 // members
533 Eigen::Vector<zono_float, -1> x_lb;
534
536 Eigen::Vector<zono_float, -1> x_ub;
537
538 private:
539 // back end for contraction operator
540
549 virtual bool contract_helper(const Eigen::SparseMatrix<zono_float, Eigen::RowMajor>& A,
550 const Eigen::Vector<zono_float, -1>& b, const int iter,
551 const std::set<int>& constraints);
552
560 bool contract_forward(const Eigen::SparseMatrix<zono_float, Eigen::RowMajor>& A,
561 const Eigen::Vector<zono_float, -1>& b, const std::set<int>& constraints);
562
569 void contract_backward(const Eigen::SparseMatrix<zono_float, Eigen::RowMajor>& A,
570 const Eigen::Vector<zono_float, -1>& b, const std::set<int>& constraints);
571
582 static void get_vars_cons(const Eigen::SparseMatrix<zono_float>& A,
583 const Eigen::SparseMatrix<zono_float, Eigen::RowMajor>& A_rm,
584 std::set<int>& constraints, std::set<int>& vars, const std::set<int>& new_vars,
585 int depth, int max_depth);
586
587 private:
588 friend class MI_Box;
589 };
590
591 // mixed-integer box (some bounds are fixed)
598 class MI_Box final : public Box
599 {
600 public:
601 // constructors
602
606 MI_Box() = default;
607
615 MI_Box(const Eigen::Vector<zono_float, -1>& x_lb, const Eigen::Vector<zono_float, -1>& x_ub,
616 const std::pair<int, int>& idx_b, bool zero_one_form);
617
618 // clone operation
619 Box* clone() const override;
620
621 // project
622 void project(Eigen::Ref<Eigen::Vector<zono_float, -1>> x) const override;
623
624 // get binary indices
629 const std::pair<int, int>& binary_indices() const { return idx_b; }
630
631 protected:
632 bool contract_helper(const Eigen::SparseMatrix<zono_float, Eigen::RowMajor>& A,
633 const Eigen::Vector<zono_float, -1>& b, const int iter,
634 const std::set<int>& constraints) override;
635
636 private:
638 std::pair<int, int> idx_b;
639 zono_float bin_low = zero, bin_high = one;
640 };
641
642
643
644}
645
646
647#endif
Interval class.
Box (i.e., interval vector) class.
Definition Box.hpp:25
std::vector< Interval > to_array() const
Convert to vector of intervals.
Definition Box.cpp:60
Interval dot(const Eigen::Vector< zono_float, -1 > &x) const
Linear map with vector.
Definition Box.cpp:518
std::string print() const
Print method.
Definition Box.cpp:539
void operator/=(const Box &other)
Elementwise division in-place.
Definition Box.cpp:350
bool operator==(const Box &other) const
Set equality operator.
Definition Box.cpp:435
void operator+=(const Box &other)
Elementwise addition in-place.
Definition Box.cpp:190
friend Box operator/(zono_float alpha, const Box &box)
Elementwise division with scalar.
Definition Box.cpp:370
Box linear_map(const Eigen::Matrix< zono_float, -1, -1 > &A) const
Linear map of box based on interval arithmetic.
Definition Box.cpp:476
Eigen::Vector< zono_float, -1 > x_ub
vector of upper bounds
Definition Box.hpp:536
bool contains_set(const Box &other) const
Check set containment.
Definition Box.cpp:166
Eigen::Vector< zono_float, -1 > x_lb
vector of lower bounds
Definition Box.hpp:533
bool contains(const Eigen::Vector< zono_float, -1 > &v)
Check vector continment.
Definition Box.cpp:154
Box intersect(const Box &other) const
Box intersection.
Definition Box.cpp:130
Box & operator=(const Box &other)
Copy assignment.
Definition Box.cpp:44
Box()=default
Default constructor.
virtual void project(Eigen::Ref< Eigen::Vector< zono_float, -1 > > x) const
Projects vector onto the Box.
Definition Box.cpp:88
const Eigen::Vector< zono_float, -1 > & upper() const
Get upper bounds.
Definition Box.hpp:135
bool contract_subset(const Eigen::SparseMatrix< zono_float, Eigen::RowMajor > &A_rm, const Eigen::Vector< zono_float, -1 > &b, int iter, const Eigen::SparseMatrix< zono_float > &A, const std::set< int > &inds, int tree_search_depth)
Interval contractor over a subset of the dimensions of the box.
Definition Box.cpp:457
Box operator|(const Box &other) const
Box interval hull.
Definition Box.cpp:420
Eigen::Vector< zono_float, -1 > center() const
Get center of box.
Definition Box.cpp:110
Box operator&(const Box &other) const
Box intersection.
Definition Box.cpp:415
void permute(const Eigen::PermutationMatrix< Eigen::Dynamic, Eigen::Dynamic > &P)
Permutes in place using permutation matrix, i.e., [x] <- P*[x].
Definition Box.cpp:533
Box interval_hull(const Box &other) const
Box interval hull.
Definition Box.cpp:142
bool operator>=(const Box &other) const
Set containment operator.
Definition Box.cpp:430
size_t size() const
Get size of Box object.
Definition Box.cpp:83
friend std::ostream & operator<<(std::ostream &os, const Box &box)
print to ostream
Definition Box.cpp:550
Box radius() const
Get radius of box.
Definition Box.cpp:120
void set_element(int i, const Interval &val)
Element-wise assignment.
Definition Box.cpp:77
Interval get_element(int i) const
Element-wise access.
Definition Box.cpp:70
virtual Box * clone() const
Clone operation.
Definition Box.cpp:95
friend Box operator*(zono_float alpha, const Box &box)
Elementwise multiplication with scalar.
Definition Box.cpp:259
zono_float width() const
Get width of box.
Definition Box.cpp:100
bool contract(const Eigen::SparseMatrix< zono_float, Eigen::RowMajor > &A, const Eigen::Vector< zono_float, -1 > &b, int iter)
Interval contractor.
Definition Box.cpp:440
virtual ~Box()=default
Virtual destructor.
friend Box operator-(const Eigen::Vector< zono_float, -1 > &v, const Box &box)
Elementwise subtraction with vector.
Definition Box.cpp:244
void operator-=(const Box &other)
Elementwise subtraction in-place.
Definition Box.cpp:229
const Eigen::Vector< zono_float, -1 > & lower() const
Get lower bounds.
Definition Box.hpp:129
void operator*=(const Box &other)
Elementwise multiplication in-place.
Definition Box.cpp:249
friend Box operator+(const Eigen::Vector< zono_float, -1 > &v, const Box &box)
Elementwise addition with vector.
Definition Box.cpp:212
bool operator<=(const Box &other) const
Set containment operator.
Definition Box.cpp:425
Interval class.
Definition Interval.hpp:48
Mixed-integer box.
Definition Box.hpp:599
Box * clone() const override
Clone operation.
Definition Box.cpp:676
MI_Box()=default
default constructor
bool contract_helper(const Eigen::SparseMatrix< zono_float, Eigen::RowMajor > &A, const Eigen::Vector< zono_float, -1 > &b, const int iter, const std::set< int > &constraints) override
Back-end for contractor.
Definition Box.cpp:702
const std::pair< int, int > & binary_indices() const
Get binary indices.
Definition Box.hpp:629
void project(Eigen::Ref< Eigen::Vector< zono_float, -1 > > x) const override
Projects vector onto the Box.
Definition Box.cpp:681
#define zono_float
Defines the floating-point type used in ZonoOpt.
Definition ZonoOpt.hpp:45
Definition ZonoOpt.hpp:58