aGrUM  0.18.1
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 501 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 593 of file LpInterface_tpl.h.

References coeffs__, cste__, GUM_ERROR, gum::credal::lp::LpExpr::ileft__, gum::credal::lp::LpExpr::iright__, gum::credal::lp::LpExpr::lCoeffs__, gum::credal::lp::LpExpr::lValue__, gum::credal::lp::LpExpr::mCoeffs__, gum::credal::lp::LpExpr::mValue__, gum::credal::lp::LpExpr::rCoeffs__, gum::credal::lp::LpExpr::rValue__, and gum::credal::lp::LpExpr::toString().

593  :
594  coeffs__(nullptr) {
595  // we write 0 <= Ax + b from Ex + f <= Cx + d
596  if (expr.ileft__ && !expr.iright__) {
597  coeffs__ = new HashTable< LpCol, double >(*expr.mCoeffs__);
598 
599  for (const auto& col: cols) {
600  double col_coeff = 0.;
601 
602  // from left side to middle side : 0 <= middle - left
603  if (expr.lCoeffs__->exists(col))
604  col_coeff = expr.lCoeffs__->operator[](col);
605 
606  coeffs__->getWithDefault(col, 0.) -= col_coeff;
607  }
608 
609  cste__ = expr.mValue__ - expr.lValue__;
610  } else if (expr.iright__ && !expr.ileft__) {
611  coeffs__ = new HashTable< LpCol, double >(*expr.rCoeffs__);
612 
613  for (const auto& col: cols) {
614  double col_coeff = 0;
615 
616  // from middle side to right side : 0 <= right - middle
617  if (expr.mCoeffs__->exists(col))
618  col_coeff = expr.mCoeffs__->operator[](col);
619 
620  coeffs__->getWithDefault(col, 0.) -= col_coeff;
621  }
622 
623  cste__ = expr.rValue__ - expr.mValue__;
624  } else
625  GUM_ERROR(OperationNotAllowed,
626  "expr : " << expr.toString()
627  << "is not a valid inequality; no <= detected");
628 
629  if (coeffs__->size() == 0)
630  GUM_ERROR(OperationNotAllowed,
631  "expr : " << expr.toString()
632  << "is not a valid inequality; "
633  "no variable in inequality, "
634  "only constants");
635 
636  GUM_CONSTRUCTOR(LpRow);
637  }
HashTable< LpCol, double > * coeffs__
The coefficients of the variables of the linear inequality.
Definition: LpInterface.h:593
LpRow(const LpExpr &expr, const std::vector< LpCol > &cols)
Constructor from an expression and the address of the vector of variables of the problem.
double cste__
The constant of the linear inequality.
Definition: LpInterface.h:589
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55
+ Here is the call graph for this function:

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

References coeffs__, cste__, GUM_ERROR, and gum::credal::lp::swap().

639  :
640  coeffs__(nullptr) {
642  if (expr.ileft__ && !expr.iright__) {
643  swap(coeffs__, expr.mCoeffs__);
644 
645  for (const auto& col: cols) {
646  double col_coeff = 0;
647 
648  if (expr.lCoeffs__->exists(col))
649  col_coeff = expr.lCoeffs__->operator[](col);
650 
651  coeffs__->getWithDefault(col, 0.) -= col_coeff;
652  }
653 
654  cste__ = expr.mValue__ - expr.lValue__;
655  } else if (expr.iright__ && !expr.ileft__) {
656  swap(coeffs__, expr.rCoeffs__);
657 
658  for (const auto& col: cols) {
659  double col_coeff = 0;
660 
661  if (expr.mCoeffs__->exists(col))
662  col_coeff = expr.mCoeffs__->operator[](col);
663 
664  coeffs__->getWithDefault(col, 0.) -= col_coeff;
665  }
666 
667  cste__ = expr.rValue__ - expr.mValue__;
668  } else
669  GUM_ERROR(OperationNotAllowed,
670  "expr : " << expr.toString()
671  << "is not a valid inequality; no <= detected");
672 
673  if (coeffs__->size() == 0)
674  GUM_ERROR(OperationNotAllowed,
675  "expr : " << expr.toString()
676  << "is not a valid inequality; "
677  "no variable in inequality, "
678  "only constants");
679 
680  GUM_CONSTRUCTOR(LpRow);
681  }
void swap(HashTable< LpCol, double > *&a, HashTable< LpCol, double > *&b)
Swap the addresses of two pointers to hashTables.
HashTable< LpCol, double > * coeffs__
The coefficients of the variables of the linear inequality.
Definition: LpInterface.h:593
LpRow(const LpExpr &expr, const std::vector< LpCol > &cols)
Constructor from an expression and the address of the vector of variables of the problem.
double cste__
The constant of the linear inequality.
Definition: LpInterface.h:589
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55
+ Here is the call graph for this function:

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

