aGrUM 3.1.1
a C++ library for (probabilistic) graphical models
gum::credal::LRSWrapper< GUM_SCALAR > Class Template Reference

Class template acting as a wrapper for Lexicographic Reverse Search by David Avis. More...

#include <agrum/CN/LrsWrapper.h>

Collaboration diagram for gum::credal::LRSWrapper< GUM_SCALAR >:

Public Member Functions

Constructors / Destructors
 LRSWrapper ()
 Default Constructor.
 ~LRSWrapper ()
 Default Destructor.
Getters and setters
const matrixgetInput () const
 Get the intput matrix of the problem.
const matrixgetOutput () const
 Get the output matrix solution of the problem.
const unsigned int & getVerticesNumber () const
 Get the number of vertices of this polytope.
const GUM_SCALAR & getVolume () const
 Get the volume of the polytope that has been computed.
setUp / tearDown
void setUpH (const Size &card)
 Sets up an H-representation.
void setUpV (const Size &card, const Size &vertices)
 Sets up a V-representation.
void tearDown ()
 Reset the wrapper as if it was built.
void nextHInput ()
 Reset the wrapper for next computation for a H-representation with the same variable cardinality and number of inequalities.
Input filling methods
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_.
void fillMatrix (const std::vector< std::vector< GUM_SCALAR > > &matrix)
 Fill the H-representation from the matrix given in argument.
void fillV (const std::vector< GUM_SCALAR > &vertex)
 Creates the V-representation of a polytope by adding a vertex to the problem input _input_.
lrs algorithms
void H2V ()
 H-representation to V-representation.
void V2H ()
 V-representation to H-representation.
void computeVolume ()
 Computes a polytope ( pseudo ) volume from it's V-representation.
void elimRedundVrep ()
 V-Redundancy elimination.

Private Types

enum class  _states_ : char {
  none = char(0) , Hup = char(1) , Vup = char(2) , H2Vready = char(3) ,
  V2Hready = char(4)
}
 The possible states of the LrsWrapper. More...
using matrix = typename std::vector< std::vector< GUM_SCALAR > >
 Shortcut for dynamic matrix using vectors.

Private Member Functions

lrs datas <-> wrapper datas
void _freeLrs_ ()
 Free lrs space.
void _initLrs_ ()
 Initialize lrs structs and first basis according to flags.
void _fill_ () const
 Fill lrs_dictionnary and datas from _input_ using integer rationals.
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.

Private Attributes

matrix _input_
 Input matrix - either a V-representation or an H-representation.
matrix _output_
 Output matrix - either a V-representation or an H-representation.
unsigned int _card_
 Cardinality of the variable.
std::unordered_set< int > _insertedModals_
 To keep track of which constraints over modalities have been inserted.
unsigned int _vertices_
 The number of vertices of the polytope.
std::vector< std::vector< GUM_SCALAR > > _insertedVertices_
 To keep track of inserted vertices and total.
std::vector< GUM_SCALAR > _vertex_
 In case we have lower = upper for all modalities, a point probability, there is no need to use lrs.
_states_ _state_
 The current state of the LrsWrapper.
GUM_SCALAR _volume_
 The volume of the polytope, if computed, 0 otherwise.
const char * _setUpStateNames_ [5]
 To print an enum field name instead of it's value.
int _oldCout_
 File descriptor of standard cout.
lrs structs
lrs_dic * _dic_
 Structure for holding current dictionary and indices of lrs.
lrs_dat * _dat_
 Structure for holding static problem data of lrs.
lrs_mp_vector _lrsOutput_
 One line of output of lrs : aither a ray, a vertex, a facet or a linearity.
lrs_mp_matrix _Lin_
 Holds lrs input linearities if any are found.
flags
bool _getVolume_
bool _hull_
bool _polytope_

Detailed Description

template<GUM_Numeric GUM_SCALAR>
class gum::credal::LRSWrapper< GUM_SCALAR >

Class template acting as a wrapper for Lexicographic Reverse Search by David Avis.

Template Parameters
GUM_SCALARA floating type ( float, double, long double ... ).
Author
Matthieu HOURBRACQ and Pierre-Henri WUILLEMIN(_at_LIP6)

Definition at line 121 of file LrsWrapper.h.

Member Typedef Documentation

◆ matrix

template<GUM_Numeric GUM_SCALAR>
using gum::credal::LRSWrapper< GUM_SCALAR >::matrix = typename std::vector< std::vector< GUM_SCALAR > >
private

Shortcut for dynamic matrix using vectors.

Definition at line 123 of file LrsWrapper.h.

Member Enumeration Documentation

◆ _states_

template<GUM_Numeric GUM_SCALAR>
enum class gum::credal::LRSWrapper::_states_ : char
strongprivate

The possible states of the LrsWrapper.

Some functions will throw an exception if the state is not correct. It allows the user to avoid making - invisible - mistakes.

Enumerator
none 
Hup 
Vup 
H2Vready 
V2Hready 

Definition at line 157 of file LrsWrapper.h.

157 : char {
158 none = char(0),
159 Hup = char(1),
160 Vup = char(2),
161 H2Vready = char(3),
162 V2Hready = char(4),
163 };

Constructor & Destructor Documentation

◆ LRSWrapper()

template<GUM_Numeric GUM_SCALAR>
gum::credal::LRSWrapper< GUM_SCALAR >::LRSWrapper ( )

Default Constructor.

Definition at line 56 of file LrsWrapper_tpl.h.

