aGrUM  0.13.2
LpInterface.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 
21 #ifndef __LP_INTERFACE__H__
22 #define __LP_INTERFACE__H__
23 
31 #include <iostream>
32 #include <sstream>
33 #include <string>
34 
35 #include <agrum/CN/LrsWrapper.h>
36 #include <agrum/core/hashFunc.h>
37 #include <agrum/core/hashTable.h>
38 #include <agrum/core/utils_misc.h>
39 
40 namespace gum {
41  namespace credal {
42  namespace lp {
43 
44  class LpCol;
45  class LpRow;
46  template < typename GUM_SCALAR >
47  class LpInterface;
48  class LpExpr;
49 
59  class LpCol {
60  public:
63 
68  explicit LpCol(unsigned int id);
69 
74  LpCol(const LpCol& col);
75 
77  ~LpCol();
78 
80 
85  unsigned int id() const;
86 
89 
99  bool operator<(const LpCol& col) const;
100 
110  bool operator==(const LpCol& col) const;
111 
122  bool operator!=(const LpCol& col) const;
123 
131  LpCol& operator=(const LpCol& col);
132 
143  friend std::ostream&
144  operator<<(std::ostream& out,
145  const LpCol& col); // calls col.toString(),
146  // friend is useless but
147  // good for documentation
148  // purpose
149 
151 
156  std::string toString() const;
157 
158  protected:
159  private:
161  unsigned int __id;
162  };
163 
164  } // end of namespace lp
165 
166  } // end of namespace credal
167 
168 } // end of namespace gum
169 
170 namespace gum {
171 
179  template <>
180  class HashFunc< credal::lp::LpCol > : public HashFuncBase< credal::lp::LpCol > {
181  public:
187  Size operator()(const credal::lp::LpCol& key) const;
188  };
189 } // end of namespace gum
190 
191 namespace gum {
192  namespace credal {
193  namespace lp {
194 
201  class LpExpr {
202  friend class LpRow;
203  template < typename GUM_SCALAR >
204  friend class LpInterface;
205 
206  public:
209 
211  LpExpr();
212 
217  LpExpr(const LpExpr& expr);
218 
232  LpExpr(const LpExpr& expr, bool copyLeft, bool copyMiddle, bool copyRight);
233 
242  LpExpr(LpExpr&& expr);
243 
263  LpExpr(LpExpr&& expr, bool copyLeft, bool copyMiddle, bool copyRight);
264 
266  ~LpExpr();
267 
269 
272 
280  LpExpr& operator=(const LpCol& rhs);
281 
287  LpExpr& operator=(const LpExpr& rhs);
288 
294  LpExpr& operator=(LpExpr&& rhs);
295 
303  template < typename SCALAR >
304  LpExpr& operator=(const SCALAR& rhs);
305 
307 
310 
318  LpExpr& operator+=(const LpCol& rhs);
319 
327  LpExpr& operator+=(const LpExpr& rhs);
328 
335  LpExpr& operator+=(LpExpr&& rhs);
336 
344  template < typename T >
345  LpExpr& operator+=(const T& rhs);
346 
348 
351 
359  LpExpr& operator-=(const LpCol& rhs);
360 
368  LpExpr& operator-=(const LpExpr& rhs);
369 
377  template < typename T >
378  LpExpr& operator-=(const T& rhs);
379 
381 
385  void clear();
386 
391  std::string toString() const;
392 
395  template < typename T1, typename T2 >
396  static LpExpr lessThan(T1&& lhs, T2&& rhs);
397  template < typename SCALAR >
398  static LpExpr multiply(const SCALAR& lhs, const LpCol& rhs);
400 
401  protected:
402  private:
406  bool __ileft;
410  bool __imiddle;
414  bool __iright;
415 
417  double __lValue;
419  double __mValue;
421  double __rValue;
422 
435 
439 
448  inline void __addSide(const LpCol& from);
449 
458  inline void __addSide(const LpExpr& from);
459 
468  inline void __addSide(LpExpr&& from);
469 
479  template < typename SCALAR >
480  inline void __addSide(const SCALAR& from);
481 
483  };
484 
492  class LpRow {
493  template < typename GUM_SCALAR >
494  friend class LpInterface;
495 
496  public:
499 
509  LpRow(const LpExpr& expr, const std::vector< LpCol >& cols);
510 
520  LpRow(LpExpr&& expr, const std::vector< LpCol >& cols);
521 
526  LpRow(const LpRow& row);
527 
532  LpRow(LpRow&& row);
533 
535  ~LpRow();
536 
538 
541 
546  LpRow& operator=(const LpRow& row);
551  LpRow& operator=(LpRow&& row);
552 
554 
564  friend std::ostream&
565  operator<<(std::ostream& out,
566  const LpRow& row); // calls row.toString(),
567  // friend is useless but
568  // good for documentation
569  // purpose
570 
575  std::string toString() const;
576 
577  protected:
578  private:
580  double __cste;
581 
585 
586  }; // end of class LpRow
587 
594  template < typename GUM_SCALAR >
595  class LpInterface {
596  public:
599 
601  LpInterface();
602 
608 
614 
616  ~LpInterface();
617 
619 
622 
629 
635 
637 
647  template < typename T >
648  friend std::ostream&
649  operator<<(std::ostream& out,
650  const LpInterface< T >& lpi); // calls lpi.toString(),
651  // friend is useless but good
652  // for documentation purpose
653 
658  LpCol addCol();
659 
665  std::vector< LpCol > addCols(const unsigned int& cols);
666 
674  void addRow(const LpExpr& expr);
675 
682  void addRow(LpExpr&& expr);
683 
687  void addPositivity();
688 
692  void addSumIsOne();
693 
699  void addProba();
700 
714  std::vector< std::vector< GUM_SCALAR > > solve();
715 
720  std::vector< LpCol > getCols() const;
721 
726  std::string toString() const;
727 
733  void clear();
738  void clearRows();
739 
740  protected:
741  private:
743  std::vector< LpRow* > __rows;
745  std::vector< LpCol > __cols;
746 
753 
754  }; // end of class LpInterface
755 
767  inline void swap(HashTable< LpCol, double >*& a,
769 
772 
791  template < typename T2 >
792  LpExpr operator+(LpExpr&& lhs, const T2& rhs);
793  template < typename T2 >
794  LpExpr operator+(LpExpr& lhs, const T2& rhs);
795 
825  template < typename T1, forbidden_type< T1, LpExpr > = 0 >
826  LpExpr operator+(const T1& lhs, LpExpr&& rhs);
827  template < typename T1, forbidden_type< T1, LpExpr > = 0 >
828  LpExpr operator+(const T1& lhs, LpExpr& rhs);
829 
833  template < typename T2, forbidden_type< T2, LpExpr > = 0 >
834  LpExpr operator+(const LpCol& lhs, const T2& rhs);
835 
839  template < typename T1,
842  LpExpr operator+(const T1& lhs, const LpCol& rhs);
844 
847 
865  template < typename T2 >
866  LpExpr operator-(LpExpr&& lhs, const T2& rhs);
867  template < typename T2 >
868  LpExpr operator-(LpExpr& lhs, const T2& rhs);
869 
899  template < typename T1, forbidden_type< T1, LpExpr > = 0 >
900  LpExpr operator-(const T1& lhs, LpExpr&& rhs);
901  template < typename T1, forbidden_type< T1, LpExpr > = 0 >
902  LpExpr operator-(const T1& lhs, LpExpr& rhs);
903 
914  template < typename T2, forbidden_type< T2, LpExpr > = 0 >
915  LpExpr operator-(const LpCol& lhs, const T2& rhs);
916 
927  template < typename T1,
928  forbidden_type< T1, LpExpr > = 0,
930  LpExpr operator-(const T1& lhs, const LpCol& rhs);
932 
935 
947  template < typename SCALAR >
948  LpExpr operator*(const SCALAR& lhs, const LpCol& rhs);
949 
961  template < typename SCALAR >
962  LpExpr operator*(const LpCol& lhs, const SCALAR& rhs);
963 
965 
968 
990  template < typename T2 >
991  LpExpr operator<=(const LpExpr& lhs, T2&& rhs);
992  template < typename T2 >
993  LpExpr operator<=(const LpCol& lhs, T2&& rhs);
994  template < typename T1,
997  LpExpr operator<=(T1&& lhs, const LpExpr& rhs);
998  template < typename T1,
999  forbidden_type< T1, LpExpr& > = 0,
1001  LpExpr operator<=(T1&& lhs, const LpCol& rhs);
1002 
1003  template < typename T2 >
1004  LpExpr operator<=(LpExpr&& lhs, T2&& rhs);
1005  template < typename T2 >
1006  LpExpr operator<=(LpCol&& lhs, T2&& rhs);
1007  template < typename T1,
1008  forbidden_type< T1, LpExpr > = 0,
1010  LpExpr operator<=(T1&& lhs, LpExpr&& rhs);
1011  template < typename T1,
1012  forbidden_type< T1, LpExpr > = 0,
1014  LpExpr operator<=(T1&& lhs, LpCol&& rhs);
1015 
1017 
1018  } // end of namespace lp
1019 
1020  } // namespace credal
1021 
1022 } // end of namespace gum
1023 
1024 #include <agrum/CN/LpInterface_tpl.h>
1025 
1026 #endif
bool __ileft
True if this expression has a non-empty left side L : L <= M <= R .
Definition: LpInterface.h:406
unsigned int id() const
Variable id accessor.
unsigned long Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:50
HashTable< LpCol, double > * __coeffs
The coefficients of the variables of the linear inequality.
Definition: LpInterface.h:584
bool operator!=(const LpCol &col) const
Opposite of equality != test between two variables.
Utilities for aGrUM.
double __cste
The constant of the linear inequality.
Definition: LpInterface.h:580
bool __iright
True if this expression has a non-empty right side R : L <= M <= R .
Definition: LpInterface.h:414
HashTable< LpCol, double > * __mCoeffs
The coefficients of each variable on the middle side L : L <= M <= R.
Definition: LpInterface.h:430
std::string toString() const
Get the string representation of a calling variable.
bool operator<(const LpCol &col) const
Test of ordering < between two variables.
Classes providing basic hash functions for hash tables.
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:612
gum is the global namespace for all aGrUM entities
Definition: agrum.h:25
double __mValue
The constant on the middle side L : L <= M <= R.
Definition: LpInterface.h:419
The class for generic Hash Tables.
Definition: hashTable.h:676
Class representing a variable ( a column ) of a linear program, i.e.
Definition: LpInterface.h:59
Class representing a linear expression.
Definition: LpInterface.h:201
double __rValue
The constant on the right side L : L <= M <= R.
Definition: LpInterface.h:421
Class representing a row of the linear program, i.e.
Definition: LpInterface.h:492
HashTable< LpCol, double > * __rCoeffs
The coefficients of each variable on the right side L : L <= M <= R.
Definition: LpInterface.h:434
Lrs wrapper.
double __lValue
The constant on the left side L : L <= M <= R.
Definition: LpInterface.h:417
Class representing a linear program.
Definition: LpInterface.h:47
~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:752
unsigned int __id
Variable id.
Definition: LpInterface.h:161
bool __positivity
true if addPositivity() has been called, false otherwise.
Definition: LpInterface.h:749
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:426
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:112
LpExpr operator*(const SCALAR &lhs, const LpCol &rhs)
Overload of operator * between a scalar and a variable.
bool operator==(const LpCol &col) const
Test of equality == between two variables.
std::vector< LpRow * > __rows
Rows of the problem.
Definition: LpInterface.h:743
All hash functions should inherit from this class.
Definition: hashFunc.h:150
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:745
Class hash tables iterators.
bool __imiddle
True if this expression has a non-empty middle side M ( the default ) : L <= M <= R ...
Definition: LpInterface.h:410