ZonoOpt 2.4.1
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
58 Box(const Eigen::Vector<zono_float, -1>& x_lb, const Eigen::Vector<zono_float, -1>& x_ub);
59
65 std::vector<Interval> to_array() const;
66
67 // virtual destructor
71 virtual ~Box() = default;
72
73 // copy assignment
79 Box& operator=(const Box& other);
80
81 // copy constructor
86 Box(const Box& other);
87
88 // element-wise assignment, access
89
96 Interval get_element(int i) const;
97
98
104 void set_element(int i, const Interval& val);
105
110 size_t size() const;
111
112 // project onto box
113
119 virtual void project(Eigen::Ref<Eigen::Vector<zono_float, -1>> x) const;
120
125 virtual Box* clone() const;
126
127 // access bounds
132 const Eigen::Vector<zono_float, -1>& lower() const { return x_lb; }
133
138 const Eigen::Vector<zono_float, -1>& upper() const { return x_ub; }
139
140 // width of box
147 zono_float width() const;
148
155 Box radius() const;
156
161 Eigen::Vector<zono_float, -1> center() const;
162
167 bool is_empty() const;
168
173 bool is_single_valued() const;
174
182 Box intersect(const Box& other) const;
183
191 Box interval_hull(const Box& other) const;
192
200 bool contains(const Eigen::Vector<zono_float, -1>& v);
201
209 bool contains_set(const Box& other) const;
210
211 // operator overloading
212
219 Box operator+(const Box& other) const;
220
226 void operator+=(const Box& other);
227
235 Box operator+(const Eigen::Vector<zono_float, -1>& v) const;
236
237
243 void operator+=(const Eigen::Vector<zono_float, -1>& v);
244
252 friend Box operator+(const Eigen::Vector<zono_float, -1>& v, const Box& box);
253
260 Box operator-(const Box& other) const;
261
267 void operator-=(const Box& other);
268
275 Box operator-(const Eigen::Vector<zono_float, -1>& v) const;
276
282 void operator-=(const Eigen::Vector<zono_float, -1>& v);
283
291 friend Box operator-(const Eigen::Vector<zono_float, -1>& v, const Box& box);
292
299 Box operator*(const Box& other) const;
300
306 void operator*=(const Box& other);
307
313 Box operator*(zono_float alpha) const;
314
319 void operator*=(zono_float alpha);
320
327 friend Box operator*(zono_float alpha, const Box& box);
328
335 Box operator*(const Eigen::Vector<zono_float, -1>& v) const;
336
342 void operator*=(const Eigen::Vector<zono_float, -1>& v);
343
351 friend Box operator*(const Eigen::Vector<zono_float, -1>& v, const Box& box);
352
358 Box operator*(const Interval& interval) const;
359
364 void operator*=(const Interval& interval);
365
372 friend Box operator*(const Interval& interval, const Box& box);
373
379 friend Box operator*(const Eigen::SparseMatrix<zono_float, Eigen::RowMajor>& A, const Box& box);
380
386 friend Box operator*(const Eigen::Matrix<zono_float, -1, -1>& A, const Box& box);
387
394 Box operator/(const Box& other) const;
395
401 void operator/=(const Box& other);
402
408 Box operator/(zono_float alpha) const;
409
414 void operator/=(zono_float alpha);
415
422 friend Box operator/(zono_float alpha, const Box& box);
423
429 Box operator/(const Interval& interval) const;
430
435 void operator/=(const Interval& interval);
436
443 friend Box operator/(const Interval& interval, const Box& box);
444
449 Box operator-() const;
450
458 Box operator&(const Box& other) const;
459
467 Box operator|(const Box& other) const;
468
475 bool operator<=(const Box& other) const;
476
483 bool operator>=(const Box& other) const;
484
491 bool operator==(const Box& other) const;
492
493 // interval contractors
494
508 bool contract(const Eigen::SparseMatrix<zono_float, Eigen::RowMajor>& A, const Eigen::Vector<zono_float, -1>& b,
509 int iter);
510
526 bool contract_subset(const Eigen::SparseMatrix<zono_float, Eigen::RowMajor>& A_rm,
527 const Eigen::Vector<zono_float, -1>& b, int iter,
528 const Eigen::SparseMatrix<zono_float>& A, const std::set<int>& inds,
529 int tree_search_depth);
530
537 Box linear_map(const Eigen::Matrix<zono_float, -1, -1>& A) const;
538
545 Box linear_map(const Eigen::SparseMatrix<zono_float, Eigen::RowMajor>& A) const;
546
553 Interval dot(const Eigen::Vector<zono_float, -1>& x) const;
554
559 void permute(const Eigen::PermutationMatrix<Eigen::Dynamic, Eigen::Dynamic>& P);
560
565 std::string print() const;
566
573 friend std::ostream& operator<<(std::ostream& os, const Box& box);
574
575 protected:
576 // members
578 Eigen::Vector<zono_float, -1> x_lb;
579
581 Eigen::Vector<zono_float, -1> x_ub;
582
583 private:
584 // back end for contraction operator
585
594 virtual bool contract_helper(const Eigen::SparseMatrix<zono_float, Eigen::RowMajor>& A,
595 const Eigen::Vector<zono_float, -1>& b, const int iter,
596 const std::set<int>& constraints);
597
605 bool contract_forward(const Eigen::SparseMatrix<zono_float, Eigen::RowMajor>& A,
606 const Eigen::Vector<zono_float, -1>& b, const std::set<int>& constraints);
607
614 void contract_backward(const Eigen::SparseMatrix<zono_float, Eigen::RowMajor>& A,
615 const Eigen::Vector<zono_float, -1>& b, const std::set<int>& constraints);
616
627 static void get_vars_cons(const Eigen::SparseMatrix<zono_float>& A,
628 const Eigen::SparseMatrix<zono_float, Eigen::RowMajor>& A_rm,
629 std::set<int>& constraints, std::set<int>& vars, const std::set<int>& new_vars,
630 int depth, int max_depth);
631
632 private:
633 friend class MI_Box;
634 };
635
636 // mixed-integer box (some bounds are fixed)
643 class MI_Box final : public Box
644 {
645 public:
646 // constructors
647
651 MI_Box() = default;
652
662 MI_Box(const Eigen::Vector<zono_float, -1>& x_lb, const Eigen::Vector<zono_float, -1>& x_ub,
663 const std::pair<int, int>& idx_b, bool zero_one_form);
664
672 MI_Box(const std::vector<Interval>& intervals, const std::pair<int, int>& idx_b, bool zero_one_form);
673
674 // clone operation
675 Box* clone() const override;
676
677 // project
678 void project(Eigen::Ref<Eigen::Vector<zono_float, -1>> x) const override;
679
680 // get binary indices
685 const std::pair<int, int>& binary_indices() const { return idx_b; }
686
687 // get binary range
688 zono_float get_bin_low() const { return bin_low; }
689 zono_float get_bin_high() const { return bin_high; }
690
691 protected:
692 bool contract_helper(const Eigen::SparseMatrix<zono_float, Eigen::RowMajor>& A,
693 const Eigen::Vector<zono_float, -1>& b, const int iter,
694 const std::set<int>& constraints) override;
695
696 private:
698 std::pair<int, int> idx_b;
699 zono_float bin_low = zero, bin_high = one;
700 };
701
702
703
704}
705
706
707#endif
Eigen::SparseMatrix< double, Eigen::RowMajor > A_rm
Definition GurobiSolver.cpp:369
Interval class.
std::vector< ScopedVar > vars
Definition SCIPSolver.cpp:250
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:514
std::string print() const
Print method.
Definition Box.cpp:535
void operator/=(const Box &other)
Elementwise division in-place.
Definition Box.cpp:346
bool operator==(const Box &other) const
Set equality operator.
Definition Box.cpp:431
void operator+=(const Box &other)
Elementwise addition in-place.
Definition Box.cpp:186
friend Box operator/(zono_float alpha, const Box &box)
Elementwise division with scalar.
Definition Box.cpp:366
Box linear_map(const Eigen::Matrix< zono_float, -1, -1 > &A) const
Linear map of box based on interval arithmetic.
Definition Box.cpp:472
Eigen::Vector< zono_float, -1 > x_ub
vector of upper bounds
Definition Box.hpp:581
bool contains_set(const Box &other) const
Check set containment.
Definition Box.cpp:162
Eigen::Vector< zono_float, -1 > x_lb
vector of lower bounds
Definition Box.hpp:578
bool contains(const Eigen::Vector< zono_float, -1 > &v)
Check vector continment.
Definition Box.cpp:150
Box intersect(const Box &other) const
Box intersection.
Definition Box.cpp:126
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:138
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:453
Box operator|(const Box &other) const
Box interval hull.
Definition Box.cpp:416
Eigen::Vector< zono_float, -1 > center() const
Get center of box.
Definition Box.cpp:105
Box operator&(const Box &other) const
Box intersection.
Definition Box.cpp:411
void permute(const Eigen::PermutationMatrix< Eigen::Dynamic, Eigen::Dynamic > &P)
Permutes in place using permutation matrix, i.e., [x] <- P*[x].
Definition Box.cpp:529
Box interval_hull(const Box &other) const
Box interval hull.
Definition Box.cpp:138
bool operator>=(const Box &other) const
Set containment operator.
Definition Box.cpp:426
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:546
Box radius() const
Get radius of box.
Definition Box.cpp:110
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:255
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:436
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:240
void operator-=(const Box &other)
Elementwise subtraction in-place.
Definition Box.cpp:225
const Eigen::Vector< zono_float, -1 > & lower() const
Get lower bounds.
Definition Box.hpp:132
bool is_single_valued() const
Determine whether box is single-valued (i.e., all intervals have width 0 within numerical tolerance)
Definition Box.cpp:121
void operator*=(const Box &other)
Elementwise multiplication in-place.
Definition Box.cpp:245
friend Box operator+(const Eigen::Vector< zono_float, -1 > &v, const Box &box)
Elementwise addition with vector.
Definition Box.cpp:208
bool is_empty() const
Determine whether box is empty (any contained interval is empty)
Definition Box.cpp:116
bool operator<=(const Box &other) const
Set containment operator.
Definition Box.cpp:421
Interval class.
Definition Interval.hpp:48
Mixed-integer box.
Definition Box.hpp:644
Box * clone() const override
Clone operation.
Definition Box.cpp:694
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:720
zono_float get_bin_high() const
Definition Box.hpp:689
const std::pair< int, int > & binary_indices() const
Get binary indices.
Definition Box.hpp:685
zono_float get_bin_low() const
Definition Box.hpp:688
void project(Eigen::Ref< Eigen::Vector< zono_float, -1 > > x) const override
Projects vector onto the Box.
Definition Box.cpp:699
#define zono_float
Defines the floating-point type used in ZonoOpt.
Definition ZonoOpt.hpp:45
Definition ZonoOpt.hpp:58