aGrUM
3.2.0
a C++ library for (probabilistic) graphical models
Toggle main menu visibility
integerVariable.cpp
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
42
#include <
agrum/base/variables/integerVariable.h
>
43
44
#include <
agrum/base/core/math/math_utils.h
>
45
46
#ifdef GUM_NO_INLINE
47
# include <
agrum/base/variables/integerVariable_inl.h
>
48
#endif
/* GUM_NO_INLINE */
49
50
namespace
gum
{
51
53
IntegerVariable::IntegerVariable
(std::string_view aName,
54
std::string_view aDesc,
55
const
std::vector< int >&
domain
) :
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
}
70
72
IntegerVariable::IntegerVariable
(std::string_view aName,
73
std::string_view aDesc,
74
int
first,
75
int
last,
76
Size
nb) :
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
}
103
105
std::string
IntegerVariable::domain
()
const
{
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
}
116
}
// namespace gum
ArgumentError
Exception base for argument error.
DefaultInLabel
Exception : default in label.
gum::DiscreteVariable::DiscreteVariable
DiscreteVariable(std::string_view aName, std::string_view aDesc)
Default constructor.
gum::DiscreteVariable::toString
std::string toString() const
string version of *this
gum::IntegerVariable::_domain_
std::vector< int > _domain_
the domain of the variable
Definition
integerVariable.h:201
gum::IntegerVariable::domainSize
Size domainSize() const override
returns the domain size of the discrete random variable
gum::IntegerVariable::isValue
bool isValue(int value) const
does this value exist in the domain ?
gum::IntegerVariable::IntegerVariable
IntegerVariable(std::string_view aName, std::string_view aDesc="")
constructor
gum::IntegerVariable::domain
std::string domain() const override
Returns the domain as a string.
Definition
integerVariable.cpp:105
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
integerVariable.h
Base class for integer discrete random variables.
integerVariable_inl.h
Base class for Integer discrete random variables.
math_utils.h
Useful macros for maths.
gum
gum is the global namespace for all aGrUM entities
Definition
agrum.h:46
gum::isfinite
bool isfinite(T arg)
Definition
math_utils_tpl.h:57
aGrUM
3.2.0
© PHW&CG&others - 2022
DoXyGeN 1.18.0