ZonoOpt 2.3.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
164 bool is_empty() const;
165
170 bool is_single_valued() const;
171
178 Box intersect(const Box& other) const;
179
186 Box interval_hull(const Box& other) const;
187
194 bool contains(const Eigen::Vector<zono_float, -1>& v);
195
202 bool contains_set(const Box& other) const;
203
204 // operator overloading
205
211 Box operator+(const Box& other) const;
212
217 void operator+=(const Box& other);
218
225 Box operator+(const Eigen::Vector<zono_float, -1>& v) const;
226
227
232 void operator+=(const Eigen::Vector<zono_float, -1>& v);
233
240 friend Box operator+(const Eigen::Vector<zono_float, -1>& v, const Box& box);
241
247 Box operator-(const Box& other) const;
248
253 void operator-=(const Box& other);
254
260 Box operator-(const Eigen::Vector<zono_float, -1>& v) const;
261
266 void operator-=(const Eigen::Vector<zono_float, -1>& v);
267
274 friend Box operator-(const Eigen::Vector<zono_float, -1>& v, const Box& box);
275
281 Box operator*(const Box& other) const;
282
287 void operator*=(const Box& other);
288
294 Box operator*(zono_float alpha) const;
295
300 void operator*=(zono_float alpha);
301
308 friend Box operator*(zono_float alpha, const Box& box);
309
315 Box operator*(const Eigen::Vector<zono_float, -1>& v) const;
316
321 void operator*=(const Eigen::Vector<zono_float, -1>& v);
322
329 friend Box operator*(const Eigen::Vector<zono_float, -1>& v, const Box& box);
330
336 Box operator*(const Interval& interval) const;
337
342 void operator*=(const Interval& interval);
343
350 friend Box operator*(const Interval& interval, const Box& box);
351
357 friend Box operator*(const Eigen::SparseMatrix<zono_float, Eigen::RowMajor>& A, const Box& box);
358
364 friend Box operator*(const Eigen::Matrix<zono_float, -1, -1>& A, const Box& box);
365
371 Box operator/(const Box& other) const;
372
377 void operator/=(const Box& other);
378
384 Box operator/(zono_float alpha) const;
385
390 void operator/=(zono_float alpha);
391
398 friend Box operator/(zono_float alpha, const Box& box);
399
405 Box operator/(const Interval& interval) const;
406
411 void operator/=(const Interval& interval);
412
419 friend Box operator/(const Interval& interval, const Box& box);
420
425 Box operator-() const;
426
433 Box operator&(const Box& other) const;
434
441 Box operator|(const Box& other) const;
442
449 bool operator<=(const Box& other) const;
450
457 bool operator>=(const Box& other) const;
458
465 bool operator==(const Box& other) const;
466
467 // interval contractors
468
480 bool contract(const Eigen::SparseMatrix<zono_float, Eigen::RowMajor>& A, const Eigen::Vector<zono_float, -1>& b,
481 int iter);
482
496 bool contract_subset(const Eigen::SparseMatrix<zono_float, Eigen::RowMajor>& A_rm,
497 const Eigen::Vector<zono_float, -1>& b, int iter,
498 const Eigen::SparseMatrix<zono_float>& A, const std::set<int>& inds,
499 int tree_search_depth);
500
506 Box linear_map(const Eigen::Matrix<zono_float, -1, -1>& A) const;
507
513 Box linear_map(const Eigen::SparseMatrix<zono_float, Eigen::RowMajor>& A) const;
514
520 Interval dot(const Eigen::Vector<zono_float, -1>& x) const;
521
526 void permute(const Eigen::PermutationMatrix<Eigen::Dynamic, Eigen::Dynamic>& P);
527
532 std::string print() const;
533
540 friend std::ostream& operator<<(std::ostream& os, const Box& box);
541
542 protected:
543 // members
545 Eigen::Vector<zono_float, -1> x_lb;
546
548 Eigen::Vector<zono_float, -1> x_ub;
549
550 private:
551 // back end for contraction operator
552
561 virtual bool contract_helper(const Eigen::SparseMatrix<zono_float, Eigen::RowMajor>& A,
562 const Eigen::Vector<zono_float, -1>& b, const int iter,
563 const std::set<int>& constraints);
564
572 bool contract_forward(const Eigen::SparseMatrix<zono_float, Eigen::RowMajor>& A,
573 const Eigen::Vector<zono_float, -1>& b, const std::set<int>& constraints);
574
581 void contract_backward(const Eigen::SparseMatrix<zono_float, Eigen::RowMajor>& A,
582 const Eigen::Vector<zono_float, -1>& b, const std::set<int>& constraints);
583
594 static void get_vars_cons(const Eigen::SparseMatrix<zono_float>& A,
595 const Eigen::SparseMatrix<zono_float, Eigen::RowMajor>& A_rm,
596 std::set<int>& constraints, std::set<int>& vars, const std::set<int>& new_vars,
597 int depth, int max_depth);
598
599 private:
600 friend class MI_Box;
601 };
602
603 // mixed-integer box (some bounds are fixed)
610 class MI_Box final : public Box
611 {
612 public:
613 // constructors
614
618 MI_Box() = default;
619
627 MI_Box(const Eigen::Vector<zono_float, -1>& x_lb, const Eigen::Vector<zono_float, -1>& x_ub,
628 const std::pair<int, int>& idx_b, bool zero_one_form);
629
636 MI_Box(const std::vector<Interval>& intervals, const std::pair<int, int>& idx_b, bool zero_one_form);
637
638 // clone operation
639 Box* clone() const override;
640
641 // project
642 void project(Eigen::Ref<Eigen::Vector<zono_float, -1>> x) const override;
643
644 // get binary indices
649 const std::pair<int, int>& binary_indices() const { return idx_b; }
650
651 // get binary range
652 zono_float get_bin_low() const { return bin_low; }
653 zono_float get_bin_high() const { return bin_high; }
654
655 protected:
656 bool contract_helper(const Eigen::SparseMatrix<zono_float, Eigen::RowMajor>& A,
657 const Eigen::Vector<zono_float, -1>& b, const int iter,
658 const std::set<int>& constraints) override;
659
660 private:
662 std::pair<int, int> idx_b;
663 zono_float bin_low = zero, bin_high = one;
664 };
665
666
667
668}
669
670
671#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:538
std::string print() const
Print method.
Definition Box.cpp:559
void operator/=(const Box &other)
Elementwise division in-place.
Definition Box.cpp:370
bool operator==(const Box &other) const
Set equality operator.
Definition Box.cpp:455
void operator+=(const Box &other)
Elementwise addition in-place.
Definition Box.cpp:210
friend Box operator/(zono_float alpha, const Box &box)
Elementwise division with scalar.
Definition Box.cpp:390
Box linear_map(const Eigen::Matrix< zono_float, -1, -1 > &A) const
Linear map of box based on interval arithmetic.
Definition Box.cpp:496
Eigen::Vector< zono_float, -1 > x_ub
vector of upper bounds
Definition Box.hpp:548
bool contains_set(const Box &other) const
Check set containment.
Definition Box.cpp:186
Eigen::Vector< zono_float, -1 > x_lb
vector of lower bounds
Definition Box.hpp:545
bool contains(const Eigen::Vector< zono_float, -1 > &v)
Check vector continment.
Definition Box.cpp:174
Box intersect(const Box &other) const
Box intersection.
Definition Box.cpp:150
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:477
Box operator|(const Box &other) const
Box interval hull.
Definition Box.cpp:440
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:435
void permute(const Eigen::PermutationMatrix< Eigen::Dynamic, Eigen::Dynamic > &P)
Permutes in place using permutation matrix, i.e., [x] <- P*[x].
Definition Box.cpp:553
Box interval_hull(const Box &other) const
Box interval hull.
Definition Box.cpp:162
bool operator>=(const Box &other) const
Set containment operator.
Definition Box.cpp:450
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:570
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:279
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:460
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:264
void operator-=(const Box &other)
Elementwise subtraction in-place.
Definition Box.cpp:249
const Eigen::Vector< zono_float, -1 > & lower() const
Get lower bounds.
Definition Box.hpp:129
bool is_single_valued() const
Determine whether box is single-valued (i.e., all intervals have width 0 within numerical tolerance)
Definition Box.cpp:140
void operator*=(const Box &other)
Elementwise multiplication in-place.
Definition Box.cpp:269
friend Box operator+(const Eigen::Vector< zono_float, -1 > &v, const Box &box)
Elementwise addition with vector.
Definition Box.cpp:232
bool is_empty() const
Determine whether box is empty (any contained interval is empty)
Definition Box.cpp:130
bool operator<=(const Box &other) const
Set containment operator.
Definition Box.cpp:445
Interval class.
Definition Interval.hpp:48
Mixed-integer box.
Definition Box.hpp:611
Box * clone() const override
Clone operation.
Definition Box.cpp:707
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:733
zono_float get_bin_high() const
Definition Box.hpp:653
const std::pair< int, int > & binary_indices() const
Get binary indices.
Definition Box.hpp:649
zono_float get_bin_low() const
Definition Box.hpp:652
void project(Eigen::Ref< Eigen::Vector< zono_float, -1 > > x) const override
Projects vector onto the Box.
Definition Box.cpp:712
#define zono_float
Defines the floating-point type used in ZonoOpt.
Definition ZonoOpt.hpp:45
Definition ZonoOpt.hpp:58