683  :
684  cste__(row.cste__),
685  coeffs__(new HashTable< LpCol, double >(*row.coeffs__)) {
686  GUM_CONS_CPY(LpRow);
687  }
HashTable< LpCol, double > * coeffs__
The coefficients of the variables of the linear inequality.
Definition: LpInterface.h:593
LpRow(const LpExpr &expr, const std::vector< LpCol > &cols)
Constructor from an expression and the address of the vector of variables of the problem.
double cste__
The constant of the linear inequality.
Definition: LpInterface.h:589

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

689  : cste__(row.cste__), coeffs__(row.coeffs__) {
690  row.coeffs__ = nullptr;
691 
692  GUM_CONS_CPY(LpRow);
693  }
HashTable< LpCol, double > * coeffs__
The coefficients of the variables of the linear inequality.
Definition: LpInterface.h:593
LpRow(const LpExpr &expr, const std::vector< LpCol > &cols)
Constructor from an expression and the address of the vector of variables of the problem.
double cste__
The constant of the linear inequality.
Definition: LpInterface.h:589

◆ ~LpRow()

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

Default destructor.

Definition at line 695 of file LpInterface_tpl.h.

References coeffs__.

695  {
696  delete coeffs__;
697 
698  GUM_DESTRUCTOR(LpRow);
699  }
HashTable< LpCol, double > * coeffs__
The coefficients of the variables of the linear inequality.
Definition: LpInterface.h:593
LpRow(const LpExpr &expr, const std::vector< LpCol > &cols)
Constructor from an expression and the address of the vector of variables of the problem.

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

References coeffs__, and cste__.

701  {
702  cste__ = row.cste__;
703  *coeffs__ = *row.coeffs__;
704  return *this;
705  }
HashTable< LpCol, double > * coeffs__
The coefficients of the variables of the linear inequality.
Definition: LpInterface.h:593
double cste__
The constant of the linear inequality.
Definition: LpInterface.h:589

◆ operator=() [2/2]

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

Definition at line 707 of file LpInterface_tpl.h.

References coeffs__, cste__, and gum::credal::lp::swap().

707  {
708  cste__ = row.cste__;
709  swap(coeffs__, row.coeffs__);
710  return *this;
711  }
void swap(HashTable< LpCol, double > *&a, HashTable< LpCol, double > *&b)
Swap the addresses of two pointers to hashTables.
HashTable< LpCol, double > * coeffs__
The coefficients of the variables of the linear inequality.
Definition: LpInterface.h:593
double cste__
The constant of the linear inequality.
Definition: LpInterface.h:589
+ Here is the call graph for this function:

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

References coeffs__, and cste__.

Referenced by gum::credal::lp::operator<<().

718  {
719  std::ostringstream s;
720 
721  s << "0 <= " << cste__;
722 
723  if (coeffs__ != nullptr) {
724  for (const auto& elt: *coeffs__) {
725  if (elt.second > 0) {
726  if (elt.second != 1) {
727  s << " +" << elt.second << "*" << elt.first.toString();
728  } else {
729  s << " +" << elt.first.toString();
730  }
731  } else {
732  if (elt.second < 0) {
733  if (elt.second != -1) {
734  s << " " << elt.second << "*" << elt.first.toString();
735  } else {
736  s << " -" << elt.first.toString();
737  }
738  }
739  }
740  }
741  }
742 
743  return s.str();
744  }
HashTable< LpCol, double > * coeffs__
The coefficients of the variables of the linear inequality.
Definition: LpInterface.h:593
double cste__
The constant of the linear inequality.
Definition: LpInterface.h:589
+ Here is the caller graph for this function:

Friends And Related Function Documentation

◆ LpInterface

template<typename GUM_SCALAR >
friend class LpInterface
friend

Definition at line 503 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 713 of file LpInterface_tpl.h.

713  {
714  out << row.toString();
715  return out;
716  }

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 593 of file LpInterface.h.

Referenced by LpRow(), operator=(), toString(), and ~LpRow().

◆ cste__

double gum::credal::lp::LpRow::cste__
private

The constant of the linear inequality.

Definition at line 589 of file LpInterface.h.

Referenced by LpRow(), operator=(), and toString().


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