aGrUM 2.3.2
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-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#pragma once
41
42
49
50// to ease parser in IDEs
52
53namespace gum {
54 namespace aggregator {
55
56 // Default constructor
57 template < typename GUM_SCALAR >
59 MultiDimReadOnly< GUM_SCALAR >(), decomposable_(false) {
60 GUM_CONSTRUCTOR(MultiDimAggregator);
61 }
62
63 // Default constructor
64 template < typename GUM_SCALAR >
70
71 // destructor
72 template < typename GUM_SCALAR >
76
77 template < typename GUM_SCALAR >
79 if (this->nbrDim() == 1) return neutralElt_();
80
81 // is i equal to f(f(f(f...(j_,neutral_elt))))
82 Idx current = neutralElt_();
83
84 bool stop_iteration = false;
85
86 for (Idx j = 1; j < this->nbrDim(); j++) {
87 current = fold_(this->variable(j), i.val(this->variable(j)), current, stop_iteration);
88
89 if (stop_iteration) break;
90 }
91
92 return current;
93 }
94
95 template < typename GUM_SCALAR >
97 if (this->nbrDim() < 1) {
98 GUM_ERROR(OperationNotAllowed, "Not enough variable for an aggregator : " << *this)
99 }
100
101 const DiscreteVariable& agg = this->variable((Idx)0);
102 auto current = buildValue_(i);
103
104 // truncate to fit in aggreegator domain size
105 if (current >= agg.domainSize()) current = agg.domainSize() - 1;
106
107 return (i.val(agg) == current) ? (GUM_SCALAR)1.0 : (GUM_SCALAR)0.0;
108 }
109
110 template < typename GUM_SCALAR >
112 std::stringstream s;
113 s << this->variable(0) << "=" << aggregatorName() << "(";
114
115 for (Idx i = 1; i < this->nbrDim(); i++) {
116 if (i > 1) s << ",";
117
118 s << this->variable(i);
119 }
120
121 s << ")";
122
123 return s.str();
124 }
125
126 template < typename GUM_SCALAR >
128 const MultiDimContainer< GUM_SCALAR >& src) const {
129 auto p = dynamic_cast< const MultiDimAggregator< GUM_SCALAR >* >(&src);
130 if (p == nullptr) {
132 } else {
133 if (p->name() != this->name()) {
135 "Can not copy from a " << p->name() << " to a " << this->name());
136 }
137 }
138 // it the types aree consistant, nothing to do...
139 }
140
141 template < typename GUM_SCALAR >
145
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 // For friendly displaying the content of the variable.
154 template < typename GUM_SCALAR >
155 INLINE std::ostream& operator<<(std::ostream& s, const MultiDimAggregator< GUM_SCALAR >& ag) {
156 return s << ag.toString();
157 }
158 } /* namespace aggregator */
159} /* 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.
MultiDimContainer()
Default constructor.
virtual void copyFrom(const MultiDimContainer< GUM_SCALAR > &src) const
Basic copy of a MultiDimContainer.
virtual Idx nbrDim() const override
Returns the number of vars in the multidimensional container.
const DiscreteVariable & variable(Idx i) const override
Returns a const ref to the ith var.
MultiDimReadOnly()
Default constructor.
Exception : operation not allowed.
<agrum/base/multidim/aggregators/multiDimAggregator.h>
void copyFrom(const MultiDimContainer< GUM_SCALAR > &src) const override
Copy of a multiDimICIModel.
virtual ~MultiDimAggregator()
Class destructor.
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.
virtual GUM_SCALAR get(const Instantiation &i) const override
virtual std::string aggregatorName() const =0
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 ...
bool decomposable_
decomposable_ indicates if the aggregator can be decomposed
const std::string & name() const override
Returns the real name of the multiDimArray.
virtual Idx neutralElt_() const =0
neutralElt_() is the result value for the first application of fold_
#define GUM_ERROR(type, msg)
Definition exceptions.h:72
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_SCALAR > &ag)
For friendly displaying the content of the array.
gum is the global namespace for all aGrUM entities
Definition agrum.h:46