aGrUM 2.3.2
a C++ library for (probabilistic) graphical models
gum::RangeVariable Class Referencefinal

Defines a discrete random variable over an integer interval. More...

#include <rangeVariable.h>

Inheritance diagram for gum::RangeVariable:
Collaboration diagram for gum::RangeVariable:

Public Member Functions

std::string domain () const final
 string represent the domain of the variable
std::string stype () const final
 string represent the type of the variable
Idx operator[] (const std::string &label) const
 from the label to its index in var.
std::string toString () const
 string version of *this
std::string toStringWithDescription () const
 string version of *this using description attribute instead of name.
Constructors / Destructors
 RangeVariable (const std::string &aName, const std::string &aDesc, long minVal, long maxVal)
 constructors
 RangeVariable (const std::string &aName, const std::string &aDesc)
 by de default min=0, max=1
 RangeVariable (const RangeVariable &aDRV)
 Copy Constructor.
 ~RangeVariable () final
 destructor
RangeVariableclone () const final
 Copy Factory.
Accessors / Modifiers
Size domainSize () const final
 returns the size of the random discrete variable domain
VarType varType () const final
 returns the type of variable
std::string toFast () const final
 returns the size of the random discrete variable domain
std::string label (Idx index) const final
 Get the index-th label.
double numerical (Idx index) const final
 get a numerical representation of the index-the value.
long minVal () const
 Returns the lower bound.
void setMinVal (long minVal)
 Set a new value for the lower bound.
long maxVal () const
 Returns the upper bound.
void setMaxVal (long maxVal)
 Set a new value of the upper bound.
bool belongs (long val) const
 Returns true if the param belongs to the variable's interval.
Idx index (const std::string &) const final
Idx closestIndex (double val) const final
 returns the closest index of the value
Operators
RangeVariableoperator= (const RangeVariable &aRV)
 Copy operator.
Accessors / Modifiers
bool empty () const
std::vector< std::string > labels () const
 vector of labels
virtual bool isEmpirical () const
std::string closestLabel (double val) const
 for numerical variables, returns the closest label for the value
Operators
bool operator== (const Variable &aRV) const
 equality operator
Accessors / Modifiers
void setName (const std::string &theValue)
 sets the name of the variable
const std::string & name () const
 returns the name of the variable
void setDescription (const std::string &theValue) const
 sets the description of the variable
const std::string & description () const
 returns the description of the variable

Protected Member Functions

void copy_ (const Variable &aRV)
 protected copy

Private Member Functions

bool _checkSameDomain_ (const Variable &aRV) const final
 check the domain

Private Attributes

std::string _name_
 the name of the variable
std::string _description_
 the description of the variable since description is not a characteristic of a variable, we allow the description to be changed even in a const reference.
Private Members.

The lower bound.

long _minBound_
 The upper bound.
long _maxBound_
 The upper bound.

Detailed Description

Defines a discrete random variable over an integer interval.

It is technically possible to create RangeVariable with minVal > maxVal (or modify in that way an already created RangeVariable). The result is an empty variable (i.e. empty() returns true). If maxVal - minVal < 0, then domainsize() = 0.

Definition at line 72 of file rangeVariable.h.

Constructor & Destructor Documentation

◆ RangeVariable() [1/3]

gum::RangeVariable::RangeVariable ( const std::string & aName,
const std::string & aDesc,
long minVal,
long maxVal )

constructors

Definition at line 60 of file rangeVariable.cpp.

63 :
65 GUM_CONSTRUCTOR(RangeVariable);
66 }
DiscreteVariable()
(protected) Default constructor
long maxVal() const
Returns the upper bound.
long _minBound_
The upper bound.
RangeVariable(const std::string &aName, const std::string &aDesc, long minVal, long maxVal)
constructors
long minVal() const
Returns the lower bound.
long _maxBound_
The upper bound.

References gum::DiscreteVariable::DiscreteVariable(), RangeVariable(), _maxBound_, _minBound_, maxVal(), and minVal().

