aGrUM  0.14.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  static Size castToSize(const credal::lp::LpCol& key);
188 
194  virtual Size operator()(const credal::lp::LpCol& key) const override final;
195  };
196 } // end of namespace gum
197 
198 namespace gum {
199  namespace credal {
200  namespace lp {
201 
208  class LpExpr {
209  friend class LpRow;
210  template < typename GUM_SCALAR >
211  friend class LpInterface;
212 
213  public:
216 
218  LpExpr();
219 
224  LpExpr(const LpExpr& expr);
225 
239  LpExpr(const LpExpr& expr, bool copyLeft, bool copyMiddle, bool copyRight);
240 
249  LpExpr(LpExpr&& expr);
250 
270  LpExpr(LpExpr&& expr, bool copyLeft, bool copyMiddle, bool copyRight);
271 
273  ~LpExpr();
274 
276 
279 
287  LpExpr& operator=(const LpCol& rhs);
288 
294  LpExpr& operator=(const LpExpr& rhs);
295 
301  LpExpr& operator=(LpExpr&& rhs);
302 
310  template < typename SCALAR >
311  LpExpr& operator=(const SCALAR& rhs);
312 
314 
317 
325  LpExpr& operator+=(const LpCol& rhs);
326 
334  LpExpr& operator+=(const LpExpr& rhs);
335 
342  LpExpr& operator+=(LpExpr&& rhs);
343 
351  template < typename T >
352  LpExpr& operator+=(const T& rhs);
353 
355 
358 
366  LpExpr& operator-=(const LpCol& rhs);
367 
375  LpExpr& operator-=(const LpExpr& rhs);
376 
384  template < typename T >
385  LpExpr& operator-=(const T& rhs);
386 
388 
392  void clear();
393 
398  std::string toString() const;
399 
402  template < typename T1, typename T2 >
403  static LpExpr lessThan(T1&& lhs, T2&& rhs);
404  template < typename SCALAR >
405  static LpExpr multiply(const SCALAR& lhs, const LpCol& rhs);
407 
408  protected:
409  private:
413  bool __ileft;
417  bool __imiddle;
421  bool __iright;
422 
424  double __lValue;
426  double __mValue;
428  double __rValue;
429 
442 
446 
455  inline void __addSide(const LpCol& from);
456 
465  inline void __addSide(const LpExpr& from);
466 
475  inline void __addSide(LpExpr&& from);
476 
486  template < typename SCALAR >
487  inline void __addSide(const SCALAR& from);
488 
490  };
491 
499  class LpRow {
500  template < typename GUM_SCALAR >
501  friend class LpInterface;
502 
503  public:
506 
516  LpRow(const LpExpr& expr, const std::vector< LpCol >& cols);
517 
527  LpRow(LpExpr&& expr, const std::vector< LpCol >& cols);
528 
533  LpRow(const LpRow& row);
534 
539  LpRow(LpRow&& row);
540 
542  ~LpRow();
543 
545 
548 
553  LpRow& operator=(const LpRow& row);
558  LpRow& operator=(LpRow&& row);
559 
561 
571  friend std::ostream&
572  operator<<(std::ostream& out,
573  const LpRow& row); // calls row.toString(),
574  // friend is useless but
575  // good for documentation
576  // purpose
577 
582  std::string toString() const;
583 
584  protected:
585  private:
587  double __cste;
588 
592 
593  }; // end of class LpRow
594 
601  template < typename GUM_SCALAR >
602  class LpInterface {
603  public:
606 
608  LpInterface();
609 
615 
621 
623  ~LpInterface();
624 
626 
629 
636 
642 
644 
654  template < typename T >
655  friend std::ostream&
656  operator<<(std::ostream& out,
657  const LpInterface< T >& lpi); // calls lpi.toString(),
658  // friend is useless but good
659  // for documentation purpose
660 
665  LpCol addCol();
666 
672  std::vector< LpCol > addCols(const unsigned int& cols);
673 
681  void addRow(const LpExpr& expr);
682 
689  void addRow(LpExpr&& expr);
690 
694  void addPositivity();
695 
699  void addSumIsOne();
700 
706  void addProba();
707 
721  std::vector< std::vector< GUM_SCALAR > > solve();
722 
727  std::vector< LpCol > getCols() const;
728 
733  std::string toString() const;
734 
740  void clear();
745  void clearRows();
746 
747  protected:
748  private:
750  std::vector< LpRow* > __rows;
752  std::vector< LpCol > __cols;
753 
760 
761  }; // end of class LpInterface
762 
774  inline void swap(HashTable< LpCol, double >*& a,
776 
779 
798  template < typename T2 >
799  LpExpr operator+(LpExpr&& lhs, const T2& rhs);
800  template < typename T2 >
801  LpExpr operator+(LpExpr& lhs, const T2& rhs);
802 
832  template < typename T1, forbidden_type< T1, LpExpr > = 0 >
833  LpExpr operator+(const T1& lhs, LpExpr&& rhs);
834  template < typename T1, forbidden_type< T1, LpExpr > = 0 >
835  LpExpr operator+(const T1& lhs, LpExpr& rhs);
836 
840  template < typename T2, forbidden_type< T2, LpExpr > = 0 >
841  LpExpr operator+(const LpCol& lhs, const T2& rhs);
842 
846  template < typename T1,
849  LpExpr operator+(const T1& lhs, const LpCol& rhs);
851 
854 
872  template < typename T2 >
873  LpExpr operator-(LpExpr&& lhs, const T2& rhs);
874  template < typename T2 >
875  LpExpr operator-(LpExpr& lhs, const T2& rhs);
876 
906  template < typename T1, forbidden_type< T1, LpExpr > = 0 >
907  LpExpr operator-(const T1& lhs, LpExpr&& rhs);
908  template < typename T1, forbidden_type< T1, LpExpr > = 0 >
909  LpExpr operator-(const T1& lhs, LpExpr& rhs);
910 
921  template < typename T2, forbidden_type< T2, LpExpr > = 0 >
922  LpExpr operator-(const LpCol& lhs, const T2& rhs);
923 
934  template < typename T1,
935  forbidden_type< T1, LpExpr > = 0,
937  LpExpr operator-(const T1& lhs, const LpCol& rhs);
939 
942 
954  template < typename SCALAR >
955  LpExpr operator*(const SCALAR& lhs, const LpCol& rhs);
956 
968  template < typename SCALAR >
969  LpExpr operator*(const LpCol& lhs, const SCALAR& rhs);
970 
972 
975 
997  template < typename T2 >
998  LpExpr operator<=(const LpExpr& lhs, T2&& rhs);
999  template < typename T2 >
1000  LpExpr operator<=(const LpCol& lhs, T2&& rhs);
1001  template < typename T1,
1004  LpExpr operator<=(T1&& lhs, const LpExpr& rhs);
1005  template < typename T1,
1006  forbidden_type< T1, LpExpr& > = 0,
1008  LpExpr operator<=(T1&& lhs, const LpCol& rhs);
1009 
1010  template < typename T2 >
1011  LpExpr operator<=(LpExpr&& lhs, T2&& rhs);
1012  template < typename T2 >
1013  LpExpr operator<=(LpCol&& lhs, T2&& rhs);
1014  template < typename T1,
1015  forbidden_type< T1, LpExpr > = 0,
1017  LpExpr operator<=(T1&& lhs, LpExpr&& rhs);
1018  template < typename T1,
1019  forbidden_type< T1, LpExpr > = 0,
1021  LpExpr operator<=(T1&& lhs, LpCol&& rhs);
1022 
1024 
1025  } // end of namespace lp
1026 
1027  } // namespace credal
1028 
1029 } // end of namespace gum
1030 
1031 #include <agrum/CN/LpInterface_tpl.h>
1032 
1033 #ifndef GUM_NO_INLINE
1034 # include <agrum/CN/LpInterface_inl.h>
1035 #endif /* GUM_NO_INLINE */
1036 
1037 #endif
bool __ileft
True if this expression has a non-empty left side L : L <= M <= R .
Definition: LpInterface.h:413
HashTable< LpCol, double > * __coeffs
The coefficients of the variables of the linear inequality.
Definition: LpInterface.h:591
std::string toString() const
Get the string representation of a calling variable.
bool operator==(const LpCol &col) const
Test of equality == between two variables.
Utilities for aGrUM.
double __cste
The constant of the linear inequality.
Definition: LpInterface.h:587
bool __iright
True if this expression has a non-empty right side R : L <= M <= R .
Definition: LpInterface.h:421
HashTable< LpCol, double > * __mCoeffs
The coefficients of each variable on the middle side L : L <= M <= R.
Definition: LpInterface.h:437
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:469
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:426
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:208
double __rValue
The constant on the right side L : L <= M <= R.
Definition: LpInterface.h:428
Class representing a row of the linear program, i.e.
Definition: LpInterface.h:499
HashTable< LpCol, double > * __rCoeffs
The coefficients of each variable on the right side L : L <= M <= R.
Definition: LpInterface.h:441
Lrs wrapper.
double __lValue
The constant on the left side L : L <= M <= R.
Definition: LpInterface.h:424
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:759
unsigned int __id
Variable id.
Definition: LpInterface.h:161
bool __positivity
true if addPositivity() has been called, false otherwise.
Definition: LpInterface.h:756
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:433
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:124
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:750
All hash functions should inherit from this class.
Definition: hashFunc.h:147
bool operator!=(const LpCol &col) const
Opposite of equality != test between two variables.
Class to include at least once this header.
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:45
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:752
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:417
unsigned int id() const
Variable id accessor.
bool operator<(const LpCol &col) const
Test of ordering < between two variables.