aGrUM  0.14.2
LrsWrapper.h
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005 by Pierre-Henri WUILLEMIN and Christophe GONZALES *
3  * {prenom.nom}_at_lip6.fr *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19  ***************************************************************************/
20 
29 #ifndef __LRSWrapper_WRAPPER__H__
30 #define __LRSWrapper_WRAPPER__H__
31 
32 #include <agrum/agrum.h>
33 #include <agrum/core/math/math.h>
34 
35 
36 #ifdef HAVE_UNISTD_H
37 # include <unistd.h>
38 #else
39 # include <agrum/core/mvsc/unistd.h>
40 #endif
41 
42 #include <chrono>
43 #include <cstdio>
44 #include <fcntl.h>
45 #include <fstream>
46 #include <unordered_set>
47 #include <vector>
48 
50 
51 // we force MP (not long or GMP)
52 #undef LONG
53 #undef LRSLONG
54 #undef GMP
55 #define MP
56 // lrs stuff
57 extern "C" {
59 }
60 /* *** from lrs, we need to know BASE to read multiple precision integers *** */
61 #ifdef B32
62 /*32 bit machines */
63 # define FORMAT "%4.4lu"
64 # define MAXD 2147483647L
65 # define BASE 10000L
66 # define BASE_DIG 4
67 # define INTSIZE 8L
68 # define BIT "32bit"
69 #else
70 /* 64 bit machines */
71 # define MAXD 9223372036854775807L
72 # define BASE 1000000000L
73 # define FORMAT "%9.9lu"
74 # define BASE_DIG 9
75 # define INTSIZE 16L
76 # define BIT "64bit"
77 #endif
78 
79 // 64 bits for windows (long is 32 bits)
80 #ifdef _MSC_VER
81 typedef __int64 int64_t;
82 typedef unsigned __int64 uint64_t;
83 #else
84 # include <stdint.h>
85 #endif
86 
87 /* ************ */
88 
89 #define enumStringify(name) #name
90 
91 namespace gum {
92  namespace credal {
93 
104  template < typename GUM_SCALAR >
105  class LRSWrapper {
106  private:
108  using matrix = typename std::vector< std::vector< GUM_SCALAR > >;
109 
113 
117 
119  unsigned int __card;
120 
123  std::unordered_set< int > __insertedModals;
124 
126  unsigned int __vertices;
127 
131  std::vector< std::vector< GUM_SCALAR > > __insertedVertices;
132 
135  std::vector< GUM_SCALAR > __vertex;
136 
142  enum class __states : char {
143  none = char(0),
144  Hup = char(1),
145  Vup = char(2),
146  H2Vready = char(3),
147  V2Hready = char(4),
148  };
149 
152 
154  GUM_SCALAR __volume;
155 
158  const char* __setUpStateNames[5] = {
160  enumStringify(__states::nHup),
161  enumStringify(__states::nVup),
162  enumStringify(__states::nH2Vready),
163  enumStringify(__states::nV2Hready),
164  };
165 
174  mutable int __oldCout;
175 
178 
180  lrs_dic* __dic;
181 
183  lrs_dat* __dat;
184 
188  lrs_mp_vector __lrsOutput;
189 
191  lrs_mp_matrix __Lin;
192 
194 
197 
199 
200  bool __hull;
201 
203 
205 
208 
210  void __coutOff() const;
211 
213  void __coutOn() const;
214 
216 
219 
221  void __freeLrs();
222 
224  void __initLrs();
225 
234  void __fill() const;
235 
246  void __getLRSWrapperOutput(lrs_mp Nin,
247  lrs_mp Din,
248  std::vector< int64_t >& Num,
249  std::vector< int64_t >& Den) const;
250 
252 
253  public:
256 
260  LRSWrapper();
261 
265  ~LRSWrapper();
266 
268 
271 
276  const matrix& getInput() const;
277 
282  const matrix& getOutput() const;
283 
288  const unsigned int& getVerticesNumber() const;
289 
306  const GUM_SCALAR& getVolume() const;
307 
309 
312 
321  void setUpH(const Size& card);
322 
333  void setUpV(const Size& card, const Size& vertices);
334 
342  void tearDown();
343 
357  void nextHInput();
358 
360 
363 
373  void fillH(const GUM_SCALAR& min, const GUM_SCALAR& max, const Size& modal);
374 
382  void fillMatrix(const std::vector< std::vector< GUM_SCALAR > >& matrix);
383 
392  void fillV(const std::vector< GUM_SCALAR >& vertex);
393 
395 
398 
406  void H2V();
407 
415  void V2H();
416 
432  void computeVolume();
433 
440  void elimRedundVrep();
441 
446  };
448 
449  } // namespace credal
450 } // namespace gum
451 
452 #include <agrum/CN/LrsWrapper_tpl.h>
453 
454 #endif
Useful macros for maths.
lrs_dat * __dat
Structure for holding static problem data of lrs.
Definition: LrsWrapper.h:183
~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:119
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:116
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.
gum is the global namespace for all aGrUM entities
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:135
typename std::vector< std::vector< GUM_SCALAR > > matrix
Shortcut for dynamic matrix using vectors.
Definition: LrsWrapper.h:108
#define enumStringify(name)
Definition: LrsWrapper.h:89
lrs_dic * __dic
Structure for holding current dictionary and indices of lrs.
Definition: LrsWrapper.h:180
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:151
Class template acting as a wrapper for Lexicographic Reverse Search by David Avis.
Definition: LrsWrapper.h:105
void setUpH(const Size &card)
Sets up an H-representation.
int __oldCout
File descriptor of standard cout.
Definition: LrsWrapper.h:174
std::unordered_set< int > __insertedModals
To keep track of which constraints over modalities have been inserted.
Definition: LrsWrapper.h:123
unsigned int __vertices
The number of vertices of the polytope.
Definition: LrsWrapper.h:126
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:112
const char * __setUpStateNames[5]
To print an enum field name instead of it&#39;s value.
Definition: LrsWrapper.h:158
lrs_mp_matrix __Lin
Holds lrs input linearities if any are found.
Definition: LrsWrapper.h:191
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:188
void H2V()
H-representation to V-representation.
GUM_SCALAR __volume
The volume of the polytope, if computed, 0 otherwise.
Definition: LrsWrapper.h:154
void elimRedundVrep()
V-Redundancy elimination.
__states
The possible states of the LrsWrapper.
Definition: LrsWrapper.h:142
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.
Class template used to approximate decimal numbers by rationals.
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:45
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:131
const unsigned int & getVerticesNumber() const
Get the number of vertices of this polytope.