56 {
58
59 _vertices_ = 0;
60 _card_ = 0;
61
62 _volume_ = 0;
63
64 _getVolume_ = false;
65 _hull_ = false;
66 _polytope_ = false;
67
69 }
Class template acting as a wrapper for Lexicographic Reverse Search by David Avis.
Definition LrsWrapper.h:121
_states_ _state_
The current state of the LrsWrapper.
Definition LrsWrapper.h:166
unsigned int _vertices_
The number of vertices of the polytope.
Definition LrsWrapper.h:141
LRSWrapper()
Default Constructor.
unsigned int _card_
Cardinality of the variable.
Definition LrsWrapper.h:134
GUM_SCALAR _volume_
The volume of the polytope, if computed, 0 otherwise.
Definition LrsWrapper.h:169

References LRSWrapper(), _card_, _getVolume_, _hull_, _polytope_, _state_, _vertices_, _volume_, and none.

Referenced by LRSWrapper(), and ~LRSWrapper().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ ~LRSWrapper()

template<GUM_Numeric GUM_SCALAR>
gum::credal::LRSWrapper< GUM_SCALAR >::~LRSWrapper ( )

Default Destructor.

Definition at line 72 of file LrsWrapper_tpl.h.

72 {
74 }

References LRSWrapper().

Here is the call graph for this function:

Member Function Documentation

◆ _fill_()

template<GUM_Numeric GUM_SCALAR>
void gum::credal::LRSWrapper< GUM_SCALAR >::_fill_ ( ) const
private

Fill lrs_dictionnary and datas from _input_ using integer rationals.

Build polyhedron constraints and objective. Rational< GUM_SCALAR >::continuedFrac is the default algorithm used to approximate reals by integer rationals.

Definition at line 557 of file LrsWrapper_tpl.h.

557 {
558 std::size_t cols = _input_[0].size();
559
560 int64_t* num = new int64_t[cols]; // ISO C++ forbids variable length array,
561 // we need to do this instead
562 int64_t* den = new int64_t[cols];
563
564 int64_t rows = int64_t(_input_.size());
565
567
568 for (int64_t row = 0; row < rows; row++) {
569 for (std::size_t col = 0; col < cols; col++) {
573
574 num[col] = numerator;
576 }
577
578 /* GE is inequality, EQ is equation */
579 /* 1L, 0L respectively */
581 _dat_,
582 int64_t(row + 1),
583 num,
584 den,
585 1L); // do NOT forget this + 1 on row
586 }
587
588 delete[] num;
589 delete[] den;
590 }
static void continuedFracFirst(int64_t &numerator, int64_t &denominator, const GUM_SCALAR &number, const double &zero=1e-6)
Find the first best rational approximation.
lrs_dat * _dat_
Structure for holding static problem data of lrs.
Definition LrsWrapper.h:198
matrix _input_
Input matrix - either a V-representation or an H-representation.
Definition LrsWrapper.h:127
lrs_dic * _dic_
Structure for holding current dictionary and indices of lrs.
Definition LrsWrapper.h:195

References _dat_, _dic_, _input_, and gum::Rational< GUM_SCALAR >::continuedFracFirst().

Referenced by _initLrs_().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ _freeLrs_()

template<GUM_Numeric GUM_SCALAR>
void gum::credal::LRSWrapper< GUM_SCALAR >::_freeLrs_ ( )
private

Free lrs space.

Definition at line 659 of file LrsWrapper_tpl.h.

659 {
660 /* free space : do not change order of next lines! */
661
663
664 if (_dat_->nredundcol > 0) lrs_clear_mp_matrix(_Lin_, _dat_->nredundcol, _dat_->n);
665
666 if (_dat_->runs > 0) {
667 free(_dat_->isave);
668 free(_dat_->jsave);
669 }
670
671 auto savem = _dic_->m; /* need this to clear _dat_*/
672
673 lrs_free_dic(_dic_, _dat_); /* deallocate lrs_dic */
674
675 _dat_->m = savem;
677
678 std::string name = "LrsWrapper:";
679 std::vector< char > chars(name.c_str(), name.c_str() + name.size() + 1u);
680
681 lrs_close(&chars[0]);
682 }
lrs_mp_vector _lrsOutput_
One line of output of lrs : aither a ray, a vertex, a facet or a linearity.
Definition LrsWrapper.h:203
lrs_mp_matrix _Lin_
Holds lrs input linearities if any are found.
Definition LrsWrapper.h:206

References _dat_, _dic_, _Lin_, and _lrsOutput_.

Referenced by computeVolume(), elimRedundVrep(), and H2V().

Here is the caller graph for this function:

◆ _getLRSWrapperOutput_()

template<GUM_Numeric GUM_SCALAR>
void gum::credal::LRSWrapper< GUM_SCALAR >::_getLRSWrapperOutput_ ( lrs_mp Nin,
lrs_mp Din,
std::vector< int64_t > & Num,
std::vector< int64_t > & Den ) const
private

Translate a single output from lrs.

Only vertices are supposed to be read at this step.

Parameters
NinInput numerators in mp format (returned by lrs).
DinInput denominators in mp format (returned by lrs).
NumOutput integer numerators.
DenOutput integer denominators.

Definition at line 499 of file LrsWrapper_tpl.h.

