aGrUM 3.2.0
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 >:
[legend]

Public Member Functions

LpCol addCol ()
 Insert a new column, i.e.
std::vector< LpCol > addCols (const unsigned int &cols)
 Insert new columns, i.e.
void addRow (const LpExpr &expr)
 Add rows to the linear program according to a given expression ( which must be at least an inequality ).
void addRow (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.
void addProba ()
 Add positivity constraints and sum of variables is 1 ( probability constraints ).
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).
std::vector< LpCol > getCols () const
 Get the variables of the LP.
std::string toString () const
 Get the string representation of a calling linear program.
void clear ()
 Reset the rows (inequalities) and columns (variables) of the LP as if it was created.
void clearRows ()
 Reset the rows (inequalities) of the LP but not the columns (variables are kept).
Constructor / Destructor
 LpInterface ()
 Default constructor, empty problem.
 LpInterface (const LpInterface< GUM_SCALAR > &from)
 Copy constructor.
 LpInterface (LpInterface< GUM_SCALAR > &&from)
 Move copy constructor.
 ~LpInterface ()
 Default destructor.
Compound assignment operator
LpInterface< GUM_SCALAR > & operator= (const LpInterface< GUM_SCALAR > &from)
 Copy compound assignment.
LpInterface< GUM_SCALAR > & operator= (LpInterface< GUM_SCALAR > &&from)
 Move coumpound assignment.

Private Attributes

std::vector< LpRow * > _rows_
 Rows of the problem.
std::vector< LpCol > _cols_
 Variables of the problem.
bool _positivity_
 true if addPositivity() has been called, false otherwise.
bool _sumIsOne_
 true if addSumIsOne() has been called, false otherwise.

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 << ).

Detailed Description

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

Class representing a linear program.

Author
Matthieu HOURBRACQ and Pierre-Henri WUILLEMIN(_at_LIP6)

Definition at line 624 of file LpInterface.h.

Constructor & Destructor Documentation

◆ LpInterface() [1/3]

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

Default constructor, empty problem.

class LpInterface

Definition at line 110 of file LpInterface_tpl.h.

110 {
111 _positivity_ = false;
112 _sumIsOne_ = false;
114 }
Class representing a linear program.
bool _sumIsOne_
true if addSumIsOne() has been called, false otherwise.
LpInterface()
Default constructor, empty problem.
bool _positivity_
true if addPositivity() has been called, false otherwise.

References LpInterface(), _positivity_, and _sumIsOne_.

Referenced by LpInterface(), LpInterface(), LpInterface(), ~LpInterface(), operator<<, operator=(), and operator=().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ LpInterface() [2/3]

template<GUM_Numeric 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 117 of file LpInterface_tpl.h.

117 :
119 _rows_.resize(from._rows_.size());
120
121 for (unsigned int i = 0, end = from._rows_.size(); i < end; i++)
122 _rows_[i] = new LpRow(*from._rows_[i]);
123
125 }
std::vector< LpCol > _cols_
Variables of the problem.
std::vector< LpRow * > _rows_
Rows of the problem.
LpRow(const LpExpr &expr, const std::vector< LpCol > &cols)
Constructor from an expression and the address of the vector of variables of the problem.

References LpInterface(), _cols_, _positivity_, _rows_, and _sumIsOne_.

Here is the call graph for this function:

◆ LpInterface() [3/3]

template<GUM_Numeric 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 128 of file LpInterface_tpl.h.

References LpInterface(), _cols_, _positivity_, _rows_, and _sumIsOne_.

Here is the call graph for this function:

◆ ~LpInterface()

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

Default destructor.

Definition at line 136 of file LpInterface_tpl.h.

136 {
137 for (const auto row: _rows_)
138 delete row;
139
141 }

References LpInterface(), and _rows_.

Here is the call graph for this function:

Member Function Documentation

◆ addCol()

template<GUM_Numeric 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 184 of file LpInterface_tpl.h.

184 {
185 LpCol col((unsigned int)_cols_.size());
186
187 _cols_.push_back(col);
188
189 return col;
190 }

References _cols_.

◆ addCols()

template<GUM_Numeric 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 193 of file LpInterface_tpl.h.

193 {
194 if (cols < 1)
196 "LpInterface::addCols ( cols ) : cols "
197 "needs must be equal or greater than 1 : "
198 << cols << " < 1");
199
200 for (unsigned int i = 0; i < cols; i++) {
201 _cols_.push_back(LpCol((unsigned int)_cols_.size()));
202 }
203
204 return _cols_;
205 }
LpCol(unsigned int id)
Default constructor.

References gum::credal::lp::LpCol::LpCol(), _cols_, and GUM_ERROR.

Here is the call graph for this function:

◆ addPositivity()

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

Add positivity constraints for all variables.

Definition at line 256 of file LpInterface_tpl.h.

