aGrUM
3.2.0
a C++ library for (probabilistic) graphical models
Toggle main menu visibility
rangeVariable_inl.h
Go to the documentation of this file.
1
/****************************************************************************
2
* This file is part of the aGrUM/pyAgrum library. *
3
* *
4
* Copyright (c) 2005-2026 by *
5
* - Pierre-Henri WUILLEMIN(_at_LIP6) *
6
* - Christophe GONZALES(_at_AMU) *
7
* *
8
* The aGrUM/pyAgrum library is free software; you can redistribute it *
9
* and/or modify it under the terms of either : *
10
* *
11
* - the GNU Lesser General Public License as published by *
12
* the Free Software Foundation, either version 3 of the License, *
13
* or (at your option) any later version, *
14
* - the MIT license (MIT), *
15
* - or both in dual license, as here. *
16
* *
17
* (see https://agrum.gitlab.io/articles/dual-licenses-lgplv3mit.html) *
18
* *
19
* This aGrUM/pyAgrum library is distributed in the hope that it will be *
20
* useful, but WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, *
21
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES MERCHANTABILITY or FITNESS *
22
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
23
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
24
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, *
25
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR *
26
* OTHER DEALINGS IN THE SOFTWARE. *
27
* *
28
* See LICENCES for more details. *
29
* *
30
* SPDX-FileCopyrightText: Copyright 2005-2026 *
31
* - Pierre-Henri WUILLEMIN(_at_LIP6) *
32
* - Christophe GONZALES(_at_AMU) *
33
* SPDX-License-Identifier: LGPL-3.0-or-later OR MIT *
34
* *
35
* Contact : info_at_agrum_dot_org *
36
* homepage : http://agrum.gitlab.io *
37
* gitlab : https://gitlab.com/agrumery/agrum *
38
* *
39
****************************************************************************/
40
41
#pragma once
42
43
50
#include <cmath>
51
#include <sstream>
52
53
// to ease IDE parsing
54
#include <
agrum/base/variables/rangeVariable.h
>
55
56
namespace
gum
{
57
58
// Copy Factory.
59
// @return Returns a pointer on a new copy of this.
60
INLINE
RangeVariable
*
RangeVariable::clone
()
const
{
return
new
RangeVariable
(*
this
); }
61
62
// returns the size of the random discrete variable domain
63
INLINE
Size
RangeVariable::domainSize
()
const
{
64
return
(
_maxBound_
<
_minBound_
) ?
Size
(0) :
Size
(
_maxBound_
+ 1 -
_minBound_
);
65
}
66
67
// Get the indice-th label. This method is pure virtual.
68
// @param indice the index of the label we wish to return
69
// @throw OutOfBound
70
INLINE std::string
RangeVariable::label
(
Idx
indice)
const
{
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
}
78
79
INLINE
80
double
RangeVariable::numerical
(
Idx
indice)
const
{
81
return
double
(
_minBound_
+
static_cast<
long
>
(indice));
82
}
83
84
INLINE
Idx
RangeVariable::index
(std::string_view
label
)
const
{
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
}
94
95
INLINE
Idx
RangeVariable::closestIndex
(
double
val)
const
{
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
}
109
110
// Returns the lower bound.
111
INLINE
long
RangeVariable::minVal
()
const
{
return
_minBound_
; }
112
113
// Set a new value for the lower bound.
114
INLINE
void
RangeVariable::setMinVal
(
long
minVal
) {
_minBound_
=
minVal
; }
115
116
// Returns the upper bound.
117
INLINE
long
RangeVariable::maxVal
()
const
{
return
_maxBound_
; }
118
119
// Set a new value of the upper bound.
120
INLINE
void
RangeVariable::setMaxVal
(
long
maxVal
) {
_maxBound_
=
maxVal
; }
121
122
// Returns true if the param belongs to the variable's interval.
123
INLINE
bool
RangeVariable::belongs
(
long
val)
const
{
124
return
((
_minBound_
<= val) && (val <=
_maxBound_
));
125
}
126
127
// Copy operator
128
// @param aRV to be copied
129
// @return a ref to *this
130
INLINE
RangeVariable
&
RangeVariable::operator=
(
const
RangeVariable
& aRV) =
default
;
131
132
INLINE
VarType
RangeVariable::varType
()
const
{
return
VarType::RANGE
; }
133
134
INLINE
bool
RangeVariable::_checkSameDomain_
(
const
gum::Variable
& aRV)
const
{
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
}
141
142
INLINE std::string
RangeVariable::stype
()
const
{
return
"Range"
; }
143
}
/* namespace gum */
double
NotFound
Exception : the element we looked for cannot be found.
OutOfBounds
Exception : out of bound.
gum::RangeVariable
Defines a discrete random variable over an integer interval.
Definition
rangeVariable.h:72
gum::RangeVariable::setMaxVal
void setMaxVal(long maxVal)
Set a new value of the upper bound.
Definition
rangeVariable_inl.h:120
gum::RangeVariable::clone
RangeVariable * clone() const final
Copy Factory.
Definition
rangeVariable_inl.h:60
gum::RangeVariable::closestIndex
Idx closestIndex(double val) const final
returns the closest index of the value
Definition
rangeVariable_inl.h:95
gum::RangeVariable::label
std::string label(Idx index) const final
Get the index-th label.
Definition
rangeVariable_inl.h:70
gum::RangeVariable::maxVal
long maxVal() const
Returns the upper bound.
Definition
rangeVariable_inl.h:117
gum::RangeVariable::_minBound_
long _minBound_
The upper bound.
Definition
rangeVariable.h:189
gum::RangeVariable::belongs
bool belongs(long val) const
Returns true if the param belongs to the variable's interval.
Definition
rangeVariable_inl.h:123
gum::RangeVariable::_checkSameDomain_
bool _checkSameDomain_(const Variable &aRV) const final
check the domain
Definition
rangeVariable_inl.h:134
gum::RangeVariable::setMinVal
void setMinVal(long minVal)
Set a new value for the lower bound.
Definition
rangeVariable_inl.h:114
gum::RangeVariable::domainSize
Size domainSize() const final
returns the size of the random discrete variable domain
Definition
rangeVariable_inl.h:63
gum::RangeVariable::varType
VarType varType() const final
returns the type of variable
Definition
rangeVariable_inl.h:132
gum::RangeVariable::operator=
RangeVariable & operator=(const RangeVariable &aRV)
Copy operator.
gum::RangeVariable::numerical
double numerical(Idx index) const final
get a numerical representation of the index-the value.
Definition
rangeVariable_inl.h:80
gum::RangeVariable::stype
std::string stype() const final
string represent the type of the variable
Definition
rangeVariable_inl.h:142
gum::RangeVariable::minVal
long minVal() const
Returns the lower bound.
Definition
rangeVariable_inl.h:111
gum::RangeVariable::RangeVariable
RangeVariable(std::string_view aName, std::string_view aDesc, long minVal, long maxVal)
constructors
Definition
rangeVariable.cpp:60
gum::RangeVariable::_maxBound_
long _maxBound_
The upper bound.
Definition
rangeVariable.h:192
gum::RangeVariable::index
Idx index(std::string_view) const final
Definition
rangeVariable_inl.h:84
gum::Variable
Base class for every random variable.
Definition
variable.h:81
GUM_ERROR
#define GUM_ERROR(type, msg)
Definition
exceptions.h:76
gum::Size
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition
types.h:74
gum::Idx
Size Idx
Type for indexes.
Definition
types.h:79
gum
gum is the global namespace for all aGrUM entities
Definition
agrum.h:46
gum::VarType
VarType
Definition
variable.h:62
gum::VarType::RANGE
@ RANGE
Definition
variable.h:62
rangeVariable.h
Header of gumRangeVariable.
aGrUM
3.2.0
© PHW&CG&others - 2022
DoXyGeN 1.18.0