502 {
503 int64_t Nsize = (Nin[0] > 0) ? Nin[0] : -Nin[0];
504 int64_t Dsize = (Din[0] > 0) ? Din[0] : -Din[0];
505
506 int64_t num = 0L;
507 int64_t den = 0L;
508
509 int64_t tmp;
510
511 for (decltype(Nsize) i = Nsize - 1; i > 0; i--) {
512 tmp = Nin[i];
513
514 for (decltype(i) j = 1; j < i; j++)
515 tmp *= BASE;
516
517 num += tmp;
518 }
519
520 if (!(Din[0] == 2L && Din[1] == 1L)) { /* rational */
521 for (decltype(Dsize) i = Dsize - 1; i > 0; i--) {
522 tmp = Din[i];
523
524 for (decltype(i) j = 1; j < i; j++)
525 tmp *= BASE;
526
527 den += tmp;
528 }
529 } else {
530 den = 1L;
531 }
532
533 int64_t Nsign = ((Nin[0] < 0) ? -1L : 1L);
534 int64_t Dsign = ((Din[0] < 0) ? -1L : 1L);
535
536 if ((Nsign * Dsign) == -1L) num = -num;
537
538 Num.push_back(num);
539 Den.push_back(den);
540 }

References BASE.

Referenced by H2V().

Here is the caller graph for this function:

◆ _initLrs_()

template<GUM_Numeric GUM_SCALAR>
void gum::credal::LRSWrapper< GUM_SCALAR >::_initLrs_ ( )
private

Initialize lrs structs and first basis according to flags.

Definition at line 593 of file LrsWrapper_tpl.h.

593 {
596 "LRSWrapper< GUM_SCALAR >:: _initLrs_ : not ready, current state "
597 "is still : "
598 << _setUpStateNames_[static_cast< int >(_state_)]);
599
600 std::string name = "\n*LrsWrapper:";
601 std::vector< char > chars(name.c_str(), name.c_str() + name.size() + 1u);
602 // use &chars[0] as a char*
603
604 if (!lrs_init(&chars[0])) {
605 GUM_ERROR(FatalError, "LRSWrapper< GUM_SCALAR >:: _initLrs_ : failed lrs_init")
606 }
607
608 name = "LRSWrapper globals";
609 chars = std::vector< char >(name.c_str(), name.c_str() + name.size() + 1u);
610
612
613 if (_dat_ == nullptr) {
614 GUM_ERROR(FatalError, "LRSWrapper< GUM_SCALAR >:: _initLrs_ : failed lrs_alloc_dat")
615 }
616
617 _dat_->n = Size(_input_[0].size());
618 _dat_->m = Size(_input_.size());
619
620 _dat_->getvolume = (_getVolume_) ? 1L : 0L;
621 _dat_->hull = (_hull_) ? 1L : 0L;
622 _dat_->polytope = (_polytope_) ? 1L : 0L;
623
625
627
628 if (_dic_ == nullptr) {
629 GUM_ERROR(FatalError, "LRSWrapper< GUM_SCALAR >:: _initLrs_ : failed lrs_alloc_dic")
630 }
631
632 _fill_();
633
634 /* Pivot to a starting dictionary */
635 if (!lrs_getfirstbasis(&_dic_, _dat_, &_Lin_, 0L)) {
636 GUM_ERROR(FatalError, "LRSWrapper< GUM_SCALAR >:: _initLrs_ : failed lrs_getfirstbasis");
637 }
638
639 /* There may have been column redundancy */
640 /* If so the linearity space is obtained and redundant */
641 /* columns are removed. User can access linearity space */
642 /* from lrs_mp_matrix Lin dimensions nredundcol x d+1 */
643
644 decltype(_dat_->nredundcol) startcol = 0;
645
646 if (_dat_->homogeneous && _dat_->hull) {
647 startcol++; /* col zero not treated as redundant */
648
649 if (!_dat_->restart) {
650 for (decltype(_dat_->nredundcol) col = startcol; col < _dat_->nredundcol; col++)
652
653 GUM_ERROR(FatalError, "LRSWrapper< GUM_SCALAR >:: _initLrs_ : redundant columns !")
654 }
655 }
656 }
const char * _setUpStateNames_[5]
To print an enum field name instead of it's value.
Definition LrsWrapper.h:173
void _fill_() const
Fill lrs_dictionnary and datas from _input_ using integer rationals.
#define GUM_ERROR(type, msg)
Definition exceptions.h:76
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition types.h:74

References _dat_, _dic_, _fill_(), _getVolume_, _hull_, _input_, _Lin_, _lrsOutput_, _polytope_, _setUpStateNames_, _state_, GUM_ERROR, H2Vready, and V2Hready.

Referenced by computeVolume(), elimRedundVrep(), and H2V().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ computeVolume()

template<GUM_Numeric GUM_SCALAR>
void gum::credal::LRSWrapper< GUM_SCALAR >::computeVolume ( )

Computes a polytope ( pseudo ) volume from it's V-representation.

Warning
Volume can only be computed using V-representation. H-representation volume computation is not predictable.
Probabilistic polytopes are not full dimensional : they lie in the simplex' hyper plane, therefore a pseudo-volume will be computed by projecting the polytope. The projection used is the lexicographically smallest coordinate subspace.

Definition at line 380 of file LrsWrapper_tpl.h.