Referenced by RangeVariable(), RangeVariable(), RangeVariable(), ~RangeVariable(), _checkSameDomain_(), clone(), and operator=().

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

◆ RangeVariable() [2/3]

gum::RangeVariable::RangeVariable ( const std::string & aName,
const std::string & aDesc )

by de default min=0, max=1

Definition at line 68 of file rangeVariable.cpp.

68 :
69 DiscreteVariable(aName, aDesc), _minBound_(0), _maxBound_(1) {
70 GUM_CONSTRUCTOR(RangeVariable);
71 }

References gum::DiscreteVariable::DiscreteVariable(), RangeVariable(), _maxBound_, and _minBound_.

Here is the call graph for this function:

◆ RangeVariable() [3/3]

gum::RangeVariable::RangeVariable ( const RangeVariable & aDRV)

Copy Constructor.

If aDRV haves any listener, it will not be copied.

Parameters
aDRVthe variable we copy

Definition at line 78 of file rangeVariable.cpp.

78 :
79 DiscreteVariable(aDRV), _minBound_(aDRV._minBound_), _maxBound_(aDRV._maxBound_) {
80 GUM_CONS_CPY(RangeVariable);
81 }

References gum::DiscreteVariable::DiscreteVariable(), RangeVariable(), _maxBound_, and _minBound_.

Here is the call graph for this function:

◆ ~RangeVariable()

gum::RangeVariable::~RangeVariable ( )
final

destructor

Definition at line 86 of file rangeVariable.cpp.

86 {
87 GUM_DESTRUCTOR(RangeVariable);
88 ;
89 }

References RangeVariable().

Here is the call graph for this function:

Member Function Documentation

◆ _checkSameDomain_()

INLINE bool gum::RangeVariable::_checkSameDomain_ ( const Variable & aRV) const
finalprivatevirtual

check the domain

this function use the assumption that the concrete type of the variable is the same as *this

Implements gum::Variable.

Definition at line 137 of file rangeVariable_inl.h.

137 {
138 // we can assume that aRV is a RangeVariable
139 const auto& cv = static_cast< const RangeVariable& >(aRV);
140 if (_minBound_ != cv._minBound_) return false;
141 if (_maxBound_ != cv._maxBound_) return false;
142 return true;
143 }

References RangeVariable(), _maxBound_, and _minBound_.

Here is the call graph for this function:

◆ belongs()

INLINE bool gum::RangeVariable::belongs ( long val) const

Returns true if the param belongs to the variable's interval.

Definition at line 122 of file rangeVariable_inl.h.

122 {
123 return ((_minBound_ <= val) && (val <= _maxBound_));
124 }

References _maxBound_, and _minBound_.

Referenced by index(), and label().

Here is the caller graph for this function:

◆ clone()

INLINE RangeVariable * gum::RangeVariable::clone ( ) const
finalvirtual

Copy Factory.

Returns
Returns a pointer on a new copy of this.

Implements gum::DiscreteVariable.

Definition at line 59 of file rangeVariable_inl.h.

59{ return new RangeVariable(*this); }

References RangeVariable().

Here is the call graph for this function:

◆ closestIndex()

INLINE Idx gum::RangeVariable::closestIndex ( double val) const
finalvirtual

returns the closest index of the value

Implements gum::DiscreteVariable.

Definition at line 96 of file rangeVariable_inl.h.

96 {
97 int res = static_cast< int >(std::rint(val));
98 if (res - val == 0.5) res--;
99
100 if (res < _minBound_) {
101 return 0;
102 } else if (res > _maxBound_) {
103 return domainSize() - 1;
104 } else {
105 return res - _minBound_;
106 }
107 }
Size domainSize() const final
returns the size of the random discrete variable domain

References _maxBound_, _minBound_, and domainSize().

Here is the call graph for this function:

◆ closestLabel()

std::string gum::DiscreteVariable::closestLabel ( double val) const
inherited

