aGrUM  0.20.2
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 500 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 521 of file LpInterface.cpp.

521  :
522  coeffs__(nullptr) {
523  // we write 0 <= Ax + b from Ex + f <= Cx + d
524  if (expr.ileft__ && !expr.iright__) {
525  coeffs__ = new HashTable< LpCol, double >(*expr.mCoeffs__);
526 
527  for (const auto& col: cols) {
528  double col_coeff = 0.;
529 
530  // from left side to middle side : 0 <= middle - left
531  if (expr.lCoeffs__->exists(col))
532  col_coeff = expr.lCoeffs__->operator[](col);
533 
534  coeffs__->getWithDefault(col, 0.) -= col_coeff;
535  }
536 
537  cste__ = expr.mValue__ - expr.lValue__;
538  } else if (expr.iright__ && !expr.ileft__) {
539  coeffs__ = new HashTable< LpCol, double >(*expr.rCoeffs__);
540 
541  for (const auto& col: cols) {
542  double col_coeff = 0;
543 
544  // from middle side to right side : 0 <= right - middle
545  if (expr.mCoeffs__->exists(col))
546  col_coeff = expr.mCoeffs__->operator[](col);
547 
548  coeffs__->getWithDefault(col, 0.) -= col_coeff;
549  }
550 
551  cste__ = expr.rValue__ - expr.mValue__;
552  } else
553  GUM_ERROR(OperationNotAllowed,
554  "expr : " << expr.toString()
555  << "is not a valid inequality; no <= detected");
556 
557  if (coeffs__->size() == 0)
558  GUM_ERROR(OperationNotAllowed,
559  "expr : " << expr.toString()
560  << "is not a valid inequality; "
561  "no variable in inequality, "
562  "only constants");
563 
564  GUM_CONSTRUCTOR(LpRow);
565  }
HashTable< LpCol, double > * coeffs__
The coefficients of the variables of the linear inequality.
Definition: LpInterface.h:592
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:588
#define GUM_ERROR(type, msg)
Definition: exceptions.h:54

◆ 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 567 of file LpInterface.cpp.

567  :
568  coeffs__(nullptr) {
570  if (expr.ileft__ && !expr.iright__) {
571  swap(coeffs__, expr.mCoeffs__);
572 
573  for (const auto& col: cols) {
574  double col_coeff = 0;
575 
576  if (expr.lCoeffs__->exists(col))
577  col_coeff = expr.lCoeffs__->operator[](col);
578 
579  coeffs__->getWithDefault(col, 0.) -= col_coeff;
580  }
581 
582  cste__ = expr.mValue__ - expr.lValue__;
583  } else if (expr.iright__ && !expr.ileft__) {
584  swap(coeffs__, expr.rCoeffs__);
585 
586  for (const auto& col: cols) {
587  double col_coeff = 0;
588 
589  if (expr.mCoeffs__->exists(col))
590  col_coeff = expr.mCoeffs__->operator[](col);
591 
592  coeffs__->getWithDefault(col, 0.) -= col_coeff;
593  }
594 
595  cste__ = expr.rValue__ - expr.mValue__;
596  } else
597  GUM_ERROR(OperationNotAllowed,
598  "expr : " << expr.toString()
599  << "is not a valid inequality; no <= detected");
600 
601  if (coeffs__->size() == 0)
602  GUM_ERROR(OperationNotAllowed,
603  "expr : " << expr.toString()
604  << "is not a valid inequality; "
605  "no variable in inequality, "
606  "only constants");
607 
608  GUM_CONSTRUCTOR(LpRow);
609  }
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:592
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:588
#define GUM_ERROR(type, msg)
Definition: exceptions.h:54

◆ 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 611 of file LpInterface.cpp.

611  :
612  cste__(row.cste__),
613  coeffs__(new HashTable< LpCol, double >(*row.coeffs__)) {
614  GUM_CONS_CPY(LpRow);
615  }
HashTable< LpCol, double > * coeffs__
The coefficients of the variables of the linear inequality.
Definition: LpInterface.h:592
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:588

◆ 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 617 of file LpInterface.cpp.

617  : cste__(row.cste__), coeffs__(row.coeffs__) {
618  row.coeffs__ = nullptr;
619 
620  GUM_CONS_CPY(LpRow);
621  }
HashTable< LpCol, double > * coeffs__
The coefficients of the variables of the linear inequality.
Definition: LpInterface.h:592
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:588

◆ ~LpRow()

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

Default destructor.

Definition at line 623 of file LpInterface.cpp.

623  {
624  delete coeffs__;
625 
626  GUM_DESTRUCTOR(LpRow);
627  }
HashTable< LpCol, double > * coeffs__
The coefficients of the variables of the linear inequality.
Definition: LpInterface.h:592
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 629 of file LpInterface.cpp.

629  {
630  cste__ = row.cste__;
631  *coeffs__ = *row.coeffs__;
632  return *this;
633  }
HashTable< LpCol, double > * coeffs__
The coefficients of the variables of the linear inequality.
Definition: LpInterface.h:592
double cste__
The constant of the linear inequality.
Definition: LpInterface.h:588

◆ operator=() [2/2]

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

Definition at line 635 of file LpInterface.cpp.

635  {
636  cste__ = row.cste__;
637  swap(coeffs__, row.coeffs__);
638  return *this;
639  }
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:592
double cste__
The constant of the linear inequality.
Definition: LpInterface.h:588

◆ 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 646 of file LpInterface.cpp.

646  {
647  std::ostringstream s;
648 
649  s << "0 <= " << cste__;
650 
651  if (coeffs__ != nullptr) {
652  for (const auto& elt: *coeffs__) {
653  if (elt.second > 0) {
654  if (elt.second != 1) {
655  s << " +" << elt.second << "*" << elt.first.toString();
656  } else {
657  s << " +" << elt.first.toString();
658  }
659  } else {
660  if (elt.second < 0) {
661  if (elt.second != -1) {
662  s << " " << elt.second << "*" << elt.first.toString();
663  } else {
664  s << " -" << elt.first.toString();
665  }
666  }
667  }
668  }
669  }
670 
671  return s.str();
672  }
HashTable< LpCol, double > * coeffs__
The coefficients of the variables of the linear inequality.
Definition: LpInterface.h:592
double cste__
The constant of the linear inequality.
Definition: LpInterface.h:588

Friends And Related Function Documentation

◆ LpInterface

template<typename GUM_SCALAR >
friend class LpInterface
friend

Definition at line 502 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 641 of file LpInterface.cpp.

641  {
642  out << row.toString();
643  return out;
644  }

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

◆ cste__

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

The constant of the linear inequality.

Definition at line 588 of file LpInterface.h.


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