aGrUM  0.16.0
LpInterface.h
Go to the documentation of this file.
1 
23 #ifndef __LP_INTERFACE__H__
24 #define __LP_INTERFACE__H__
25 
33 #include <iostream>
34 #include <sstream>
35 #include <string>
36 
37 #include <agrum/CN/LrsWrapper.h>
38 #include <agrum/core/hashFunc.h>
39 #include <agrum/core/hashTable.h>
40 #include <agrum/core/utils_misc.h>
41 
42 namespace gum {
43  namespace credal {
44  namespace lp {
45 
46  class LpCol;
47  class LpRow;
48  template < typename GUM_SCALAR >
49  class LpInterface;
50  class LpExpr;
51 
61  class LpCol {
62  public:
65 
70  explicit LpCol(unsigned int id);
71 
76  LpCol(const LpCol& col);
77 
79  ~LpCol();
80 
82 
87  unsigned int id() const;
88 
91 
101  bool operator<(const LpCol& col) const;
102 
112  bool operator==(const LpCol& col) const;
113 
124  bool operator!=(const LpCol& col) const;
125 
133  LpCol& operator=(const LpCol& col);
134 
145  friend std::ostream&
146  operator<<(std::ostream& out,
147  const LpCol& col); // calls col.toString(),
148  // friend is useless but
149  // good for documentation
150  // purpose
151 
153 
158  std::string toString() const;
159 
160  protected:
161  private:
163  unsigned int __id;
164  };
165 
166  } // end of namespace lp
167 
168  } // end of namespace credal
169 
170 } // end of namespace gum
171 
172 namespace gum {
173 
181  template <>
182  class HashFunc< credal::lp::LpCol > : public HashFuncBase< credal::lp::LpCol > {
183  public:
189  static Size castToSize(const credal::lp::LpCol& key);
190 
196  virtual Size operator()(const credal::lp::LpCol& key) const override final;
197  };
198 } // end of namespace gum
199 
200 namespace gum {
201  namespace credal {
202  namespace lp {
203 
210  class LpExpr {
211  friend class LpRow;
212  template < typename GUM_SCALAR >
213  friend class LpInterface;
214 
215  public:
218 
220  LpExpr();
221 
226  LpExpr(const LpExpr& expr);
227 
241  LpExpr(const LpExpr& expr, bool copyLeft, bool copyMiddle, bool copyRight);
242 
251  LpExpr(LpExpr&& expr);
252 
272  LpExpr(LpExpr&& expr, bool copyLeft, bool copyMiddle, bool copyRight);
273 
275  ~LpExpr();
276 
278 
281 
289  LpExpr& operator=(const LpCol& rhs);
290 
296  LpExpr& operator=(const LpExpr& rhs);
297 
303  LpExpr& operator=(LpExpr&& rhs);
304 
312  template < typename SCALAR >
313  LpExpr& operator=(const SCALAR& rhs);
314 
316 
319 
327  LpExpr& operator+=(const LpCol& rhs);
328 
336  LpExpr& operator+=(const LpExpr& rhs);
337 
344  LpExpr& operator+=(LpExpr&& rhs);
345 
353  template < typename T >
354  LpExpr& operator+=(const T& rhs);
355 
357 
360 
368  LpExpr& operator-=(const LpCol& rhs);
369 
377  LpExpr& operator-=(const LpExpr& rhs);
378 
386  template < typename T >
387  LpExpr& operator-=(const T& rhs);
388 
390 
394  void clear();
395 
400  std::string toString() const;
401 
404  template < typename T1, typename T2 >
405  static LpExpr lessThan(T1&& lhs, T2&& rhs);
406  template < typename SCALAR >
407  static LpExpr multiply(const SCALAR& lhs, const LpCol& rhs);
409 
410  protected:
411  private:
415  bool __ileft;
419  bool __imiddle;
423  bool __iright;
424 
426  double __lValue;
428  double __mValue;
430  double __rValue;
431 
444 
448 
457  inline void __addSide(const LpCol& from);
458 
467  inline void __addSide(const LpExpr& from);
468 
477  inline void __addSide(LpExpr&& from);
478 
488  template < typename SCALAR >
489  inline void __addSide(const SCALAR& from);
490 
492  };
493 
501  class LpRow {
502  template < typename GUM_SCALAR >
503  friend class LpInterface;
504 
505  public:
508 
518  LpRow(const LpExpr& expr, const std::vector< LpCol >& cols);
519 
529  LpRow(LpExpr&& expr, const std::vector< LpCol >& cols);
530 
535  LpRow(const LpRow& row);
536 
541  LpRow(LpRow&& row);
542 
544  ~LpRow();
545 
547 
550 
555  LpRow& operator=(const LpRow& row);
560  LpRow& operator=(LpRow&& row);
561 
563 
573  friend std::ostream&
574  operator<<(std::ostream& out,
575  const LpRow& row); // calls row.toString(),
576  // friend is useless but
577  // good for documentation
578  // purpose
579 
584  std::string toString() const;
585 
586  protected:
587  private:
589  double __cste;
590 
594 
595  }; // end of class LpRow
596 
603  template < typename GUM_SCALAR >
604  class LpInterface {
605  public:
608 
610  LpInterface();
611 
617 
623 
625  ~LpInterface();
626 
628 
631 
638 
644 
646 
656  template < typename T >
657  friend std::ostream&
658  operator<<(std::ostream& out,
659  const LpInterface< T >& lpi); // calls lpi.toString(),
660  // friend is useless but good
661  // for documentation purpose
662 
667  LpCol addCol();
668 
674  std::vector< LpCol > addCols(const unsigned int& cols);
675 
683  void addRow(const LpExpr& expr);
684 
691  void addRow(LpExpr&& expr);
692 
696  void addPositivity();
697 
701  void addSumIsOne();
702 
708  void addProba();
709 
723  std::vector< std::vector< GUM_SCALAR > > solve();
724 
729  std::vector< LpCol > getCols() const;
730 
735  std::string toString() const;
736 
742  void clear();
747  void clearRows();
748 
749  protected:
750  private:
752  std::vector< LpRow* > __rows;
754  std::vector< LpCol > __cols;
755 
762 
763  }; // end of class LpInterface
764 
776  inline void swap(HashTable< LpCol, double >*& a,
778 
781 
800  template < typename T2 >
801  LpExpr operator+(LpExpr&& lhs, const T2& rhs);
802  template < typename T2 >
803  LpExpr operator+(LpExpr& lhs, const T2& rhs);
804 
834  template < typename T1, forbidden_type< T1, LpExpr > = 0 >
835  LpExpr operator+(const T1& lhs, LpExpr&& rhs);
836  template < typename T1, forbidden_type< T1, LpExpr > = 0 >
837  LpExpr operator+(const T1& lhs, LpExpr& rhs);
838 
842  template < typename T2, forbidden_type< T2, LpExpr > = 0 >
843  LpExpr operator+(const LpCol& lhs, const T2& rhs);
844 
848  template < typename T1,
851  LpExpr operator+(const T1& lhs, const LpCol& rhs);
853 
856 
874  template < typename T2 >
875  LpExpr operator-(LpExpr&& lhs, const T2& rhs);
876  template < typename T2 >
877  LpExpr operator-(LpExpr& lhs, const T2& rhs);
878 
908  template < typename T1, forbidden_type< T1, LpExpr > = 0 >
909  LpExpr operator-(const T1& lhs, LpExpr&& rhs);
910  template < typename T1, forbidden_type< T1, LpExpr > = 0 >
911  LpExpr operator-(const T1& lhs, LpExpr& rhs);
912 
923  template < typename T2, forbidden_type< T2, LpExpr > = 0 >
924  LpExpr operator-(const LpCol& lhs, const T2& rhs);
925 
936  template < typename T1,
937  forbidden_type< T1, LpExpr > = 0,
939  LpExpr operator-(const T1& lhs, const LpCol& rhs);
941 
944 
956  template < typename SCALAR >
957  LpExpr operator*(const SCALAR& lhs, const LpCol& rhs);
958 
970  template < typename SCALAR >
971  LpExpr operator*(const LpCol& lhs, const SCALAR& rhs);
972 
974 
977 
999  template < typename T2 >
1000  LpExpr operator<=(const LpExpr& lhs, T2&& rhs);
1001  template < typename T2 >
1002  LpExpr operator<=(const LpCol& lhs, T2&& rhs);
1003  template < typename T1,
1006  LpExpr operator<=(T1&& lhs, const LpExpr& rhs);
1007  template < typename T1,
1008  forbidden_type< T1, LpExpr& > = 0,
1010  LpExpr operator<=(T1&& lhs, const LpCol& rhs);
1011 
1012  template < typename T2 >
1013  LpExpr operator<=(LpExpr&& lhs, T2&& rhs);
1014  template < typename T2 >
1015  LpExpr operator<=(LpCol&& lhs, T2&& rhs);
1016  template < typename T1,
1017  forbidden_type< T1, LpExpr > = 0,
1019  LpExpr operator<=(T1&& lhs, LpExpr&& rhs);
1020  template < typename T1,
1021  forbidden_type< T1, LpExpr > = 0,
1023  LpExpr operator<=(T1&& lhs, LpCol&& rhs);
1024 
1026 
1027  } // end of namespace lp
1028 
1029  } // namespace credal
1030 
1031 } // end of namespace gum
1032 
1033 #include <agrum/CN/LpInterface_tpl.h>
1034 
1035 #ifndef GUM_NO_INLINE
1036 # include <agrum/CN/LpInterface_inl.h>
1037 #endif /* GUM_NO_INLINE */
1038 
1039 #endif
bool __ileft
True if this expression has a non-empty left side L : L <= M <= R .
Definition: LpInterface.h:415
HashTable< LpCol, double > * __coeffs
The coefficients of the variables of the linear inequality.
Definition: LpInterface.h:593
std::string toString() const
Get the string representation of a calling variable.
bool operator==(const LpCol &col) const
Test of equality == between two variables.
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
double __cste
The constant of the linear inequality.
Definition: LpInterface.h:589
bool __iright
True if this expression has a non-empty right side R : L <= M <= R .
Definition: LpInterface.h:423
HashTable< LpCol, double > * __mCoeffs
The coefficients of each variable on the middle side L : L <= M <= R.
Definition: LpInterface.h:439
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
void swap(HashTable< LpCol, double > *&a, HashTable< LpCol, double > *&b)
Swap the addresses of two pointers to hashTables.
LpCol(unsigned int id)
Default constructor.
LpExpr operator<=(const LpExpr &lhs, T2 &&rhs)
Overload of operator <= between anything and anything.
Class template representing hashing function of LpCol.
Definition: hashFunc.h:471
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Definition: agrum.h:25
double __mValue
The constant on the middle side L : L <= M <= R.
Definition: LpInterface.h:428
The class for generic Hash Tables.
Definition: hashTable.h:679
Class representing a variable ( a column ) of a linear program, i.e.
Definition: LpInterface.h:61
Class representing a linear expression.
Definition: LpInterface.h:210
double __rValue
The constant on the right side L : L <= M <= R.
Definition: LpInterface.h:430
Class representing a row of the linear program, i.e.
Definition: LpInterface.h:501
HashTable< LpCol, double > * __rCoeffs
The coefficients of each variable on the right side L : L <= M <= R.
Definition: LpInterface.h:443
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
double __lValue
The constant on the left side L : L <= M <= R.
Definition: LpInterface.h:426
Class representing a linear program.
Definition: LpInterface.h:49
~LpCol()
Default destructor.
LpExpr operator+(LpExpr &&lhs, const T2 &rhs)
Overload of operator + between anything ( a scalar, a variable or an expression ) and anything except...
bool __sumIsOne
true if addSumIsOne() has been called, false otherwise.
Definition: LpInterface.h:761
unsigned int __id
Variable id.
Definition: LpInterface.h:163
bool __positivity
true if addPositivity() has been called, false otherwise.
Definition: LpInterface.h:758
friend std::ostream & operator<<(std::ostream &out, const LpCol &col)
Overload of << to use with output streams ( such as std::cout << ).
HashTable< LpCol, double > * __lCoeffs
The coefficients of each variable on the left side L : L <= M <= R.
Definition: LpInterface.h:435
typename std::enable_if< !std::is_same< T1, T2 >::value, int >::type forbidden_type
Forbidden_type<T1,T2> return the "int" type if T1 and T2 are of the same type, else nothing...
Definition: utils_misc.h:127
LpExpr operator*(const SCALAR &lhs, const LpCol &rhs)
Overload of operator * between a scalar and a variable.
std::vector< LpRow *> __rows
Rows of the problem.
Definition: LpInterface.h:752
All hash functions should inherit from this class.
Definition: hashFunc.h:149
bool operator!=(const LpCol &col) const
Opposite of equality != test between two variables.
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:48
LpCol & operator=(const LpCol &col)
Assignment operator = by copy.
LpExpr operator-(LpExpr &&lhs, const T2 &rhs)
Overload of operator - between anything ( a scalar, a variable or an expression ) and anything except...
std::vector< LpCol > __cols
Variables of the problem.
Definition: LpInterface.h:754
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
bool __imiddle
True if this expression has a non-empty middle side M ( the default ) : L <= M <= R ...
Definition: LpInterface.h:419
unsigned int id() const
Variable id accessor.
bool operator<(const LpCol &col) const
Test of ordering < between two variables.