256 {
257 if (_positivity_) return;
258
259 for (const auto& col: _cols_)
260 addRow(0 <= col);
261
262 _positivity_ = true;
263 }
void addRow(const LpExpr &expr)
Add rows to the linear program according to a given expression ( which must be at least an inequality...

References _cols_, _positivity_, and addRow().

Referenced by addProba().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ addProba()

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

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

Definition at line 280 of file LpInterface_tpl.h.

280 {
281 if (_positivity_ && _sumIsOne_) {
282 return;
283 } else if (_positivity_ && !_sumIsOne_) {
284 addSumIsOne();
285 return;
286 } else if (!_positivity_ && _sumIsOne_) {
288 return;
289 }
290
291 // we can do both with one loop, don't call the above functions.
292 // addPositivity();
293 // addSumIsOne();
294 LpExpr expr;
295
296 for (const auto& col: _cols_) {
297 addRow(0 <= col);
298 expr += col;
299 }
300
301 addRow(1 <= std::move(expr) <= 1);
302
303 _sumIsOne_ = true;
304 _positivity_ = true;
305 }
void addSumIsOne()
Add sum of variables is 1 constraints.
void addPositivity()
Add positivity constraints for all variables.

References _cols_, _positivity_, _sumIsOne_, addPositivity(), addRow(), and addSumIsOne().

Here is the call graph for this function:

◆ addRow() [1/2]

template<GUM_Numeric 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 208 of file LpInterface_tpl.h.

208 {
209 if (!expr._ileft_ && !expr._iright_)
211 "addRow ( const LpExpr & expr ) : expr : " << expr.toString()
212 << "is not an inequality.");
213
214 if ((expr._ileft_ && !expr._iright_) || (!expr._ileft_ && expr._iright_)) {
215 _rows_.push_back(new LpRow(expr, _cols_));
216 } else {
217 LpExpr lexpr(expr, true, true, false);
218 LpExpr rexpr(expr, false, true, true);
219
220 _rows_.push_back(new LpRow(std::move(lexpr),
221 _cols_));
222 _rows_.push_back(new LpRow(std::move(rexpr),
223 _cols_));
224 }
225 }
std::string toString() const
Get the string representation of a calling linear program.

References gum::credal::lp::LpRow::LpRow(), _cols_, gum::credal::lp::LpExpr::_ileft_, gum::credal::lp::LpExpr::_iright_, _rows_, GUM_ERROR, and gum::credal::lp::LpExpr::toString().

Referenced by addPositivity(), addProba(), and addSumIsOne().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ addRow() [2/2]

template<GUM_Numeric 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 228 of file LpInterface_tpl.h.

228 {
229 if (!expr._ileft_ && !expr._iright_)
231 "addRow ( const LpExpr & expr ) : expr : " << expr.toString()
232 << "is not an inequality.");
233
234 if ((expr._ileft_ && !expr._iright_) || (!expr._ileft_ && expr._iright_)) {
235 _rows_.push_back(new LpRow(std::move(expr), _cols_));
236 } else {
237 LpExpr lexpr(std::move(expr), true, true, false);
238
240 LpExpr rexpr(std::move(expr), false, false, true);
241
243
244 *rexpr._mCoeffs_ = *lexpr._mCoeffs_;
245 rexpr._mValue_ = lexpr._mValue_;
246 rexpr._imiddle_ = true;
247
248 _rows_.push_back(new LpRow(std::move(lexpr),
249 _cols_));
250 _rows_.push_back(new LpRow(std::move(rexpr),
251 _cols_));
252 }
253 }

References gum::credal::lp::LpRow::LpRow(), _cols_, gum::credal::lp::LpExpr::_imiddle_, gum::credal::lp::LpExpr::_mCoeffs_, gum::credal::lp::LpExpr::_mValue_, _rows_, and GUM_ERROR.

Here is the call graph for this function:

◆ addSumIsOne()

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

Add sum of variables is 1 constraints.

Definition at line 266 of file LpInterface_tpl.h.

266 {
267 if (_sumIsOne_) return;
268
269 LpExpr expr;
270
271 for (const auto& col: _cols_)
272 expr += col;
273
274 addRow(1 <= std::move(expr) <= 1);
275
276 _sumIsOne_ = true;
277 }

References _cols_, _sumIsOne_, and addRow().

Referenced by addProba().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ clear()

template<GUM_Numeric 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 358 of file LpInterface_tpl.h.

358 {
359 for (const auto& row: _rows_)
360 delete row;
361
362 _rows_.clear();
363 _rows_.shrink_to_fit();
367
368 _cols_.clear();
369 _cols_.shrink_to_fit();
370
371 _positivity_ = false;
372 _sumIsOne_ = false;
373 }
void clear()
Reset the rows (inequalities) and columns (variables) of the LP as if it was created.

