From 63300b25985b597a154ed7a06864d8fbe2b669c2 Mon Sep 17 00:00:00 2001 From: lucaperju Date: Fri, 12 Jul 2024 13:51:04 +0200 Subject: [PATCH] Unit test for Random Order Polytope, and minor changes --- include/generators/order_polytope_generator.h | 8 ++++++-- include/misc/poset.h | 8 ++++---- test/order_polytope.cpp | 14 ++++++++++++++ 3 files changed, 24 insertions(+), 6 deletions(-) diff --git a/include/generators/order_polytope_generator.h b/include/generators/order_polytope_generator.h index 1cf020336..e9199ba2c 100644 --- a/include/generators/order_polytope_generator.h +++ b/include/generators/order_polytope_generator.h @@ -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 @@ -55,15 +56,18 @@ static const std::unordered_map instances = // generates a Polytope from a poset /// @tparam Polytope Type of returned polytope template -Polytope get_orderpoly(Poset &poset) { +Polytope get_orderpoly(Poset const &poset) { typedef typename Polytope::PointType Point; OrderPolytope OP(poset); if constexpr (std::is_same< Polytope, OrderPolytope >::value ) { return OP; - } else { + } else if constexpr (std::is_same >::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"; } } diff --git a/include/misc/poset.h b/include/misc/poset.h index c8ddb94be..084bf66aa 100644 --- a/include/misc/poset.h +++ b/include/misc/poset.h @@ -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 &res, const unsigned int &n, const RV &relations) + static void sorted_list(const unsigned int &n, const RV &relations, std::vector &res) { std::vector > adjList(n); std::vector indegree(n, 0); @@ -75,9 +75,9 @@ class Poset { } std::vector 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"; } @@ -132,7 +132,7 @@ class Poset { std::vector topologically_sorted_list() const { std::vector res; - sorted_list(res, n, order_relations); + sorted_list(n, order_relations, res); return res; } }; diff --git a/test/order_polytope.cpp b/test/order_polytope.cpp index e2ad4300d..9a83e13a4 100644 --- a/test/order_polytope.cpp +++ b/test/order_polytope.cpp @@ -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" @@ -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 HP = random_orderpoly, NT>(10, 30); + + d = HP.dimension(); + m = HP.num_of_hyperplanes(); + + CHECK(d == 10); + CHECK(m == 30); + }