55 template < GUM_Numeric GUM_SCALAR >
71 template < GUM_Numeric GUM_SCALAR >
76 template < GUM_Numeric GUM_SCALAR >
81 template < GUM_Numeric GUM_SCALAR >
86 template < GUM_Numeric GUM_SCALAR >
91 template < GUM_Numeric GUM_SCALAR >
96 "LRSWrapper< GUM_SCALAR >::getVolume () : "
97 "volume computation was not asked for this "
98 "credal set, call computeVolume() from a "
102 template < GUM_Numeric GUM_SCALAR >
106 "LRSWrapper< GUM_SCALAR >::setUpH : "
107 "cardinality must be at least 2");
111 _input_ = std::vector< std::vector< GUM_SCALAR > >(card * 2 + 2,
112 std::vector< GUM_SCALAR >(card + 1, 0));
114 _input_[card * 2] = std::vector< GUM_SCALAR >(card + 1, -1);
117 _input_[card * 2 + 1] = std::vector< GUM_SCALAR >(card + 1, 1);
120 _output_ = std::vector< std::vector< GUM_SCALAR > >();
122 _vertex_ = std::vector< GUM_SCALAR >(card);
126 _card_ = (
unsigned int)card;
129 template < GUM_Numeric GUM_SCALAR >
133 "LRSWrapper< GUM_SCALAR >::setUpV : "
134 "cardinality must be at least 2");
138 "LRSWrapper< GUM_SCALAR >::setUpV : vertices "
139 "must be at least 2 to build a polytope");
143 _input_ = std::vector< std::vector< GUM_SCALAR > >(vertices,
144 std::vector< GUM_SCALAR >(card + 1, 1));
146 _output_ = std::vector< std::vector< GUM_SCALAR > >();
150 _card_ = (
unsigned int)card;
154 template < GUM_Numeric GUM_SCALAR >
174 template < GUM_Numeric GUM_SCALAR >
193 "LRSWrapper< GUM_SCALAR >::nextHInput : only for H-representation "
194 "as input. Previous state was : "
204 template < GUM_Numeric GUM_SCALAR >
206 const GUM_SCALAR& max,
210 "LRSWrapper< GUM_SCALAR >::fillH : setUpH or nextInput has not "
211 "been called or H-representation is complete, current state is : "
216 "LRSWrapper< GUM_SCALAR >::fillH : modality is "
217 "greater or equal than cardinality : "
218 << modal <<
" >= " <<
_card_);
221 _input_[modal * 2][modal + 1] = 1;
223 _input_[modal * 2 + 1][0] = max;
224 _input_[modal * 2 + 1][modal + 1] = -1;
233 template < GUM_Numeric GUM_SCALAR >
235 const std::vector< std::vector< GUM_SCALAR > >&
matrix) {
238 "LRSWrapper< GUM_SCALAR >::fillH : setUpH or nextInput has not "
239 "been called or H-representation is complete, current state is : "
244 "LRSWrapper< GUM_SCALAR >::fillMatrix : size is "
245 "different than cardinality : "
250 for (
unsigned int modal = 0; modal <
_card_; modal++) {
257 template < GUM_Numeric GUM_SCALAR >
261 "LRSWrapper< GUM_SCALAR >::fillV : setUpV or nextInput has not "
262 "been called or V-representation is complete, current state is : "
267 "LRSWrapper< GUM_SCALAR >::fillV : input is already full with " <<
_vertices_
276 if (std::fabs(v[mod] - vertex[mod]) > 1e-6) {
293 _input_[row][mod + 1] = vertex[mod];
300 template < GUM_Numeric GUM_SCALAR >
304 "LRSWrapper< GUM_SCALAR >::H2V : fillH has not been called with "
305 "all modalities, current state is still : "
333 std::vector< int64_t > Num;
334 std::vector< int64_t > Den;
337 for (
decltype(
_dic_->d) col = 0, end =
_dic_->d; col <= end; col++)
346 "LRSWrapper< GUM_SCALAR >::H2V : asked for "
347 "Q-hull computation or not reading a vertex !");
349 for (
decltype(
_dat_->n) i = 1, end =
_dat_->n; i < end; i++)
352 }
while (lrs_getnextbasis(&
_dic_,
_dat_, 0L));
354 auto vtx = Num.size();
355 std::vector< GUM_SCALAR > vertex(
_card_);
357 for (
decltype(vtx) i = 1; i <= vtx; i++) {
358 vertex[(i - 1) %
_card_] = GUM_SCALAR(Num[i - 1] * 1.0 / Den[i - 1]);
369 template < GUM_Numeric GUM_SCALAR >
373 "LRSWrapper< GUM_SCALAR >::V2H : fillV has "
374 "not been called with all vertices, current "
379 template < GUM_Numeric GUM_SCALAR >
383 "LRSWrapper< GUM_SCALAR >::computeVolume : "
384 "volume is only for V-representation or "
385 "fillV has not been called with all "
386 "vertices, current state is still : "
394 for (
decltype(
_dic_->d) col = 0, end =
_dic_->d; col <= end; col++)
396 }
while (lrs_getnextbasis(&
_dic_,
_dat_, 0L));
398 int64_t Nsize = (
_dat_->Nvolume[0] > 0) ?
_dat_->Nvolume[0] : -
_dat_->Nvolume[0];
399 int64_t Dsize = (
_dat_->Dvolume[0] > 0) ?
_dat_->Dvolume[0] : -
_dat_->Dvolume[0];
401 int64_t num = 0L, den = 0L;
404 for (
decltype(Nsize) i = Nsize - 1; i > 0; i--) {
405 tmp =
_dat_->Nvolume[i];
407 for (
decltype(i) j = 1; j < i; j++)
413 for (
decltype(Dsize) i = Dsize - 1; i > 0; i--) {
414 tmp =
_dat_->Dvolume[i];
416 for (
decltype(i) j = 1; j < i; j++)
427 template < GUM_Numeric GUM_SCALAR >
431 "LRSWrapper< GUM_SCALAR >::elimRedundVrep : only for "
432 "V-representation or fillV has not been called with all vertices, "
433 "current state is still : "
449 auto nlinearity =
_dat_->nlinearity;
450 auto lastdv =
_dat_->lastdv;
453 redineq = (int64_t*)calloc(std::size_t(m + 1),
sizeof(int64_t));
455 for (
decltype(nlinearity) i = 0; i < nlinearity; i++)
456 redineq[
_dat_->linearity[i]] = 2L;
461 for (
decltype(m + d) index = lastdv + 1, end = m + d; index <= end; index++) {
463 auto ineq =
_dat_->inequality[index - lastdv];
466 redineq[ineq] = checkindex(
_dic_,
_dat_, index);
472 "LRSWrapper< GUM_SCALAR >::elimRedundVrep : not "
473 "reading a vertex but a linearity !");
488 for (
decltype(m) i = 1; i <= m; i++)
490 _output_.push_back(std::vector< GUM_SCALAR >(++
_input_[std::size_t(i - 1)].begin(),
491 _input_[std::size_t(i - 1)].end()));
498 template < GUM_Numeric GUM_SCALAR >
501 std::vector< int64_t >& Num,
502 std::vector< int64_t >& Den)
const {
503 int64_t Nsize = (Nin[0] > 0) ? Nin[0] : -Nin[0];
504 int64_t Dsize = (Din[0] > 0) ? Din[0] : -Din[0];
511 for (
decltype(Nsize) i = Nsize - 1; i > 0; i--) {
514 for (
decltype(i) j = 1; j < i; j++)
520 if (!(Din[0] == 2L && Din[1] == 1L)) {
521 for (
decltype(Dsize) i = Dsize - 1; i > 0; i--) {
524 for (
decltype(i) j = 1; j < i; j++)
533 int64_t Nsign = ((Nin[0] < 0) ? -1L : 1L);
534 int64_t Dsign = ((Din[0] < 0) ? -1L : 1L);
536 if ((Nsign * Dsign) == -1L) num = -num;
556 template < GUM_Numeric GUM_SCALAR >
558 std::size_t cols =
_input_[0].size();
560 int64_t* num =
new int64_t[cols];
562 int64_t* den =
new int64_t[cols];
564 int64_t rows = int64_t(
_input_.size());
566 int64_t numerator, denominator;
568 for (int64_t row = 0; row < rows; row++) {
569 for (std::size_t col = 0; col < cols; col++) {
572 _input_[std::size_t(row)][col]);
574 num[col] = numerator;
575 den[col] = denominator;
592 template < GUM_Numeric GUM_SCALAR >
596 "LRSWrapper< GUM_SCALAR >:: _initLrs_ : not ready, current state "
600 std::string name =
"\n*LrsWrapper:";
601 std::vector< char > chars(name.c_str(), name.c_str() + name.size() + 1u);
604 if (!lrs_init(&chars[0])) {
608 name =
"LRSWrapper globals";
609 chars = std::vector< char >(name.c_str(), name.c_str() + name.size() + 1u);
611 _dat_ = lrs_alloc_dat(&chars[0]);
613 if (
_dat_ ==
nullptr) {
628 if (
_dic_ ==
nullptr) {
644 decltype(
_dat_->nredundcol) startcol = 0;
649 if (!
_dat_->restart) {
650 for (
decltype(
_dat_->nredundcol) col = startcol; col < _dat_->nredundcol; col++)
658 template < GUM_Numeric GUM_SCALAR >
666 if (
_dat_->runs > 0) {
671 auto savem =
_dic_->m;
678 std::string name =
"LrsWrapper:";
679 std::vector< char > chars(name.c_str(), name.c_str() + name.size() + 1u);
681 lrs_close(&chars[0]);
Exception : fatal (unknown ?) error.
Exception : operation not allowed.
Exception : out of bound.
static void continuedFracFirst(int64_t &numerator, int64_t &denominator, const GUM_SCALAR &number, const double &zero=1e-6)
Find the first best rational approximation.
std::unordered_set< int > _insertedModals_
To keep track of which constraints over modalities have been inserted.
_states_ _state_
The current state of the LrsWrapper.
const matrix & getOutput() const
Get the output matrix solution of the problem.
typename std::vector< std::vector< GUM_SCALAR > > matrix
Shortcut for dynamic matrix using vectors.
lrs_mp_vector _lrsOutput_
One line of output of lrs : aither a ray, a vertex, a facet or a linearity.
std::vector< std::vector< GUM_SCALAR > > _insertedVertices_
To keep track of inserted vertices and total.
const char * _setUpStateNames_[5]
To print an enum field name instead of it's value.
void setUpV(const Size &card, const Size &vertices)
Sets up a V-representation.
void H2V()
H-representation to V-representation.
void computeVolume()
Computes a polytope ( pseudo ) volume from it's V-representation.
lrs_mp_matrix _Lin_
Holds lrs input linearities if any are found.
void _initLrs_()
Initialize lrs structs and first basis according to flags.
std::vector< GUM_SCALAR > _vertex_
In case we have lower = upper for all modalities, a point probability, there is no need to use lrs.
lrs_dat * _dat_
Structure for holding static problem data of lrs.
void elimRedundVrep()
V-Redundancy elimination.
void fillMatrix(const std::vector< std::vector< GUM_SCALAR > > &matrix)
Fill the H-representation from the matrix given in argument.
void fillH(const GUM_SCALAR &min, const GUM_SCALAR &max, const Size &modal)
Creates the H-representation of min <= p(X=modal | .) <= max and add it to the problem input _input_.
const matrix & getInput() const
Get the intput matrix of the problem.
matrix _output_
Output matrix - either a V-representation or an H-representation.
void _getLRSWrapperOutput_(lrs_mp Nin, lrs_mp Din, std::vector< int64_t > &Num, std::vector< int64_t > &Den) const
Translate a single output from lrs.
unsigned int _vertices_
The number of vertices of the polytope.
LRSWrapper()
Default Constructor.
void fillV(const std::vector< GUM_SCALAR > &vertex)
Creates the V-representation of a polytope by adding a vertex to the problem input _input_.
unsigned int _card_
Cardinality of the variable.
void _fill_() const
Fill lrs_dictionnary and datas from _input_ using integer rationals.
void V2H()
V-representation to H-representation.
GUM_SCALAR _volume_
The volume of the polytope, if computed, 0 otherwise.
void _freeLrs_()
Free lrs space.
void tearDown()
Reset the wrapper as if it was built.
matrix _input_
Input matrix - either a V-representation or an H-representation.
const unsigned int & getVerticesNumber() const
Get the number of vertices of this polytope.
lrs_dic * _dic_
Structure for holding current dictionary and indices of lrs.
const GUM_SCALAR & getVolume() const
Get the volume of the polytope that has been computed.
~LRSWrapper()
Default Destructor.
void setUpH(const Size &card)
Sets up an H-representation.
void nextHInput()
Reset the wrapper for next computation for a H-representation with the same variable cardinality and ...
#define GUM_ERROR(type, msg)
std::size_t Size
In aGrUM, hashed values are unsigned long int.
namespace for all credal networks entities
gum is the global namespace for all aGrUM entities
bool isCloseToOne(T x, T tol=T(1e-9))