aGrUM 3.1.1
a C++ library for (probabilistic) graphical models
multiDimAggregator_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// to ease parser in IDEs
53
54namespace gum {
55 namespace aggregator {
56
57 // Default constructor
58 template < typename GUM_SCALAR >
63
64 // Default constructor
65 template < typename GUM_SCALAR >
68 MultiDimReadOnly< GUM_SCALAR >(from), decomposable_(from.decomposable_) {
69 GUM_CONS_CPY(MultiDimAggregator);
70 }
71
72 // destructor
73 template < typename GUM_SCALAR >
77
78 template < typename GUM_SCALAR >
80 if (this->nbrDim() == 1) return neutralElt_();
81
82 // is i equal to f(f(f(f...(j_,neutral_elt))))
83 Idx current = neutralElt_();
84
85 bool stop_iteration = false;
86
87 for (Idx j = 1; j < this->nbrDim(); j++) {
88 current = fold_(this->variable(j), i.val(this->variable(j)), current, stop_iteration);
89
90 if (stop_iteration) break;
91 }
92
93 return current;
94 }
95
96 template < typename GUM_SCALAR >
98 if (this->nbrDim() < 1) {
99 GUM_ERROR(OperationNotAllowed, "Not enough variable for an aggregator : " << *this)
100 }
101
102 const DiscreteVariable& agg = this->variable((Idx)0);
103 auto current = buildValue_(i);
105 // truncate to fit in aggreegator domain size
106 if (current >= agg.domainSize()) current = agg.domainSize() - 1;
107
108 return (i.val(agg) == current) ? (GUM_SCALAR)1.0 : (GUM_SCALAR)0.0;
109 }
110
111 template < typename GUM_SCALAR >
113 std::stringstream s;
114 s << this->variable(0) << "=" << aggregatorName() << "(";
115
116 for (Idx i = 1; i < this->nbrDim(); i++) {
117 if (i > 1) s << ",";
118
119 s << this->variable(i);
120 }
121
122 s << ")";
123
124 return s.str();
125 }
126
127 template < typename GUM_SCALAR >
129 const MultiDimContainer< GUM_SCALAR >& src) const {
130 if (auto p = dynamic_cast< const MultiDimAggregator< GUM_SCALAR >* >(&src)) {
131 if (p->name() != this->name()) {
133 "Can not copy from a " << p->name() << " to a " << this->name());
134 }
135 } else {
137 }
138 // it the types aree consistant, nothing to do...
139 }
140
141 template < typename GUM_SCALAR >
146 // returns the name of the implementation
147 template < typename GUM_SCALAR >
148 const std::string& MultiDimAggregator< GUM_SCALAR >::name() const {
149 static const std::string str = "MultiDimAggregator";
150 return str;
151 }
152
153 template < typename GUM_SCALAR >
154 std::optional< Idx > MultiDimAggregator< GUM_SCALAR >::value() const {
155 return std::nullopt;
156 }
158 // For friendly displaying the content of the variable.
159 template < typename GUM_SCALAR >
160 std::ostream& operator<<(std::ostream& s, const MultiDimAggregator< GUM_SCALAR >& ag) {
161 return s << ag.toString();
162 }
163
164 template < typename GUM_SCALAR >
169
170 template < typename GUM_SCALAR >
172
173 template < typename GUM_SCALAR >
175
176 template < typename GUM_SCALAR >
178
179 template < typename GUM_SCALAR >
181
182 template < typename GUM_SCALAR >
184
185 template < typename GUM_SCALAR >
187 return i->toString();
188 }
189
190 template < typename GUM_SCALAR >
195 } /* namespace aggregator */
196} /* namespace gum */
Base class for discrete random variable.
virtual Size domainSize() const =0
Class for assigning/browsing values to tuples of discrete variables.
Idx val(Idx i) const
Returns the current value of the variable at position i.
std::string toString() const
Give a string version of instantiation.
virtual void copyFrom(const MultiDimContainer< GUM_ELEMENT > &src) const
Basic copy of a MultiDimContainer.
const DiscreteVariable & variable(Idx i) const override
Returns a const ref to the ith var.
Idx nbrDim() const override
Returns the number of vars in the multidimensional container.
Abstract base class for all multi dimensionnal read only structure.
MultiDimReadOnly()
Default constructor.
Exception : operation not allowed.
~MultiDimAggregator() override
Class destructor.
void setFirstNotification(const gum::Instantiation &) override
Returns the scalar parameter of this aggregator, if it has one.
GUM_ELEMENT get(const Instantiation &i) const override
Returns the scalar parameter of this aggregator, if it has one.
void setChangeNotification(const gum::Instantiation &) override
Returns the scalar parameter of this aggregator, if it has one.
void setIncNotification(const gum::Instantiation &) override
Returns the scalar parameter of this aggregator, if it has one.
void changeNotification(const gum::Instantiation &, const gum::DiscreteVariable *const, gum::Idx, gum::Idx) override
Returns the scalar parameter of this aggregator, if it has one.
virtual Idx fold_(const DiscreteVariable &v, Idx i1, Idx i2, bool &stop_iteration) const =0
fold_ is applied on value i1 for variable v. the actual result for precedent applications is i2.
std::string toString() const override
Returns the scalar parameter of this aggregator, if it has one.
void setDecNotification(const gum::Instantiation &) override
Returns the scalar parameter of this aggregator, if it has one.
virtual Idx buildValue_(const gum::Instantiation &i) const
by default, buildValue_ uses a "fold" scheme and the user has to implement neutralElt_ and fold_ but ...
void setLastNotification(const gum::Instantiation &) override
Returns the scalar parameter of this aggregator, if it has one.
virtual std::optional< Idx > value() const
Returns the scalar parameter of this aggregator, if it has one.
virtual Idx neutralElt_() const =0
neutralElt_() is the result value for the first application of fold_
virtual std::string aggregatorName() const =0
Returns the scalar parameter of this aggregator, if it has one.
void copyFrom(const MultiDimContainer< GUM_ELEMENT > &src) const override
Copy of a multiDimICIModel.
const std::string & name() const override
Returns the real name of the multiDimArray.
bool decomposable_
decomposable_ indicates if the aggregator can be decomposed
#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
MultiDimAggregator.
Aggregators are functional description of CPTs.
Definition agrum.h:55
std::ostream & operator<<(std::ostream &s, const MultiDimAggregator< GUM_ELEMENT > &ag)
For friendly displaying the content of the array.
gum is the global namespace for all aGrUM entities
Definition agrum.h:46