Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
139 changes: 32 additions & 107 deletions include/convex_bodies/zpolytope.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <limits>

#include <iostream>
#include <vector>
#include <Eigen/Eigen>
#include "lp_oracles/vpolyoracles.h"
#include "lp_oracles/zpolyoracles.h"
Expand All @@ -38,8 +39,12 @@ class Zonotope {
NT maxNT = std::numeric_limits<NT>::max();
NT minNT = std::numeric_limits<NT>::lowest();

REAL *conv_comb, *row_mem, *row;
int *colno, *colno_mem;
// Scratch buffers handed to the lp_solve based oracles. They are mutable
// because the const oracles (e.g. line_intersect) write into them. Being
// std::vectors, the implicitly generated copy/move operations and the
// destructor are correct (rule of zero).
mutable std::vector<REAL> conv_comb, row_mem, row;
mutable std::vector<int> colno, colno_mem;
MT sigma;
MT Q0;

Expand All @@ -50,11 +55,11 @@ class Zonotope {

Zonotope(const unsigned int &dim, const MT &_V, const VT &_b):
_d{dim}, V{_V}, b{_b},
conv_comb{new REAL[V.rows() + 1]},
row_mem{new REAL[V.rows()]},
row{new REAL[V.rows() + 1]},
colno{new int[V.rows() + 1]},
colno_mem{new int[V.rows()]}
conv_comb(V.rows() + 1),
row_mem(V.rows()),
row(V.rows() + 1),
colno(V.rows() + 1),
colno_mem(V.rows())
{
compute_eigenvectors(V.transpose());
}
Expand Down Expand Up @@ -94,95 +99,15 @@ class Zonotope {
}
}

conv_comb = new REAL[Pin.size()];
row_mem = new REAL[V.rows()];
row = new REAL[V.rows() + 1];
colno = new int[V.rows() + 1];
colno_mem = new int[V.rows()];
conv_comb.resize(Pin.size());
row_mem.resize(V.rows());
row.resize(V.rows() + 1);
colno.resize(V.rows() + 1);
colno_mem.resize(V.rows());

compute_eigenvectors(V.transpose());
}

template <typename T>
void copy_array(T* source, T* result, size_t count)
{
T* tarray;
tarray = new T[count];
std::copy_n(source, count, tarray);
delete [] result;
result = tarray;
}

Zonotope& operator=(const Zonotope& other)
{
if (this != &other) { // protect against invalid self-assignment
_d = other._d;
V = other.V;
b = other.b;
T = other.T;

copy_array(other.conv_comb, conv_comb, V.rows() + 1);
copy_array(other.row_mem, row_mem, V.rows());
copy_array(other.row, row, V.rows() + 1);
copy_array(other.colno, colno, V.rows() + 1);
copy_array(other.colno_mem, colno_mem, V.rows());
}
return *this;
}

Zonotope& operator=(Zonotope&& other)
{
if (this != &other) { // protect against invalid self-assignment
_d = other._d;
V = other.V;
b = other.b;
T = other.T;

conv_comb = other.conv_comb; other.conv_comb = nullptr;
row_mem = other.row_mem; other.row_mem = nullptr;
row = other.row; other.row = nullptr;
colno = other.colno; colno = nullptr;
colno_mem = other.colno_mem; colno_mem = nullptr;
}
return *this;
}


Zonotope(const Zonotope& other) :
_d{other._d}, V{other.V}, b{other.b}, T{other.T},
conv_comb{new REAL[V.rows() + 1]},
row_mem{new REAL[V.rows()]},
row{new REAL[V.rows() + 1]},
colno{new int[V.rows() + 1]},
colno_mem{new int[V.rows()]}
{
std::copy_n(other.conv_comb, V.rows() + 1, conv_comb);
std::copy_n(other.row_mem, V.rows(), row_mem);
std::copy_n(other.row, V.rows() + 1, row);
std::copy_n(other.colno, V.rows() + 1, colno);
std::copy_n(other.colno_mem, V.rows(), colno_mem);
}

Zonotope(Zonotope&& other) :
_d{other._d}, V{other.V}, b{other.b}, T{other.T},
conv_comb{nullptr}, row_mem{nullptr}, row{nullptr},
colno{nullptr}, colno_mem{nullptr}
{
conv_comb = other.conv_comb; other.conv_comb = nullptr;
row_mem = other.row_mem; other.row_mem = nullptr;
row = other.row; other.row = nullptr;
colno = other.colno; colno = nullptr;
colno_mem = other.colno_mem; colno_mem = nullptr;
}

~Zonotope() {
delete [] conv_comb;
delete [] colno;
delete [] colno_mem;
delete [] row;
delete [] row_mem;
}

