Skip to content

Commit

Permalink
Unit test for Random Order Polytope, and minor changes
Browse files Browse the repository at this point in the history
  • Loading branch information
lucaperju committed Jul 17, 2024
1 parent 9b4d325 commit 63300b2
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
8 changes: 6 additions & 2 deletions include/generators/order_polytope_generator.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include "generators/boost_random_number_generator.hpp"

#include "convex_bodies/orderpolytope.h"
#include "convex_bodies/hpolytope.h"


// Instances taken from: https://github.com/ttalvitie/le-counting-practice
Expand Down Expand Up @@ -55,15 +56,18 @@ static const std::unordered_map<std::string, std::string> instances =
// generates a Polytope from a poset
/// @tparam Polytope Type of returned polytope
template <class Polytope>
Polytope get_orderpoly(Poset &poset) {
Polytope get_orderpoly(Poset const &poset) {
typedef typename Polytope::PointType Point;

OrderPolytope<Point> OP(poset);
if constexpr (std::is_same< Polytope, OrderPolytope<Point> >::value ) {
return OP;
} else {
} else if constexpr (std::is_same<Polytope, HPolytope<Point> >::value ){
Polytope HP(OP.dimension(), OP.get_full_mat(), OP.get_vec());
return HP;
} else {
// TODO: implement functionality for more polytope types
throw "Unable to generate an Order Polytope of requested type";
}
}

Expand Down
8 changes: 4 additions & 4 deletions include/misc/poset.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class Poset {
unsigned int n; // elements will be from 0 to n-1
RV order_relations; // pairs of form a <= b

static void sorted_list(std::vector<unsigned int> &res, const unsigned int &n, const RV &relations)
static void sorted_list(const unsigned int &n, const RV &relations, std::vector<unsigned int> &res)
{
std::vector<std::vector<unsigned int> > adjList(n);
std::vector<unsigned int> indegree(n, 0);
Expand Down Expand Up @@ -75,9 +75,9 @@ class Poset {
}

std::vector<unsigned int> order;
sorted_list(order, n, relations);
sorted_list(n, relations, order);

if(order.size() < n) {
if(order.size() < n) { // TODO: accept cycles in the poset
throw "corresponding DAG is not acyclic";
}

Expand Down Expand Up @@ -132,7 +132,7 @@ class Poset {
std::vector<unsigned int> topologically_sorted_list() const
{
std::vector<unsigned int> res;
sorted_list(res, n, order_relations);
sorted_list(n, order_relations, res);
return res;
}
};
Expand Down
14 changes: 14 additions & 0 deletions test/order_polytope.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@
#include "cartesian_geom/cartesian_kernel.h"
#include "cartesian_geom/point.h"
#include "convex_bodies/orderpolytope.h"
#include "convex_bodies/hpolytope.h"

#include "generators/order_polytope_generator.h"

#include "misc/poset.h"
#include "misc/misc.h"

Expand Down Expand Up @@ -150,6 +154,16 @@ void call_test_basics() {
CHECK(OP.is_in(Point(4, {0.5, 0.5, 0.0, 1.0})) == 0); // a0 <= a2 violated
CHECK(OP.is_in(Point(4, {-0.1, 0.5, 1.0, 1.0})) == 0); // a0 >= 0 violated
CHECK(OP.is_in(Point(4, {1.0, 0.5, 1.0, 1.1})) == 0); // a3 <= 1 violated

// Create a random Order Polytope of dimension 10 with 30 facets as an Hpolytope class
HPolytope<Point> HP = random_orderpoly<HPolytope<Point>, NT>(10, 30);

d = HP.dimension();
m = HP.num_of_hyperplanes();

CHECK(d == 10);
CHECK(m == 30);

}


Expand Down

0 comments on commit 63300b2

Please sign in to comment.