aGrUM  0.20.2
a C++ library for (probabilistic) graphical models
gum::credal::lp::LpInterface< GUM_SCALAR > Class Template Reference

Class representing a linear program. More...

#include <agrum/CN/LpInterface.h>

+ Collaboration diagram for gum::credal::lp::LpInterface< GUM_SCALAR >:

Public Member Functions

LpCol addCol ()
 Insert a new column, i.e. More...
 
std::vector< LpColaddCols (const unsigned int &cols)
 Insert new columns, i.e. More...
 
void addRow (const LpExpr &expr)
 Add rows to the linear program according to a given expression ( which must be at least an inequality ). More...
 
void addRow (LpExpr &&expr)
 Add rows to the linear program according to a given expression ( which must be at least an inequality ). More...
 
void addPositivity ()
 Add positivity constraints for all variables. More...
 
void addSumIsOne ()
 Add sum of variables is 1 constraints. More...
 
void addProba ()
 Add positivity constraints and sum of variables is 1 ( probability constraints ) More...
 
std::vector< std::vector< GUM_SCALAR > > solve ()
 Solve the linear program (H-representation of the polytope) by enumeration (of the polytope vertices) using lexicographic reverse search (lrs). More...
 
std::vector< LpColgetCols () const
 Get the variables of the LP. More...
 
std::string toString () const
 Get the string representation of a calling linear program. More...
 
void clear ()
 Reset the rows (inequalities) and columns (variables) of the LP as if it was created. More...
 
void clearRows ()
 Reset the rows (inequalities) of the LP but not the columns (variables are kept). More...
 
Constructor / Destructor
 LpInterface ()
 Default constructor, empty problem. More...
 
 LpInterface (const LpInterface< GUM_SCALAR > &from)
 Copy constructor. More...
 
 LpInterface (LpInterface< GUM_SCALAR > &&from)
 Move copy constructor. More...
 
 ~LpInterface ()
 Default destructor. More...
 
Compound assignment operator
LpInterface< GUM_SCALAR > & operator= (const LpInterface< GUM_SCALAR > &from)
 Copy compound assignment. More...
 
LpInterface< GUM_SCALAR > & operator= (LpInterface< GUM_SCALAR > &&from)
 Move coumpound assignment. More...
 

Friends

template<typename T >
std::ostream & operator<< (std::ostream &out, const LpInterface< T > &lpi)
 Overload of << to use with output streams ( such as std::cout << ). More...
 

Detailed Description

template<typename GUM_SCALAR>
class gum::credal::lp::LpInterface< GUM_SCALAR >

Class representing a linear program.

Author
Matthieu HOURBRACQ and Pierre-Henri WUILLEMIN()

Definition at line 48 of file LpInterface.h.

Constructor & Destructor Documentation

◆ LpInterface() [1/3]

template<typename GUM_SCALAR >
gum::credal::lp::LpInterface< GUM_SCALAR >::LpInterface ( )

Default constructor, empty problem.

class LpInterface

Definition at line 88 of file LpInterface_tpl.h.

88  {
89  positivity__ = false;
90  sumIsOne__ = false;
91  GUM_CONSTRUCTOR(LpInterface);
92  }
bool sumIsOne__
true if addSumIsOne() has been called, false otherwise.
Definition: LpInterface.h:760
LpInterface()
Default constructor, empty problem.
bool positivity__
true if addPositivity() has been called, false otherwise.
Definition: LpInterface.h:757

◆ LpInterface() [2/3]

template<typename GUM_SCALAR >
gum::credal::lp::LpInterface< GUM_SCALAR >::LpInterface ( const LpInterface< GUM_SCALAR > &  from)

Copy constructor.

Parameters
fromThe LpInterface to be copied.

Definition at line 95 of file LpInterface_tpl.h.

96  :
97  cols__(from.cols__),
98  positivity__(from.positivity__), sumIsOne__(from.sumIsOne__) {
99  rows__.resize(from.rows__.size());
100 
101  for (unsigned int i = 0, end = from.rows__.size(); i < end; i++)
102  rows__[i] = new LpRow(*from.rows__[i]);
103 
104  GUM_CONS_CPY(LpInterface);
105  }
bool sumIsOne__
true if addSumIsOne() has been called, false otherwise.
Definition: LpInterface.h:760
LpInterface()
Default constructor, empty problem.
std::vector< LpCol > cols__
Variables of the problem.
Definition: LpInterface.h:753
bool positivity__
true if addPositivity() has been called, false otherwise.
Definition: LpInterface.h:757
std::vector< LpRow *> rows__
Rows of the problem.
Definition: LpInterface.h:751