380 {
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 : "
388
389 _getVolume_ = true;
390
391 _initLrs_();
392
393 do {
394 for (decltype(_dic_->d) col = 0, end = _dic_->d; col <= end; col++)
396 } while (lrs_getnextbasis(&_dic_, _dat_, 0L));
397
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];
400
401 int64_t num = 0L, den = 0L;
402 int64_t tmp;
403
404 for (decltype(Nsize) i = Nsize - 1; i > 0; i--) {
405 tmp = _dat_->Nvolume[i];
406
407 for (decltype(i) j = 1; j < i; j++)
408 tmp *= BASE;
409
410 num += tmp;
411 }
412
413 for (decltype(Dsize) i = Dsize - 1; i > 0; i--) {
414 tmp = _dat_->Dvolume[i];
415
416 for (decltype(i) j = 1; j < i; j++)
417 tmp *= BASE;
418
419 den += tmp;
420 }
421
422 _volume_ = num * 1.0 / den;
423
424 _freeLrs_();
425 }
void _initLrs_()
Initialize lrs structs and first basis according to flags.
void _freeLrs_()
Free lrs space.

References _dat_, _dic_, _freeLrs_(), _getVolume_, _initLrs_(), _lrsOutput_, _setUpStateNames_, _state_, _volume_, BASE, GUM_ERROR, and V2Hready.

Here is the call graph for this function:

◆ elimRedundVrep()

template<GUM_Numeric GUM_SCALAR>
void gum::credal::LRSWrapper< GUM_SCALAR >::elimRedundVrep ( )

V-Redundancy elimination.

Eliminates redundant vertices from a polytope V-representation input input.

Definition at line 428 of file LrsWrapper_tpl.h.

428 {
431 "LRSWrapper< GUM_SCALAR >::elimRedundVrep : only for "
432 "V-representation or fillV has not been called with all vertices, "
433 "current state is still : "
434 << _setUpStateNames_[static_cast< int >(_state_)]);
435
436 _initLrs_();
437
438 int64_t* redineq; /* redineq[i]=0 if ineq i non-red,1 if red,2 linearity */
439
440 /*********************************************************************************/
441 /* Test each row of the dictionary to see if it is redundant */
442 /*********************************************************************************/
443
444 /* note some of these may have been changed in getting initial dictionary
445 */
446 auto m = _dic_->m_A;
447 auto d = _dic_->d;
448 /* number of linearities in input */ /* should be 0 ! */
449 auto nlinearity = _dat_->nlinearity;
450 auto lastdv = _dat_->lastdv;
451
452 /* linearities are not considered for redundancy */
453 redineq = (int64_t*)calloc(std::size_t(m + 1), sizeof(int64_t));
454
455 for (decltype(nlinearity) i = 0; i < nlinearity; i++)
456 redineq[_dat_->linearity[i]] = 2L;
457
458 /* rows 0..lastdv are cost, decision variables, or linearities */
459 /* other rows need to be tested */
460
461 for (decltype(m + d) index = lastdv + 1, end = m + d; index <= end; index++) {
462 /* input inequality number of current index */
463 auto ineq = _dat_->inequality[index - lastdv]; /* the input inequality number
464 corr. to this index */
465
467 }
468
469 /* linearities */
470 if (nlinearity > 0)
472 "LRSWrapper< GUM_SCALAR >::elimRedundVrep : not "
473 "reading a vertex but a linearity !");
474
475 /* count number of non-redundant inequalities */
476 /*
477 auto nredund = nlinearity;
478 for ( decltype ( m ) i = 1; i <= m; i++ )
479 if ( redineq[ i ] == 0 )
480 nredund++;
481 */
482
483 // __vertices = nredund;
484 // __output = std::vector< std::vector< GUM_SCALAR > > ( nredund,
485 // std::vector<
486 // GUM_SCALAR > ( _dat_->n - 1 ) );
487
488 for (decltype(m) i = 1; i <= m; i++)
489 if (redineq[i] == 0)
491 _input_[std::size_t(i - 1)].end()));
492
493 _vertices_ = (unsigned int)_output_.size();
494
495 _freeLrs_();
496 }
matrix _output_
Output matrix - either a V-representation or an H-representation.
Definition LrsWrapper.h:131

References _dat_, _dic_, _freeLrs_(), _initLrs_(), _input_, _output_, _setUpStateNames_, _state_, _vertices_, GUM_ERROR, and V2Hready.

Here is the call graph for this function:

◆ fillH()

template<GUM_Numeric GUM_SCALAR>
void gum::credal::LRSWrapper< GUM_SCALAR >::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_.

Parameters
minThe lower value of p(X=modal | .).
maxThe upper value of p(X=modal | .).
modalThe modality on which we put constraints.

Definition at line 205 of file LrsWrapper_tpl.h.

207 {
208 if (_state_ != _states_::Hup)
210 "LRSWrapper< GUM_SCALAR >::fillH : setUpH or nextInput has not "
211 "been called or H-representation is complete, current state is : "
212 << _setUpStateNames_[static_cast< int >(_state_)]);
213
214 if (modal >= _card_)
216 "LRSWrapper< GUM_SCALAR >::fillH : modality is "
217 "greater or equal than cardinality : "
218 << modal << " >= " << _card_);
219
220 _input_[modal * 2][0] = -min;
221 _input_[modal * 2][modal + 1] = 1;
222
223 _input_[modal * 2 + 1][0] = max;
224 _input_[modal * 2 + 1][modal + 1] = -1;
225
226 _vertex_[modal] = max;
227
228 _insertedModals_.insert(int(modal));
229
231 }
std::unordered_set< int > _insertedModals_
To keep track of which constraints over modalities have been inserted.
Definition LrsWrapper.h:138
std::vector< GUM_SCALAR > _vertex_
In case we have lower = upper for all modalities, a point probability, there is no need to use lrs.
Definition LrsWrapper.h:150

References _card_, _input_, _insertedModals_, _setUpStateNames_, _state_, _vertex_, GUM_ERROR, H2Vready, and Hup.

