aGrUM 2.3.2
a C++ library for (probabilistic) graphical models
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-2025 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-2025 *
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
43
45
46#ifdef GUM_NO_INLINE
48#endif /* GUM_NO_INLINE */
49
50namespace gum {
51
53 IntegerVariable::IntegerVariable(const std::string& aName,
54 const std::string& 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)) {
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(const std::string& aName,
73 const std::string& 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)
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 std::stringstream s;
107 const Size size = domainSize();
108
109 s << "{";
110 if (size > 0) {
111 s << _domain_[0];
112 for (Idx i = 1; i < size; ++i)
113 s << '|' << _domain_[i];
114 }
115 s << "}";
116 return s.str();
117 }
118} // namespace gum
Exception base for argument error.
Exception : default in label.
DiscreteVariable(const std::string &aName, const std::string &aDesc)
Default constructor.
std::string toString() const
string version of *this
std::string domain() const final
Returns the domain as a string.
Size domainSize() const final
returns the domain size of the discrete random variable
std::vector< int > _domain_
the domain of the variable
IntegerVariable(const std::string &aName, const std::string &aDesc="")
constructor
bool isValue(int value) const
does this value exist in the domain ?
#define GUM_ERROR(type, msg)
Definition exceptions.h:72
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
Base class for integer discrete random variables.
Base class for Integer discrete random variables.
Useful macros for maths.
gum is the global namespace for all aGrUM entities
Definition agrum.h:46
bool isfinite(T arg)
Definition math_utils.h:75