ZonoOpt v1.0.0
Loading...
Searching...
No Matches
Zono.hpp
Go to the documentation of this file.
1#ifndef ZONOOPT_ZONO_HPP_
2#define ZONOOPT_ZONO_HPP_
3
15#include "ConZono.hpp"
16
17namespace ZonoOpt
18{
19
20using namespace detail;
21
32class Zono : public ConZono
33{
34 public:
35
36 // constructors
41 Zono() { sharp = true; }
42
50 Zono(const Eigen::SparseMatrix<zono_float>& G, const Eigen::Vector<zono_float, -1>& c,
51 const bool zero_one_form=false)
52 {
54 sharp = true;
55 }
56
57 // virtual destructor
58 ~Zono() override = default;
59
60 // set method
68 void set(const Eigen::SparseMatrix<zono_float>& G, const Eigen::Vector<zono_float, -1>& c,
69 bool zero_one_form=false);
70
74 HybZono* clone() const override
75 {
76 return new Zono(*this);
77 }
78
85 std::unique_ptr<Zono> reduce_order(int n_o);
86
87 // generator conversion between [-1,1] and [0,1]
88 void convert_form() override;
89
90 // display methods
91 std::string print() const override;
92
93 protected:
94
95 bool do_is_empty(const OptSettings &settings, OptSolution* solution) const override;
96
97 Box do_bounding_box(const OptSettings &settings, OptSolution* solution) override;
98
99 zono_float do_support(const Eigen::Vector<zono_float, -1>& d, const OptSettings &settings,
100 OptSolution* solution) override;
101};
102
103// forward declarations
111std::unique_ptr<Zono> interval_2_zono(const Box& box);
112
123std::unique_ptr<Zono> make_regular_zono_2D(zono_float radius, int n_sides, bool outer_approx=false, const Eigen::Vector<zono_float, 2>& c=Eigen::Vector<zono_float, 2>::Zero());
124
125// implementation
126inline void Zono::set(const Eigen::SparseMatrix<zono_float>& G, const Eigen::Vector<zono_float, -1>& c,
127 const bool zero_one_form)
128{
129 // check dimensions
130 if (G.rows() != c.size())
131 {
132 throw std::invalid_argument("Zono: inconsistent dimensions.");
133 }
134
135 // zonotope parameters
136 this->G = G;
137 this->c = c;
138 this->nG = static_cast<int>(this->G.cols());
139 this->n = static_cast<int>(this->G.rows());
140 this->zero_one_form = zero_one_form;
141
142 // abstract zono parameters
143 this->nGc = this->nG;
144 this->nGb = 0;
145 this->nC = 0;
146 this->Gc = this->G;
147 this->Gb.resize(this->n, 0);
148 this->A.resize(0, this->nG);
149 this->Ac = this->A;
150 this->Ab.resize(0, 0);
151 this->b.resize(0);
152}
153
155{
156 Eigen::Vector<zono_float, -1> c;
157 Eigen::SparseMatrix<zono_float> G;
158
159 if (!this->zero_one_form) // convert to [0,1] generators
160 {
161 c = this->c - this->G*Eigen::Vector<zono_float, -1>::Ones(this->nG);
162 G = 2.0*this->G;
163
164 set(G, c, true);
165 }
166 else // convert to [-1,1] generators
167 {
168 c = this->c + 0.5*this->G*Eigen::Vector<zono_float, -1>::Ones(this->nG);
169 G = 0.5*this->G;
170
171 set(G, c, false);
172 }
173}
174
175inline std::string Zono::print() const
176{
177 std::stringstream ss;
178 ss << "Zono: " << std::endl;
179 ss << "n: " << this->n << std::endl;
180 ss << "nG: " << this->nG << std::endl;
181 ss << "G: " << Eigen::Matrix<zono_float, -1, -1>(this->G) << std::endl;
182 ss << "c: " << this->c << std::endl;
183 ss << "zero_one_form: " << this->zero_one_form;
184 return ss.str();
185}
186
187inline bool Zono::do_is_empty(const OptSettings &settings, OptSolution* solution) const
188{
189 if (this->n == 0)
190 return true;
191 else
192 return false;
193}
194
195inline Box Zono::do_bounding_box(const OptSettings &settings, OptSolution* solution)
196{
197 // convert to [-1,1] form
198 if (this->zero_one_form) this->convert_form();
199
200 // init bounds
201 Eigen::Vector<zono_float, -1> l = this->c;
202 Eigen::Vector<zono_float, -1> u = this->c;
203
204 // compute bounds
205 for (int i=0; i<this->nG; ++i)
206 {
207 l -= this->G.col(i).cwiseAbs();
208 u += this->G.col(i).cwiseAbs();
209 }
210
211 // return zonotope bounding box
212 return {l, u};
213}
214
215inline std::unique_ptr<Zono> Zono::reduce_order(const int n_o)
216{
217 // check validity
218 if (n_o < this->n)
219 throw std::invalid_argument("Zono reduce_order: desired order is less than dimension of set");
220
221 // trivial case
222 if (this->nG <= n_o)
223 return std::make_unique<Zono>(*this);
224
225 // convert to [-1,1] form
226 if (this->zero_one_form) this->convert_form();
227
228 // sort columns by decreasing 2-norm
229
230 // vector of 2-norms and indices
231 std::vector<std::pair<int, zono_float>> sort_vec;
232 for (int i=0; i<this->nG; ++i)
233 {
234 sort_vec.emplace_back(i, this->G.col(i).norm());
235 }
236
237 // sort
238 auto comp = [](const std::pair<int, zono_float>& a, const std::pair<int, zono_float>& b) -> bool
239 {
240 return a.second > b.second;
241 };
242 std::sort(sort_vec.begin(), sort_vec.end(), comp);
243
244 // zonotope to keep
245 const int n_K = n_o - this->n;
246 Eigen::SparseMatrix<zono_float> G_K (this->n, n_K);
247 std::vector<Eigen::Triplet<zono_float>> triplets;
248 for (int i=0; i < n_K; ++i)
249 {
250 const int k = sort_vec[i].first; // column
251 for (Eigen::SparseMatrix<zono_float>::InnerIterator it(this->G, k); it; ++it)
252 {
253 triplets.emplace_back(it.row(), i, it.value());
254 }
255 }
256 G_K.setFromSortedTriplets(triplets.begin(), triplets.end());
257 const Zono K (G_K, this->c);
258
259 // zonotope to over-approximate
260 Eigen::SparseMatrix<zono_float> G_L (this->n, this->nG - n_K);
261 triplets.clear();
262 for (int i=n_K; i < this->nG; ++i)
263 {
264 const int k = sort_vec[i].first; // column
265 for (Eigen::SparseMatrix<zono_float>::InnerIterator it(this->G, k); it; ++it)
266 {
267 triplets.emplace_back(it.row(), i-n_K, it.value());
268 }
269 }
270 G_L.setFromSortedTriplets(triplets.begin(), triplets.end());
271 Zono L (G_L, Eigen::Vector<zono_float, -1>::Zero(this->n));
272
273 // get bounding box
274 const auto L_R = interval_2_zono(L.bounding_box());
275
276 // minkowski sum
277 auto Z = minkowski_sum(K, *L_R);
278
279 // check that dynamic cast is valid
280 if (!Z->is_zono())
281 throw std::runtime_error("In Zono::ReduceOrder, return type is not a zonotope?");
282
283 // cast to zono and return
284 return std::unique_ptr<Zono>(dynamic_cast<Zono*>(Z.release()));
285}
286
287inline zono_float Zono::do_support(const Eigen::Vector<zono_float, -1>& d, const OptSettings &settings, OptSolution* solution)
288{
289 if (this->zero_one_form) this->convert_form();
290
291 zono_float h = d.dot(this->c);
292 const Eigen::Matrix<zono_float, -1, -1> Gd = this->G.toDense();
293 for (int i=0; i<this->nG; ++i)
294 {
295 h += std::abs(d.dot(Gd.col(i)));
296 }
297 return h;
298}
299
300} // namespace ZonoOpt
301
302#endif
Constrained zonotope class for ZonoOpt library.
Box (i.e., interval vector) class.
Definition Intervals.hpp:720
Constrained zonotope class.
Definition ConZono.hpp:33
Hybrid zonotope class.
Definition HybZono.hpp:44
int nC
number of constraints
Definition HybZono.hpp:482
int n
set dimension
Definition HybZono.hpp:470
friend std::unique_ptr< HybZono > minkowski_sum(const HybZono &Z1, HybZono &Z2)
Computes Minkowski sum of two sets Z1 and Z2.
Definition PolymorphicFunctions.hpp:133
bool zero_one_form
flag to indicate whether the set is in 0-1 or -1-1 form
Definition HybZono.hpp:485
Eigen::SparseMatrix< zono_float > Gc
continuous generator matrix
Definition HybZono.hpp:449
Eigen::SparseMatrix< zono_float > Gb
binary generator matrix
Definition HybZono.hpp:452
int nG
total number of factors. nG = nGc + nGb
Definition HybZono.hpp:473
bool sharp
flag to indicate whether the set is known to be sharp (i.e., convex relaxation = convex hull)
Definition HybZono.hpp:488
Eigen::Vector< zono_float, -1 > c
center vector
Definition HybZono.hpp:464
int nGb
number of binary factors
Definition HybZono.hpp:479
Box bounding_box(const OptSettings &settings=OptSettings(), OptSolution *solution=nullptr)
Computes a bounding box of the set object as a Box object.
Definition HybZono.hpp:398
Eigen::SparseMatrix< zono_float > A
constraint matrix A = [Ac, Ab]
Definition HybZono.hpp:455
Eigen::Vector< zono_float, -1 > b
constraint vector
Definition HybZono.hpp:467
Eigen::SparseMatrix< zono_float > Ab
binary constraint matrix
Definition HybZono.hpp:461
Eigen::SparseMatrix< zono_float > Ac
continuous constraint matrix
Definition HybZono.hpp:458
Eigen::SparseMatrix< zono_float > G
generator matrix G = [Gc, Gb]
Definition HybZono.hpp:446
int nGc
number of continuous factors
Definition HybZono.hpp:476
Zonotope class.
Definition Zono.hpp:33
bool do_is_empty(const OptSettings &settings, OptSolution *solution) const override
Definition Zono.hpp:187
std::string print() const override
Returns set information as a string.
Definition Zono.hpp:175
HybZono * clone() const override
Clone method for polymorphic behavior.
Definition Zono.hpp:74
~Zono() override=default
std::unique_ptr< Zono > reduce_order(int n_o)
Perform zonotope order reduction.
Definition Zono.hpp:215
void set(const Eigen::SparseMatrix< zono_float > &G, const Eigen::Vector< zono_float, -1 > &c, bool zero_one_form=false)
Reset zonotope object with the given parameters.
Definition Zono.hpp:126
Zono(const Eigen::SparseMatrix< zono_float > &G, const Eigen::Vector< zono_float, -1 > &c, const bool zero_one_form=false)
Zono constructor.
Definition Zono.hpp:50
zono_float do_support(const Eigen::Vector< zono_float, -1 > &d, const OptSettings &settings, OptSolution *solution) override
Definition Zono.hpp:287
Zono()
Default constructor for Zono class.
Definition Zono.hpp:41
void convert_form() override
Converts the set representation between -1-1 and 0-1 forms.
Definition Zono.hpp:154
Box do_bounding_box(const OptSettings &settings, OptSolution *solution) override
Definition Zono.hpp:195
#define zono_float
Defines the floating-point type used in ZonoOpt.
Definition ZonoOpt.hpp:43
std::unique_ptr< Zono > interval_2_zono(const Box &box)
Builds a zonotope from a Box object.
Definition PolymorphicFunctions.hpp:1566
std::unique_ptr< Zono > make_regular_zono_2D(const zono_float radius, const int n_sides, const bool outer_approx, const Eigen::Vector< zono_float, 2 > &c)
Builds a 2D regular zonotope with a given radius and number of sides.
Definition PolymorphicFunctions.hpp:1585
Definition ADMM.hpp:41
Settings for optimization routines in ZonoOpt library.
Definition SolverDataStructures.hpp:26
Solution data structure for optimization routines in ZonoOpt library.
Definition SolverDataStructures.hpp:153