Referenced by gum::credal::CredalNet< GUM_SCALAR >::intervalToCredal().

Here is the caller graph for this function:

◆ fillMatrix()

template<GUM_Numeric GUM_SCALAR>
void gum::credal::LRSWrapper< GUM_SCALAR >::fillMatrix ( const std::vector< std::vector< GUM_SCALAR > > & matrix)

Fill the H-representation from the matrix given in argument.

Parameters
matrixThe H-representation of the polytope of the form 0 <= -b
  • Ax, A is the matrix, each column the coefficient of the variable in x.

Definition at line 234 of file LrsWrapper_tpl.h.

235 {
236 if (_state_ != _states_::Hup)
238 "LRSWrapper< GUM_SCALAR >::fillH : setUpH or nextInput has not "
239 "been called or H-representation is complete, current state is : "
240 << _setUpStateNames_[static_cast< int >(_state_)]);
241
242 if (matrix[0].size() - 1 != _card_)
244 "LRSWrapper< GUM_SCALAR >::fillMatrix : size is "
245 "different than cardinality : "
246 << (matrix[0].size() - 1) << " != " << _card_);
247
248 _input_ = matrix;
249
250 for (unsigned int modal = 0; modal < _card_; modal++) {
251 _insertedModals_.insert(modal);
252 }
253
255 }
typename std::vector< std::vector< GUM_SCALAR > > matrix
Shortcut for dynamic matrix using vectors.
Definition LrsWrapper.h:123

References _card_, _input_, _insertedModals_, _setUpStateNames_, _state_, GUM_ERROR, H2Vready, and Hup.

Referenced by gum::credal::lp::LpInterface< GUM_SCALAR >::solve().

Here is the caller graph for this function:

◆ fillV()

template<GUM_Numeric GUM_SCALAR>
void gum::credal::LRSWrapper< GUM_SCALAR >::fillV ( const std::vector< GUM_SCALAR > & vertex)

Creates the V-representation of a polytope by adding a vertex to the problem input _input_.

Parameters
vertexThe vertex we wish to add to the V-representation of the polytope.

Definition at line 258 of file LrsWrapper_tpl.h.

258 {
259 if (_state_ != _states_::Vup)
261 "LRSWrapper< GUM_SCALAR >::fillV : setUpV or nextInput has not "
262 "been called or V-representation is complete, current state is : "
263 << _setUpStateNames_[static_cast< int >(_state_)]);
264
265 if (_insertedVertices_.size() == _vertices_)
267 "LRSWrapper< GUM_SCALAR >::fillV : input is already full with " << _vertices_
268 << " vertices.");
269
270 bool eq = true;
271
272 for (const auto& v: _insertedVertices_) {
273 eq = true;
274
275 for (decltype(_card_) mod = 0; mod < _card_; mod++)
276 if (std::fabs(v[mod] - vertex[mod]) > 1e-6) {
277 eq = false;
278 break;
279 }
280
281 if (eq) {
282 _vertices_--;
283 return;
284 // GUM_ERROR ( DuplicateElement, "LRSWrapper< GUM_SCALAR >::fillV :
285 // vertex
286 // already present : " << vertex );
287 }
288 }
289
290 auto row = _insertedVertices_.size();
291
292 for (decltype(_card_) mod = 0; mod < _card_; mod++)
293 _input_[row][mod + 1] = vertex[mod];
294
295 _insertedVertices_.push_back(vertex);
296
298 }
std::vector< std::vector< GUM_SCALAR > > _insertedVertices_
To keep track of inserted vertices and total.
Definition LrsWrapper.h:146

References _card_, _input_, _insertedVertices_, _setUpStateNames_, _state_, _vertices_, GUM_ERROR, V2Hready, and Vup.

◆ getInput()

template<GUM_Numeric GUM_SCALAR>
auto gum::credal::LRSWrapper< GUM_SCALAR >::getInput ( ) const

Get the intput matrix of the problem.

Returns
A constant reference to the _intput_ matrix.

Definition at line 77 of file LrsWrapper_tpl.h.

77 {
78 return _input_;
79 }

References _input_.

◆ getOutput()

template<GUM_Numeric GUM_SCALAR>
auto gum::credal::LRSWrapper< GUM_SCALAR >::getOutput ( ) const

Get the output matrix solution of the problem.

Returns
A constant reference to the _output_ matrix.

Definition at line 82 of file LrsWrapper_tpl.h.

82 {
83 return _output_;
84 }

References _output_.

Referenced by gum::credal::CredalNet< GUM_SCALAR >::intervalToCredal(), and gum::credal::lp::LpInterface< GUM_SCALAR >::solve().

Here is the caller graph for this function:

◆ getVerticesNumber()

template<GUM_Numeric GUM_SCALAR>
const unsigned int & gum::credal::LRSWrapper< GUM_SCALAR >::getVerticesNumber ( ) const

Get the number of vertices of this polytope.

Returns
A constant reference to the number of vertices _vertices_.

Definition at line 87 of file LrsWrapper_tpl.h.

87 {
88 return _vertices_;
89 }

References _vertices_.

◆ getVolume()

template<GUM_Numeric GUM_SCALAR>
const GUM_SCALAR & gum::credal::LRSWrapper< GUM_SCALAR >::getVolume ( ) const

Get the volume of the polytope that has been computed.

Warning
Volume can only be computed using V-representation. H-representation volume computation is not predictable.
Probabilistic polytopes are not full dimensional : they lie in the simplex' hyper plane, therefore a pseudo-volume will be computed by projecting the polytope. The projection used is the lexicographically smallest coordinate subspace.
Returns
A constant reference to the polytope volume.