◆ LpInterface() [3/3]

template<typename GUM_SCALAR >
gum::credal::lp::LpInterface< GUM_SCALAR >::LpInterface ( LpInterface< GUM_SCALAR > &&  from)

Move copy constructor.

Parameters
fromThe temporary LpInterface to be moved.

Definition at line 108 of file LpInterface_tpl.h.

108  :
109  positivity__(from.positivity__), sumIsOne__(from.sumIsOne__) {
110  rows__.swap(from.rows__);
111  cols__.swap(from.cols__);
112  GUM_CONS_CPY(LpInterface);
113  }
bool sumIsOne__
true if addSumIsOne() has been called, false otherwise.
Definition: LpInterface.h:760
LpInterface()
Default constructor, empty problem.
std::vector< LpCol > cols__
Variables of the problem.
Definition: LpInterface.h:753
bool positivity__
true if addPositivity() has been called, false otherwise.
Definition: LpInterface.h:757
std::vector< LpRow *> rows__
Rows of the problem.
Definition: LpInterface.h:751

◆ ~LpInterface()

template<typename GUM_SCALAR >
gum::credal::lp::LpInterface< GUM_SCALAR >::~LpInterface ( )

Default destructor.

Definition at line 116 of file LpInterface_tpl.h.

116  {
117  for (const auto row: rows__)
118  delete row;
119 
120  GUM_DESTRUCTOR(LpInterface);
121  }
LpInterface()
Default constructor, empty problem.
std::vector< LpRow *> rows__
Rows of the problem.
Definition: LpInterface.h:751

Member Function Documentation

◆ addCol()

template<typename GUM_SCALAR >
LpCol gum::credal::lp::LpInterface< GUM_SCALAR >::addCol ( )

Insert a new column, i.e.

a new variable.

Returns
A copy of the variable.

Definition at line 164 of file LpInterface_tpl.h.

164  {
165  LpCol col((unsigned int)cols__.size());
166 
167  cols__.push_back(col);
168 
169  return col;
170  }
std::vector< LpCol > cols__
Variables of the problem.
Definition: LpInterface.h:753

◆ addCols()

template<typename GUM_SCALAR >
std::vector< LpCol > gum::credal::lp::LpInterface< GUM_SCALAR >::addCols ( const unsigned int &  cols)

Insert new columns, i.e.

new variables.

Parameters
colsthe constant reference to the number of variables we want.
Returns
The copy of the vector of all variables of the problem.

Definition at line 174 of file LpInterface_tpl.h.

174  {
175  if (cols < 1)
176  GUM_ERROR(OperationNotAllowed,
177  "LpInterface::addCols ( cols ) : cols "
178  "needs must be equal or greater than 1 : "
179  << cols << " < 1");
180 
181  for (unsigned int i = 0; i < cols; i++) {
182  cols__.push_back(LpCol((unsigned int)cols__.size()));
183  }
184 
185  return cols__;
186  }
std::vector< LpCol > cols__
Variables of the problem.
Definition: LpInterface.h:753
#define GUM_ERROR(type, msg)
Definition: exceptions.h:54

◆ addPositivity()

template<typename GUM_SCALAR >
void gum::credal::lp::LpInterface< GUM_SCALAR >::addPositivity ( )

Add positivity constraints for all variables.

Definition at line 241 of file LpInterface_tpl.h.

