aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
gum::credal::lp::LpRow Class Reference

Class representing a row of the linear program, i.e. More...

#include <agrum/CN/LpInterface.h>

+ Collaboration diagram for gum::credal::lp::LpRow:

Public Member Functions

std::string toString () const
 Get the string representation of a calling row. More...
 
Constructors / Destructors
 LpRow (const LpExpr &expr, const std::vector< LpCol > &cols)
 Constructor from an expression and the address of the vector of variables of the problem. More...
 
 LpRow (LpExpr &&expr, const std::vector< LpCol > &cols)
 Move constructor from a temporary expression and the address of the vector of variables of the problem. More...
 
 LpRow (const LpRow &row)
 Copy constructor. More...
 
 LpRow (LpRow &&row)
 Move copy constructor from temporary. More...
 
 ~LpRow ()
 Default destructor. More...
 
Assignment operators
LpRowoperator= (const LpRow &row)
 
LpRowoperator= (LpRow &&row)
 

Friends

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

Detailed Description

Class representing a row of the linear program, i.e.

an inequality.

Author
Matthieu HOURBRACQ and Pierre-Henri WUILLEMIN()

Definition at line 499 of file LpInterface.h.

Constructor & Destructor Documentation

◆ LpRow() [1/4]

gum::credal::lp::LpRow::LpRow ( const LpExpr expr,
const std::vector< LpCol > &  cols 
)

Constructor from an expression and the address of the vector of variables of the problem.

class LpRow

Parameters
exprthe constant reference to the expression to convert into rows ( inequalities ).
colsthe constant reference to the vector of variables of the problem.

Definition at line 513 of file LpInterface.cpp.

513  : _coeffs_(nullptr) {
514  // we write 0 <= Ax + b from Ex + f <= Cx + d
515  if (expr._ileft_ && !expr._iright_) {
516  _coeffs_ = new HashTable< LpCol, double >(*expr._mCoeffs_);
517 
518  for (const auto& col: cols) {
519  double col_coeff = 0.;
520 
521  // from left side to middle side : 0 <= middle - left
522  if (expr._lCoeffs_->exists(col)) col_coeff = expr._lCoeffs_->operator[](col);
523 
524  _coeffs_->getWithDefault(col, 0.) -= col_coeff;
525  }
526 
527  _cste_ = expr._mValue_ - expr._lValue_;
528  } else if (expr._iright_ && !expr._ileft_) {
529  _coeffs_ = new HashTable< LpCol, double >(*expr._rCoeffs_);
530 
531  for (const auto& col: cols) {
532  double col_coeff = 0;
533 
534  // from middle side to right side : 0 <= right - middle
535  if (expr._mCoeffs_->exists(col)) col_coeff = expr._mCoeffs_->operator[](col);
536 
537  _coeffs_->getWithDefault(col, 0.) -= col_coeff;
538  }
539 
540  _cste_ = expr._rValue_ - expr._mValue_;
541  } else
542  GUM_ERROR(OperationNotAllowed,
543  "expr : " << expr.toString() << "is not a valid inequality; no <= detected");
544 
545  if (_coeffs_->size() == 0)
546  GUM_ERROR(OperationNotAllowed,
547  "expr : " << expr.toString()
548  << "is not a valid inequality; "
549  "no variable in inequality, "
550  "only constants");
551 
552  GUM_CONSTRUCTOR(LpRow);
553  }
double _cste_
The constant of the linear inequality.
Definition: LpInterface.h:586
LpRow(const LpExpr &expr, const std::vector< LpCol > &cols)
Constructor from an expression and the address of the vector of variables of the problem.
HashTable< LpCol, double > * _coeffs_
The coefficients of the variables of the linear inequality.
Definition: LpInterface.h:590
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51

◆ LpRow() [2/4]

gum::credal::lp::LpRow::LpRow ( LpExpr &&  expr,
const std::vector< LpCol > &  cols 
)

Move constructor from a temporary expression and the address of the vector of variables of the problem.

Parameters
exprthe temporary expression to move into rows ( inequalities ).
colsthe constant reference to the vector of variables of the problem.

Definition at line 555 of file LpInterface.cpp.

555  : _coeffs_(nullptr) {
557  if (expr._ileft_ && !expr._iright_) {
558  swap(_coeffs_, expr._mCoeffs_);
559 
560  for (const auto& col: cols) {
561  double col_coeff = 0;
562 
563  if (expr._lCoeffs_->exists(col)) col_coeff = expr._lCoeffs_->operator[](col);
564 
565  _coeffs_->getWithDefault(col, 0.) -= col_coeff;
566  }
567 
568  _cste_ = expr._mValue_ - expr._lValue_;
569  } else if (expr._iright_ && !expr._ileft_) {
570  swap(_coeffs_, expr._rCoeffs_);
571 
572  for (const auto& col: cols) {
573  double col_coeff = 0;
574 
575  if (expr._mCoeffs_->exists(col)) col_coeff = expr._mCoeffs_->operator[](col);
576 
577  _coeffs_->getWithDefault(col, 0.) -= col_coeff;
578  }
579 
580  _cste_ = expr._rValue_ - expr._mValue_;
581  } else
582  GUM_ERROR(OperationNotAllowed,
583  "expr : " << expr.toString() << "is not a valid inequality; no <= detected");
584 
585  if (_coeffs_->size() == 0)
586  GUM_ERROR(OperationNotAllowed,
587  "expr : " << expr.toString()
588  << "is not a valid inequality; "
589  "no variable in inequality, "
590  "only constants");
591 
592  GUM_CONSTRUCTOR(LpRow);
593  }
void swap(HashTable< LpCol, double > *&a, HashTable< LpCol, double > *&b)
Swap the addresses of two pointers to hashTables.
double _cste_
The constant of the linear inequality.
Definition: LpInterface.h:586
LpRow(const LpExpr &expr, const std::vector< LpCol > &cols)
Constructor from an expression and the address of the vector of variables of the problem.
HashTable< LpCol, double > * _coeffs_
The coefficients of the variables of the linear inequality.
Definition: LpInterface.h:590
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51

