aGrUM 3.1.1
a C++ library for (probabilistic) graphical models
gum::IntegerVariable Class Referencefinal

class IntegerVariable More...

#include <integerVariable.h>

Inheritance diagram for gum::IntegerVariable:
Collaboration diagram for gum::IntegerVariable:

Public Member Functions

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
 IntegerVariable (std::string_view aName, std::string_view aDesc="")
 constructor
 IntegerVariable (std::string_view aName, std::string_view aDesc, const std::vector< int > &domain)
 constructor assigning a domain to the variable
 IntegerVariable (std::string_view aName, std::string_view aDesc, int first, int last, Size nb)
 constructor assigning a domain to the variable
 IntegerVariable (const IntegerVariable &from)
 Copy constructor.
 IntegerVariable (IntegerVariable &&from) noexcept
 move constructor
IntegerVariableclone () const override
 virtual copy constructor
 ~IntegerVariable () override
 destructor
Operators
IntegerVariableoperator= (const IntegerVariable &from)
 copy operator
IntegerVariableoperator= (IntegerVariable &&from)
 move operator
Accessors / Modifiers
Size domainSize () const override
 returns the domain size of the discrete random variable
VarType varType () const override
 returns the type of variable
std::string toFast () const override
 returns the domain size of the discrete random variable
Idx index (std::string_view label) const override
 returns the index of a given label
Idx closestIndex (double val) const override
 returns the closest index of the value
std::string label (Idx index) const override
 returns a string corresponding to the ith value of the domain
double numerical (Idx index) const override
 get an integer representation of the value at a given index
std::string domain () const override
 Returns the domain as a string.
std::string stype () const override
 string represent the type of the variable
const std::vector< int > & integerDomain () const
 returns the domain as a sequence of values
void addValue (int value)
 add a new value to the domain size
bool isValue (int value) const
 does this value exist in the domain ?
void changeValue (int old_value, int new_value)
 substitute a value by another one
void eraseValue (int value)
 erase a value from the domain of the variable
void eraseValues ()
 clear the domain of the variable
std::string closestLabel (double val) const override
 gives the value closets to val
Accessors / Modifiers
bool empty () const
std::vector< std::string > labels () const
 vector of labels
virtual bool isEmpirical () const
bool isNumerical () const
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 override
 check the domain

Private Attributes

std::vector< int > _domain_
 the domain of the variable
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.

Detailed Description

class IntegerVariable

The class representing discrete integer random variables

Definition at line 62 of file integerVariable.h.

Constructor & Destructor Documentation

◆ IntegerVariable() [1/5]

gum::IntegerVariable::IntegerVariable ( std::string_view aName,
std::string_view aDesc = "" )

constructor

Parameters
aNamethe name of the variable
aDescthe Description of the variable, if any

References domain().

Referenced by IntegerVariable(), IntegerVariable(), IntegerVariable(), IntegerVariable(), clone(), operator=(), and operator=().

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

◆ IntegerVariable() [2/5]

gum::IntegerVariable::IntegerVariable ( std::string_view aName,
std::string_view aDesc,
const std::vector< int > & domain )

constructor assigning a domain to the variable

Parameters
aNamethe name of the variable
aDescthe Description of the variable, if any
domainthe domain (set of values) of the variable

Definition at line 53 of file integerVariable.cpp.

55 :
56 DiscreteVariable(aName, aDesc) {
57 // get the values in increasing order
58 for (const auto value: domain) {
59 if (!gum::isfinite< double >(value)) {
60 GUM_ERROR(DefaultInLabel,
61 "Value '" << value << "' is not allowed for variable " << toString())
62 }
63 if (!isValue(value)) { _domain_.push_back(value); }
64 }
65 std::sort(_domain_.begin(), _domain_.end());
66
67 // for debugging purposes
68 GUM_CONSTRUCTOR(IntegerVariable)
69 }
DiscreteVariable()
(protected) Default constructor
std::string toString() const
string version of *this
std::vector< int > _domain_
the domain of the variable
bool isValue(int value) const
does this value exist in the domain ?
IntegerVariable(std::string_view aName, std::string_view aDesc="")
constructor
std::string domain() const override
Returns the domain as a string.
#define GUM_ERROR(type, msg)
Definition exceptions.h:76
bool isfinite(T arg)

References gum::DiscreteVariable::DiscreteVariable(), IntegerVariable(), _domain_, domain(), GUM_ERROR, gum::isfinite(), isValue(), and gum::DiscreteVariable::toString().

Here is the call graph for this function:

◆ IntegerVariable() [3/5]

gum::IntegerVariable::IntegerVariable ( std::string_view aName,
std::string_view aDesc,
int first,
int last,
Size nb )

constructor assigning a domain to the variable

Parameters
aNamethe name of the variable
aDescthe Description of the variable, if any
firstthe first value
lastthe last value
nbthe number of values

Definition at line 72 of file integerVariable.cpp.

76 : DiscreteVariable(aName, aDesc) {
77 // store the sorted values into a sequence
78 if (nb < 2) GUM_ERROR(ArgumentError, "The size of the domain must be >2 (here :" << nb << ").")
79 if (first >= last)
80 GUM_ERROR(ArgumentError, "first (here :" << first << " must be <last (here :" << last << ").")
81
82 double step = (last - first) / double(nb - 1);
83 if (step <= 1)
84 GUM_ERROR(ArgumentError,
85 "With nb=" << nb << ", increment is less (or equal) than 1 ! (" << step << ")")
86
87 _domain_.resize(nb);
88 _domain_.clear();
89
90 _domain_.push_back(first);
91 double current = first;
92 for (Idx i = 1; i < nb - 1; i++) {
93 current += step;
94 _domain_.push_back(int(current));
95 }
96 _domain_.push_back(last);
97
98 std::sort(_domain_.begin(), _domain_.end());
99
100 // for debugging purposes
101 GUM_CONSTRUCTOR(IntegerVariable)
102 }
Size Idx
Type for indexes.
Definition types.h:79

