1#ifndef ZONOOPT_ZONO_HPP_
2#define ZONOOPT_ZONO_HPP_
20using namespace detail;
50 Zono(
const Eigen::SparseMatrix<zono_float>&
G,
const Eigen::Vector<zono_float, -1>&
c,
68 void set(
const Eigen::SparseMatrix<zono_float>&
G,
const Eigen::Vector<zono_float, -1>&
c,
76 return new Zono(*
this);
100 std::string
print()
const override;
132std::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());
140 void combinations_util(
const std::vector<T>& elements,
const size_t k,
const size_t start_index,
141 std::vector<T>& current_combination,
142 std::vector<std::vector<T>>& result)
145 if (current_combination.size() == k)
147 result.push_back(current_combination);
152 for (
size_t i = start_index; i < elements.size(); ++i)
155 if (elements.size() - i < k - current_combination.size())
161 current_combination.push_back(elements[i]);
164 combinations_util(elements, k, i + 1, current_combination, result);
167 current_combination.pop_back();
173 std::vector<std::vector<T>> get_combinations(
const std::vector<T>& input_set,
const size_t k)
175 if (k > input_set.size())
181 std::vector<T> elements(input_set.begin(), input_set.end());
183 std::vector<std::vector<T>> result;
184 std::vector<T> current_combination;
187 combinations_util(elements, k, 0, current_combination, result);
195inline void Zono::set(
const Eigen::SparseMatrix<zono_float>& G,
const Eigen::Vector<zono_float, -1>& c,
196 const bool zero_one_form)
199 if (
G.rows() !=
c.size())
201 throw std::invalid_argument(
"Zono: inconsistent dimensions.");
207 this->
nG =
static_cast<int>(this->G.cols());
208 this->
n =
static_cast<int>(this->G.rows());
212 this->
nGc = this->
nG;
216 this->
Gb.resize(this->
n, 0);
217 this->
A.resize(0, this->
nG);
219 this->
Ab.resize(0, 0);
226 Eigen::SparseMatrix<zono_float>
G;
230 c = this->c - this->G*Eigen::Vector<zono_float, -1>::Ones(this->
nG);
237 c = this->c + 0.5*this->G*Eigen::Vector<zono_float, -1>::Ones(this->
nG);
246 std::stringstream
ss;
247 ss <<
"Zono: " << std::endl;
248 ss <<
"n: " << this->
n << std::endl;
249 ss <<
"nG: " << this->
nG << std::endl;
250 ss <<
"G: " << Eigen::Matrix<
zono_float, -1, -1>(this->
G) << std::endl;
251 ss <<
"c: " << this->
c << std::endl;
274 for (
int i=0;
i<this->
nG; ++
i)
276 l -= this->
G.col(
i).cwiseAbs();
277 u += this->
G.col(
i).cwiseAbs();
288 throw std::invalid_argument(
"Zono reduce_order: desired order is less than dimension of set");
292 return std::make_unique<Zono>(*
this);
300 std::vector<std::pair<int, zono_float>>
sort_vec;
301 for (
int i=0;
i<this->
nG; ++
i)
303 sort_vec.emplace_back(
i, this->
G.col(
i).norm());
307 auto comp = [](
const std::pair<int, zono_float>&
a,
const std::pair<int, zono_float>&
b) ->
bool
309 return a.second >
b.second;
315 Eigen::SparseMatrix<zono_float>
G_K (this->n,
n_K);
316 std::vector<Eigen::Triplet<zono_float>>
triplets;
317 for (
int i=0;
i <
n_K; ++
i)
320 for (Eigen::SparseMatrix<zono_float>::InnerIterator
it(this->
G, k);
it; ++
it)
322 triplets.emplace_back(
static_cast<int>(
it.row()),
i,
it.value());
329 Eigen::SparseMatrix<zono_float>
G_L (this->n, this->nG -
n_K);
334 for (Eigen::SparseMatrix<zono_float>::InnerIterator
it(this->
G, k);
it; ++
it)
340 Zono L (
G_L, Eigen::Vector<zono_float, -1>::Zero(this->n));
350 throw std::runtime_error(
"In Zono::ReduceOrder, return type is not a zonotope?");
353 return std::unique_ptr<Zono>(
dynamic_cast<Zono*
>(
Z.release()));
361 const Eigen::Matrix<
zono_float, -1, -1>
Gd = this->
G.toDense();
362 for (
int i=0;
i<this->
nG; ++
i)
364 h += std::abs(
d.dot(
Gd.col(
i)));
376 const Eigen::Matrix<
zono_float, -1, -1>
Gd = this->
G.toDense();
379 std::vector<int>
cols;
380 cols.reserve(
static_cast<size_t>(this->
nG));
381 for (
int i=0;
i<this->
nG; ++
i)
Constrained zonotope class for ZonoOpt library.
Box (i.e., interval vector) class.
Definition Intervals.hpp:718
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:143
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 &, OptSolution *) const override
Definition Zono.hpp:256
std::string print() const override
Returns set information as a string.
Definition Zono.hpp:244
zono_float do_support(const Eigen::Vector< zono_float, -1 > &d, const OptSettings &, OptSolution *) override
Definition Zono.hpp:356
HybZono * clone() const override
Clone method for polymorphic behavior.
Definition Zono.hpp:74
std::unique_ptr< Zono > reduce_order(int n_o)
Perform zonotope order reduction.
Definition Zono.hpp:284
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:195
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 get_volume()
Get volume of zonotope.
Definition Zono.hpp:370
Box do_bounding_box(const OptSettings &, OptSolution *) override
Definition Zono.hpp:264
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:223
#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:1575
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:1594
Settings for optimization routines in ZonoOpt library.
Definition SolverDataStructures.hpp:26
Solution data structure for optimization routines in ZonoOpt library.
Definition SolverDataStructures.hpp:153