for numerical variables, returns the closest label for the value

◆ copy_()

void gum::Variable::copy_ ( const Variable & aRV)
protectedinherited

protected copy

Parameters
aRVto be copied

References Variable().

Here is the call graph for this function:

◆ description()

const std::string & gum::Variable::description ( ) const
inherited

returns the description of the variable

◆ domain()

std::string gum::RangeVariable::domain ( ) const
finalvirtual

string represent the domain of the variable

Implements gum::DiscreteVariable.

Definition at line 91 of file rangeVariable.cpp.

91 {
92 std::stringstream s;
93 s << "[" << minVal() << "," << maxVal() << "]";
94 return s.str();
95 }

References maxVal(), and minVal().

Here is the call graph for this function:

◆ domainSize()

INLINE Size gum::RangeVariable::domainSize ( ) const
finalvirtual

returns the size of the random discrete variable domain

Warning
: It is technically possible to create RangeVariable with minVal

maxVal (or modify in that way an already created RangeVariable). The result is an empty variable (i.e. empty() returns true). If maxVal - minVal < 0, then domainsize() = 0.

Implements gum::DiscreteVariable.

Definition at line 62 of file rangeVariable_inl.h.

62 {
63 return (_maxBound_ < _minBound_) ? Size(0) : Size(_maxBound_ + 1 - _minBound_);
64 }
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition types.h:74

References _maxBound_, and _minBound_.

Referenced by closestIndex().

Here is the caller graph for this function:

◆ empty()

bool gum::DiscreteVariable::empty ( ) const
inherited
Returns
true if the domainSize() < 2;

◆ index()

INLINE Idx gum::RangeVariable::index ( const std::string & label) const
finalvirtual
Returns
the modality index from the label
Exceptions
NotFound

Implements gum::DiscreteVariable.

Definition at line 85 of file rangeVariable_inl.h.

85 {
86 std::istringstream i(label);
87 long target;
88
89 if (!(i >> target)) { GUM_ERROR(NotFound, "Bad label : " << label << " for " << *this) }
90
91 if (!belongs(target)) { GUM_ERROR(NotFound, "Bad label : " << label << " for " << *this) }
92
93 return static_cast< Idx >(target - _minBound_);
94 }
std::string label(Idx index) const final
Get the index-th label.
bool belongs(long val) const
Returns true if the param belongs to the variable's interval.
#define GUM_ERROR(type, msg)
Definition exceptions.h:72
Size Idx
Type for indexes.
Definition types.h:79

References _minBound_, belongs(), GUM_ERROR, and label().

Here is the call graph for this function:

◆ isEmpirical()

virtual bool gum::DiscreteVariable::isEmpirical ( ) const
inlinevirtualinherited
Returns
true if the domainSize() < 2;

Reimplemented in gum::IDiscretizedVariable.

Definition at line 124 of file discreteVariable.h.

124{ return false; };

◆ label()

INLINE std::string gum::RangeVariable::label ( Idx index) const
finalvirtual

Get the index-th label.

Parameters
indexthe index of the label we wish to return
Exceptions
OutOfBound

Implements gum::DiscreteVariable.

Definition at line 69 of file rangeVariable_inl.h.

69 {
70 long target = static_cast< long >(indice) + _minBound_;
71 if (belongs(target)) {
72 std::stringstream strBuff;
73 strBuff << target;
74 return strBuff.str();
75 } else {
76 GUM_ERROR(OutOfBounds, "Indice out of bounds.")
77 }
78 }

References _minBound_, belongs(), and GUM_ERROR.

Referenced by index().

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

◆ labels()

std::vector< std::string > gum::DiscreteVariable::labels ( ) const
inherited

vector of labels

Referenced by gum::LabelizedVariable::LabelizedVariable().

Here is the caller graph for this function:

◆ maxVal()

INLINE long gum::RangeVariable::maxVal ( ) const

Returns the upper bound.

Definition at line 116 of file rangeVariable_inl.h.