References gum::DiscreteVariable::DiscreteVariable(), IntegerVariable(), _domain_, and GUM_ERROR.

Here is the call graph for this function:

◆ IntegerVariable() [4/5]

gum::IntegerVariable::IntegerVariable ( const IntegerVariable & from)

Copy constructor.

Parameters
fromthe variable we copy

References IntegerVariable().

Here is the call graph for this function:

◆ IntegerVariable() [5/5]

gum::IntegerVariable::IntegerVariable ( IntegerVariable && from)
noexcept

move constructor

References IntegerVariable().

Here is the call graph for this function:

◆ ~IntegerVariable()

gum::IntegerVariable::~IntegerVariable ( )
override

destructor

Member Function Documentation

◆ _checkSameDomain_()

bool gum::IntegerVariable::_checkSameDomain_ ( const Variable & aRV) const
overrideprivatevirtual

check the domain

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

Implements gum::Variable.

◆ addValue()

void gum::IntegerVariable::addValue ( int value)

add a new value to the domain size

Exceptions
DuplicateElementis raised if the variable already contains the value

◆ changeValue()

void gum::IntegerVariable::changeValue ( int old_value,
int new_value )

substitute a value by another one

◆ clone()

IntegerVariable * gum::IntegerVariable::clone ( ) const
nodiscardoverridevirtual

virtual copy constructor

Implements gum::DiscreteVariable.

References IntegerVariable().

Here is the call graph for this function:

◆ closestIndex()

Idx gum::IntegerVariable::closestIndex ( double val) const
overridevirtual

returns the closest index of the value

Implements gum::DiscreteVariable.

◆ closestLabel()

std::string gum::IntegerVariable::closestLabel ( double val) const
overridevirtual

gives the value closets to val

Parameters
valthe desired value
Returns
the value

Reimplemented from gum::DiscreteVariable.

◆ 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::IntegerVariable::domain ( ) const
overridevirtual

Returns the domain as a string.

Implements gum::DiscreteVariable.

Definition at line 105 of file integerVariable.cpp.

105 {
106 const Size size = domainSize();
107 std::string s = "{";
108 if (size > 0) {
109 s += std::to_string(_domain_[0]);
110 for (Idx i = 1; i < size; ++i)
111 s += std::format("|{}", _domain_[i]);
112 }
113 s += "}";
114 return s;
115 }
Size domainSize() const override
returns the domain size of the discrete random variable
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition types.h:74

References _domain_, and domainSize().

Referenced by IntegerVariable(), and IntegerVariable().

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

◆ domainSize()

Size gum::IntegerVariable::domainSize ( ) const
overridevirtual

returns the domain size of the discrete random variable

Implements gum::DiscreteVariable.

Referenced by domain().

Here is the caller graph for this function:

◆ empty()

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

◆ eraseValue()

void gum::IntegerVariable::eraseValue ( int value)

erase a value from the domain of the variable

◆ eraseValues()

void gum::IntegerVariable::eraseValues ( )

clear the domain of the variable

◆ index()

Idx gum::IntegerVariable::index ( std::string_view label) const
overridevirtual

returns the index of a given label

Parameters
labelsearched label
Returns
the index of this label
Exceptions
NotFound

Implements gum::DiscreteVariable.

References label().

Referenced by label(), and numerical().

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

◆ integerDomain()

const std::vector< int > & gum::IntegerVariable::integerDomain ( ) const

returns the domain as a sequence of values

◆ 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)

◆ isValue()

bool gum::IntegerVariable::isValue ( int value) const

does this value exist in the domain ?

Referenced by IntegerVariable().

Here is the caller graph for this function:

◆ label()

std::string gum::IntegerVariable::label ( Idx index) const
overridevirtual

returns a string corresponding to the ith value of the domain

Implements gum::DiscreteVariable.

References index().

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:

◆ 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()

double gum::IntegerVariable::numerical ( Idx index) const
overridevirtual

get an integer representation of the value at a given index

Implements gum::DiscreteVariable.

References index().

Here is the call graph for this function:

◆ operator=() [1/2]

IntegerVariable & gum::IntegerVariable::operator= ( const IntegerVariable & from)

copy operator

Parameters
fromthe integer discrete random variable we copy

References IntegerVariable().

Here is the call graph for this function:

◆ operator=() [2/2]

IntegerVariable & gum::IntegerVariable::operator= ( IntegerVariable && from)

move operator

Parameters
fromthe integer discrete random variable we copy

References IntegerVariable().

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

◆ setName()

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

sets the name of the variable

Parameters
theValue

◆ stype()

std::string gum::IntegerVariable::stype ( ) const
overridevirtual

string represent the type of the variable

Implements gum::DiscreteVariable.

◆ toFast()

std::string gum::IntegerVariable::toFast ( ) const
overridevirtual

returns the domain size of the discrete random variable

Implements gum::DiscreteVariable.

◆ 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()

VarType gum::IntegerVariable::varType ( ) const
overridevirtual

returns the type of variable

Implements gum::DiscreteVariable.

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.

◆ _domain_

std::vector< int > gum::IntegerVariable::_domain_
private

the domain of the variable

Definition at line 201 of file integerVariable.h.

Referenced by IntegerVariable(), IntegerVariable(), and domain().

◆ _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: