aGrUM 2.3.2
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-2025 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-2025 *
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 __LRSWrapper_WRAPPER__H__
51#define __LRSWrapper_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/* *** from lrs, we need to know BASE to read multiple precision integers *** */
75#ifdef B32
76/*32 bit machines */
77# define FORMAT "%4.4lu"
78# define MAXD 2147483647L
79# define BASE 10000L
80# define BASE_DIG 4
81# define INTSIZE 8L
82# define BIT "32bit"
83#else
84/* 64 bit machines */
85# define MAXD 9223372036854775807L
86# define BASE 1000000000L
87# define FORMAT "%9.9lu"
88# define BASE_DIG 9
89# define INTSIZE 16L
90# define BIT "64bit"
91#endif
92
93// 64 bits for windows (long is 32 bits)
94#ifdef _MSC_VER
95typedef __int64 int64_t;
96typedef unsigned __int64 uint64_t;
97#else
98# include <stdint.h>
99#endif
100
101/* ************ */
102
103#define enumStringify(name) #name
104
105namespace gum {
106 namespace credal {
107
118 template < typename GUM_SCALAR >
121 using matrix = typename std::vector< std::vector< GUM_SCALAR > >;
122
126
130
132 unsigned int _card_;
133
136 std::unordered_set< int > _insertedModals_;
137
139 unsigned int _vertices_;
140
144 std::vector< std::vector< GUM_SCALAR > > _insertedVertices_;
145
148 std::vector< GUM_SCALAR > _vertex_;
149
155 enum class _states_ : char {
156 none = char(0),
157 Hup = char(1),
158 Vup = char(2),
159 H2Vready = char(3),
160 V2Hready = char(4),
161 };
162
165
167 GUM_SCALAR _volume_;
168
171 const char* _setUpStateNames_[5] = {
173 enumStringify(_states_::nHup),
174 enumStringify(_states_::nVup),
175 enumStringify(_states_::nH2Vready),
176 enumStringify(_states_::nV2Hready),
177 };
178
187 mutable int _oldCout_;
188
191
193 lrs_dic* _dic_;
194
196 lrs_dat* _dat_;
197
201 lrs_mp_vector _lrsOutput_;
202
204 lrs_mp_matrix _Lin_;
205
207
210
212
213 bool _hull_;
214
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
447
449 };
450
451 } // namespace credal
452} // namespace gum
453
455
456#endif
#define enumStringify(name)
Definition LrsWrapper.h:103
std::unordered_set< int > _insertedModals_
To keep track of which constraints over modalities have been inserted.
Definition LrsWrapper.h:136
_states_ _state_
The current state of the LrsWrapper.
Definition LrsWrapper.h:164
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:121
lrs_mp_vector _lrsOutput_
One line of output of lrs : aither a ray, a vertex, a facet or a linearity.
Definition LrsWrapper.h:201
std::vector< std::vector< GUM_SCALAR > > _insertedVertices_
To keep track of inserted vertices and total.
Definition LrsWrapper.h:144
const char * _setUpStateNames_[5]
To print an enum field name instead of it's value.
Definition LrsWrapper.h:171
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:204
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:148
lrs_dat * _dat_
Structure for holding static problem data of lrs.
Definition LrsWrapper.h:196
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:129
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:139
LRSWrapper()
Default Constructor.
_states_
The possible states of the LrsWrapper.
Definition LrsWrapper.h:155
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:132
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:167
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:125
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:193
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:187
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.