aGrUM
3.3.2
a C++ library for (probabilistic) graphical models
Toggle main menu visibility
multiDimValueAggregator_tpl.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
51
#include <algorithm>
52
#include <cmath>
53
54
// to ease parser in IDEs
55
#include <
agrum/base/multidim/aggregators/multiDimValueAggregator.h
>
56
57
namespace
gum
{
58
namespace
aggregator
{
59
60
template
<
typename
GUM_SCALAR >
61
MultiDimValueAggregator< GUM_SCALAR >::MultiDimValueAggregator
() :
62
MultiDimAggregator
< GUM_SCALAR >() {
63
GUM_CONSTRUCTOR(
MultiDimValueAggregator
);
64
}
65
66
template
<
typename
GUM_SCALAR >
67
MultiDimValueAggregator< GUM_SCALAR >::MultiDimValueAggregator
(
68
const
MultiDimValueAggregator< GUM_SCALAR >
& from) :
69
MultiDimAggregator
< GUM_SCALAR >(from) {
70
GUM_CONS_CPY(
MultiDimValueAggregator
);
71
}
72
73
template
<
typename
GUM_SCALAR >
74
MultiDimValueAggregator< GUM_SCALAR >::~MultiDimValueAggregator
() {
75
GUM_DESTRUCTOR(
MultiDimValueAggregator
);
76
}
77
78
template
<
typename
GUM_SCALAR >
79
void
MultiDimValueAggregator< GUM_SCALAR >::checkNumericalVariable_
(
80
const
DiscreteVariable
& v)
const
{
81
if
(!v.
isNumerical
()) {
82
GUM_ERROR
(
TypeError
,
83
"The aggregator "
<< this->
aggregatorName
() <<
" needs numerical variables, "
84
<< v.
name
() <<
" is labelized"
)
85
}
86
}
87
88
template
<
typename
GUM_SCALAR >
89
void
MultiDimValueAggregator< GUM_SCALAR >::checkHomogeneousVariable_
(
90
const
DiscreteVariable
& v)
const
{
91
if
(this->
nbrDim
() > 0 && v.
isNumerical
() != this->variable(0).isNumerical()) {
92
GUM_ERROR
(
TypeError
,
93
"The aggregator "
<< this->
aggregatorName
()
94
<<
" cannot mix labelized and numerical variables ("
95
<< this->
variable
(0).
name
() <<
" and "
<< v.
name
() <<
")"
)
96
}
97
}
98
99
template
<
typename
GUM_SCALAR >
100
double
MultiDimValueAggregator< GUM_SCALAR >::parentValue_
(
const
Instantiation
& i,
101
Idx
j)
const
{
102
const
auto
& v = this->
variable
(j);
103
// fast path (no lookup of v in i) when i has the variables of the aggregator in its order
104
if
(j < i.
nbrDim
() && &i.
variable
(j) == &v)
return
v.
numerical
(i.
val
(j));
105
return
v.
numerical
(i.
val
(v));
106
}
107
108
template
<
typename
GUM_SCALAR >
109
Idx
MultiDimValueAggregator< GUM_SCALAR >::valueToIndex_
(
double
value
)
const
{
110
const
auto
& agg = this->
variable
((
Idx
)0);
111
if
(agg.isNumerical())
return
agg.closestIndex(
value
);
112
113
// rounded half down (as RangeVariable::closestIndex()), saturated at both bounds
114
const
auto
last =
static_cast<
double
>
(agg.domainSize() - 1);
115
return
static_cast<
Idx
>
(std::ceil(std::clamp(
value
, 0.0, last) - 0.5));
116
}
117
118
}
/* namespace aggregator */
119
}
/* namespace gum */
gum::DiscreteVariable
Base class for discrete random variable.
Definition
discreteVariable.h:80
gum::DiscreteVariable::numerical
virtual double numerical(Idx indice) const =0
get a numerical representation of the indice-th value.
gum::DiscreteVariable::isNumerical
bool isNumerical() const
gum::Instantiation
Class for assigning/browsing values to tuples of discrete variables.
Definition
instantiation.h:102
gum::Instantiation::val
Idx val(Idx i) const
Returns the current value of the variable at position i.
Definition
instantiation_inl.h:166
gum::Instantiation::variable
const DiscreteVariable & variable(Idx i) const final
Returns the variable at position i in the tuple.
Definition
instantiation_inl.h:188
gum::Instantiation::nbrDim
Idx nbrDim() const final
Returns the number of variables in the Instantiation.
Definition
instantiation_inl.h:163
gum::aggregator::MultiDimValueAggregator< GUM_SCALAR >::variable
const DiscreteVariable & variable(Idx i) const override
Definition
multiDimImplementation_tpl.h:192
gum::MultiDimImplementation::nbrDim
Idx nbrDim() const override
Returns the number of vars in the multidimensional container.
Definition
multiDimImplementation_tpl.h:182
TypeError
Exception : wrong type for this operation.
gum::Variable::name
const std::string & name() const
returns the name of the variable
gum::aggregator::MultiDimAggregator
<agrum/base/multidim/aggregators/multiDimAggregator.h>
Definition
multiDimAggregator.h:85
gum::aggregator::MultiDimAggregator::MultiDimAggregator
MultiDimAggregator()
Default constructor.
Definition
multiDimAggregator_tpl.h:59
gum::aggregator::MultiDimAggregator::value
virtual std::optional< Idx > value() const
Returns the scalar parameter of this aggregator, if it has one.
Definition
multiDimAggregator_tpl.h:145
gum::aggregator::MultiDimValueAggregator< GUM_SCALAR >::aggregatorName
virtual std::string aggregatorName() const=0
gum::aggregator::MultiDimValueAggregator< GUM_SCALAR >::name
const std::string & name() const override
Definition
multiDimAggregator_tpl.h:139
gum::aggregator::MultiDimValueAggregator< GUM_SCALAR >
gum::aggregator::MultiDimValueAggregator::parentValue_
double parentValue_(const gum::Instantiation &i, Idx j) const
Definition
multiDimValueAggregator_tpl.h:100
gum::aggregator::MultiDimValueAggregator::valueToIndex_
Idx valueToIndex_(double value) const
Maps a value onto an index of the aggregator.
Definition
multiDimValueAggregator_tpl.h:109
gum::aggregator::MultiDimValueAggregator::~MultiDimValueAggregator
~MultiDimValueAggregator() override
Class destructor.
Definition
multiDimValueAggregator_tpl.h:74
gum::aggregator::MultiDimValueAggregator::checkNumericalVariable_
void checkNumericalVariable_(const DiscreteVariable &v) const
Definition
multiDimValueAggregator_tpl.h:79
gum::aggregator::MultiDimValueAggregator::MultiDimValueAggregator
MultiDimValueAggregator()
Default constructor.
Definition
multiDimValueAggregator_tpl.h:61
gum::aggregator::MultiDimValueAggregator::checkHomogeneousVariable_
void checkHomogeneousVariable_(const DiscreteVariable &v) const
Definition
multiDimValueAggregator_tpl.h:89
GUM_ERROR
#define GUM_ERROR(type, msg)
Definition
exceptions.h:76
gum::Idx
Size Idx
Type for indexes.
Definition
types.h:79
multiDimValueAggregator.h
MultiDimValueAggregator: aggregators computed on the values of the parents.
gum::aggregator
Aggregators are functional description of CPTs.
Definition
agrum.h:55
gum
gum is the global namespace for all aGrUM entities
Definition
agrum.h:46
aGrUM
3.3.2
© PHW&CG&others - 2022
DoXyGeN 1.18.0