Definition at line 92 of file LrsWrapper_tpl.h.

92 {
93 if (_volume_ != 0) return _volume_;
94 else
96 "LRSWrapper< GUM_SCALAR >::getVolume () : "
97 "volume computation was not asked for this "
98 "credal set, call computeVolume() from a "
99 "V-representation.");
100 }

References _volume_, and GUM_ERROR.

◆ H2V()

template<GUM_Numeric GUM_SCALAR>
void gum::credal::LRSWrapper< GUM_SCALAR >::H2V ( )

H-representation to V-representation.

Computes the V-representation of a polytope, i.e. it's vertices, from it's H-representation, i.e. the hyper-plan inequalities.

Definition at line 301 of file LrsWrapper_tpl.h.

301 {
304 "LRSWrapper< GUM_SCALAR >::H2V : fillH has not been called with "
305 "all modalities, current state is still : "
306 << _setUpStateNames_[static_cast< int >(_state_)]);
307
308 // check that we have a credal set and not a precise point probability,
309 // i.e.
310 // sum of vertex elements is close to one ( floating type precision )
311 GUM_SCALAR sum = 0;
312
313 for (const auto elem: _vertex_)
314 sum += elem;
315
316 if (gum::isCloseToOne(sum, GUM_SCALAR(1e-6))) {
318 return;
319 }
320
321 // not precise point probability, initialize lrs
322
323 _initLrs_();
324
325 /* We initiate reverse search from this dictionary */
326 /* getting new dictionaries until the search is complete */
327 /* User can access each output line from output which is */
328 /* vertex/ray/facet from the lrs_mp_vector output */
329 /* prune is TRUE if tree should be pruned at current node */
330
331 // pruning is not used
332
333 std::vector< int64_t > Num; /* numerators of all vertices */
334 std::vector< int64_t > Den; /* denominators of all vertices */
335
336 do {
337 for (decltype(_dic_->d) col = 0, end = _dic_->d; col <= end; col++)
339 // iszero macro could be used here for the test on right
340 if (_dat_->hull
341 || ((((_lrsOutput_[0])[0] == 2 || (_lrsOutput_[0])[0] == -2)
342 && (_lrsOutput_[0])[1] == 0)
343 ? 1L
344 : 0L)) {
346 "LRSWrapper< GUM_SCALAR >::H2V : asked for "
347 "Q-hull computation or not reading a vertex !");
348 } else
349 for (decltype(_dat_->n) i = 1, end = _dat_->n; i < end; i++)
351 }
352 } while (lrs_getnextbasis(&_dic_, _dat_, 0L));
353
354 auto vtx = Num.size();
356
357 for (decltype(vtx) i = 1; i <= vtx; i++) {
358 vertex[(i - 1) % _card_] = GUM_SCALAR(Num[i - 1] * 1.0 / Den[i - 1]);
359
360 if (i % _card_ == 0) {
361 _output_.push_back(vertex);
362 _vertices_++;
363 }
364 }
365
366 _freeLrs_();
367 }
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.

References _card_, _dat_, _dic_, _freeLrs_(), _getLRSWrapperOutput_(), _initLrs_(), _lrsOutput_, _output_, _setUpStateNames_, _state_, _vertex_, _vertices_, GUM_ERROR, H2Vready, and gum::isCloseToOne().

Referenced by gum::credal::CredalNet< GUM_SCALAR >::intervalToCredal(), and gum::credal::lp::LpInterface< GUM_SCALAR >::solve().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ nextHInput()

template<GUM_Numeric GUM_SCALAR>
void gum::credal::LRSWrapper< GUM_SCALAR >::nextHInput ( )

Reset the wrapper for next computation for a H-representation with the same variable cardinality and number of inequalities.

Usefull when creating credal networks specified as intervals over modalities.

Reset wrapper state _state_ to it's previous state and clear output matrix _output_. Keeps the cardinality _card_ of the variable and therefore the input matrix _intput_ structure.

Definition at line 175 of file LrsWrapper_tpl.h.

175 {
176 _insertedModals_.clear();
177 _insertedVertices_.clear();
178 _output_.clear();
179 _vertex_.clear();
180 _vertex_.resize(_card_, 0);
181
182 _volume_ = 0;
183 _vertices_ = 0;
184
185 _getVolume_ = false;
186 _hull_ = false;
187 _polytope_ = false;
188
190 else if (_state_ == _states_::V2Hready) {
193 "LRSWrapper< GUM_SCALAR >::nextHInput : only for H-representation "
194 "as input. Previous state was : "
195 << _setUpStateNames_[static_cast< int >(_state_)]);
196 } else {
197 _input_.clear();
199 _card_ = 0;
200 _vertex_.clear();
201 }
202 }

References _card_, _getVolume_, _hull_, _input_, _insertedModals_, _insertedVertices_, _output_, _polytope_, _setUpStateNames_, _state_, _vertex_, _vertices_, _volume_, GUM_ERROR, H2Vready, Hup, none, V2Hready, and Vup.

Referenced by gum::credal::CredalNet< GUM_SCALAR >::intervalToCredal().

Here is the caller graph for this function:

◆ setUpH()

template<GUM_Numeric GUM_SCALAR>
void gum::credal::LRSWrapper< GUM_SCALAR >::setUpH ( const Size & card)

Sets up an H-representation.

Initialize input matrix _input_ to correct dimensions and wrapper state _state_ to _states_::Hup.

