52 template <
typename GUM_SCALAR >
68 template <
typename GUM_SCALAR >
73 template <
typename GUM_SCALAR >
78 template <
typename GUM_SCALAR >
83 template <
typename GUM_SCALAR >
88 template <
typename GUM_SCALAR >
93 "LRSWrapper< GUM_SCALAR >::getVolume () : "
94 "volume computation was not asked for this "
95 "credal set, call computeVolume() from a "
99 template <
typename GUM_SCALAR >
103 "LRSWrapper< GUM_SCALAR >::setUpH : "
104 "cardinality must be at least 2");
108 _input_ = std::vector< std::vector< GUM_SCALAR > >(card * 2 + 2,
109 std::vector< GUM_SCALAR >(card + 1, 0));
111 _input_[card * 2] = std::vector< GUM_SCALAR >(card + 1, -1);
114 _input_[card * 2 + 1] = std::vector< GUM_SCALAR >(card + 1, 1);
117 _output_ = std::vector< std::vector< GUM_SCALAR > >();
119 _vertex_ = std::vector< GUM_SCALAR >(card);
123 _card_ = (
unsigned int)card;
126 template <
typename GUM_SCALAR >
130 "LRSWrapper< GUM_SCALAR >::setUpV : "
131 "cardinality must be at least 2");
135 "LRSWrapper< GUM_SCALAR >::setUpV : vertices "
136 "must be at least 2 to build a polytope");
140 _input_ = std::vector< std::vector< GUM_SCALAR > >(vertices,
141 std::vector< GUM_SCALAR >(card + 1, 1));
143 _output_ = std::vector< std::vector< GUM_SCALAR > >();
147 _card_ = (
unsigned int)card;
151 template <
typename GUM_SCALAR >
171 template <
typename GUM_SCALAR >
190 "LRSWrapper< GUM_SCALAR >::nextHInput : only for H-representation "
191 "as input. Previous state was : "
201 template <
typename GUM_SCALAR >
203 const GUM_SCALAR& max,
207 "LRSWrapper< GUM_SCALAR >::fillH : setUpH or nextInput has not "
208 "been called or H-representation is complete, current state is : "
213 "LRSWrapper< GUM_SCALAR >::fillH : modality is "
214 "greater or equal than cardinality : "
215 << modal <<
" >= " <<
_card_);
218 _input_[modal * 2][modal + 1] = 1;
220 _input_[modal * 2 + 1][0] = max;
221 _input_[modal * 2 + 1][modal + 1] = -1;
230 template <
typename GUM_SCALAR >
232 const std::vector< std::vector< GUM_SCALAR > >&
matrix) {
235 "LRSWrapper< GUM_SCALAR >::fillH : setUpH or nextInput has not "
236 "been called or H-representation is complete, current state is : "
241 "LRSWrapper< GUM_SCALAR >::fillMatrix : size is "
242 "different than cardinality : "
247 for (
unsigned int modal = 0; modal <
_card_; modal++) {
254 template <
typename GUM_SCALAR >
258 "LRSWrapper< GUM_SCALAR >::fillV : setUpV or nextInput has not "
259 "been called or V-representation is complete, current state is : "
264 "LRSWrapper< GUM_SCALAR >::fillV : input is already full with " <<
_vertices_
273 if (std::fabs(v[mod] - vertex[mod]) > 1e-6) {
290 _input_[row][mod + 1] = vertex[mod];
297 template <
typename GUM_SCALAR >
301 "LRSWrapper< GUM_SCALAR >::H2V : fillH has not been called with "
302 "all modalities, current state is still : "
313 if (std::fabs(sum - 1) < 1e-6) {
330 std::vector< int64_t > Num;
331 std::vector< int64_t > Den;
334 for (
decltype(
_dic_->d) col = 0, end =
_dic_->d; col <= end; col++)
343 "LRSWrapper< GUM_SCALAR >::H2V : asked for "
344 "Q-hull computation or not reading a vertex !");
346 for (
decltype(
_dat_->n) i = 1, end =
_dat_->n; i < end; i++)
349 }
while (lrs_getnextbasis(&
_dic_,
_dat_, 0L));
351 auto vtx = Num.size();
352 std::vector< GUM_SCALAR > vertex(
_card_);
354 for (
decltype(vtx) i = 1; i <= vtx; i++) {
355 vertex[(i - 1) %
_card_] = GUM_SCALAR(Num[i - 1] * 1.0 / Den[i - 1]);
366 template <
typename GUM_SCALAR >
370 "LRSWrapper< GUM_SCALAR >::V2H : fillV has "
371 "not been called with all vertices, current "
376 template <
typename GUM_SCALAR >
380 "LRSWrapper< GUM_SCALAR >::computeVolume : "
381 "volume is only for V-representation or "
382 "fillV has not been called with all "
383 "vertices, current state is still : "
391 for (
decltype(
_dic_->d) col = 0, end =
_dic_->d; col <= end; col++)
393 }
while (lrs_getnextbasis(&
_dic_,
_dat_, 0L));
395 int64_t Nsize = (
_dat_->Nvolume[0] > 0) ?
_dat_->Nvolume[0] : -
_dat_->Nvolume[0];
396 int64_t Dsize = (
_dat_->Dvolume[0] > 0) ?
_dat_->Dvolume[0] : -
_dat_->Dvolume[0];
398 int64_t num = 0L, den = 0L;
401 for (
decltype(Nsize) i = Nsize - 1; i > 0; i--) {
402 tmp =
_dat_->Nvolume[i];
404 for (
decltype(i) j = 1; j < i; j++)
410 for (
decltype(Dsize) i = Dsize - 1; i > 0; i--) {
411 tmp =
_dat_->Dvolume[i];
413 for (
decltype(i) j = 1; j < i; j++)
424 template <
typename GUM_SCALAR >
428 "LRSWrapper< GUM_SCALAR >::elimRedundVrep : only for "
429 "V-representation or fillV has not been called with all vertices, "
430 "current state is still : "
446 auto nlinearity =
_dat_->nlinearity;
447 auto lastdv =
_dat_->lastdv;
450 redineq = (int64_t*)calloc(std::size_t(m + 1),
sizeof(int64_t));
452 for (
decltype(nlinearity) i = 0; i < nlinearity; i++)
453 redineq[
_dat_->linearity[i]] = 2L;
458 for (
decltype(m + d) index = lastdv + 1, end = m + d; index <= end; index++) {
460 auto ineq =
_dat_->inequality[index - lastdv];
463 redineq[ineq] = checkindex(
_dic_,
_dat_, index);
469 "LRSWrapper< GUM_SCALAR >::elimRedundVrep : not "
470 "reading a vertex but a linearity !");
485 for (
decltype(m) i = 1; i <= m; i++)
487 _output_.push_back(std::vector< GUM_SCALAR >(++
_input_[std::size_t(i - 1)].begin(),
488 _input_[std::size_t(i - 1)].end()));
495 template <
typename GUM_SCALAR >
498 std::vector< int64_t >& Num,
499 std::vector< int64_t >& Den)
const {
500 int64_t Nsize = (Nin[0] > 0) ? Nin[0] : -Nin[0];
501 int64_t Dsize = (Din[0] > 0) ? Din[0] : -Din[0];
508 for (
decltype(Nsize) i = Nsize - 1; i > 0; i--) {
511 for (
decltype(i) j = 1; j < i; j++)
517 if (!(Din[0] == 2L && Din[1] == 1L)) {
518 for (
decltype(Dsize) i = Dsize - 1; i > 0; i--) {
521 for (
decltype(i) j = 1; j < i; j++)
530 int64_t Nsign = ((Nin[0] < 0) ? -1L : 1L);
531 int64_t Dsign = ((Din[0] < 0) ? -1L : 1L);
533 if ((Nsign * Dsign) == -1L) num = -num;
553 template <
typename GUM_SCALAR >
555 std::size_t cols =
_input_[0].size();
557 int64_t* num =
new int64_t[cols];
559 int64_t* den =
new int64_t[cols];
561 int64_t rows = int64_t(
_input_.size());
563 int64_t numerator, denominator;
565 for (int64_t row = 0; row < rows; row++) {
566 for (std::size_t col = 0; col < cols; col++) {
569 _input_[std::size_t(row)][col]);
571 num[col] = numerator;
572 den[col] = denominator;
589 template <
typename GUM_SCALAR >
593 "LRSWrapper< GUM_SCALAR >:: _initLrs_ : not ready, current state "
597 std::string name =
"\n*LrsWrapper:";
598 std::vector< char > chars(name.c_str(), name.c_str() + name.size() + 1u);
601 if (!lrs_init(&chars[0])) {
605 name =
"LRSWrapper globals";
606 chars = std::vector< char >(name.c_str(), name.c_str() + name.size() + 1u);
608 _dat_ = lrs_alloc_dat(&chars[0]);
610 if (
_dat_ ==
nullptr) {
625 if (
_dic_ ==
nullptr) {
641 decltype(
_dat_->nredundcol) startcol = 0;
646 if (!
_dat_->restart) {
647 for (
decltype(
_dat_->nredundcol) col = startcol; col < _dat_->nredundcol; col++)
655 template <
typename GUM_SCALAR >
663 if (
_dat_->runs > 0) {
668 auto savem =
_dic_->m;
675 std::string name =
"LrsWrapper:";
676 std::vector< char > chars(name.c_str(), name.c_str() + name.size() + 1u);
678 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