aGrUM  0.20.3
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 86 of file LpInterface_tpl.h.

86  {
87  _positivity_ = false;
88  _sumIsOne_ = false;
89  GUM_CONSTRUCTOR(LpInterface);
90  }
bool _sumIsOne_
true if addSumIsOne() has been called, false otherwise.
Definition: LpInterface.h:757
LpInterface()
Default constructor, empty problem.
bool _positivity_
true if addPositivity() has been called, false otherwise.
Definition: LpInterface.h:754

◆ 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 93 of file LpInterface_tpl.h.

93  :
94  _cols_(from._cols_), _positivity_(from._positivity_), _sumIsOne_(from._sumIsOne_) {
95  _rows_.resize(from._rows_.size());
96 
97  for (unsigned int i = 0, end = from._rows_.size(); i < end; i++)
98  _rows_[i] = new LpRow(*from._rows_[i]);
99 
100  GUM_CONS_CPY(LpInterface);
101  }
std::vector< LpCol > _cols_
Variables of the problem.
Definition: LpInterface.h:750
bool _sumIsOne_
true if addSumIsOne() has been called, false otherwise.
Definition: LpInterface.h:757
std::vector< LpRow *> _rows_
Rows of the problem.
Definition: LpInterface.h:748
LpInterface()
Default constructor, empty problem.
bool _positivity_
true if addPositivity() has been called, false otherwise.
Definition: LpInterface.h:754

◆ 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 104 of file LpInterface_tpl.h.

104  :
105  _positivity_(from._positivity_), _sumIsOne_(from._sumIsOne_) {
106  _rows_.swap(from._rows_);
107  _cols_.swap(from._cols_);
108  GUM_CONS_CPY(LpInterface);
109  }
std::vector< LpCol > _cols_
Variables of the problem.
Definition: LpInterface.h:750
bool _sumIsOne_
true if addSumIsOne() has been called, false otherwise.
Definition: LpInterface.h:757
std::vector< LpRow *> _rows_
Rows of the problem.
Definition: LpInterface.h:748
LpInterface()
Default constructor, empty problem.
bool _positivity_
true if addPositivity() has been called, false otherwise.
Definition: LpInterface.h:754

◆ ~LpInterface()

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

Default destructor.

Definition at line 112 of file LpInterface_tpl.h.

112  {
113  for (const auto row: _rows_)
114  delete row;
115 
116  GUM_DESTRUCTOR(LpInterface);
117  }
std::vector< LpRow *> _rows_
Rows of the problem.
Definition: LpInterface.h:748
LpInterface()
Default constructor, empty problem.

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 160 of file LpInterface_tpl.h.

160  {
161  LpCol col((unsigned int)_cols_.size());
162 
163  _cols_.push_back(col);
164 
165  return col;
166  }
std::vector< LpCol > _cols_
Variables of the problem.
Definition: LpInterface.h:750

◆ 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 169 of file LpInterface_tpl.h.

169  {
170  if (cols < 1)
171  GUM_ERROR(OperationNotAllowed,
172  "LpInterface::addCols ( cols ) : cols "
173  "needs must be equal or greater than 1 : "
174  << cols << " < 1");
175 
176  for (unsigned int i = 0; i < cols; i++) {
177  _cols_.push_back(LpCol((unsigned int)_cols_.size()));
178  }
179 
180  return _cols_;
181  }
std::vector< LpCol > _cols_
Variables of the problem.
Definition: LpInterface.h:750
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51

◆ addPositivity()

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

Add positivity constraints for all variables.

Definition at line 232 of file LpInterface_tpl.h.