References _cols_, _positivity_, _rows_, and _sumIsOne_.

Referenced by operator=().

Here is the caller graph for this function:

◆ clearRows()

template<GUM_Numeric 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 376 of file LpInterface_tpl.h.

376 {
377 for (const auto& row: _rows_)
378 delete row;
379
380 _rows_.clear();
381 _rows_.shrink_to_fit();
382
383 _positivity_ = false;
384 _sumIsOne_ = false;
385 }

References _positivity_, _rows_, and _sumIsOne_.

◆ getCols()

template<GUM_Numeric 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 334 of file LpInterface_tpl.h.

334 {
335 return _cols_;
336 }

References _cols_.

◆ operator=() [1/2]

template<GUM_Numeric 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 145 of file LpInterface_tpl.h.

145 {
147 for (const auto& row: _rows_)
148 delete row;
149
150 _rows_.clear();
151 _rows_.shrink_to_fit();
152
153 _rows_.resize(from._rows_.size());
154
155 for (unsigned int i = 0, end = from._rows_.size(); i < end; i++)
156 _rows_[i] = new LpRow(*from._rows_[i]);
157
161
162 return *this;
163 }

References LpInterface(), gum::credal::lp::LpRow::LpRow(), _cols_, _positivity_, _rows_, _sumIsOne_, and clear().

Here is the call graph for this function:

◆ operator=() [2/2]

template<GUM_Numeric 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 167 of file LpInterface_tpl.h.

167 {
168 _rows_.swap(from._rows_);
169 _cols_.swap(from._cols_);
170
173
174 return *this;
175 }

References LpInterface(), _cols_, _positivity_, _rows_, and _sumIsOne_.

Here is the call graph for this function:

◆ solve()

template<GUM_Numeric 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 308 of file LpInterface_tpl.h.

308 {
310
311 lrs.setUpH((unsigned int)_cols_.size());
312
314
315 for (const auto& row: _rows_) {
317
318 expandedRow[0] = row->_cste_;
319
320 for (const auto& elt: *row->_coeffs_)
321 expandedRow[elt.first.id() + 1] = elt.second;
322
323 lrsMatrix.push_back(expandedRow);
324 }
325
326 lrs.fillMatrix(lrsMatrix);
327
328 lrs.H2V();
329
330 return lrs.getOutput();
331 }

References _cols_, _rows_, gum::credal::LRSWrapper< GUM_SCALAR >::fillMatrix(), gum::credal::LRSWrapper< GUM_SCALAR >::getOutput(), gum::credal::LRSWrapper< GUM_SCALAR >::H2V(), and gum::credal::LRSWrapper< GUM_SCALAR >::setUpH().

Here is the call graph for this function:

◆ toString()

template<GUM_Numeric 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 339 of file LpInterface_tpl.h.

339 {
341
342 s << std::endl << std::endl << "Variables : " << std::endl;
343
344 for (const auto& col: _cols_)
345 s << " " << col.toString();
346
347 s << std::endl;
348
349 for (const auto& row: _rows_)
350 s << std::endl << row->toString();
351
352 s << std::endl << std::endl;
353
354 return s.str();
355 }

References _cols_, and _rows_.

Referenced by operator<<.

Here is the caller graph for this function:

◆ operator<<

template<GUM_Numeric 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 178 of file LpInterface_tpl.h.

178 {
179 out << lpi.toString();
180 return out;
181 }

References LpInterface(), operator<<, and toString().

Referenced by operator<<.

Member Data Documentation

◆ _cols_

template<GUM_Numeric GUM_SCALAR>
std::vector< LpCol > gum::credal::lp::LpInterface< GUM_SCALAR >::_cols_
private

◆ _positivity_

template<GUM_Numeric GUM_SCALAR>
bool gum::credal::lp::LpInterface< GUM_SCALAR >::_positivity_
private

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

Definition at line 778 of file LpInterface.h.

Referenced by LpInterface(), LpInterface(), LpInterface(), addPositivity(), addProba(), clear(), clearRows(), operator=(), and operator=().

◆ _rows_

template<GUM_Numeric GUM_SCALAR>
std::vector< LpRow* > gum::credal::lp::LpInterface< GUM_SCALAR >::_rows_
private

Rows of the problem.

Definition at line 772 of file LpInterface.h.

Referenced by LpInterface(), LpInterface(), ~LpInterface(), addRow(), addRow(), clear(), clearRows(), operator=(), operator=(), solve(), and toString().

◆ _sumIsOne_

template<GUM_Numeric GUM_SCALAR>
bool gum::credal::lp::LpInterface< GUM_SCALAR >::_sumIsOne_
private

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

Definition at line 781 of file LpInterface.h.

Referenced by LpInterface(), LpInterface(), LpInterface(), addProba(), addSumIsOne(), clear(), clearRows(), operator=(), and operator=().


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