aGrUM 3.1.1
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[] (std::string_view 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 (std::string_view aName, std::string_view aDesc, long minVal, long maxVal)
 constructors
 RangeVariable (std::string_view aName, std::string_view 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 (std::string_view) 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
bool isNumerical () const
virtual 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 (std::string_view theValue)
 sets the name of the variable
const std::string & name () const
 returns the name of the variable
void setDescription (std::string_view 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 ( std::string_view aName,
std::string_view 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.
long minVal() const
Returns the lower bound.
RangeVariable(std::string_view aName, std::string_view aDesc, long minVal, long maxVal)
constructors
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 ( std::string_view aName,
std::string_view 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 134 of file rangeVariable_inl.h.

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

References RangeVariable(), _maxBound_, and _minBound_.

Referenced by operator=().

Here is the call graph for this function:
Here is the caller 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 123 of file rangeVariable_inl.h.

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

References _maxBound_, and _minBound_.

Referenced by index(), and label().

Here is the caller graph for this function:

◆ clone()

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

Copy Factory.

Returns
Returns a pointer on a new copy of this.

Implements gum::DiscreteVariable.

Definition at line 60 of file rangeVariable_inl.h.

60{ 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 95 of file rangeVariable_inl.h.

95 {
96 // std::rint uses round-half-to-even (IEEE default) which is platform-dependent at .5
97 // boundaries. Use floor + explicit > 0.5 check to implement round-half-down deterministically.
98 long res = static_cast< long >(std::floor(val));
99 if (val - static_cast< double >(res) > 0.5) ++res;
100
101 if (res < _minBound_) {
102 return 0;
103 } else if (res > _maxBound_) {
104 return domainSize() - 1;
105 } else {
106 return static_cast< Idx >(res - _minBound_);
107 }
108 }
Size domainSize() const final
returns the size of the random discrete variable domain
Size Idx
Type for indexes.
Definition types.h:79

References _maxBound_, _minBound_, and domainSize().

Here is the call graph for this function:

◆ closestLabel()

virtual std::string gum::DiscreteVariable::closestLabel ( double val) const
virtualinherited

for numerical variables, returns the closest label for the value

Reimplemented in gum::IntegerVariable, and gum::NumericalDiscreteVariable.

◆ 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{ return std::format("[{},{}]", minVal(), maxVal()); }

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 63 of file rangeVariable_inl.h.

63 {
64 return (_maxBound_ < _minBound_) ? Size(0) : Size(_maxBound_ + 1 - _minBound_);
65 }
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 ( std::string_view label) const
finalvirtual
Returns
the modality index from the label
Exceptions
NotFound

Implements gum::DiscreteVariable.

Definition at line 84 of file rangeVariable_inl.h.

84 {
85 std::istringstream i(std::string{label});
86 long target;
87
88 if (!(i >> target)) { GUM_ERROR(NotFound, "Bad label : " << label << " for " << *this) }
89
90 if (!belongs(target)) { GUM_ERROR(NotFound, "Bad label : " << label << " for " << *this) }
91
92 return static_cast< Idx >(target - _minBound_);
93 }
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:76

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

Here is the call graph for this function:

◆ isEmpirical()

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

Reimplemented in gum::IDiscretizedVariable.

◆ isNumerical()

bool gum::DiscreteVariable::isNumerical ( ) const
inherited
Returns
true if the variable has a meaningful numerical representation (i.e. not Labelized)

◆ 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 70 of file rangeVariable_inl.h.

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

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 117 of file rangeVariable_inl.h.

117{ 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 111 of file rangeVariable_inl.h.

111{ 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::NumericalDiscreteVariable::NumericalDiscreteVariable(), gum::MultiDimImplementation< double >::~MultiDimImplementation(), gum::BNdistance< GUM_SCALAR >::_checkCompatibility_(), gum::ASTposteriorProba< GUM_SCALAR >::_compute_knw_from_bn(), gum::prm::PRMFactory< GUM_SCALAR >::_retrieveCommonType_(), gum::prm::PRMFactory< GUM_SCALAR >::_retrieveInputs_(), gum::BayesNetFactory< GUM_SCALAR >::_setCPTAndParents_(), gum::Instantiation::add(), gum::MultiDimImplementation< GUM_ELEMENT >::add(), gum::Instantiation::chgVal(), gum::GibbsBNdistance< GUM_SCALAR >::computeKL_(), gum::InfluenceDiagram< GUM_SCALAR >::copyStructureAndTables_(), gum::FMDP< double >::cost(), gum::prm::PRMFactory< GUM_SCALAR >::endDiscreteType(), gum::prm::PRMFactory< GUM_SCALAR >::endDiscretizedType(), gum::BayesNetFactory< GUM_SCALAR >::endVariableDeclaration(), gum::FMDPFactory< GUM_ELEMENT >::endVariableDeclaration(), gum::BayesNetFragment< GUM_SCALAR >::nodeId(), gum::IBayesNet< GUM_SCALAR >::operator==(), 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_ELEMENT, TerminalNodePolicy >::toDot(), gum::RangeVariable::toFast(), gum::Estimator< GUM_SCALAR >::update(), and gum::MultiDimFunctionGraph< GUM_ELEMENT, 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 80 of file rangeVariable_inl.h.

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

References _minBound_.

◆ operator=()

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

Copy operator.

Parameters
aRVto be copied
Returns
a ref to *this

References RangeVariable(), _checkSameDomain_(), and stype().

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[] ( std::string_view label) const
inherited

from the label to its index in var.

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

References label().

Here is the call graph for this function:

◆ setDescription()

void gum::Variable::setDescription ( std::string_view 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 120 of file rangeVariable_inl.h.

120{ _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 114 of file rangeVariable_inl.h.

114{ _minBound_ = minVal; }

References _minBound_, and minVal().

Here is the call graph for this function:

◆ setName()

void gum::Variable::setName ( std::string_view theValue)
inherited

sets the name of the variable

Parameters
theValue

◆ stype()

INLINE std::string gum::RangeVariable::stype ( ) const
finalvirtual

string represent the type of the variable

Implements gum::DiscreteVariable.

Definition at line 142 of file rangeVariable_inl.h.

142{ return "Range"; }

Referenced by operator=().

Here is the caller graph for this function:

◆ 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 93 of file rangeVariable.cpp.

93 {
94 if (const auto m = minVal(); m != 0) {
95 return std::format("{}[{},{}]", name(), m, maxVal());
96 } else {
97 return std::format("{}[{}]", name(), maxVal() + 1);
98 }
99 }
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 132 of file rangeVariable_inl.h.

132{ 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 167 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 162 of file variable.h.


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