56 template < GUM_Numeric GUM_SCALAR >
72 template < GUM_Numeric GUM_SCALAR >
77 template < GUM_Numeric GUM_SCALAR >
82 template < GUM_Numeric GUM_SCALAR >
87 template < GUM_Numeric GUM_SCALAR >
92 template < GUM_Numeric GUM_SCALAR >
97 "LRSWrapper< GUM_SCALAR >::getVolume () : "
98 "volume computation was not asked for this "
99 "credal set, call computeVolume() from a "
100 "V-representation.");
103 template < GUM_Numeric GUM_SCALAR >
107 "LRSWrapper< GUM_SCALAR >::setUpH : "
108 "cardinality must be at least 2");
112 _input_ = std::vector< std::vector< GUM_SCALAR > >(card * 2 + 2,
113 std::vector< GUM_SCALAR >(card + 1, 0));
115 _input_[card * 2] = std::vector< GUM_SCALAR >(card + 1, -1);
118 _input_[card * 2 + 1] = std::vector< GUM_SCALAR >(card + 1, 1);
121 _output_ = std::vector< std::vector< GUM_SCALAR > >();
123 _vertex_ = std::vector< GUM_SCALAR >(card);
127 _card_ = (
unsigned int)card;
130 template < GUM_Numeric GUM_SCALAR >
134 "LRSWrapper< GUM_SCALAR >::setUpV : "
135 "cardinality must be at least 2");
139 "LRSWrapper< GUM_SCALAR >::setUpV : vertices "
140 "must be at least 2 to build a polytope");
144 _input_ = std::vector< std::vector< GUM_SCALAR > >(vertices,
145 std::vector< GUM_SCALAR >(card + 1, 1));
147 _output_ = std::vector< std::vector< GUM_SCALAR > >();
151 _card_ = (
unsigned int)card;
155 template < GUM_Numeric GUM_SCALAR >
175 template < GUM_Numeric GUM_SCALAR >
194 "LRSWrapper< GUM_SCALAR >::nextHInput : only for H-representation "
195 "as input. Previous state was : "
205 template < GUM_Numeric GUM_SCALAR >
207 const GUM_SCALAR& max,
211 "LRSWrapper< GUM_SCALAR >::fillH : setUpH or nextInput has not "
212 "been called or H-representation is complete, current state is : "
217 "LRSWrapper< GUM_SCALAR >::fillH : modality is "
218 "greater or equal than cardinality : "
219 << modal <<
" >= " <<
_card_);
222 _input_[modal * 2][modal + 1] = 1;
224 _input_[modal * 2 + 1][0] = max;
225 _input_[modal * 2 + 1][modal + 1] = -1;
234 template < GUM_Numeric GUM_SCALAR >
236 const std::vector< std::vector< GUM_SCALAR > >&
matrix) {
239 "LRSWrapper< GUM_SCALAR >::fillH : setUpH or nextInput has not "
240 "been called or H-representation is complete, current state is : "
245 "LRSWrapper< GUM_SCALAR >::fillMatrix : size is "
246 "different than cardinality : "
251 for (
unsigned int modal = 0; modal <
_card_; modal++) {
258 template < GUM_Numeric GUM_SCALAR >
262 "LRSWrapper< GUM_SCALAR >::fillV : setUpV or nextInput has not "
263 "been called or V-representation is complete, current state is : "
268 "LRSWrapper< GUM_SCALAR >::fillV : input is already full with " <<
_vertices_
278 if (std::fabs(v[mod] - vertex[mod]) > epsilon) {
295 _input_[row][mod + 1] = vertex[mod];
302 template < GUM_Numeric GUM_SCALAR >
306 "LRSWrapper< GUM_SCALAR >::H2V : fillH has not been called with "
307 "all modalities, current state is still : "
335 std::vector< int64_t > Num;
336 std::vector< int64_t > Den;
339 for (
decltype(
_dic_->d) col = 0, end =
_dic_->d; col <= end; col++)
348 "LRSWrapper< GUM_SCALAR >::H2V : asked for "
349 "Q-hull computation or not reading a vertex !");
351 for (
decltype(
_dat_->n) i = 1, end =
_dat_->n; i < end; i++)
354 }
while (lrs_getnextbasis(&
_dic_,
_dat_, 0L));
356 auto vtx = Num.size();
357 std::vector< GUM_SCALAR > vertex(
_card_);
359 for (
decltype(vtx) i = 1; i <= vtx; i++) {
360 vertex[(i - 1) %
_card_] = GUM_SCALAR(Num[i - 1] * 1.0 / Den[i - 1]);
371 template < GUM_Numeric GUM_SCALAR >
375 "LRSWrapper< GUM_SCALAR >::V2H : fillV has "
376 "not been called with all vertices, current "
381 template < GUM_Numeric GUM_SCALAR >
385 "LRSWrapper< GUM_SCALAR >::computeVolume : "
386 "volume is only for V-representation or "
387 "fillV has not been called with all "
388 "vertices, current state is still : "
396 for (
decltype(
_dic_->d) col = 0, end =
_dic_->d; col <= end; col++)
398 }
while (lrs_getnextbasis(&
_dic_,
_dat_, 0L));
400 int64_t Nsize = (
_dat_->Nvolume[0] > 0) ?
_dat_->Nvolume[0] : -
_dat_->Nvolume[0];
401 int64_t Dsize = (
_dat_->Dvolume[0] > 0) ?
_dat_->Dvolume[0] : -
_dat_->Dvolume[0];
403 int64_t num = 0L, den = 0L;
406 for (
decltype(Nsize) i = Nsize - 1; i > 0; i--) {
407 tmp =
_dat_->Nvolume[i];
409 for (
decltype(i) j = 1; j < i; j++)
415 for (
decltype(Dsize) i = Dsize - 1; i > 0; i--) {
416 tmp =
_dat_->Dvolume[i];
418 for (
decltype(i) j = 1; j < i; j++)
429 template < GUM_Numeric GUM_SCALAR >
433 "LRSWrapper< GUM_SCALAR >::elimRedundVrep : only for "
434 "V-representation or fillV has not been called with all vertices, "
435 "current state is still : "
451 auto nlinearity =
_dat_->nlinearity;
452 auto lastdv =
_dat_->lastdv;
455 redineq = (int64_t*)calloc(std::size_t(m + 1),
sizeof(int64_t));
457 for (
decltype(nlinearity) i = 0; i < nlinearity; i++)
458 redineq[
_dat_->linearity[i]] = 2L;
463 for (
decltype(m + d) index = lastdv + 1, end = m + d; index <= end; index++) {
465 auto ineq =
_dat_->inequality[index - lastdv];
468 redineq[ineq] = checkindex(
_dic_,
_dat_, index);
474 "LRSWrapper< GUM_SCALAR >::elimRedundVrep : not "
475 "reading a vertex but a linearity !");
490 for (
decltype(m) i = 1; i <= m; i++)
492 _output_.push_back(std::vector< GUM_SCALAR >(++
_input_[std::size_t(i - 1)].begin(),
493 _input_[std::size_t(i - 1)].end()));
500 template < GUM_Numeric GUM_SCALAR >
503 std::vector< int64_t >& Num,
504 std::vector< int64_t >& Den)
const {
505 int64_t Nsize = (Nin[0] > 0) ? Nin[0] : -Nin[0];
506 int64_t Dsize = (Din[0] > 0) ? Din[0] : -Din[0];
513 for (
decltype(Nsize) i = Nsize - 1; i > 0; i--) {
516 for (
decltype(i) j = 1; j < i; j++)
522 if (!(Din[0] == 2L && Din[1] == 1L)) {
523 for (
decltype(Dsize) i = Dsize - 1; i > 0; i--) {
526 for (
decltype(i) j = 1; j < i; j++)
535 int64_t Nsign = ((Nin[0] < 0) ? -1L : 1L);
536 int64_t Dsign = ((Din[0] < 0) ? -1L : 1L);
538 if ((Nsign * Dsign) == -1L) num = -num;
558 template < GUM_Numeric GUM_SCALAR >
560 std::size_t cols =
_input_[0].size();
562 int64_t* num =
new int64_t[cols];
564 int64_t* den =
new int64_t[cols];
566 int64_t rows = int64_t(
_input_.size());
568 int64_t numerator, denominator;
570 for (int64_t row = 0; row < rows; row++) {
571 for (std::size_t col = 0; col < cols; col++) {
574 _input_[std::size_t(row)][col]);
576 num[col] = numerator;
577 den[col] = denominator;
594 template < GUM_Numeric GUM_SCALAR >
598 "LRSWrapper< GUM_SCALAR >:: _initLrs_ : not ready, current state "
602 std::string name =
"\n*LrsWrapper:";
603 std::vector< char > chars(name.c_str(), name.c_str() + name.size() + 1u);
606 if (!lrs_init(&chars[0])) {
610 name =
"LRSWrapper globals";
611 chars = std::vector< char >(name.c_str(), name.c_str() + name.size() + 1u);
613 _dat_ = lrs_alloc_dat(&chars[0]);
615 if (
_dat_ ==
nullptr) {
630 if (
_dic_ ==
nullptr) {
646 decltype(
_dat_->nredundcol) startcol = 0;
651 if (!
_dat_->restart) {
652 for (
decltype(
_dat_->nredundcol) col = startcol; col < _dat_->nredundcol; col++)
660 template < GUM_Numeric GUM_SCALAR >
668 if (
_dat_->runs > 0) {
673 auto savem =
_dic_->m;
680 std::string name =
"LrsWrapper:";
681 std::vector< char > chars(name.c_str(), name.c_str() + name.size() + 1u);
683 lrs_close(&chars[0]);
double epsilonStandard() const noexcept
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=config().epsilonStandard())
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 ...
Typed, documented, sectioned configuration singleton read from an .ini content.
#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
Configuration & config()
A short name for Configuration::instance() : gum::config().epsilonStandard(). It is not inline so tha...
bool isCloseToOne(T x, T tol=static_cast< T >(config().epsilonFine()))
Is |x - 1| <= tol ? By default, tol is the key core.epsilonFine of the configuration.