aGrUM  0.16.0
LrsWrapper.h
Go to the documentation of this file.
1 
31 #ifndef __LRSWrapper_WRAPPER__H__
32 #define __LRSWrapper_WRAPPER__H__
33 
34 #include <agrum/agrum.h>
35 #include <agrum/core/math/math.h>
36 
37 
38 #ifdef HAVE_UNISTD_H
39 # include <unistd.h>
40 #else
41 # include <agrum/core/mvsc/unistd.h>
42 #endif
43 
44 #include <chrono>
45 #include <cstdio>
46 #include <fcntl.h>
47 #include <fstream>
48 #include <unordered_set>
49 #include <vector>
50 
52 
53 // we force MP (not long or GMP)
54 #undef LONG
55 #undef LRSLONG
56 #undef GMP
57 #define MP
58 // lrs stuff
59 extern "C" {
61 }
62 /* *** from lrs, we need to know BASE to read multiple precision integers *** */
63 #ifdef B32
64 /*32 bit machines */
65 # define FORMAT "%4.4lu"
66 # define MAXD 2147483647L
67 # define BASE 10000L
68 # define BASE_DIG 4
69 # define INTSIZE 8L
70 # define BIT "32bit"
71 #else
72 /* 64 bit machines */
73 # define MAXD 9223372036854775807L
74 # define BASE 1000000000L
75 # define FORMAT "%9.9lu"
76 # define BASE_DIG 9
77 # define INTSIZE 16L
78 # define BIT "64bit"
79 #endif
80 
81 // 64 bits for windows (long is 32 bits)
82 #ifdef _MSC_VER
83 typedef __int64 int64_t;
84 typedef unsigned __int64 uint64_t;
85 #else
86 # include <stdint.h>
87 #endif
88 
89 /* ************ */
90 
91 #define enumStringify(name) #name
92 
93 namespace gum {
94  namespace credal {
95 
106  template < typename GUM_SCALAR >
107  class LRSWrapper {
108  private:
110  using matrix = typename std::vector< std::vector< GUM_SCALAR > >;
111 
115 
119 
121  unsigned int __card;
122 
125  std::unordered_set< int > __insertedModals;
126 
128  unsigned int __vertices;
129 
133  std::vector< std::vector< GUM_SCALAR > > __insertedVertices;
134 
137  std::vector< GUM_SCALAR > __vertex;
138 
144  enum class __states : char {
145  none = char(0),
146  Hup = char(1),
147  Vup = char(2),
148  H2Vready = char(3),
149  V2Hready = char(4),
150  };
151 
154 
156  GUM_SCALAR __volume;
157 
160  const char* __setUpStateNames[5] = {
162  enumStringify(__states::nHup),
163  enumStringify(__states::nVup),
164  enumStringify(__states::nH2Vready),
165  enumStringify(__states::nV2Hready),
166  };
167 
176  mutable int __oldCout;
177 
180 
182  lrs_dic* __dic;
183 
185  lrs_dat* __dat;
186 
190  lrs_mp_vector __lrsOutput;
191 
193  lrs_mp_matrix __Lin;
194 
196 
199 
201 
202  bool __hull;
203 
205 
207 
210 
212  void __coutOff() const;
213 
215  void __coutOn() const;
216 
218 
221 
223  void __freeLrs();
224 
226  void __initLrs();
227 
236  void __fill() const;
237 
248  void __getLRSWrapperOutput(lrs_mp Nin,
249  lrs_mp Din,
250  std::vector< int64_t >& Num,
251  std::vector< int64_t >& Den) const;
252 
254 
255  public:
258 
262  LRSWrapper();
263 
267  ~LRSWrapper();
268 
270 
273 
278  const matrix& getInput() const;
279 
284  const matrix& getOutput() const;
285 
290  const unsigned int& getVerticesNumber() const;
291 
308  const GUM_SCALAR& getVolume() const;
309 
311 
314 
323  void setUpH(const Size& card);
324 
335  void setUpV(const Size& card, const Size& vertices);
336 
344  void tearDown();
345 
359  void nextHInput();
360 
362 
365 
375  void fillH(const GUM_SCALAR& min, const GUM_SCALAR& max, const Size& modal);
376 
384  void fillMatrix(const std::vector< std::vector< GUM_SCALAR > >& matrix);
385 
394  void fillV(const std::vector< GUM_SCALAR >& vertex);
395 
397 
400 
408  void H2V();
409 
417  void V2H();
418 
434  void computeVolume();
435 
442  void elimRedundVrep();
443 
448  };
450 
451  } // namespace credal
452 } // namespace gum
453 
454 #include <agrum/CN/LrsWrapper_tpl.h>
455 
456 #endif
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
lrs_dat * __dat
Structure for holding static problem data of lrs.
Definition: LrsWrapper.h:185
~LRSWrapper()
Default Destructor.
void V2H()
V-representation to H-representation.
void fillMatrix(const std::vector< std::vector< GUM_SCALAR > > &matrix)
Fill the H-representation from the matrix given in argument.
unsigned int __card
Cardinality of the variable.
Definition: LrsWrapper.h:121
void __coutOff() const
The function that redirects standard cout to /dev/null.
matrix __output
Output matrix - either a V-representation or an H-representation.
Definition: LrsWrapper.h:118
const GUM_SCALAR & getVolume() const
Get the volume of the polytope that has been computed.
const matrix & getInput() const
Get the intput matrix of the problem.
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.
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Definition: agrum.h:25
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...
Definition: LrsWrapper.h:137
typename std::vector< std::vector< GUM_SCALAR > > matrix
Shortcut for dynamic matrix using vectors.
Definition: LrsWrapper.h:110
#define enumStringify(name)
Definition: LrsWrapper.h:91
lrs_dic * __dic
Structure for holding current dictionary and indices of lrs.
Definition: LrsWrapper.h:182
void __freeLrs()
Free lrs space.
void fillV(const std::vector< GUM_SCALAR > &vertex)
Creates the V-representation of a polytope by adding a vertex to the problem input __input...
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...
__states __state
The current state of the LrsWrapper.
Definition: LrsWrapper.h:153
Class template acting as a wrapper for Lexicographic Reverse Search by David Avis.
Definition: LrsWrapper.h:107
void setUpH(const Size &card)
Sets up an H-representation.
int __oldCout
File descriptor of standard cout.
Definition: LrsWrapper.h:176
std::unordered_set< int > __insertedModals
To keep track of which constraints over modalities have been inserted.
Definition: LrsWrapper.h:125
unsigned int __vertices
The number of vertices of the polytope.
Definition: LrsWrapper.h:128
void nextHInput()
Reset the wrapper for next computation for a H-representation with the same variable cardinality and ...
void tearDown()
Reset the wrapper as if it was built.
void __coutOn() const
The function that restores standard cout.
matrix __input
Input matrix - either a V-representation or an H-representation.
Definition: LrsWrapper.h:114
const char * __setUpStateNames[5]
To print an enum field name instead of it&#39;s value.
Definition: LrsWrapper.h:160
lrs_mp_matrix __Lin
Holds lrs input linearities if any are found.
Definition: LrsWrapper.h:193
void setUpV(const Size &card, const Size &vertices)
Sets up a V-representation.
lrs_mp_vector __lrsOutput
One line of output of lrs : aither a ray, a vertex, a facet or a linearity.
Definition: LrsWrapper.h:190
void H2V()
H-representation to V-representation.
GUM_SCALAR __volume
The volume of the polytope, if computed, 0 otherwise.
Definition: LrsWrapper.h:156
void elimRedundVrep()
V-Redundancy elimination.
__states
The possible states of the LrsWrapper.
Definition: LrsWrapper.h:144
void __fill() const
Fill lrs_dictionnary and datas from __input using integer rationals.
const matrix & getOutput() const
Get the output matrix solution of the problem.
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:48
LRSWrapper()
Default Constructor.
void computeVolume()
Computes a polytope ( pseudo ) volume from it&#39;s V-representation.
std::vector< std::vector< GUM_SCALAR > > __insertedVertices
To keep track of inserted vertices and total.
Definition: LrsWrapper.h:133
const unsigned int & getVerticesNumber() const
Get the number of vertices of this polytope.