aGrUM 3.1.1
a C++ library for (probabilistic) graphical models
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
55
56namespace 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
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.
115
116 // Returns the upper bound.
117 INLINE long RangeVariable::maxVal() const { return _maxBound_; }
118
119 // Set a new value of the upper bound.
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
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 */
Exception : the element we looked for cannot be found.
Exception : out of bound.
Defines a discrete random variable over an integer interval.
void setMaxVal(long maxVal)
Set a new value of the upper bound.
RangeVariable * clone() const final
Copy Factory.
Idx closestIndex(double val) const final
returns the closest index of the value
std::string label(Idx index) const final
Get the index-th label.
long maxVal() const
Returns the upper bound.
long _minBound_
The upper bound.
bool belongs(long val) const
Returns true if the param belongs to the variable's interval.
bool _checkSameDomain_(const Variable &aRV) const final
check the domain
void setMinVal(long minVal)
Set a new value for the lower bound.
Size domainSize() const final
returns the size of the random discrete variable domain
VarType varType() const final
returns the type of variable
RangeVariable & operator=(const RangeVariable &aRV)
Copy operator.
double numerical(Idx index) const final
get a numerical representation of the index-the value.
std::string stype() const final
string represent the type of the variable
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.
Idx index(std::string_view) const final
Base class for every random variable.
Definition variable.h:81
#define GUM_ERROR(type, msg)
Definition exceptions.h:76
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition types.h:74
Size Idx
Type for indexes.
Definition types.h:79
gum is the global namespace for all aGrUM entities
Definition agrum.h:46
VarType
Definition variable.h:62
Header of gumRangeVariable.