Parameters
cardA constant reference to the cardinality of the variable.

Definition at line 103 of file LrsWrapper_tpl.h.

103 {
104 if (card < 2)
106 "LRSWrapper< GUM_SCALAR >::setUpH : "
107 "cardinality must be at least 2");
108
109 tearDown();
110
113
115 _input_[card * 2][0] = 1;
116
117 _input_[card * 2 + 1] = std::vector< GUM_SCALAR >(card + 1, 1);
118 _input_[card * 2 + 1][0] = -1;
119
121
123
125
126 _card_ = (unsigned int)card;
127 }
void tearDown()
Reset the wrapper as if it was built.

References _card_, _input_, _output_, _state_, _vertex_, GUM_ERROR, Hup, and tearDown().

Referenced by gum::credal::CredalNet< GUM_SCALAR >::intervalToCredal(), and gum::credal::lp::LpInterface< GUM_SCALAR >::solve().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ setUpV()

template<GUM_Numeric GUM_SCALAR>
void gum::credal::LRSWrapper< GUM_SCALAR >::setUpV ( const Size & card,
const Size & vertices )

Sets up a V-representation.

Initialize input matrix _input_ to correct dimensions and wrapper state _state_ to _states_::Vup.

Parameters
cardA constant reference to the cardinality of the variable.
verticesA constant reference to the number of vertices of the polytope.

Definition at line 130 of file LrsWrapper_tpl.h.

130 {
131 if (card < 2)
133 "LRSWrapper< GUM_SCALAR >::setUpV : "
134 "cardinality must be at least 2");
135
136 if (vertices < 2)
138 "LRSWrapper< GUM_SCALAR >::setUpV : vertices "
139 "must be at least 2 to build a polytope");
140
141 tearDown();
142
145
147
149
150 _card_ = (unsigned int)card;
151 _vertices_ = (unsigned int)vertices;
152 }

References _card_, _input_, _output_, _state_, _vertices_, GUM_ERROR, tearDown(), and Vup.

Here is the call graph for this function:

◆ tearDown()

template<GUM_Numeric GUM_SCALAR>
void gum::credal::LRSWrapper< GUM_SCALAR >::tearDown ( )

Reset the wrapper as if it was built.

Reset wrapper state _state_ to _states_::none and clear all member datas.

Definition at line 155 of file LrsWrapper_tpl.h.

155 {
156 _input_.clear();
157 _output_.clear();
158 _vertex_.clear();
159 _insertedModals_.clear();
160
161 _insertedVertices_.clear();
162 _vertices_ = 0;
163
164 _volume_ = 0;
165
167 _card_ = 0;
168
169 _getVolume_ = false;
170 _hull_ = false;
171 _polytope_ = false;
172 }

References _card_, _getVolume_, _hull_, _input_, _insertedModals_, _insertedVertices_, _output_, _polytope_, _state_, _vertex_, _vertices_, _volume_, and none.

Referenced by setUpH(), and setUpV().

Here is the caller graph for this function:

◆ V2H()

template<GUM_Numeric GUM_SCALAR>
void gum::credal::LRSWrapper< GUM_SCALAR >::V2H ( )

V-representation to H-representation.

Warning
Not complete yet.

Computes the H-representation of a polytope from it's V-representation.

Definition at line 370 of file LrsWrapper_tpl.h.

370 {
373 "LRSWrapper< GUM_SCALAR >::V2H : fillV has "
374 "not been called with all vertices, current "
375 "state is still : "
377 }

References _setUpStateNames_, _state_, GUM_ERROR, and V2Hready.

Member Data Documentation

◆ _card_

template<GUM_Numeric GUM_SCALAR>
unsigned int gum::credal::LRSWrapper< GUM_SCALAR >::_card_
private

Cardinality of the variable.

Definition at line 134 of file LrsWrapper.h.

Referenced by LRSWrapper(), fillH(), fillMatrix(), fillV(), H2V(), nextHInput(), setUpH(), setUpV(), and tearDown().

◆ _dat_

template<GUM_Numeric GUM_SCALAR>
lrs_dat* gum::credal::LRSWrapper< GUM_SCALAR >::_dat_
private

Structure for holding static problem data of lrs.

Definition at line 198 of file LrsWrapper.h.

Referenced by _fill_(), _freeLrs_(), _initLrs_(), computeVolume(), elimRedundVrep(), and H2V().

◆ _dic_

template<GUM_Numeric GUM_SCALAR>
lrs_dic* gum::credal::LRSWrapper< GUM_SCALAR >::_dic_
private

Structure for holding current dictionary and indices of lrs.

Definition at line 195 of file LrsWrapper.h.

Referenced by _fill_(), _freeLrs_(), _initLrs_(), computeVolume(), elimRedundVrep(), and H2V().

◆ _getVolume_

template<GUM_Numeric GUM_SCALAR>
bool gum::credal::LRSWrapper< GUM_SCALAR >::_getVolume_
private

Definition at line 213 of file LrsWrapper.h.

Referenced by LRSWrapper(), _initLrs_(), computeVolume(), nextHInput(), and tearDown().

◆ _hull_

template<GUM_Numeric GUM_SCALAR>
bool gum::credal::LRSWrapper< GUM_SCALAR >::_hull_
private

Definition at line 215 of file LrsWrapper.h.

Referenced by LRSWrapper(), _initLrs_(), nextHInput(), and tearDown().

◆ _input_

template<GUM_Numeric GUM_SCALAR>
matrix gum::credal::LRSWrapper< GUM_SCALAR >::_input_
private