116{ return _maxBound_; }

References _maxBound_.

Referenced by RangeVariable(), gum::AggregatorDecomposition< GUM_SCALAR >::addDepthLayer_(), gum::AggregatorDecomposition< GUM_SCALAR >::decomposeAggregator_(), domain(), setMaxVal(), and toFast().

Here is the caller graph for this function:

◆ minVal()

INLINE long gum::RangeVariable::minVal ( ) const

Returns the lower bound.

Definition at line 110 of file rangeVariable_inl.h.

110{ return _minBound_; }

References _minBound_.

Referenced by RangeVariable(), gum::AggregatorDecomposition< GUM_SCALAR >::addDepthLayer_(), gum::AggregatorDecomposition< GUM_SCALAR >::decomposeAggregator_(), domain(), setMinVal(), and toFast().

Here is the caller graph for this function:

◆ name()

const std::string & gum::Variable::name ( ) const
inherited

returns the name of the variable

Referenced by gum::learning::IBNLearner::Database::Database(), gum::Estimator< GUM_SCALAR >::Estimator(), gum::MultiDimImplementation< double >::MultiDimImplementation(), gum::NumericalDiscreteVariable::NumericalDiscreteVariable(), gum::BNdistance< GUM_SCALAR >::_checkCompatibility_(), gum::BayesNet< double >::_copyTensors_(), gum::prm::PRMFactory< GUM_SCALAR >::_retrieveCommonType_(), gum::prm::PRMFactory< GUM_SCALAR >::_retrieveInputs_(), gum::BayesNetFactory< GUM_SCALAR >::_setCPTAndParents_(), gum::Instantiation::add(), gum::FMDP< double >::addCostForAction(), gum::FMDP< GUM_SCALAR >::addVariable(), gum::MultiDimICIModel< GUM_SCALAR >::causalWeight(), gum::Instantiation::chgVal(), gum::GibbsBNdistance< GUM_SCALAR >::computeKL_(), gum::InfluenceDiagram< GUM_SCALAR >::copyStructureAndTables_(), gum::prm::PRMFactory< GUM_SCALAR >::endDiscreteType(), gum::prm::PRMFactory< GUM_SCALAR >::endDiscretizedType(), gum::BayesNetFactory< GUM_SCALAR >::endVariableDeclaration(), gum::FMDPFactory< GUM_SCALAR >::endVariableDeclaration(), gum::Tensor< GUM_SCALAR >::fillWith(), gum::BayesNetFragment< GUM_SCALAR >::nodeId(), gum::Estimator< GUM_SCALAR >::posterior(), gum::Estimator< GUM_SCALAR >::setFromBN(), gum::Instantiation::setValsFrom(), gum::BayesNetFactory< GUM_SCALAR >::setVariable(), gum::BayesNetFactory< GUM_SCALAR >::setVariableValuesUnchecked(), gum::MultiDimFunctionGraph< GUM_SCALAR, TerminalNodePolicy >::toDot(), gum::RangeVariable::toFast(), gum::Estimator< GUM_SCALAR >::update(), and gum::MultiDimFunctionGraph< GUM_SCALAR, TerminalNodePolicy >::varNodeListe().

◆ numerical()

INLINE double gum::RangeVariable::numerical ( Idx index) const
finalvirtual

get a numerical representation of the index-the value.

Implements gum::DiscreteVariable.

Definition at line 81 of file rangeVariable_inl.h.

81 {
82 return double(_minBound_ + static_cast< long >(indice));
83 }

References _minBound_.

◆ operator=()

INLINE RangeVariable & gum::RangeVariable::operator= ( const RangeVariable & aRV)

Copy operator.

Parameters
aRVto be copied
Returns
a ref to *this

Definition at line 129 of file rangeVariable_inl.h.

129 {
130 _minBound_ = aRV._minBound_;
131 _maxBound_ = aRV._maxBound_;
132 return *this;
133 }