◆ LpRow() [3/4]

gum::credal::lp::LpRow::LpRow ( const LpRow row)

Copy constructor.

Parameters
rowThe constant reference to the row to be copied.

Definition at line 595 of file LpInterface.cpp.

595  :
596  _cste_(row._cste_), _coeffs_(new HashTable< LpCol, double >(*row._coeffs_)) {
597  GUM_CONS_CPY(LpRow);
598  }
double _cste_
The constant of the linear inequality.
Definition: LpInterface.h:586
LpRow(const LpExpr &expr, const std::vector< LpCol > &cols)
Constructor from an expression and the address of the vector of variables of the problem.
HashTable< LpCol, double > * _coeffs_
The coefficients of the variables of the linear inequality.
Definition: LpInterface.h:590

◆ LpRow() [4/4]

gum::credal::lp::LpRow::LpRow ( LpRow &&  row)

Move copy constructor from temporary.

Parameters
rowThe temporary row to be copied.

Definition at line 600 of file LpInterface.cpp.

600  : _cste_(row._cste_), _coeffs_(row._coeffs_) {
601  row._coeffs_ = nullptr;
602 
603  GUM_CONS_CPY(LpRow);
604  }
double _cste_
The constant of the linear inequality.
Definition: LpInterface.h:586
LpRow(const LpExpr &expr, const std::vector< LpCol > &cols)
Constructor from an expression and the address of the vector of variables of the problem.
HashTable< LpCol, double > * _coeffs_
The coefficients of the variables of the linear inequality.
Definition: LpInterface.h:590

◆ ~LpRow()

gum::credal::lp::LpRow::~LpRow ( )

Default destructor.

Definition at line 606 of file LpInterface.cpp.

606  {
607  delete _coeffs_;
608 
609  GUM_DESTRUCTOR(LpRow);
610  }
LpRow(const LpExpr &expr, const std::vector< LpCol > &cols)
Constructor from an expression and the address of the vector of variables of the problem.
HashTable< LpCol, double > * _coeffs_
The coefficients of the variables of the linear inequality.
Definition: LpInterface.h:590

Member Function Documentation

◆ operator=() [1/2]

LpRow & gum::credal::lp::LpRow::operator= ( const LpRow row)
Parameters
rowThe constant reference to the row to be copied.

Definition at line 612 of file LpInterface.cpp.

612  {
613  _cste_ = row._cste_;
614  *_coeffs_ = *row._coeffs_;
615  return *this;
616  }
double _cste_
The constant of the linear inequality.
Definition: LpInterface.h:586
HashTable< LpCol, double > * _coeffs_
The coefficients of the variables of the linear inequality.
Definition: LpInterface.h:590

◆ operator=() [2/2]

LpRow & gum::credal::lp::LpRow::operator= ( LpRow &&  row)
Parameters
rowThe temporary row to be moved to this.

Definition at line 618 of file LpInterface.cpp.

618  {
619  _cste_ = row._cste_;
620  swap(_coeffs_, row._coeffs_);
621  return *this;
622  }
void swap(HashTable< LpCol, double > *&a, HashTable< LpCol, double > *&b)
Swap the addresses of two pointers to hashTables.
double _cste_
The constant of the linear inequality.
Definition: LpInterface.h:586
HashTable< LpCol, double > * _coeffs_
The coefficients of the variables of the linear inequality.
Definition: LpInterface.h:590

◆ toString()

std::string gum::credal::lp::LpRow::toString ( ) const

Get the string representation of a calling row.

Returns
The string representation of the calling row.

Definition at line 629 of file LpInterface.cpp.

629  {
630  std::ostringstream s;
631 
632  s << "0 <= " << _cste_;
633 
634  if (_coeffs_ != nullptr) {
635  for (const auto& elt: *_coeffs_) {
636  if (elt.second > 0) {
637  if (elt.second != 1) {
638  s << " +" << elt.second << "*" << elt.first.toString();
639  } else {
640  s << " +" << elt.first.toString();
641  }
642  } else {
643  if (elt.second < 0) {
644  if (elt.second != -1) {
645  s << " " << elt.second << "*" << elt.first.toString();
646  } else {
647  s << " -" << elt.first.toString();
648  }
649  }
650  }
651  }
652  }
653 
654  return s.str();
655  }
double _cste_
The constant of the linear inequality.
Definition: LpInterface.h:586
HashTable< LpCol, double > * _coeffs_
The coefficients of the variables of the linear inequality.
Definition: LpInterface.h:590

Friends And Related Function Documentation

◆ LpInterface

template<typename GUM_SCALAR >
friend class LpInterface
friend

Definition at line 501 of file LpInterface.h.

◆ operator<<

std::ostream& operator<< ( std::ostream &  out,
const LpRow row 
)
friend

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

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

Definition at line 624 of file LpInterface.cpp.

624  {
625  out << row.toString();
626  return out;
627  }

Member Data Documentation

◆ _coeffs_

HashTable< LpCol, double >* gum::credal::lp::LpRow::_coeffs_
private

The coefficients of the variables of the linear inequality.

Definition at line 590 of file LpInterface.h.

◆ _cste_

double gum::credal::lp::LpRow::_cste_
private

The constant of the linear inequality.

Definition at line 586 of file LpInterface.h.


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