aGrUM 3.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-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 >
83
84 template < typename GUM_SCALAR >
86
87 template < typename GUM_SCALAR >
89 if (this->nbrDim() < 1) {
90 GUM_ERROR(OperationNotAllowed, "Not enough variable for an aggregator : " << *this)
91 }
92
93 const DiscreteVariable& agg = this->variable((Idx)0);
94 auto current = buildValue_(i);
96 // truncate to fit in aggreegator domain size
97 if (current >= agg.domainSize()) current = agg.domainSize() - 1;
98
99 return (i.val(agg) == current) ? (GUM_SCALAR)1.0 : (GUM_SCALAR)0.0;
100 }
101
102 template < typename GUM_SCALAR >
104 std::stringstream s;
105 s << this->variable(0) << "=" << aggregatorName() << "(";
106
107 for (Idx i = 1; i < this->nbrDim(); i++) {
108 if (i > 1) s << ",";
109
110 s << this->variable(i);
111 }
112
113 s << ")";
114
115 return s.str();
116 }
117
118 template < typename GUM_SCALAR >
120 const MultiDimContainer< GUM_SCALAR >& src) const {
121 if (auto p = dynamic_cast< const MultiDimAggregator< GUM_SCALAR >* >(&src)) {
122 if (p->name() != this->name()) {
124 "Can not copy from a " << p->name() << " to a " << this->name());
125 }
126 } else {
128 }
129 // it the types aree consistant, nothing to do...
130 }
131
132 template < typename GUM_SCALAR >
135 }
136
137 // returns the name of the implementation
138 template < typename GUM_SCALAR >
139 const std::string& MultiDimAggregator< GUM_SCALAR >::name() const {
140 static const std::string str = "MultiDimAggregator";
141 return str;
143
144 template < typename GUM_SCALAR >
145 std::optional< Idx > MultiDimAggregator< GUM_SCALAR >::value() const {
146 return std::nullopt;
147 }
148
149 // For friendly displaying the content of the variable.
150 template < typename GUM_SCALAR >
151 std::ostream& operator<<(std::ostream& s, const MultiDimAggregator< GUM_SCALAR >& ag) {
152 return s << ag.toString();
153 }
155 template < typename GUM_SCALAR >
160
161 template < typename GUM_SCALAR >
163
167 template < typename GUM_SCALAR >
169
170 template < typename GUM_SCALAR >
173 template < typename GUM_SCALAR >
175
176 template < typename GUM_SCALAR >
178 return i->toString();
180
181 template < typename GUM_SCALAR >
185
186 } /* namespace aggregator */
187} /* 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.
void add(const DiscreteVariable &v) override
Adds a new var to the variables of the multidimensional matrix.
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
Adds a variable: the first one is the aggregator, the next ones are its parents.
GUM_ELEMENT get(const Instantiation &i) const override
Adds a variable: the first one is the aggregator, the next ones are its parents.
void setChangeNotification(const gum::Instantiation &) override
Adds a variable: the first one is the aggregator, the next ones are its parents.
virtual Idx buildValue_(const gum::Instantiation &i) const =0
void setIncNotification(const gum::Instantiation &) override
Adds a variable: the first one is the aggregator, the next ones are its parents.
void changeNotification(const gum::Instantiation &, const gum::DiscreteVariable *const, gum::Idx, gum::Idx) override
Adds a variable: the first one is the aggregator, the next ones are its parents.
std::string toString() const override
Adds a variable: the first one is the aggregator, the next ones are its parents.
void setDecNotification(const gum::Instantiation &) override
Adds a variable: the first one is the aggregator, the next ones are its parents.
void setLastNotification(const gum::Instantiation &) override
Adds a variable: the first one is the aggregator, the next ones are its parents.
virtual std::optional< Idx > value() const
Returns the scalar parameter of this aggregator, if it has one.
void add(const DiscreteVariable &v) override
Adds a variable: the first one is the aggregator, the next ones are its parents.
virtual std::string aggregatorName() const=0
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
virtual void checkVariable_(const DiscreteVariable &v) const
Checks that a variable can be added to this aggregator (nothing by default).
#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