241  {
242  if (positivity__) return;
243 
244  for (const auto& col: cols__)
245  addRow(0 <= col);
246 
247  positivity__ = true;
248  }
void addRow(const LpExpr &expr)
Add rows to the linear program according to a given expression ( which must be at least an inequality...
std::vector< LpCol > cols__
Variables of the problem.
Definition: LpInterface.h:753
bool positivity__
true if addPositivity() has been called, false otherwise.
Definition: LpInterface.h:757

◆ addProba()

template<typename GUM_SCALAR >
void gum::credal::lp::LpInterface< GUM_SCALAR >::addProba ( )

Add positivity constraints and sum of variables is 1 ( probability constraints )

Definition at line 265 of file LpInterface_tpl.h.

265  {
266  if (positivity__ && sumIsOne__) {
267  return;
268  } else if (positivity__ && !sumIsOne__) {
269  addSumIsOne();
270  return;
271  } else if (!positivity__ && sumIsOne__) {
272  addPositivity();
273  return;
274  }
275 
276  // we can do both with one loop, don't call the above functions.
277  // addPositivity();
278  // addSumIsOne();
279  LpExpr expr;
280 
281  for (const auto& col: cols__) {
282  addRow(0 <= col);
283  expr += col;
284  }
285 
286  addRow(1 <= std::move(expr) <= 1);
287 
288  sumIsOne__ = true;
289  positivity__ = true;
290  }
void addRow(const LpExpr &expr)
Add rows to the linear program according to a given expression ( which must be at least an inequality...
void addPositivity()
Add positivity constraints for all variables.
void addSumIsOne()
Add sum of variables is 1 constraints.
bool sumIsOne__
true if addSumIsOne() has been called, false otherwise.
Definition: LpInterface.h:760
std::vector< LpCol > cols__
Variables of the problem.
Definition: LpInterface.h:753
bool positivity__
true if addPositivity() has been called, false otherwise.
Definition: LpInterface.h:757

◆ addRow() [1/2]

template<typename GUM_SCALAR >
void gum::credal::lp::LpInterface< GUM_SCALAR >::addRow ( const LpExpr expr)

Add rows to the linear program according to a given expression ( which must be at least an inequality ).

Parameters
exprthe constant reference to the expression to convert to rows.

Definition at line 189 of file LpInterface_tpl.h.

189  {
190  if (!expr.ileft__ && !expr.iright__)
191  GUM_ERROR(OperationNotAllowed,
192  "addRow ( const LpExpr & expr ) : expr : "
193  << expr.toString() << "is not an inequality.");
194 
195  if ((expr.ileft__ && !expr.iright__) || (!expr.ileft__ && expr.iright__)) {
196  rows__.push_back(new LpRow(expr, cols__));
197  } else {
198  LpExpr lexpr(expr, true, true, false);
199  LpExpr rexpr(expr, false, true, true);
200 
201  rows__.push_back(
202  new LpRow(std::move(lexpr),
203  cols__));
204  rows__.push_back(
205  new LpRow(std::move(rexpr),
206  cols__));
207  }
208  }
std::vector< LpCol > cols__
Variables of the problem.
Definition: LpInterface.h:753
std::vector< LpRow *> rows__
Rows of the problem.
Definition: LpInterface.h:751
#define GUM_ERROR(type, msg)
Definition: exceptions.h:54

◆ addRow() [2/2]

template<typename GUM_SCALAR >
void gum::credal::lp::LpInterface< GUM_SCALAR >::addRow ( LpExpr &&  expr)

Add rows to the linear program according to a given expression ( which must be at least an inequality ).

Parameters
exprthe temporary expression to move to rows.

Definition at line 211 of file LpInterface_tpl.h.

211  {
212  if (!expr.ileft__ && !expr.iright__)
213  GUM_ERROR(OperationNotAllowed,
214  "addRow ( const LpExpr & expr ) : expr : "
215  << expr.toString() << "is not an inequality.");
216 
217  if ((expr.ileft__ && !expr.iright__) || (!expr.ileft__ && expr.iright__)) {
218  rows__.push_back(new LpRow(std::move(expr), cols__));
219  } else {
220  LpExpr lexpr(std::move(expr), true, true, false);
221 
223  LpExpr rexpr(std::move(expr), false, false, true);
224 
226 
227  *rexpr.mCoeffs__ = *lexpr.mCoeffs__;
228  rexpr.mValue__ = lexpr.mValue__;
229  rexpr.imiddle__ = true;
230 
231  rows__.push_back(
232  new LpRow(std::move(lexpr),
233  cols__));
234  rows__.push_back(
235  new LpRow(std::move(rexpr),
236  cols__));
237  }
238  }
std::vector< LpCol > cols__
Variables of the problem.
Definition: LpInterface.h:753
std::vector< LpRow *> rows__
Rows of the problem.
Definition: LpInterface.h:751
#define GUM_ERROR(type, msg)
Definition: exceptions.h:54

◆ addSumIsOne()

template<typename GUM_SCALAR >
void gum::credal::lp::LpInterface< GUM_SCALAR >::addSumIsOne ( )

Add sum of variables is 1 constraints.

Definition at line 251 of file LpInterface_tpl.h.

251  {
252  if (sumIsOne__) return;
253 
254  LpExpr expr;
255 
256  for (const auto& col: cols__)
257  expr += col;
258 
259  addRow(1 <= std::move(expr) <= 1);
260 
261  sumIsOne__ = true;
262  }
void addRow(const LpExpr &expr)
Add rows to the linear program according to a given expression ( which must be at least an inequality...
bool sumIsOne__
true if addSumIsOne() has been called, false otherwise.
Definition: LpInterface.h:760
std::vector< LpCol > cols__
Variables of the problem.
Definition: LpInterface.h:753

◆ clear()

template<typename GUM_SCALAR >
void gum::credal::lp::LpInterface< GUM_SCALAR >::clear ( )

Reset the rows (inequalities) and columns (variables) of the LP as if it was created.

Definition at line 343 of file LpInterface_tpl.h.

343  {
344  for (const auto& row: rows__)
345  delete row;
346 
347  rows__.clear();
348  rows__.shrink_to_fit();
349 
353  cols__.clear();
354  cols__.shrink_to_fit();
355 
356  positivity__ = false;
357  sumIsOne__ = false;
358  }
bool sumIsOne__
true if addSumIsOne() has been called, false otherwise.
Definition: LpInterface.h:760
std::vector< LpCol > cols__
Variables of the problem.
Definition: LpInterface.h:753
bool positivity__
true if addPositivity() has been called, false otherwise.
Definition: LpInterface.h:757
std::vector< LpRow *> rows__
Rows of the problem.
Definition: LpInterface.h:751

◆ clearRows()

template<typename GUM_SCALAR >
void gum::credal::lp::LpInterface< GUM_SCALAR >::clearRows ( )

Reset the rows (inequalities) of the LP but not the columns (variables are kept).

Definition at line 361 of file LpInterface_tpl.h.

361  {
362  for (const auto& row: rows__)
363  delete row;
364 
365  rows__.clear();
366  rows__.shrink_to_fit();
367 
368  positivity__ = false;
369  sumIsOne__ = false;
370  }
bool sumIsOne__
true if addSumIsOne() has been called, false otherwise.
Definition: LpInterface.h:760
bool positivity__
true if addPositivity() has been called, false otherwise.
Definition: LpInterface.h:757
std::vector< LpRow *> rows__
Rows of the problem.
Definition: LpInterface.h:751

◆ getCols()

template<typename GUM_SCALAR >
std::vector< LpCol > gum::credal::lp::LpInterface< GUM_SCALAR >::getCols ( ) const

Get the variables of the LP.

Returns
A copy of the variables as a vector of variables.

Definition at line 319 of file LpInterface_tpl.h.

319  {
320  return cols__;
321  }
std::vector< LpCol > cols__
Variables of the problem.
Definition: LpInterface.h:753

◆ operator=() [1/2]

template<typename GUM_SCALAR >
LpInterface< GUM_SCALAR > & gum::credal::lp::LpInterface< GUM_SCALAR >::operator= ( const LpInterface< GUM_SCALAR > &  from)

Copy compound assignment.

Parameters
fromThe LpInterface to be copied.

Definition at line 124 of file LpInterface_tpl.h.

125  {
127  for (const auto& row: rows__)
128  delete row;
129 
130  rows__.clear();
131  rows__.shrink_to_fit();
132 
133  rows__.resize(from.rows__.size());
134 
135  for (unsigned int i = 0, end = from.rows__.size(); i < end; i++)
136  rows__[i] = new LpRow(*from.rows__[i]);
137 
138  cols__ = from.cols__;
139  positivity__ = from.positivity__;
140  sumIsOne__ = from.sumIsOne__;
141 
142  return *this;
143  }
bool sumIsOne__
true if addSumIsOne() has been called, false otherwise.
Definition: LpInterface.h:760
std::vector< LpCol > cols__
Variables of the problem.
Definition: LpInterface.h:753
bool positivity__
true if addPositivity() has been called, false otherwise.
Definition: LpInterface.h:757
std::vector< LpRow *> rows__
Rows of the problem.
Definition: LpInterface.h:751

◆ operator=() [2/2]

template<typename GUM_SCALAR >
LpInterface< GUM_SCALAR > & gum::credal::lp::LpInterface< GUM_SCALAR >::operator= ( LpInterface< GUM_SCALAR > &&  from)

Move coumpound assignment.

Parameters
fromThe temporary LpInterface to be moved.

Definition at line 147 of file LpInterface_tpl.h.

147  {
148  rows__.swap(from.rows__);
149  cols__.swap(from.cols__);
150 
151  positivity__ = from.positivity__;
152  sumIsOne__ = from.sumIsOne__;
153 
154  return *this;
155  }
bool sumIsOne__
true if addSumIsOne() has been called, false otherwise.
Definition: LpInterface.h:760
std::vector< LpCol > cols__
Variables of the problem.
Definition: LpInterface.h:753
bool positivity__
true if addPositivity() has been called, false otherwise.
Definition: LpInterface.h:757
std::vector< LpRow *> rows__
Rows of the problem.
Definition: LpInterface.h:751

◆ solve()

template<typename GUM_SCALAR >
std::vector< std::vector< GUM_SCALAR > > gum::credal::lp::LpInterface< GUM_SCALAR >::solve ( )

Solve the linear program (H-representation of the polytope) by enumeration (of the polytope vertices) using lexicographic reverse search (lrs).

Only works with bounded polyhedron (polytopes) and not unbounded ones (i.e. defined by vertices and rays).

Returns
The V-representation (vertices) of the polytope as a vector of vectors (vector of vertices).

Definition at line 293 of file LpInterface_tpl.h.

293  {
294  LRSWrapper< GUM_SCALAR > lrs;
295 
296  lrs.setUpH((unsigned int)cols__.size());
297 
298  std::vector< std::vector< GUM_SCALAR > > lrsMatrix;
299 
300  for (const auto& row: rows__) {
301  std::vector< GUM_SCALAR > expandedRow(cols__.size() + 1, 0);
302 
303  expandedRow[0] = row->cste__;
304 
305  for (const auto& elt: *row->coeffs__)
306  expandedRow[elt.first.id() + 1] = elt.second;
307 
308  lrsMatrix.push_back(expandedRow);
309  }
310 
311  lrs.fillMatrix(lrsMatrix);
312 
313  lrs.H2V();
314 
315  return lrs.getOutput();
316  }
std::vector< LpCol > cols__
Variables of the problem.
Definition: LpInterface.h:753
std::vector< LpRow *> rows__
Rows of the problem.
Definition: LpInterface.h:751

◆ toString()

template<typename GUM_SCALAR >
std::string gum::credal::lp::LpInterface< GUM_SCALAR >::toString ( ) const

Get the string representation of a calling linear program.

Returns
The string representation of the calling linear program.

Definition at line 324 of file LpInterface_tpl.h.

324  {
325  std::ostringstream s;
326 
327  s << std::endl << std::endl << "Variables : " << std::endl;
328 
329  for (const auto& col: cols__)
330  s << " " << col.toString();
331 
332  s << std::endl;
333 
334  for (const auto& row: rows__)
335  s << std::endl << row->toString();
336 
337  s << std::endl << std::endl;
338 
339  return s.str();
340  }
std::vector< LpCol > cols__
Variables of the problem.
Definition: LpInterface.h:753
std::vector< LpRow *> rows__
Rows of the problem.
Definition: LpInterface.h:751

Friends And Related Function Documentation

◆ operator<<

template<typename GUM_SCALAR >
template<typename T >
std::ostream& operator<< ( std::ostream &  out,
const LpInterface< T > &  lpi 
)
friend

Overload of << to use with output streams ( such as std::cout << ).

Parameters
outthe reference to the caller, i.e. left side of <<.
lpithe constant reference to the linear program whose representation we want.
Returns
The address of the caller.

Definition at line 158 of file LpInterface_tpl.h.

158  {
159  out << lpi.toString();
160  return out;
161  }

Member Data Documentation

◆ cols__

template<typename GUM_SCALAR >
std::vector< LpCol > gum::credal::lp::LpInterface< GUM_SCALAR >::cols__
private

Variables of the problem.

Definition at line 753 of file LpInterface.h.

◆ positivity__

template<typename GUM_SCALAR >
bool gum::credal::lp::LpInterface< GUM_SCALAR >::positivity__
private

true if addPositivity() has been called, false otherwise.

Definition at line 757 of file LpInterface.h.

◆ rows__

template<typename GUM_SCALAR >
std::vector< LpRow* > gum::credal::lp::LpInterface< GUM_SCALAR >::rows__
private

Rows of the problem.

Definition at line 751 of file LpInterface.h.

◆ sumIsOne__

template<typename GUM_SCALAR >
bool gum::credal::lp::LpInterface< GUM_SCALAR >::sumIsOne__
private

true if addSumIsOne() has been called, false otherwise.

Definition at line 760 of file LpInterface.h.


The documentation for this class was generated from the following files: