aGrUM  0.13.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 
34 #include <cmath>
35 #ifdef HAVE_UNISTD_H
36 # include <unistd.h>
37 #else
38 # include <agrum/core/mvsc/unistd.h>
39 #endif
40 
41 #include <chrono>
42 #include <cstdio>
43 #include <fcntl.h>
44 #include <fstream>
45 #include <unordered_set>
46 #include <vector>
47 
49 
50 // we force MP (not long or GMP)
51 #undef LONG
52 #undef LRSLONG
53 #undef GMP
54 #define MP
55 // lrs stuff
56 extern "C" {
58 }
59 /* *** from lrs, we need to know BASE to read multiple precision integers *** */
60 #ifdef B32
61 /*32 bit machines */
62 # define FORMAT "%4.4lu"
63 # define MAXD 2147483647L
64 # define BASE 10000L
65 # define BASE_DIG 4
66 # define INTSIZE 8L
67 # define BIT "32bit"
68 #else
69 /* 64 bit machines */
70 # define MAXD 9223372036854775807L
71 # define BASE 1000000000L
72 # define FORMAT "%9.9lu"
73 # define BASE_DIG 9
74 # define INTSIZE 16L
75 # define BIT "64bit"
76 #endif
77 
78 // 64 bits for windows (long is 32 bits)
79 #ifdef _MSC_VER
80 typedef __int64 int64_t;
81 typedef unsigned __int64 uint64_t;
82 #else
83 # include <stdint.h>
84 #endif
85 
86 /* ************ */
87 
88 #define enumStringify(name) #name
89 
90 namespace gum {
91  namespace credal {
92 
103  template < typename GUM_SCALAR >
104  class LRSWrapper {
105  private:
107  using matrix = typename std::vector< std::vector< GUM_SCALAR > >;
108 
112 
116 
118  unsigned int __card;
119 
122  std::unordered_set< int > __insertedModals;
123 
125  unsigned int __vertices;
126 
130  std::vector< std::vector< GUM_SCALAR > > __insertedVertices;
131 
134  std::vector< GUM_SCALAR > __vertex;
135 
141  enum class __states : char {
142  none = char(0),
143  Hup = char(1),
144  Vup = char(2),
145  H2Vready = char(3),
146  V2Hready = char(4),
147  };
148 
151 
153  GUM_SCALAR __volume;
154 
157  const char* __setUpStateNames[5] = {
159  enumStringify(__states::nHup),
160  enumStringify(__states::nVup),
161  enumStringify(__states::nH2Vready),
162  enumStringify(__states::nV2Hready),
163  };
164 
173  mutable int __oldCout;
174 
177 
179  lrs_dic* __dic;
180 
182  lrs_dat* __dat;
183 
187  lrs_mp_vector __lrsOutput;
188 
190  lrs_mp_matrix __Lin;
191 
193 
196 
198 
199  bool __hull;
200 
202 
204 
207 
209  void __coutOff() const;
210 
212  void __coutOn() const;
213 
215 
218 
220  void __freeLrs();
221 
223  void __initLrs();
224 
233  void __fill() const;
234 
245  void __getLRSWrapperOutput(lrs_mp Nin,
246  lrs_mp Din,
247  std::vector< int64_t >& Num,
248  std::vector< int64_t >& Den) const;
249 
251 
252  public:
255 
259  LRSWrapper();
260 
264  ~LRSWrapper();
265 
267 
270 
275  const matrix& getInput() const;
276 
281  const matrix& getOutput() const;
282 
287  const unsigned int& getVerticesNumber() const;
288 
305  const GUM_SCALAR& getVolume() const;
306 
308 
311 
320  void setUpH(const unsigned int& card);
321 
332  void setUpV(const unsigned int& card, const unsigned int& vertices);
333 
341  void tearDown();
342 
356  void nextHInput();
357 
359 
362 
372  void fillH(const GUM_SCALAR& min,
373  const GUM_SCALAR& max,
374  const unsigned int& modal);
375 
383  void fillMatrix(const std::vector< std::vector< GUM_SCALAR > >& matrix);
384 
393  void fillV(const std::vector< GUM_SCALAR >& vertex);
394 
396 
399 
407  void H2V();
408 
416  void V2H();
417 
433  void computeVolume();
434 
441  void elimRedundVrep();
442 
447  };
449 
450  } // namespace credal
451 } // namespace gum
452 
453 #include <agrum/CN/LrsWrapper_tpl.h>
454 
455 #endif
const GUM_SCALAR & getVolume() const
Get the volume of the polytope that has been computed.
lrs_dat * __dat
Structure for holding static problem data of lrs.
Definition: LrsWrapper.h:182
void setUpH(const unsigned int &card)
Sets up an H-representation.
~LRSWrapper()
Default Destructor.
void setUpV(const unsigned int &card, const unsigned int &vertices)
Sets up a V-representation.
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:118
matrix __output
Output matrix - either a V-representation or an H-representation.
Definition: LrsWrapper.h:115
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:134
typename std::vector< std::vector< GUM_SCALAR > > matrix
Shortcut for dynamic matrix using vectors.
Definition: LrsWrapper.h:107
const matrix & getOutput() const
Get the output matrix solution of the problem.
#define enumStringify(name)
Definition: LrsWrapper.h:88
lrs_dic * __dic
Structure for holding current dictionary and indices of lrs.
Definition: LrsWrapper.h:179
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...
__states __state
The current state of the LrsWrapper.
Definition: LrsWrapper.h:150
Class template acting as a wrapper for Lexicographic Reverse Search by David Avis.
Definition: LrsWrapper.h:104
int __oldCout
File descriptor of standard cout.
Definition: LrsWrapper.h:173
std::unordered_set< int > __insertedModals
To keep track of which constraints over modalities have been inserted.
Definition: LrsWrapper.h:122
unsigned int __vertices
The number of vertices of the polytope.
Definition: LrsWrapper.h:125
void nextHInput()
Reset the wrapper for next computation for a H-representation with the same variable cardinality and ...
const unsigned int & getVerticesNumber() const
Get the number of vertices of this polytope.
void tearDown()
Reset the wrapper as if it was built.
const matrix & getInput() const
Get the intput matrix of the problem.
matrix __input
Input matrix - either a V-representation or an H-representation.
Definition: LrsWrapper.h:111
void __coutOn() const
The function that restores standard cout.
const char * __setUpStateNames[5]
To print an enum field name instead of it&#39;s value.
Definition: LrsWrapper.h:157
void __fill() const
Fill lrs_dictionnary and datas from __input using integer rationals.
lrs_mp_matrix __Lin
Holds lrs input linearities if any are found.
Definition: LrsWrapper.h:190
lrs_mp_vector __lrsOutput
One line of output of lrs : aither a ray, a vertex, a facet or a linearity.
Definition: LrsWrapper.h:187
void H2V()
H-representation to V-representation.
void __coutOff() const
The function that redirects standard cout to /dev/null.
void fillH(const GUM_SCALAR &min, const GUM_SCALAR &max, const unsigned int &modal)
Creates the H-representation of min <= p(X=modal | .) <= max and add it to the problem input __input...
GUM_SCALAR __volume
The volume of the polytope, if computed, 0 otherwise.
Definition: LrsWrapper.h:153
void elimRedundVrep()
V-Redundancy elimination.
__states
The possible states of the LrsWrapper.
Definition: LrsWrapper.h:141
Class template used to approximate decimal numbers by rationals.
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:130
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.