void set_interior_point(Point const& r)
{
_inner_ball.first = r;
Expand Down Expand Up @@ -358,7 +283,7 @@ class Zonotope {
// check if point p belongs to the convex hull of V-Polytope P
int is_in(Point const& p, NT tol=NT(0)) const
{
if(memLP_Zonotope(V, p, row_mem, colno_mem))
if(memLP_Zonotope(V, p, row_mem.data(), colno_mem.data()))
{
return -1;
}
Expand All @@ -377,8 +302,8 @@ class Zonotope {
temp.assign(_d,0);
temp[i] = 1.0;
Point v(_d,temp.begin(), temp.end());
min_plus = intersect_line_Vpoly<NT>(V, center, v, conv_comb,
row, colno, false, true);
min_plus = intersect_line_Vpoly<NT>(V, center, v, conv_comb.data(),
row.data(), colno.data(), false, true);
if (min_plus < radius) radius = min_plus;
}

Expand All @@ -391,7 +316,7 @@ class Zonotope {
// with the Zonotope
std::pair<NT,NT> line_intersect(Point const& r, Point const& v) const
{
return intersect_line_zono(V, r, v, conv_comb, colno);
return intersect_line_zono(V, r, v, conv_comb.data(), colno.data());
}


Expand All @@ -402,7 +327,7 @@ class Zonotope {
VT const& Ar,
VT const& Av) const
{
return intersect_line_zono(V, r, v, conv_comb, colno);
return intersect_line_zono(V, r, v, conv_comb.data(), colno.data());
}

// compute intersection point of ray starting from r and pointing to v
Expand All @@ -413,16 +338,16 @@ class Zonotope {
VT const& Av,
NT const& lambda_prev) const
{
return intersect_line_zono(V, r, v, conv_comb, colno);
return intersect_line_zono(V, r, v, conv_comb.data(), colno.data());
}

std::pair<NT, int> line_positive_intersect(Point const& r,
Point const& v,
VT const& Ar,
VT const& Av) const
{
return std::pair<NT, int> (intersect_line_Vpoly(V, r, v, conv_comb,
row, colno,
return std::pair<NT, int> (intersect_line_Vpoly(V, r, v, conv_comb.data(),
row.data(), colno.data(),
false, true), 1);
}

Expand Down Expand Up @@ -482,7 +407,7 @@ class Zonotope {
temp[rand_coord]=1.0;
Point v(_d,temp.begin(), temp.end());

return intersect_line_zono(V, r, v, conv_comb, colno);
return intersect_line_zono(V, r, v, conv_comb.data(), colno.data());

}

Expand Down Expand Up @@ -577,10 +502,10 @@ class Zonotope {
const NT e = 0.0000000001;
for (int j = 0; j < num_of_generators(); ++j)
{
if (((1.0 - *(conv_comb + j) ) > e || (1.0 - *(conv_comb + j) )
> e*std::abs(*(conv_comb + j))) &&
((1.0 + *(conv_comb + j) ) > e || (1.0 + *(conv_comb + j) )
> e*std::abs(*(conv_comb + j))))
if (((1.0 - conv_comb[j] ) > e || (1.0 - conv_comb[j] )
> e*std::abs(conv_comb[j])) &&
((1.0 + conv_comb[j] ) > e || (1.0 + conv_comb[j] )
> e*std::abs(conv_comb[j])))
{
Fmat.row(count) = V.row(j);
count++;
Expand Down Expand Up @@ -609,8 +534,8 @@ class Zonotope {
MT Fmat(_d-1,_d);
const NT e = 0.0000000001;
for (int j = 0; j < num_of_generators(); ++j) {
if (((1.0 - *(conv_comb + j) ) > e || (1.0 - *(conv_comb + j) ) > e*std::abs(*(conv_comb + j))) &&
((1.0 + *(conv_comb + j) ) > e || (1.0 + *(conv_comb + j) ) > e*std::abs(*(conv_comb + j)))) {
if (((1.0 - conv_comb[j] ) > e || (1.0 - conv_comb[j] ) > e*std::abs(conv_comb[j])) &&
((1.0 + conv_comb[j] ) > e || (1.0 + conv_comb[j] ) > e*std::abs(conv_comb[j]))) {
Fmat.row(count) = V.row(j);
count++;
}
Expand Down
4 changes: 4 additions & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,9 @@ add_executable (boundary_oracles_test boundary_oracles_test.cpp $<TARGET_OBJECTS
add_test(NAME boundary_oracles_test_h_poly_oracles
COMMAND boundary_oracles_test -tc=h_poly_oracles)

add_executable (zonotope_test zonotope_test.cpp $<TARGET_OBJECTS:test_main>)
add_test(NAME zonotope_copy_move COMMAND zonotope_test)

add_executable (volume_sob_hpolytope volume_sob_hpolytope.cpp $<TARGET_OBJECTS:test_main>)
add_test(NAME volume_sob_hpolytope_cube COMMAND volume_sob_hpolytope -tc=cube)
add_test(NAME volume_sob_hpolytope_cross COMMAND volume_sob_hpolytope -tc=cross)
Expand Down Expand Up @@ -467,6 +470,7 @@ TARGET_LINK_LIBRARIES(volume_cg_vpolytope lp_solve coverage_config)
TARGET_LINK_LIBRARIES(volume_cb_hpolytope lp_solve coverage_config)
TARGET_LINK_LIBRARIES(volume_cb_vpolytope lp_solve coverage_config)
TARGET_LINK_LIBRARIES(volume_cb_zonotopes lp_solve coverage_config)
TARGET_LINK_LIBRARIES(zonotope_test lp_solve coverage_config)
TARGET_LINK_LIBRARIES(volume_cb_vpoly_intersection_vpoly lp_solve coverage_config)
TARGET_LINK_LIBRARIES(volume_cb_vpoly_intersection_vpoly lp_solve ${MKL_LINK} coverage_config)
TARGET_LINK_LIBRARIES(rounding_test lp_solve ${MKL_LINK} coverage_config)
Expand Down
110 changes: 110 additions & 0 deletions test/zonotope_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
// VolEsti (volume computation and sampling library)

// Licensed under GNU LGPL.3, see LICENCE file

// Regression tests for the special member functions (copy/move construction
// and assignment) of Zonotope. Zonotope owns scratch buffers that are passed
// to the lp_solve based oracles, so a copy or a move that leaves them dangling
// or null breaks every later oracle call. Run under AddressSanitizer
// (-fsanitize=address) to also detect use-after-free and leaks.

#include "doctest.h"

#include <utility>

#include "cartesian_geom/cartesian_kernel.h"
#include "convex_bodies/zpolytope.h"

typedef Cartesian<double> Kernel;
typedef Kernel::Point Point;
typedef Zonotope<Point> zonotope;
typedef zonotope::MT MT;
typedef zonotope::VT VT;

// Helper to construct a hypercube zonotope [-1, 1]^d from standard basis vectors
static zonotope make_cube_zonotope(unsigned int d)
{
MT V = MT::Identity(d, d);
VT b = VT::Ones(d);
return zonotope(d, V, b);
}

static std::pair<double, double> shoot_ray(zonotope const& P)
{
unsigned int d = P.dimension();
Point origin(d), v(d);
for (unsigned int i = 0; i < d; ++i) {
origin.set_coord(i, 0.0);
v.set_coord(i, i == 0 ? 1.0 : 0.0);
}
return P.line_intersect(origin, v);
}

static void check_oracles(zonotope const& P)
{
std::pair<double, double> res = shoot_ray(P);
CHECK(res.first == doctest::Approx(1.0));
CHECK(res.second == doctest::Approx(-1.0));

unsigned int d = P.dimension();
Point inside(d), outside(d);
for (unsigned int i = 0; i < d; ++i) {
inside.set_coord(i, 0.0);
outside.set_coord(i, 2.0);
}
CHECK(P.is_in(inside) == -1);
CHECK(P.is_in(outside) == 0);
}

TEST_CASE("zonotope_copy_constructor") {
zonotope P = make_cube_zonotope(3);
zonotope Q(P);
check_oracles(Q);
check_oracles(P);
}

TEST_CASE("zonotope_copy_assignment") {
zonotope P = make_cube_zonotope(3);
zonotope Q = make_cube_zonotope(3);
Q = P;
check_oracles(Q);
check_oracles(P);
}

TEST_CASE("zonotope_move_constructor") {
zonotope P = make_cube_zonotope(3);
zonotope Q(std::move(P));
check_oracles(Q);
}

TEST_CASE("zonotope_move_assignment") {
zonotope Q = make_cube_zonotope(3);
Q = make_cube_zonotope(3);
check_oracles(Q);
}

TEST_CASE("zonotope_default_then_move_assignment") {
// the pattern used throughout the volume tests
zonotope P;
P = make_cube_zonotope(3);
check_oracles(P);
}

TEST_CASE("zonotope_copy_and_move_preserve_inner_ball") {
zonotope P = make_cube_zonotope(3);
P.set_InnerBall(std::make_pair(Point(3), 0.5));

zonotope copy_constructed(P);
CHECK(copy_constructed.InnerBall().second == doctest::Approx(0.5));

zonotope copy_assigned;
copy_assigned = P;
CHECK(copy_assigned.InnerBall().second == doctest::Approx(0.5));

zonotope move_constructed(std::move(P));
CHECK(move_constructed.InnerBall().second == doctest::Approx(0.5));

zonotope move_assigned;
move_assigned = std::move(move_constructed);
CHECK(move_assigned.InnerBall().second == doctest::Approx(0.5));
}