aGrUM 2.3.2
a C++ library for (probabilistic) graphical models
LpInterface.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
42#ifndef __LP_INTERFACE__H__
43#define __LP_INTERFACE__H__
44
51
52#include <iostream>
53#include <sstream>
54#include <string>
55
58
60
61namespace gum {
62 namespace credal {
63 namespace lp {
64
65 class LpCol;
66 class LpRow;
67 template < typename GUM_SCALAR >
68 class LpInterface;
69 class LpExpr;
70
80 class LpCol {
81 public:
84
89 explicit LpCol(unsigned int id);
90
95 LpCol(const LpCol& col);
96
98 ~LpCol();
99
101
106 unsigned int id() const;
107
110
120 bool operator<(const LpCol& col) const;
121
131 bool operator==(const LpCol& col) const;
132
143 bool operator!=(const LpCol& col) const;
144
152 LpCol& operator=(const LpCol& col);
153
164 friend std::ostream& operator<<(std::ostream& out,
165 const LpCol& col); // calls col.toString(),
166 // friend is useless but
167 // good for documentation
168 // purpose
169
171
176 std::string toString() const;
177
178 protected:
179
180 private:
182 unsigned int _id_;
183 };
184
185 } // end of namespace lp
186
187 } // end of namespace credal
188
189} // end of namespace gum
190
191namespace gum {
192
200 template <>
201 class HashFunc< credal::lp::LpCol >: public HashFuncBase< credal::lp::LpCol > {
202 public:
209
215 virtual Size operator()(const credal::lp::LpCol& key) const override final;
216 };
217} // end of namespace gum
218
219namespace gum {
220 namespace credal {
221 namespace lp {
222
229 class LpExpr {
230 friend class LpRow;
231 template < typename GUM_SCALAR >
232 friend class LpInterface;
233
234 public:
237
239 LpExpr();
240
245 LpExpr(const LpExpr& expr);
246
260 LpExpr(const LpExpr& expr, bool copyLeft, bool copyMiddle, bool copyRight);
261
270 LpExpr(LpExpr&& expr);
271
291 LpExpr(LpExpr&& expr, bool copyLeft, bool copyMiddle, bool copyRight);
292
294 ~LpExpr();
295
297
300
308 LpExpr& operator=(const LpCol& rhs);
309
315 LpExpr& operator=(const LpExpr& rhs);
316
322 LpExpr& operator=(LpExpr&& rhs);
323
331 template < typename SCALAR >
332 LpExpr& operator=(const SCALAR& rhs);
333
335
338
346 LpExpr& operator+=(const LpCol& rhs);
347
355 LpExpr& operator+=(const LpExpr& rhs);
356
363 LpExpr& operator+=(LpExpr&& rhs);
364
372 template < typename T >
373 LpExpr& operator+=(const T& rhs);
374
376
379
387 LpExpr& operator-=(const LpCol& rhs);
388
396 LpExpr& operator-=(const LpExpr& rhs);
397
405 template < typename T >
406 LpExpr& operator-=(const T& rhs);
407
409
413 void clear();
414
419 std::string toString() const;
420
423 template < typename T1, typename T2 >
424 static LpExpr lessThan(T1&& lhs, T2&& rhs);
425 template < typename SCALAR >
426 static LpExpr multiply(const SCALAR& lhs, const LpCol& rhs);
428
429 protected:
430
431 private:
444
446 double _lValue_;
448 double _mValue_;
450 double _rValue_;
451
464
468
477 void _addSide_(const LpCol& from);
478
487 void _addSide_(const LpExpr& from);
488
497 void _addSide_(LpExpr&& from);
498
508 template < typename SCALAR >
509 void _addSide_(const SCALAR& from);
510
512 };
513
521 class LpRow {
522 template < typename GUM_SCALAR >
523 friend class LpInterface;
524
525 public:
528
538 LpRow(const LpExpr& expr, const std::vector< LpCol >& cols);
539
549 LpRow(LpExpr&& expr, const std::vector< LpCol >& cols);
550
555 LpRow(const LpRow& row);
556
561 LpRow(LpRow&& row);
562
564 ~LpRow();
565
567
570
575 LpRow& operator=(const LpRow& row);
580 LpRow& operator=(LpRow&& row);
581
583
593 friend std::ostream& operator<<(std::ostream& out,
594 const LpRow& row); // calls row.toString(),
595 // friend is useless but
596 // good for documentation
597 // purpose
598
603 std::string toString() const;
604
605 protected:
606
607 private:
609 double _cste_;
610
614
615 }; // end of class LpRow
616
623 template < typename GUM_SCALAR >
625 public:
628
630 LpInterface();
631
637
643
645 ~LpInterface();
646
648
651
657
663
665
675 template < typename T >
676 friend std::ostream&
677 operator<<(std::ostream& out,
678 const LpInterface< T >& lpi); // calls lpi.toString(),
679 // friend is useless but good
680 // for documentation purpose
681
686 LpCol addCol();
687
693 std::vector< LpCol > addCols(const unsigned int& cols);
694
702 void addRow(const LpExpr& expr);
703
710 void addRow(LpExpr&& expr);
711
715 void addPositivity();
716
720 void addSumIsOne();
721
727 void addProba();
728
742 std::vector< std::vector< GUM_SCALAR > > solve();
743
748 std::vector< LpCol > getCols() const;
749
754 std::string toString() const;
755
761 void clear();
766 void clearRows();
767
768 protected:
769
770 private:
772 std::vector< LpRow* > _rows_;
774 std::vector< LpCol > _cols_;
775
782
783 }; // end of class LpInterface
784
797
800
819 template < typename T2 >
820 LpExpr operator+(LpExpr&& lhs, const T2& rhs);
821 template < typename T2 >
822 LpExpr operator+(LpExpr& lhs, const T2& rhs);
823
853 template < typename T1, forbidden_type< T1, LpExpr > = 0 >
854 LpExpr operator+(const T1& lhs, LpExpr&& rhs);
855 template < typename T1, forbidden_type< T1, LpExpr > = 0 >
856 LpExpr operator+(const T1& lhs, LpExpr& rhs);
857
861 template < typename T2, forbidden_type< T2, LpExpr > = 0 >
862 LpExpr operator+(const LpCol& lhs, const T2& rhs);
863
867 template < typename T1, forbidden_type< T1, LpExpr > = 0, forbidden_type< T1, LpCol > = 0 >
868 LpExpr operator+(const T1& lhs, const LpCol& rhs);
870
873
891 template < typename T2 >
892 LpExpr operator-(LpExpr&& lhs, const T2& rhs);
893 template < typename T2 >
894 LpExpr operator-(LpExpr& lhs, const T2& rhs);
895
925 template < typename T1, forbidden_type< T1, LpExpr > = 0 >
926 LpExpr operator-(const T1& lhs, LpExpr&& rhs);
927 template < typename T1, forbidden_type< T1, LpExpr > = 0 >
928 LpExpr operator-(const T1& lhs, LpExpr& rhs);
929
940 template < typename T2, forbidden_type< T2, LpExpr > = 0 >
941 LpExpr operator-(const LpCol& lhs, const T2& rhs);
942
953 template < typename T1, forbidden_type< T1, LpExpr > = 0, forbidden_type< T1, LpCol > = 0 >
954 LpExpr operator-(const T1& lhs, const LpCol& rhs);
956
959
971 template < typename SCALAR >
972 LpExpr operator*(const SCALAR& lhs, const LpCol& rhs);
973
985 template < typename SCALAR >
986 LpExpr operator*(const LpCol& lhs, const SCALAR& rhs);
987
989
992
1014 template < typename T2 >
1015 LpExpr operator<=(const LpExpr& lhs, T2&& rhs);
1016 template < typename T2 >
1017 LpExpr operator<=(const LpCol& lhs, T2&& rhs);
1018 template < typename T1, forbidden_type< T1, LpExpr& > = 0, forbidden_type< T1, LpCol& > = 0 >
1019 LpExpr operator<=(T1&& lhs, const LpExpr& rhs);
1020 template < typename T1, forbidden_type< T1, LpExpr& > = 0, forbidden_type< T1, LpCol& > = 0 >
1021 LpExpr operator<=(T1&& lhs, const LpCol& rhs);
1022
1023 template < typename T2 >
1024 LpExpr operator<=(LpExpr&& lhs, T2&& rhs);
1025 template < typename T2 >
1026 LpExpr operator<=(LpCol&& lhs, T2&& rhs);
1027 template < typename T1, forbidden_type< T1, LpExpr > = 0, forbidden_type< T1, LpCol > = 0 >
1028 LpExpr operator<=(T1&& lhs, LpExpr&& rhs);
1029 template < typename T1, forbidden_type< T1, LpExpr > = 0, forbidden_type< T1, LpCol > = 0 >
1030 LpExpr operator<=(T1&& lhs, LpCol&& rhs);
1031
1033
1034 } // end of namespace lp
1035
1036 } // namespace credal
1037
1038} // end of namespace gum
1039
1041
1042#ifndef GUM_NO_INLINE
1044#endif /* GUM_NO_INLINE */
1045
1046#endif
Class to include at least once this header.
Lrs wrapper.
All hash functions should inherit from this class.
Definition hashFunc.h:169
virtual Size operator()(const credal::lp::LpCol &key) const override final
Overload of operator () to compute hash of a variable LpCol.
static Size castToSize(const credal::lp::LpCol &key)
Returns the value of a key as a Size.
This class should be useless as only its specializations should be used.
Definition hashFunc.h:487
The class for generic Hash Tables.
Definition hashTable.h:637
Class representing a variable ( a column ) of a linear program, i.e.
Definition LpInterface.h:80
LpCol(unsigned int id)
Default constructor.
bool operator!=(const LpCol &col) const
Opposite of equality != test between two variables.
LpCol & operator=(const LpCol &col)
Assignment operator = by copy.
bool operator<(const LpCol &col) const
Test of ordering < between two variables.
std::string toString() const
Get the string representation of a calling variable.
friend std::ostream & operator<<(std::ostream &out, const LpCol &col)
Overload of << to use with output streams ( such as std::cout << ).
unsigned int id() const
Variable id accessor.
~LpCol()
Default destructor.
unsigned int _id_
Variable id.
bool operator==(const LpCol &col) const
Test of equality == between two variables.
Class representing a linear expression.
LpExpr & operator+=(const LpCol &rhs)
Compound assignment operator += with a variable.
double _rValue_
The constant on the right side L : L <= M <= R.
bool _ileft_
True if this expression has a non-empty left side L : L <= M <= R .
bool _imiddle_
True if this expression has a non-empty middle side M ( the default ) : L <= M <= R .
LpExpr & operator-=(const LpCol &rhs)
Compound assignment operator -= with a variable.
HashTable< LpCol, double > * _lCoeffs_
The coefficients of each variable on the left side L : L <= M <= R.
LpExpr()
Default constructor.
std::string toString() const
Get the string representation of a calling expression.
void clear()
Clear all data of the calling expression as if it was constructed.
static LpExpr multiply(const SCALAR &lhs, const LpCol &rhs)
bool _iright_
True if this expression has a non-empty right side R : L <= M <= R .
static LpExpr lessThan(T1 &&lhs, T2 &&rhs)
HashTable< LpCol, double > * _mCoeffs_
The coefficients of each variable on the middle side L : L <= M <= R.
~LpExpr()
Default destructor.
HashTable< LpCol, double > * _rCoeffs_
The coefficients of each variable on the right side L : L <= M <= R.
double _mValue_
The constant on the middle side L : L <= M <= R.
double _lValue_
The constant on the left side L : L <= M <= R.
LpExpr & operator=(const LpCol &rhs)
Assignment operator = with a variable.
void _addSide_(const LpCol &from)
Set the side of the calling expression, from LEFT TO RIGHT : L <= M <= R.
Class representing a linear program.
bool _sumIsOne_
true if addSumIsOne() has been called, false otherwise.
std::string toString() const
Get the string representation of a calling linear program.
void addSumIsOne()
Add sum of variables is 1 constraints.
void clear()
Reset the rows (inequalities) and columns (variables) of the LP as if it was created.
std::vector< LpCol > getCols() const
Get the variables of the LP.
void clearRows()
Reset the rows (inequalities) of the LP but not the columns (variables are kept).
std::vector< LpCol > _cols_
Variables of the problem.
std::vector< LpRow * > _rows_
Rows of the problem.
LpInterface< GUM_SCALAR > & operator=(const LpInterface< GUM_SCALAR > &from)
Copy compound assignment.
LpInterface()
Default constructor, empty problem.
void addPositivity()
Add positivity constraints for all variables.
std::vector< std::vector< GUM_SCALAR > > solve()
Solve the linear program (H-representation of the polytope) by enumeration (of the polytope vertices)...
std::vector< LpCol > addCols(const unsigned int &cols)
Insert new columns, i.e.
LpCol addCol()
Insert a new column, i.e.
void addProba()
Add positivity constraints and sum of variables is 1 ( probability constraints ).
friend std::ostream & operator<<(std::ostream &out, const LpInterface< T > &lpi)
Overload of << to use with output streams ( such as std::cout << ).
void addRow(const LpExpr &expr)
Add rows to the linear program according to a given expression ( which must be at least an inequality...
~LpInterface()
Default destructor.
bool _positivity_
true if addPositivity() has been called, false otherwise.
Class representing a row of the linear program, i.e.
std::string toString() const
Get the string representation of a calling row.
LpRow & operator=(const LpRow &row)
LpRow(const LpExpr &expr, const std::vector< LpCol > &cols)
Constructor from an expression and the address of the vector of variables of the problem.
friend class LpInterface
HashTable< LpCol, double > * _coeffs_
The coefficients of the variables of the linear inequality.
double _cste_
The constant of the linear inequality.
friend std::ostream & operator<<(std::ostream &out, const LpRow &row)
Overload of << to use with output streams ( such as std::cout << ).
~LpRow()
Default destructor.
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition types.h:74
Class hash tables iterators.
namespace for constraint-based description of credal sets
Definition agrum.h:64
LpExpr operator+(LpExpr &&lhs, const T2 &rhs)
Overload of operator + between anything ( a scalar, a variable or an expression ) and anything except...
LpExpr operator<=(const LpExpr &lhs, T2 &&rhs)
Overload of operator <= between anything and anything.
LpExpr operator-(LpExpr &&lhs, const T2 &rhs)
Overload of operator - between anything ( a scalar, a variable or an expression ) and anything except...
void swap(HashTable< LpCol, double > *&a, HashTable< LpCol, double > *&b)
Swap the addresses of two pointers to hashTables.
namespace for all credal networks entities
Definition agrum.h:61
gum is the global namespace for all aGrUM entities
Definition agrum.h:46
value_type & operator*()
Returns the value pointed to by the iterator.
Utilities for aGrUM.