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

Public Member Functions

std::string toString () const
 Get the string representation of a calling row.
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.
 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.
 LpRow (const LpRow &row)
 Copy constructor.
 LpRow (LpRow &&row)
 Move copy constructor from temporary.
 ~LpRow ()
 Default destructor.
Assignment operators
LpRow & operator= (const LpRow &row)
LpRow & operator= (LpRow &&row)

Private Attributes

double _cste_
 The constant of the linear inequality.
HashTable< LpCol, double > * _coeffs_
 The coefficients of the variables of the linear inequality.

Friends

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

Detailed Description

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

an inequality.

Author
Matthieu HOURBRACQ and Pierre-Henri WUILLEMIN(_at_LIP6)

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

519 : _coeffs_(nullptr) {
520 // we write 0 <= Ax + b from Ex + f <= Cx + d
521 if (expr._ileft_ && !expr._iright_) {
522 _coeffs_ = new HashTable< LpCol, double >(*expr._mCoeffs_);
523
524 for (const auto& col: cols) {
525 double col_coeff = 0.;
526
527 // from left side to middle side : 0 <= middle - left
528 if (expr._lCoeffs_->exists(col)) col_coeff = expr._lCoeffs_->operator[](col);
529
530 _coeffs_->getWithDefault(col, 0.) -= col_coeff;
531 }
532
533 _cste_ = expr._mValue_ - expr._lValue_;
534 } else if (expr._iright_ && !expr._ileft_) {
535 _coeffs_ = new HashTable< LpCol, double >(*expr._rCoeffs_);
536
537 for (const auto& col: cols) {
538 double col_coeff = 0;
539
540 // from middle side to right side : 0 <= right - middle
541 if (expr._mCoeffs_->exists(col)) col_coeff = expr._mCoeffs_->operator[](col);
542
543 _coeffs_->getWithDefault(col, 0.) -= col_coeff;
544 }
545
546 _cste_ = expr._rValue_ - expr._mValue_;
547 } else
548 GUM_ERROR(OperationNotAllowed,
549 "expr : " << expr.toString() << "is not a valid inequality; no <= detected");
550
551 if (_coeffs_->size() == 0)
552 GUM_ERROR(OperationNotAllowed,
553 "expr : " << expr.toString()
554 << "is not a valid inequality; "
555 "no variable in inequality, "
556 "only constants");
557
558 GUM_CONSTRUCTOR(LpRow);
559 }
HashTable(Size size_param=HashTableConst::default_size, bool resize_pol=HashTableConst::default_resize_policy, bool key_uniqueness_pol=HashTableConst::default_uniqueness_policy)
Default constructor.
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.
double _cste_
The constant of the linear inequality.
#define GUM_ERROR(type, msg)
Definition exceptions.h:76

References _coeffs_, _cste_, gum::credal::lp::LpExpr::_ileft_, gum::credal::lp::LpExpr::_iright_, gum::credal::lp::LpExpr::_lValue_, and gum::credal::lp::LpExpr::_mValue_.

Referenced by LpRow(), LpRow(), LpRow(), ~LpRow(), gum::credal::lp::LpInterface< GUM_SCALAR >::addRow(), gum::credal::lp::LpInterface< GUM_SCALAR >::addRow(), LpInterface, operator<<, gum::credal::lp::LpInterface< GUM_SCALAR >::operator=(), operator=(), and operator=().

Here is the caller 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 561 of file LpInterface.cpp.

561 : _coeffs_(nullptr) {
563 if (expr._ileft_ && !expr._iright_) {
564 swap(_coeffs_, expr._mCoeffs_);
565
566 for (const auto& col: cols) {
567 double col_coeff = 0;
568
569 if (expr._lCoeffs_->exists(col)) col_coeff = expr._lCoeffs_->operator[](col);
570
571 _coeffs_->getWithDefault(col, 0.) -= col_coeff;
572 }
573
574 _cste_ = expr._mValue_ - expr._lValue_;
575 } else if (expr._iright_ && !expr._ileft_) {
576 swap(_coeffs_, expr._rCoeffs_);
577
578 for (const auto& col: cols) {
579 double col_coeff = 0;
580
581 if (expr._mCoeffs_->exists(col)) col_coeff = expr._mCoeffs_->operator[](col);
582
583 _coeffs_->getWithDefault(col, 0.) -= col_coeff;
584 }
585
586 _cste_ = expr._rValue_ - expr._mValue_;
587 } else
588 GUM_ERROR(OperationNotAllowed,
589 "expr : " << expr.toString() << "is not a valid inequality; no <= detected");
590
591 if (_coeffs_->size() == 0)
592 GUM_ERROR(OperationNotAllowed,
593 "expr : " << expr.toString()
594 << "is not a valid inequality; "
595 "no variable in inequality, "
596 "only constants");
597
598 GUM_CONSTRUCTOR(LpRow);
599 }
void swap(HashTable< LpCol, double > *&a, HashTable< LpCol, double > *&b)
Swap the addresses of two pointers to hashTables.

References LpRow(), _coeffs_, and _cste_.

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

601 :
602 _cste_(row._cste_), _coeffs_(new HashTable< LpCol, double >(*row._coeffs_)) {
603 GUM_CONS_CPY(LpRow);
604 }

References LpRow(), _coeffs_, and _cste_.

Here is the call graph for this function:

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

606 : _cste_(row._cste_), _coeffs_(row._coeffs_) {
607 row._coeffs_ = nullptr;
608
609 GUM_CONS_CPY(LpRow);
610 }

References LpRow(), _coeffs_, and _cste_.

Here is the call graph for this function:

◆ ~LpRow()

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

Default destructor.

Definition at line 612 of file LpInterface.cpp.

612 {
613 delete _coeffs_;
614
615 GUM_DESTRUCTOR(LpRow);
616 }

References LpRow(), and _coeffs_.

Here is the call graph for this function:

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

618 {
619 _cste_ = row._cste_;
620 *_coeffs_ = *row._coeffs_;
621 return *this;
622 }

References LpRow(), _coeffs_, and _cste_.

Referenced by LpInterface.

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

◆ operator=() [2/2]

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

Definition at line 624 of file LpInterface.cpp.

624 {
625 _cste_ = row._cste_;
626 swap(_coeffs_, row._coeffs_);
627 return *this;
628 }

References LpRow(), _coeffs_, _cste_, and gum::credal::lp::swap().

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

635 {
636 std::string s = std::format("0 <= {}", _cste_);
637
638 if (_coeffs_ != nullptr) {
639 for (const auto& elt: *_coeffs_) {
640 if (elt.second > 0) {
641 if (elt.second != 1) {
642 s += std::format(" +{}*{}", elt.second, elt.first.toString());
643 } else {
644 s += std::format(" +{}", elt.first.toString());
645 }
646 } else {
647 if (elt.second < 0) {
648 if (elt.second != -1) {
649 s += std::format(" {}*{}", elt.second, elt.first.toString());
650 } else {
651 s += std::format(" -{}", elt.first.toString());
652 }
653 }
654 }
655 }
656 }
657
658 return s;
659 }

References _coeffs_, and _cste_.

Referenced by LpInterface, and operator<<.

Here is the caller graph for this function:

◆ LpInterface

template<GUM_Numeric GUM_SCALAR>
friend class LpInterface
friend

Definition at line 523 of file LpInterface.h.

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

Referenced by LpInterface.

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

630 {
631 out << row.toString();
632 return out;
633 }

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

Referenced by LpInterface, and operator<<.

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

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

◆ _cste_

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

The constant of the linear inequality.

Definition at line 609 of file LpInterface.h.

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


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