232  {
233  if (_positivity_) return;
234 
235  for (const auto& col: _cols_)
236  addRow(0 <= col);
237 
238  _positivity_ = true;
239  }
std::vector< LpCol > _cols_
Variables of the problem.
Definition: LpInterface.h:750
void addRow(const LpExpr &expr)
Add rows to the linear program according to a given expression ( which must be at least an inequality...
bool _positivity_
true if addPositivity() has been called, false otherwise.
Definition: LpInterface.h:754

◆ 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 256 of file LpInterface_tpl.h.

256  {
257  if (_positivity_ && _sumIsOne_) {
258  return;
259  } else if (_positivity_ && !_sumIsOne_) {
260  addSumIsOne();
261  return;
262  } else if (!_positivity_ && _sumIsOne_) {
263  addPositivity();
264  return;
265  }
266 
267  // we can do both with one loop, don't call the above functions.
268  // addPositivity();
269  // addSumIsOne();
270  LpExpr expr;
271 
272  for (const auto& col: _cols_) {
273  addRow(0 <= col);
274  expr += col;
275  }
276 
277  addRow(1 <= std::move(expr) <= 1);
278 
279  _sumIsOne_ = true;
280  _positivity_ = true;
281  }
std::vector< LpCol > _cols_
Variables of the problem.
Definition: LpInterface.h:750
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:757
void addPositivity()
Add positivity constraints for all variables.
void addSumIsOne()
Add sum of variables is 1 constraints.
bool _positivity_
true if addPositivity() has been called, false otherwise.
Definition: LpInterface.h:754

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

184  {
185  if (!expr._ileft_ && !expr._iright_)
186  GUM_ERROR(OperationNotAllowed,
187  "addRow ( const LpExpr & expr ) : expr : " << expr.toString()
188  << "is not an inequality.");
189 
190  if ((expr._ileft_ && !expr._iright_) || (!expr._ileft_ && expr._iright_)) {
191  _rows_.push_back(new LpRow(expr, _cols_));
192  } else {
193  LpExpr lexpr(expr, true, true, false);
194  LpExpr rexpr(expr, false, true, true);
195 
196  _rows_.push_back(new LpRow(std::move(lexpr),
197  _cols_));
198  _rows_.push_back(new LpRow(std::move(rexpr),
199  _cols_));
200  }
201  }
std::vector< LpCol > _cols_
Variables of the problem.
Definition: LpInterface.h:750
std::vector< LpRow *> _rows_
Rows of the problem.
Definition: LpInterface.h:748
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51

◆ 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 204 of file LpInterface_tpl.h.

204  {
205  if (!expr._ileft_ && !expr._iright_)
206  GUM_ERROR(OperationNotAllowed,
207  "addRow ( const LpExpr & expr ) : expr : " << expr.toString()
208  << "is not an inequality.");
209 
210  if ((expr._ileft_ && !expr._iright_) || (!expr._ileft_ && expr._iright_)) {
211  _rows_.push_back(new LpRow(std::move(expr), _cols_));
212  } else {
213  LpExpr lexpr(std::move(expr), true, true, false);
214 
216  LpExpr rexpr(std::move(expr), false, false, true);
217 
219 
220  *rexpr._mCoeffs_ = *lexpr._mCoeffs_;
221  rexpr._mValue_ = lexpr._mValue_;
222  rexpr._imiddle_ = true;
223 
224  _rows_.push_back(new LpRow(std::move(lexpr),
225  _cols_));
226  _rows_.push_back(new LpRow(std::move(rexpr),
227  _cols_));
228  }
229  }
std::vector< LpCol > _cols_
Variables of the problem.
Definition: LpInterface.h:750
std::vector< LpRow *> _rows_
Rows of the problem.
Definition: LpInterface.h:748
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51

◆ addSumIsOne()

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

Add sum of variables is 1 constraints.

Definition at line 242 of file LpInterface_tpl.h.

242  {
243  if (_sumIsOne_) return;
244 
245  LpExpr expr;
246 
247  for (const auto& col: _cols_)
248  expr += col;
249 
250  addRow(1 <= std::move(expr) <= 1);
251 
252  _sumIsOne_ = true;
253  }
std::vector< LpCol > _cols_
Variables of the problem.
Definition: LpInterface.h:750
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:757

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

334  {
335  for (const auto& row: _rows_)
336  delete row;
337 
338  _rows_.clear();
339  _rows_.shrink_to_fit();
340 
344  _cols_.clear();
345  _cols_.shrink_to_fit();
346 
347  _positivity_ = false;
348  _sumIsOne_ = false;
349  }
std::vector< LpCol > _cols_
Variables of the problem.
Definition: LpInterface.h:750
bool _sumIsOne_
true if addSumIsOne() has been called, false otherwise.
Definition: LpInterface.h:757
std::vector< LpRow *> _rows_
Rows of the problem.
Definition: LpInterface.h:748
bool _positivity_
true if addPositivity() has been called, false otherwise.
Definition: LpInterface.h:754

◆ 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 352 of file LpInterface_tpl.h.

352  {
353  for (const auto& row: _rows_)
354  delete row;
355 
356  _rows_.clear();
357  _rows_.shrink_to_fit();
358 
359  _positivity_ = false;
360  _sumIsOne_ = false;
361  }
bool _sumIsOne_
true if addSumIsOne() has been called, false otherwise.
Definition: LpInterface.h:757
std::vector< LpRow *> _rows_
Rows of the problem.
Definition: LpInterface.h:748
bool _positivity_
true if addPositivity() has been called, false otherwise.
Definition: LpInterface.h:754

◆ 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 310 of file LpInterface_tpl.h.

310  {
311  return _cols_;
312  }
std::vector< LpCol > _cols_
Variables of the problem.
Definition: LpInterface.h:750

◆ 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 121 of file LpInterface_tpl.h.

121  {
123  for (const auto& row: _rows_)
124  delete row;
125 
126  _rows_.clear();
127  _rows_.shrink_to_fit();
128 
129  _rows_.resize(from._rows_.size());
130 
131  for (unsigned int i = 0, end = from._rows_.size(); i < end; i++)
132  _rows_[i] = new LpRow(*from._rows_[i]);
133 
134  _cols_ = from._cols_;
135  _positivity_ = from._positivity_;
136  _sumIsOne_ = from._sumIsOne_;
137 
138  return *this;
139  }
std::vector< LpCol > _cols_
Variables of the problem.
Definition: LpInterface.h:750
bool _sumIsOne_
true if addSumIsOne() has been called, false otherwise.
Definition: LpInterface.h:757
std::vector< LpRow *> _rows_
Rows of the problem.
Definition: LpInterface.h:748
bool _positivity_
true if addPositivity() has been called, false otherwise.
Definition: LpInterface.h:754

◆ 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 143 of file LpInterface_tpl.h.

143  {
144  _rows_.swap(from._rows_);
145  _cols_.swap(from._cols_);
146 
147  _positivity_ = from._positivity_;
148  _sumIsOne_ = from._sumIsOne_;
149 
150  return *this;
151  }
std::vector< LpCol > _cols_
Variables of the problem.
Definition: LpInterface.h:750
bool _sumIsOne_
true if addSumIsOne() has been called, false otherwise.
Definition: LpInterface.h:757
std::vector< LpRow *> _rows_
Rows of the problem.
Definition: LpInterface.h:748
bool _positivity_
true if addPositivity() has been called, false otherwise.
Definition: LpInterface.h:754

◆ 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 284 of file LpInterface_tpl.h.

284  {
285  LRSWrapper< GUM_SCALAR > lrs;
286 
287  lrs.setUpH((unsigned int)_cols_.size());
288 
289  std::vector< std::vector< GUM_SCALAR > > lrsMatrix;
290 
291  for (const auto& row: _rows_) {
292  std::vector< GUM_SCALAR > expandedRow(_cols_.size() + 1, 0);
293 
294  expandedRow[0] = row->_cste_;
295 
296  for (const auto& elt: *row->_coeffs_)
297  expandedRow[elt.first.id() + 1] = elt.second;
298 
299  lrsMatrix.push_back(expandedRow);
300  }
301 
302  lrs.fillMatrix(lrsMatrix);
303 
304  lrs.H2V();
305 
306  return lrs.getOutput();
307  }
std::vector< LpCol > _cols_
Variables of the problem.
Definition: LpInterface.h:750
std::vector< LpRow *> _rows_
Rows of the problem.
Definition: LpInterface.h:748

◆ 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 315 of file LpInterface_tpl.h.

315  {
316  std::ostringstream s;
317 
318  s << std::endl << std::endl << "Variables : " << std::endl;
319 
320  for (const auto& col: _cols_)
321  s << " " << col.toString();
322 
323  s << std::endl;
324 
325  for (const auto& row: _rows_)
326  s << std::endl << row->toString();
327 
328  s << std::endl << std::endl;
329 
330  return s.str();
331  }
std::vector< LpCol > _cols_
Variables of the problem.
Definition: LpInterface.h:750
std::vector< LpRow *> _rows_
Rows of the problem.
Definition: LpInterface.h:748

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 154 of file LpInterface_tpl.h.

154  {
155  out << lpi.toString();
156  return out;
157  }

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 750 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 754 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 748 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 757 of file LpInterface.h.


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