aGrUM 3.1.1
a C++ library for (probabilistic) graphical models
LrsWrapper.h
Go to the documentation of this file.
1/****************************************************************************
2 * This file is part of the aGrUM/pyAgrum library. *
3 * *
4 * Copyright (c) 2005-2026 by *
5 * - Pierre-Henri WUILLEMIN(_at_LIP6) *
6 * - Christophe GONZALES(_at_AMU) *
7 * *
8 * The aGrUM/pyAgrum library is free software; you can redistribute it *
9 * and/or modify it under the terms of either : *
10 * *
11 * - the GNU Lesser General Public License as published by *
12 * the Free Software Foundation, either version 3 of the License, *
13 * or (at your option) any later version, *
14 * - the MIT license (MIT), *
15 * - or both in dual license, as here. *
16 * *
17 * (see https://agrum.gitlab.io/articles/dual-licenses-lgplv3mit.html) *
18 * *
19 * This aGrUM/pyAgrum library is distributed in the hope that it will be *
20 * useful, but WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, *
21 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES MERCHANTABILITY or FITNESS *
22 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, *
25 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR *
26 * OTHER DEALINGS IN THE SOFTWARE. *
27 * *
28 * See LICENCES for more details. *
29 * *
30 * SPDX-FileCopyrightText: Copyright 2005-2026 *
31 * - Pierre-Henri WUILLEMIN(_at_LIP6) *
32 * - Christophe GONZALES(_at_AMU) *
33 * SPDX-License-Identifier: LGPL-3.0-or-later OR MIT *
34 * *
35 * Contact : info_at_agrum_dot_org *
36 * homepage : http://agrum.gitlab.io *
37 * gitlab : https://gitlab.com/agrumery/agrum *
38 * *
39 ****************************************************************************/
40
41
49
50#ifndef GUM_LRS_WRAPPER_H
51#define GUM_LRS_WRAPPER_H
52
53#include <chrono>
54#include <cstdio>
55#include <fcntl.h>
56#include <fstream>
57#include <vector>
58
59#include <agrum/agrum.h>
60
62
63#include <unordered_set>
64
65// we force MP (not long or GMP)
66#undef LONG
67#undef LRSLONG
68#undef GMP
69#define MP
70// lrs stuff
71extern "C" {
73}
74#undef one // conflict between lrs and json
75
76/* *** from lrs, we need to know BASE to read multiple precision integers *** */
77#ifdef B32
78/*32 bit machines */
79# define FORMAT "%4.4lu"
80# define MAXD 2147483647L
81# define BASE 10000L
82# define BASE_DIG 4
83# define INTSIZE 8L
84# define BIT "32bit"
85#else
86/* 64 bit machines */
87# define MAXD 9223372036854775807L
88# define BASE 1000000000L
89# define FORMAT "%9.9lu"
90# define BASE_DIG 9
91# define INTSIZE 16L
92# define BIT "64bit"
93#endif
94
95// 64 bits for windows (long is 32 bits)
96#ifdef _MSC_VER
97typedef __int64 int64_t;
98typedef unsigned __int64 uint64_t;
99#else
100# include <stdint.h>
101#endif
102
103/* ************ */
104
105#define enumStringify(name) #name
106
107namespace gum {
108 namespace credal {
109
120 template < GUM_Numeric GUM_SCALAR >
123 using matrix = typename std::vector< std::vector< GUM_SCALAR > >;
124
128
132
134 unsigned int _card_;
135
138 std::unordered_set< int > _insertedModals_;
139
141 unsigned int _vertices_;
142
146 std::vector< std::vector< GUM_SCALAR > > _insertedVertices_;
147
150 std::vector< GUM_SCALAR > _vertex_;
151
157 enum class _states_ : char {
158 none = char(0),
159 Hup = char(1),
160 Vup = char(2),
161 H2Vready = char(3),
162 V2Hready = char(4),
163 };
164
167
169 GUM_SCALAR _volume_;
170
173 const char* _setUpStateNames_[5] = {
175 enumStringify(_states_::nHup),
176 enumStringify(_states_::nVup),
177 enumStringify(_states_::nH2Vready),
178 enumStringify(_states_::nV2Hready),
179 };
180
189 mutable int _oldCout_;
190
193
195 lrs_dic* _dic_;
196
198 lrs_dat* _dat_;
199
203 lrs_mp_vector _lrsOutput_;
204
206 lrs_mp_matrix _Lin_;
207
209
212
214
215 bool _hull_;
216
218
220
223
225 void _freeLrs_();
226
228 void _initLrs_();
229
238 void _fill_() const;
239
250 void _getLRSWrapperOutput_(lrs_mp Nin,
251 lrs_mp Din,
252 std::vector< int64_t >& Num,
253 std::vector< int64_t >& Den) const;
254
256
257 public:
260
264 LRSWrapper();
265
269 ~LRSWrapper();
270
272
275
280 const matrix& getInput() const;
281
286 const matrix& getOutput() const;
287
292 const unsigned int& getVerticesNumber() const;
293
310 const GUM_SCALAR& getVolume() const;
311
313
316
325 void setUpH(const Size& card);
326
337 void setUpV(const Size& card, const Size& vertices);
338
346 void tearDown();
347
361 void nextHInput();
362
364
367
377 void fillH(const GUM_SCALAR& min, const GUM_SCALAR& max, const Size& modal);
378
386 void fillMatrix(const std::vector< std::vector< GUM_SCALAR > >& matrix);
387
396 void fillV(const std::vector< GUM_SCALAR >& vertex);
397
399
402
410 void H2V();
411
419 void V2H();
420
436 void computeVolume();
437
444 void elimRedundVrep();
445
449
451 };
452
453 } // namespace credal
454} // namespace gum
455
457
458#endif
#define enumStringify(name)
Definition LrsWrapper.h:105
std::unordered_set< int > _insertedModals_
To keep track of which constraints over modalities have been inserted.
Definition LrsWrapper.h:138
_states_ _state_
The current state of the LrsWrapper.
Definition LrsWrapper.h:166
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.
Definition LrsWrapper.h:123
lrs_mp_vector _lrsOutput_
One line of output of lrs : aither a ray, a vertex, a facet or a linearity.
Definition LrsWrapper.h:203
std::vector< std::vector< GUM_SCALAR > > _insertedVertices_
To keep track of inserted vertices and total.
Definition LrsWrapper.h:146
const char * _setUpStateNames_[5]
To print an enum field name instead of it's value.
Definition LrsWrapper.h:173
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.
Definition LrsWrapper.h:206
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:150
lrs_dat * _dat_
Structure for holding static problem data of lrs.
Definition LrsWrapper.h:198
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.
Definition LrsWrapper.h:131
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.
Definition LrsWrapper.h:141
LRSWrapper()
Default Constructor.
_states_
The possible states of the LrsWrapper.
Definition LrsWrapper.h:157
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.
Definition LrsWrapper.h:134
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.
Definition LrsWrapper.h:169
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.
Definition LrsWrapper.h:127
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.
Definition LrsWrapper.h:195
const GUM_SCALAR & getVolume() const
Get the volume of the polytope that has been computed.
~LRSWrapper()
Default Destructor.
int _oldCout_
File descriptor of standard cout.
Definition LrsWrapper.h:189
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 ...
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition types.h:74
gum is the global namespace for all aGrUM entities
Definition agrum.h:46
Class template used to approximate decimal numbers by rationals.