References RangeVariable(), _maxBound_, and _minBound_.

Here is the call graph for this function:

◆ operator==()

bool gum::Variable::operator== ( const Variable & aRV) const
inherited

equality operator

References Variable().

Here is the call graph for this function:

◆ operator[]()

Idx gum::DiscreteVariable::operator[] ( const std::string & label) const
inlineinherited

from the label to its index in var.

Warning
This operation may have different complexity in different subclasses.
Exceptions
NotFound

Definition at line 156 of file discreteVariable.h.

156{ return index(label); };
virtual Idx index(const std::string &label) const =0
virtual std::string label(Idx i) const =0
get the indice-th label. This method is pure virtual.

References index(), and label().

Here is the call graph for this function:

◆ setDescription()

void gum::Variable::setDescription ( const std::string & theValue) const
inherited

sets the description of the variable

Warning
since description is mutable, setDescription() is const
Parameters
theValue

◆ setMaxVal()

INLINE void gum::RangeVariable::setMaxVal ( long maxVal)

Set a new value of the upper bound.

Definition at line 119 of file rangeVariable_inl.h.

119{ _maxBound_ = maxVal; }

References _maxBound_, and maxVal().

Here is the call graph for this function:

◆ setMinVal()

INLINE void gum::RangeVariable::setMinVal ( long minVal)

Set a new value for the lower bound.

Definition at line 113 of file rangeVariable_inl.h.

113{ _minBound_ = minVal; }

References _minBound_, and minVal().

Here is the call graph for this function:

◆ setName()

void gum::Variable::setName ( const std::string & theValue)
inherited

sets the name of the variable

Parameters
theValue

◆ stype()

std::string gum::RangeVariable::stype ( ) const
inlinefinalvirtual

string represent the type of the variable

Implements gum::DiscreteVariable.

Definition at line 177 of file rangeVariable.h.

177{ return "Range"; };

◆ toFast()

std::string gum::RangeVariable::toFast ( ) const
finalvirtual

returns the size of the random discrete variable domain

Warning
: It is technically possible to create RangeVariable with minVal

maxVal (or modify in that way an already created RangeVariable). The result is an empty variable (i.e. empty() returns true). If maxVal - minVal < 0, then domainsize() = 0.

Implements gum::DiscreteVariable.

Definition at line 97 of file rangeVariable.cpp.

97 {
98 std::stringstream s;
99 s << name();
100 if (const auto m = minVal(); m != 0) {
101 s << "[" << m << "," << maxVal() << "]";
102 } else {
103 s << "[" << maxVal() + 1 << "]";
104 }
105 return s.str();
106 }
const std::string & name() const
returns the name of the variable

References maxVal(), minVal(), and gum::Variable::name().

Here is the call graph for this function:

◆ toString()

std::string gum::DiscreteVariable::toString ( ) const
inherited

string version of *this

Referenced by gum::IntegerVariable::IntegerVariable(), and gum::BNdistance< GUM_SCALAR >::_checkCompatibility_().

Here is the caller graph for this function:

◆ toStringWithDescription()

std::string gum::DiscreteVariable::toStringWithDescription ( ) const
inherited

string version of *this using description attribute instead of name.

◆ varType()

INLINE VarType gum::RangeVariable::varType ( ) const
finalvirtual

returns the type of variable

Implements gum::DiscreteVariable.

Definition at line 135 of file rangeVariable_inl.h.

135{ return VarType::RANGE; }

References gum::RANGE.

Member Data Documentation

◆ _description_

std::string gum::Variable::_description_
mutableprivateinherited

the description of the variable since description is not a characteristic of a variable, we allow the description to be changed even in a const reference.

Definition at line 165 of file variable.h.

◆ _maxBound_

long gum::RangeVariable::_maxBound_
private

◆ _minBound_

long gum::RangeVariable::_minBound_
private

◆ _name_

std::string gum::Variable::_name_
privateinherited

the name of the variable

Definition at line 160 of file variable.h.


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