Input matrix - either a V-representation or an H-representation.

Definition at line 127 of file LrsWrapper.h.

Referenced by _fill_(), _initLrs_(), elimRedundVrep(), fillH(), fillMatrix(), fillV(), getInput(), nextHInput(), setUpH(), setUpV(), and tearDown().

◆ _insertedModals_

template<GUM_Numeric GUM_SCALAR>
std::unordered_set< int > gum::credal::LRSWrapper< GUM_SCALAR >::_insertedModals_
private

To keep track of which constraints over modalities have been inserted.

When the set is full, the state changes from up to ready.

Definition at line 138 of file LrsWrapper.h.

Referenced by fillH(), fillMatrix(), nextHInput(), and tearDown().

◆ _insertedVertices_

template<GUM_Numeric GUM_SCALAR>
std::vector< std::vector< GUM_SCALAR > > gum::credal::LRSWrapper< GUM_SCALAR >::_insertedVertices_
private

To keep track of inserted vertices and total.

When set is full, the state changes from up to ready.

Definition at line 146 of file LrsWrapper.h.

Referenced by fillV(), nextHInput(), and tearDown().

◆ _Lin_

template<GUM_Numeric GUM_SCALAR>
lrs_mp_matrix gum::credal::LRSWrapper< GUM_SCALAR >::_Lin_
private

Holds lrs input linearities if any are found.

Definition at line 206 of file LrsWrapper.h.

Referenced by _freeLrs_(), and _initLrs_().

◆ _lrsOutput_

template<GUM_Numeric GUM_SCALAR>
lrs_mp_vector gum::credal::LRSWrapper< GUM_SCALAR >::_lrsOutput_
private

One line of output of lrs : aither a ray, a vertex, a facet or a linearity.

Definition at line 203 of file LrsWrapper.h.

Referenced by _freeLrs_(), _initLrs_(), computeVolume(), and H2V().

◆ _oldCout_

template<GUM_Numeric GUM_SCALAR>
int gum::credal::LRSWrapper< GUM_SCALAR >::_oldCout_
mutableprivate

File descriptor of standard cout.

Lrs writes a lot of stuff on standard cout. oldCout is used to save the current cout before redirecting it to /dev/null when calling lrs. The standard cout is restored when lrs is done.

Definition at line 189 of file LrsWrapper.h.

◆ _output_

template<GUM_Numeric GUM_SCALAR>
matrix gum::credal::LRSWrapper< GUM_SCALAR >::_output_
private

Output matrix - either a V-representation or an H-representation.

Definition at line 131 of file LrsWrapper.h.

Referenced by elimRedundVrep(), getOutput(), H2V(), nextHInput(), setUpH(), setUpV(), and tearDown().

◆ _polytope_

template<GUM_Numeric GUM_SCALAR>
bool gum::credal::LRSWrapper< GUM_SCALAR >::_polytope_
private

Definition at line 217 of file LrsWrapper.h.

Referenced by LRSWrapper(), _initLrs_(), nextHInput(), and tearDown().

◆ _setUpStateNames_

template<GUM_Numeric GUM_SCALAR>
const char* gum::credal::LRSWrapper< GUM_SCALAR >::_setUpStateNames_[5]
private
Initial value:
= {
enumStringify(_states_::nHup),
enumStringify(_states_::nVup),
enumStringify(_states_::nH2Vready),
enumStringify(_states_::nV2Hready),
}
#define enumStringify(name)
Definition LrsWrapper.h:105

To print an enum field name instead of it's value.

Used with GUM_ERROR.

Definition at line 173 of file LrsWrapper.h.

Referenced by _initLrs_(), computeVolume(), elimRedundVrep(), fillH(), fillMatrix(), fillV(), H2V(), nextHInput(), and V2H().

◆ _state_

template<GUM_Numeric GUM_SCALAR>
_states_ gum::credal::LRSWrapper< GUM_SCALAR >::_state_
private

The current state of the LrsWrapper.

Definition at line 166 of file LrsWrapper.h.

Referenced by LRSWrapper(), _initLrs_(), computeVolume(), elimRedundVrep(), fillH(), fillMatrix(), fillV(), H2V(), nextHInput(), setUpH(), setUpV(), tearDown(), and V2H().

◆ _vertex_

template<GUM_Numeric GUM_SCALAR>
std::vector< GUM_SCALAR > gum::credal::LRSWrapper< GUM_SCALAR >::_vertex_
private

In case we have lower = upper for all modalities, a point probability, there is no need to use lrs.

Definition at line 150 of file LrsWrapper.h.

Referenced by fillH(), H2V(), nextHInput(), setUpH(), and tearDown().

◆ _vertices_

template<GUM_Numeric GUM_SCALAR>
unsigned int gum::credal::LRSWrapper< GUM_SCALAR >::_vertices_
private

The number of vertices of the polytope.

Definition at line 141 of file LrsWrapper.h.

Referenced by LRSWrapper(), elimRedundVrep(), fillV(), getVerticesNumber(), H2V(), nextHInput(), setUpV(), and tearDown().

◆ _volume_

template<GUM_Numeric GUM_SCALAR>
GUM_SCALAR gum::credal::LRSWrapper< GUM_SCALAR >::_volume_
private

The volume of the polytope, if computed, 0 otherwise.

Definition at line 169 of file LrsWrapper.h.

Referenced by LRSWrapper(), computeVolume(), getVolume(), nextHInput(), and tearDown().


The documentation for this class was generated from the following files: