ZonoOpt 2.5.0
Loading...
Searching...
No Matches
Box.hpp
Go to the documentation of this file.
1#pragma once
2
14#include <vector>
15#include <set>
16#include <utility>
17#include <ostream>
18
19#include "Eigen/Dense"
20#include "Eigen/Sparse"
21
22#include "Interval.hpp"
23#include "Defines.hpp"
24
25namespace ZonoOpt
26{
27 using namespace detail;
28
29 namespace detail
30 {
31 class MI_Box;
32 }
33
45 class Box
46 {
47 public:
48 // constructors
49
53 Box() = default;
54
59 explicit Box(const size_t size);
60
65 explicit Box(const std::vector<Interval>& vals);
66
71 explicit Box(const Eigen::Vector<Interval, -1>& vals);
72
79 Box(const Eigen::Vector<zono_float, -1>& x_lb, const Eigen::Vector<zono_float, -1>& x_ub);
80
86 std::vector<Interval> to_array() const;
87
88 // virtual destructor
92 virtual ~Box() = default;
93
94 // copy assignment
100 Box& operator=(const Box& other);
101
102 // copy constructor
107 Box(const Box& other);
108
109 // element-wise assignment, access
110
117 Interval get_element(int i) const;
118
119
125 void set_element(int i, const Interval& val);
126
131 size_t size() const;
132
133 // project onto box
134
140 virtual void project(Eigen::Ref<Eigen::Vector<zono_float, -1>> x) const;
141
146 virtual Box* clone() const;
147
148 // access bounds
153 const Eigen::Vector<zono_float, -1>& lower() const { return x_lb; }
154
159 const Eigen::Vector<zono_float, -1>& upper() const { return x_ub; }
160
161 // width of box
168 zono_float width() const;
169
176 Box radius() const;
177
182 Eigen::Vector<zono_float, -1> center() const;
183
188 bool is_empty() const;
189
194 bool is_single_valued() const;
195
203 Box intersect(const Box& other) const;
204
212 Box interval_hull(const Box& other) const;
213
225 Box intersection_over_dims(const Box& other, const std::vector<int>& dims) const;
226
234 bool contains(const Eigen::Vector<zono_float, -1>& v);
235
243 bool contains_set(const Box& other) const;
244
254 Box cartesian_product(const Box& other) const;
255
266 Box minkowski_sum(const Box& other) const;
267
279 Box pontry_diff(const Box& other) const;
280
291 Box project_onto_dims(const std::vector<int>& dims) const;
292
293 // operator overloading
294
301 Box operator+(const Box& other) const;
302
308 void operator+=(const Box& other);
309
317 Box operator+(const Eigen::Vector<zono_float, -1>& v) const;
318
319
325 void operator+=(const Eigen::Vector<zono_float, -1>& v);
326
334 friend Box operator+(const Eigen::Vector<zono_float, -1>& v, const Box& box);
335
342 Box operator-(const Box& other) const;
343
349 void operator-=(const Box& other);
350
357 Box operator-(const Eigen::Vector<zono_float, -1>& v) const;
358
364 void operator-=(const Eigen::Vector<zono_float, -1>& v);
365
373 friend Box operator-(const Eigen::Vector<zono_float, -1>& v, const Box& box);
374
381 Box operator*(const Box& other) const;
382
388 void operator*=(const Box& other);
389
395 Box operator*(zono_float alpha) const;
396
401 void operator*=(zono_float alpha);
402
409 friend Box operator*(zono_float alpha, const Box& box);
410
417 Box operator*(const Eigen::Vector<zono_float, -1>& v) const;
418
424 void operator*=(const Eigen::Vector<zono_float, -1>& v);
425
433 friend Box operator*(const Eigen::Vector<zono_float, -1>& v, const Box& box);
434
440 Box operator*(const Interval& interval) const;
441
446 void operator*=(const Interval& interval);
447
454 friend Box operator*(const Interval& interval, const Box& box);
455
461 friend Box operator*(const Eigen::SparseMatrix<zono_float, Eigen::RowMajor>& A, const Box& box);
462
468 friend Box operator*(const Eigen::Matrix<zono_float, -1, -1>& A, const Box& box);
469
476 Box operator/(const Box& other) const;
477
483 void operator/=(const Box& other);
484
490 Box operator/(zono_float alpha) const;
491
496 void operator/=(zono_float alpha);
497
504 friend Box operator/(zono_float alpha, const Box& box);
505
511 Box operator/(const Interval& interval) const;
512
517 void operator/=(const Interval& interval);
518
525 friend Box operator/(const Interval& interval, const Box& box);
526
531 Box operator-() const;
532
540 Box operator&(const Box& other) const;
541
549 Box operator|(const Box& other) const;
550
557 bool operator<=(const Box& other) const;
558
565 bool operator>=(const Box& other) const;
566
573 bool operator==(const Box& other) const;
574
575 // interval contractors
576
590 bool contract(const Eigen::SparseMatrix<zono_float, Eigen::RowMajor>& A, const Eigen::Vector<zono_float, -1>& b,
591 int iter);
592
608 bool contract_subset(const Eigen::SparseMatrix<zono_float, Eigen::RowMajor>& A_rm,
609 const Eigen::Vector<zono_float, -1>& b, int iter,
610 const Eigen::SparseMatrix<zono_float>& A, const std::set<int>& inds,
611 int tree_search_depth);
612
619 Box linear_map(const Eigen::Matrix<zono_float, -1, -1>& A) const;
620
627 Box linear_map(const Eigen::SparseMatrix<zono_float, Eigen::RowMajor>& A) const;
628
635 Box linear_map(const Eigen::SparseMatrix<zono_float>& A) const;
636
644 Box affine_map(const Eigen::Matrix<zono_float, -1, -1>& R,
645 const Eigen::Vector<zono_float, -1>& s = Eigen::Vector<zono_float, -1>()) const;
646
654 Box affine_map(const Eigen::SparseMatrix<zono_float, Eigen::RowMajor>& R,
655 const Eigen::Vector<zono_float, -1>& s = Eigen::Vector<zono_float, -1>()) const;
656
664 Box affine_map(const Eigen::SparseMatrix<zono_float>& R,
665 const Eigen::Vector<zono_float, -1>& s = Eigen::Vector<zono_float, -1>()) const;
666
673 Interval dot(const Eigen::Vector<zono_float, -1>& x) const;
674
687 zono_float support(const Eigen::Vector<zono_float, -1>& d) const;
688
693 void permute(const Eigen::PermutationMatrix<Eigen::Dynamic, Eigen::Dynamic>& P);
694
699 std::string print() const;
700
707 friend std::ostream& operator<<(std::ostream& os, const Box& box);
708
709 protected:
710 // members
712 Eigen::Vector<zono_float, -1> x_lb;
713
715 Eigen::Vector<zono_float, -1> x_ub;
716
717 private:
718 // back end for contraction operator
719
728 virtual bool contract_helper(const Eigen::SparseMatrix<zono_float, Eigen::RowMajor>& A,
729 const Eigen::Vector<zono_float, -1>& b, const int iter,
730 const std::set<int>& constraints);
731
739 bool contract_forward(const Eigen::SparseMatrix<zono_float, Eigen::RowMajor>& A,
740 const Eigen::Vector<zono_float, -1>& b, const std::set<int>& constraints);
741
748 void contract_backward(const Eigen::SparseMatrix<zono_float, Eigen::RowMajor>& A,
749 const Eigen::Vector<zono_float, -1>& b, const std::set<int>& constraints);
750
761 static void get_vars_cons(const Eigen::SparseMatrix<zono_float>& A,
762 const Eigen::SparseMatrix<zono_float, Eigen::RowMajor>& A_rm,
763 std::set<int>& constraints, std::set<int>& vars, const std::set<int>& new_vars,
764 int depth, int max_depth);
765
766 private:
767 friend class detail::MI_Box;
768 };
769
770 namespace detail
771 {
772
773 // mixed-integer box (some bounds are fixed)
784 class MI_Box final : public Box
785 {
786 public:
787 // constructors
788
792 MI_Box() = default;
793
803 MI_Box(const Eigen::Vector<zono_float, -1>& x_lb, const Eigen::Vector<zono_float, -1>& x_ub,
804 const std::pair<int, int>& idx_b, bool zero_one_form);
805
813 MI_Box(const std::vector<Interval>& intervals, const std::pair<int, int>& idx_b, bool zero_one_form);
814
815 // clone operation
816 Box* clone() const override;
817
818 // project
819 void project(Eigen::Ref<Eigen::Vector<zono_float, -1>> x) const override;
820
821 // get binary indices
826 const std::pair<int, int>& binary_indices() const { return idx_b; }
827
828 // get binary range
829 zono_float get_bin_low() const { return bin_low; }
830 zono_float get_bin_high() const { return bin_high; }
831
832 protected:
833 bool contract_helper(const Eigen::SparseMatrix<zono_float, Eigen::RowMajor>& A,
834 const Eigen::Vector<zono_float, -1>& b, const int iter,
835 const std::set<int>& constraints) override;
836
837 private:
839 std::pair<int, int> idx_b;
840 zono_float bin_low = zero, bin_high = one;
841 };
842
843 } // namespace detail
844
845 // forward declarations
846
858 Box cartesian_product(const Box& Z1, const Box& Z2);
859
870 Box minkowski_sum(const Box& Z1, const Box& Z2);
871
882 Box pontry_diff(const Box& Z1, const Box& Z2);
883
894 Box project_onto_dims(const Box& Z, const std::vector<int>& dims);
895
907 Box affine_map(const Box& Z, const Eigen::SparseMatrix<zono_float>& R,
908 const Eigen::Vector<zono_float, -1>& s = Eigen::Vector<zono_float, -1>());
909
920 Box interval_hull(const Box& Z1, const Box& Z2);
921
931 Box interval_hull(const std::vector<Box>& boxes);
932
951 Box intersection(const Box& Z1, const Box& Z2,
952 const Eigen::SparseMatrix<zono_float>& R = Eigen::SparseMatrix<zono_float>(),
953 int contractor_iter = 10);
954
967 Box intersection_over_dims(const Box& Z1, const Box& Z2, const std::vector<int>& dims);
968}
Defines for ZonoOpt library.
Eigen::SparseMatrix< double, Eigen::RowMajor > A_rm
Definition GurobiSolver.cpp:370
Interval class.
std::vector< ScopedVar > vars
Definition SCIPSolver.cpp:250
Box (i.e., interval vector) class.
Definition Box.hpp:46
zono_float support(const Eigen::Vector< zono_float, -1 > &d) const
Computes the support function of the box in the direction d.
Definition Box.cpp:701
std::vector< Interval > to_array() const
Convert to vector of intervals.
Definition Box.cpp:116
Interval dot(const Eigen::Vector< zono_float, -1 > &x) const
Linear map with vector.
Definition Box.cpp:686
std::string print() const
Print method.
Definition Box.cpp:717
void operator/=(const Box &other)
Elementwise division in-place.
Definition Box.cpp:480
bool operator==(const Box &other) const
Set equality operator.
Definition Box.cpp:565
void operator+=(const Box &other)
Elementwise addition in-place.
Definition Box.cpp:320
friend Box operator/(zono_float alpha, const Box &box)
Elementwise division with scalar.
Definition Box.cpp:500
Box linear_map(const Eigen::Matrix< zono_float, -1, -1 > &A) const
Linear map of box based on interval arithmetic.
Definition Box.cpp:606
Eigen::Vector< zono_float, -1 > x_ub
vector of upper bounds
Definition Box.hpp:715
bool contains_set(const Box &other) const
Check set containment.
Definition Box.cpp:245
Box intersection_over_dims(const Box &other, const std::vector< int > &dims) const
Box intersection over a subset of dimensions.
Definition Box.cpp:212
Box cartesian_product(const Box &other) const
Cartesian product of this box and other.
Definition Box.cpp:257
Eigen::Vector< zono_float, -1 > x_lb
vector of lower bounds
Definition Box.hpp:712
bool contains(const Eigen::Vector< zono_float, -1 > &v)
Check vector continment.
Definition Box.cpp:233
Box affine_map(const Eigen::Matrix< zono_float, -1, -1 > &R, const Eigen::Vector< zono_float, -1 > &s=Eigen::Vector< zono_float, -1 >()) const
Affine map R*[x] + s of the box based on interval arithmetic.
Definition Box.cpp:671
Box intersect(const Box &other) const
Box intersection.
Definition Box.cpp:188
Box & operator=(const Box &other)
Copy assignment.
Definition Box.cpp:100
Box()=default
Default constructor.
virtual void project(Eigen::Ref< Eigen::Vector< zono_float, -1 > > x) const
Projects vector onto the Box.
Definition Box.cpp:146
const Eigen::Vector< zono_float, -1 > & upper() const
Get upper bounds.
Definition Box.hpp:159
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:587
Box operator|(const Box &other) const
Box interval hull.
Definition Box.cpp:550
Eigen::Vector< zono_float, -1 > center() const
Get center of box.
Definition Box.cpp:163
Box operator&(const Box &other) const
Box intersection.
Definition Box.cpp:545
void permute(const Eigen::PermutationMatrix< Eigen::Dynamic, Eigen::Dynamic > &P)
Permutes in place using permutation matrix, i.e., [x] <- P*[x].
Definition Box.cpp:711
Box interval_hull(const Box &other) const
Box interval hull.
Definition Box.cpp:200
bool operator>=(const Box &other) const
Set containment operator.
Definition Box.cpp:560
size_t size() const
Get size of Box object.
Definition Box.cpp:141
friend std::ostream & operator<<(std::ostream &os, const Box &box)
print to ostream
Definition Box.cpp:728
Box radius() const
Get radius of box.
Definition Box.cpp:168
void set_element(int i, const Interval &val)
Element-wise assignment.
Definition Box.cpp:133
friend class detail::MI_Box
Definition Box.hpp:767
Interval get_element(int i) const
Element-wise access.
Definition Box.cpp:126
virtual Box * clone() const
Clone operation.
Definition Box.cpp:153
friend Box operator*(zono_float alpha, const Box &box)
Elementwise multiplication with scalar.
Definition Box.cpp:389
zono_float width() const
Get width of box.
Definition Box.cpp:158
bool contract(const Eigen::SparseMatrix< zono_float, Eigen::RowMajor > &A, const Eigen::Vector< zono_float, -1 > &b, int iter)
Interval contractor.
Definition Box.cpp:570
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:374
void operator-=(const Box &other)
Elementwise subtraction in-place.
Definition Box.cpp:359
const Eigen::Vector< zono_float, -1 > & lower() const
Get lower bounds.
Definition Box.hpp:153
bool is_single_valued() const
Determine whether box is single-valued (i.e., all intervals have width 0 within numerical tolerance)
Definition Box.cpp:183
void operator*=(const Box &other)
Elementwise multiplication in-place.
Definition Box.cpp:379
friend Box operator+(const Eigen::Vector< zono_float, -1 > &v, const Box &box)
Elementwise addition with vector.
Definition Box.cpp:342
Box pontry_diff(const Box &other) const
Pontryagin difference of this box and other, i.e., {x : x + y is in this for all y in other}.
Definition Box.cpp:273
Box project_onto_dims(const std::vector< int > &dims) const
Projects the box onto the dimensions specified in dims.
Definition Box.cpp:286
bool is_empty() const
Determine whether box is empty (any contained interval is empty)
Definition Box.cpp:174
bool operator<=(const Box &other) const
Set containment operator.
Definition Box.cpp:555
Box minkowski_sum(const Box &other) const
Minkowski sum of this box and other.
Definition Box.cpp:266
Interval class.
Definition Interval.hpp:50
#define zono_float
Defines the floating-point type used in ZonoOpt.
Definition Defines.hpp:22
Box minkowski_sum(const Box &Z1, const Box &Z2)
Computes the Minkowski sum of two boxes Z1 and Z2.
Definition Box.cpp:939
Box intersection(const Box &Z1, const Box &Z2, const Eigen::SparseMatrix< zono_float > &R=Eigen::SparseMatrix< zono_float >(), int contractor_iter=10)
Computes the generalized intersection of boxes Z1 and Z2 over the matrix R.
Definition Box.cpp:980
Box interval_hull(const Box &Z1, const Box &Z2)
Computes the interval hull of two boxes.
Definition Box.cpp:959
Box pontry_diff(const Box &Z1, const Box &Z2)
Computes the Pontryagin difference Z1 - Z2 of two boxes.
Definition Box.cpp:944
Box affine_map(const Box &Z, const Eigen::SparseMatrix< zono_float > &R, const Eigen::Vector< zono_float, -1 > &s=Eigen::Vector< zono_float, -1 >())
Returns affine map R*Z + s of box Z.
Definition Box.cpp:954
Box cartesian_product(const Box &Z1, const Box &Z2)
Computes the Cartesian product of two boxes Z1 and Z2.
Definition Box.cpp:934
Box intersection_over_dims(const Box &Z1, const Box &Z2, const std::vector< int > &dims)
Computes the intersection of boxes Z1 and Z2 over the specified dimensions.
Definition Box.cpp:1039
Box project_onto_dims(const Box &Z, const std::vector< int > &dims)
Projects box Z onto the dimensions specified in dims.
Definition Box.cpp:949
Definition ZonoOpt.hpp:51