aGrUM 3.3.2
a C++ library for (probabilistic) graphical models
aggregatorDecomposition_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
51#include <algorithm>
52#include <cmath>
53#include <limits>
54#include <list>
55#include <typeinfo>
56
58
59namespace gum {
60
61 template < GUM_Numeric GUM_SCALAR >
63 long& minVal,
64 long& maxVal) {
65 if (aggType == "min") minVal = maxVal = std::numeric_limits< long >::max();
66 else if (aggType == "max") minVal = maxVal = std::numeric_limits< long >::min();
67 else minVal = maxVal = 0;
68 }
69
70 template < GUM_Numeric GUM_SCALAR >
72 const DiscreteVariable& v,
73 long& minVal,
74 long& maxVal) {
75 if (!v.isNumerical()) return;
76 const double first = v.numerical(0);
77 const double last = v.numerical(v.domainSize() - 1);
78 const auto lo = static_cast< long >(std::floor(std::min(first, last)));
79 const auto hi = static_cast< long >(std::ceil(std::max(first, last)));
80 if (aggType == "min") {
81 minVal = std::min(minVal, lo);
82 maxVal = std::min(maxVal, hi);
83 } else if (aggType == "max") {
84 minVal = std::max(minVal, lo);
85 maxVal = std::max(maxVal, hi);
86 } else {
87 minVal += lo;
88 maxVal += hi;
89 }
90 }
91
92 template < GUM_Numeric GUM_SCALAR >
96
97 template < GUM_Numeric GUM_SCALAR >
101
102 template < GUM_Numeric GUM_SCALAR >
103 BayesNet< GUM_SCALAR >&
105 for (NodeId node: bn.nodes().asNodeSet()) {
106 std::string description = bn.cpt(node).toString();
107 if (auto p = dynamic_cast< const gum::aggregator::MultiDimAggregator< GUM_SCALAR >* >(
108 bn.cpt(node).content());
109 p != nullptr && p->isDecomposable()) {
110 decomposeAggregator_(bn, node);
111 }
112 }
113 return bn;
114 }
115
116 template < GUM_Numeric GUM_SCALAR >
118 std::string_view aggType,
119 const DiscreteVariable& var,
120 Idx value) {
121 return bn._addAggregator_(aggType, var, value);
122 }
123
124 template < GUM_Numeric GUM_SCALAR >
125 BayesNet< GUM_SCALAR >&
127 NodeId initialAggregator) {
128 auto p = static_cast< const gum::aggregator::MultiDimAggregator< GUM_SCALAR >* >(
129 bn.cpt(initialAggregator).content());
130 auto newAgg = bn.variable(initialAggregator).clone();
131
132 Set< NodeId > parents = bn.parents(initialAggregator);
133
134 std::list< NodeId > orderedParents = {};
135
136 for (const auto& elt: parents) {
137 orderedParents.push_back(elt);
138 }
139
140 orderedParents.sort();
141
142 auto newAggs = Set< NodeId >();
143 List< NodeId > newAggParents;
144
145 const gum::Size arity = getMaximumArity();
146 gum::Size q = 0;
147 gum::Size i = 0;
148
149 long minVal = 0;
150 long maxVal = 0;
151
152 int j = 1;
153
154 std::string newName
155 = std::string(bn.variable(initialAggregator).name()) + "_" + std::to_string(j);
156 const std::string aggType = p->aggregatorName();
157 _resetBounds_(aggType, minVal, maxVal);
158
159 for (auto parent: parents) {
160 bn.eraseArc(parent, initialAggregator);
161 }
162
163 /*
164 * We are constructing the new aggregator with a clone of the former
165 */
166 newAgg->setName(newName);
167 newAgg->setDescription(aggType);
168
169 // for(Set<NodeId>::iterator it = parents.begin(); it!= parents.end(); ++it){
170 // for (auto it = orderedParents.begin(); it != orderedParents.end(); ++it) {
171 for (const auto& parent: orderedParents) {
172 if (q < parents.size() - parents.size() % arity) {
173 if (i == arity) {
174 i = 0;
175 j++;
176
177 if (newAgg->varType() == VarType::LABELIZED) {
178 addAggregator_(bn, aggType, *newAgg, p->domainSize());
179 } else if (newAgg->varType() == VarType::RANGE) {
180 static_cast< RangeVariable* >(newAgg)->setMinVal(minVal);
181 static_cast< RangeVariable* >(newAgg)->setMaxVal(maxVal);
182 addAggregator_(bn, aggType, *newAgg, 0);
183 } else {
184 GUM_ERROR(OperationNotAllowed, "Decomposition is not available for type : " + aggType)
185 }
186
187 /*
188 * Adding arcs in the new node from its parents and adding thoses into
189 * the temporary tensor
190 */
191 for (NodeId node: newAggParents) {
192 bn.addArc(node, bn.idFromName(newName));
193 }
194
195 /*
196 * Adding the new aggregator in t
197 */
198 newAggs.insert(bn.idFromName(newName));
199
200 newAggParents.clear();
201
202 _resetBounds_(aggType, minVal, maxVal);
203
204 newName = std::string(bn.variable(initialAggregator).name()) + "_" + std::to_string(j);
205
206 delete (newAgg);
207 newAgg = bn.variable(initialAggregator).clone();
208 newAgg->setName(newName);
209 newAgg->setDescription(aggType);
210 }
211
212 _accumulateBounds_(aggType, bn.variable(parent), minVal, maxVal);
213 newAggParents.push_back(parent);
214 i++;
215 } else {
216 newAggs.insert(parent);
217 }
218 q++;
219 }
220
221 if (newAgg->varType() == VarType::LABELIZED) {
222 addAggregator_(bn, aggType, *newAgg, p->domainSize());
223 } else if (newAgg->varType() == VarType::RANGE) {
224 static_cast< RangeVariable* >(newAgg)->setMinVal(minVal);
225 static_cast< RangeVariable* >(newAgg)->setMaxVal(maxVal);
226 addAggregator_(bn, aggType, *newAgg, 0);
227 } else {
228 GUM_ERROR(OperationNotAllowed, "Decomposition is not available for type : " + aggType)
229 }
230
231 newAggs.insert(bn.idFromName(newName));
232
233 for (NodeId node: newAggParents) {
234 bn.addArc(node, bn.idFromName(newName));
235 }
236
237 for (auto agg: addDepthLayer_(bn, newAggs, initialAggregator, j)) {
238 bn.addArc(agg, initialAggregator);
239 }
240
241 delete (newAgg);
242 return bn;
243 }
244
245 template < GUM_Numeric GUM_SCALAR >
247 Set< NodeId > nodes,
248 NodeId initialAggregator,
249 int& j) {
250 auto p = static_cast< const gum::aggregator::MultiDimAggregator< GUM_SCALAR >* >(
251 bn.cpt(initialAggregator).content());
252
253 gum::Size arity = getMaximumArity();
254 std::string aggType = p->aggregatorName();
255
256 if (nodes.size() <= arity) {
257 return nodes;
258 } else {
259 auto newAgg = bn.variable(initialAggregator).clone();
260
261 auto newAggs = Set< NodeId >();
262
263 List< NodeId > newAggParents;
264
265 std::list< NodeId > orderedParents = {};
266
267 for (const auto& elt: nodes) {
268 orderedParents.push_back(elt);
269 }
270
271 orderedParents.sort();
272
273 gum::Size i = 0;
274 gum::Size q = 0;
275 long minVal = 0;
276 long maxVal = 0;
277 _resetBounds_(aggType, minVal, maxVal);
278
279 j++;
280
281 std::string newName
282 = std::string(bn.variable(initialAggregator).name()) + "_" + std::to_string(j);
283
284 newAgg->setName(newName);
285 newAgg->setDescription(aggType);
286
287 // for(Set<NodeId>::iterator it = nodes.begin(); it!= nodes.end(); ++it){
288 // for (auto it = orderedParents.begin(); it != orderedParents.end(); ++it) {
289 for (const auto parent: orderedParents) {
290 if (q < nodes.size() - nodes.size() % arity) {
291 if (i == arity) {
292 i = 0;
293 j++;
294
295 if (newAgg->varType() == VarType::LABELIZED) {
296 addAggregator_(bn, aggType, *newAgg, p->domainSize());
297 } else if (newAgg->varType() == VarType::RANGE) {
298 static_cast< RangeVariable* >(newAgg)->setMinVal(minVal);
299 static_cast< RangeVariable* >(newAgg)->setMaxVal(maxVal);
300 addAggregator_(bn, aggType, *newAgg, 0);
301 } else {
302 GUM_ERROR(OperationNotAllowed, "Decomposition is not available for type : " + aggType)
303 }
304
305 for (NodeId node: newAggParents) {
306 bn.addArc(node, bn.idFromName(newName));
307 }
308
309 newAggs.insert(bn.idFromName(newName));
310
311 newAggParents.clear();
312
313 _resetBounds_(aggType, minVal, maxVal);
314
315 newName = std::string(bn.variable(initialAggregator).name()) + "_" + std::to_string(j);
316
317 delete newAgg;
318 newAgg = bn.variable(initialAggregator).clone();
319 newAgg->setName(newName);
320 newAgg->setDescription(aggType);
321 }
322
323 _accumulateBounds_(aggType, bn.variable(parent), minVal, maxVal);
324 newAggParents.push_back(parent);
325 i++;
326 } else {
327 newAggs.insert(parent);
328 }
329 q++;
330 }
331
332 if (newAgg->varType() == VarType::LABELIZED) {
333 addAggregator_(bn, aggType, *newAgg, p->domainSize());
334 } else if (newAgg->varType() == VarType::RANGE) {
335 static_cast< RangeVariable* >(newAgg)->setMinVal(minVal);
336 static_cast< RangeVariable* >(newAgg)->setMaxVal(maxVal);
337 addAggregator_(bn, aggType, *newAgg, 0);
338 } else {
339 GUM_ERROR(OperationNotAllowed, "Decomposition is not available for type : " + aggType)
340 }
341
342 newAggs.insert(bn.idFromName(newName));
343
344 for (NodeId node: newAggParents) {
345 bn.addArc(node, bn.idFromName(newName));
346 }
347
348 delete newAgg;
349 return addDepthLayer_(bn, newAggs, initialAggregator, j);
350 }
351 }
352
353 template < GUM_Numeric GUM_SCALAR >
355 if (arity < 2) { GUM_ERROR(OperationNotAllowed, "Maximum arity should be at least 2") }
356 _arity_ = arity;
357 }
358
359 template < GUM_Numeric GUM_SCALAR >
363
364 template < GUM_Numeric GUM_SCALAR >
366 return "aggregator decomposition";
367 }
368
369} /* namespace gum */
Headers of AggregatorDecomposition.
static void _resetBounds_(const std::string &aggType, long &minVal, long &maxVal)
bounds of a RANGE intermediate aggregator without parent: the identity element of aggType (+infinity ...
Set< NodeId > addDepthLayer_(BayesNet< GUM_SCALAR > &bn, Set< NodeId > nodes, NodeId initialAggregator, int &j)
BayesNet< GUM_SCALAR > & decomposeAggregator_(BayesNet< GUM_SCALAR > &bn, NodeId node)
static void _accumulateBounds_(const std::string &aggType, const DiscreteVariable &v, long &minVal, long &maxVal)
updates the bounds of a RANGE intermediate aggregator with those of a new (numerical) parent v,...
BayesNet< GUM_SCALAR > & getDecomposedAggregator(BayesNet< GUM_SCALAR > &bn)
Default constructor.
NodeId addAggregator_(BayesNet< GUM_SCALAR > &bn, std::string_view aggType, const DiscreteVariable &var, Idx value)
Base class for discrete random variable.
virtual double numerical(Idx indice) const =0
get a numerical representation of the indice-th value.
bool isNumerical() const
virtual Size domainSize() const =0
Generic doubly linked lists.
Definition list.h:378
Val & push_back(Args &&... args)
An alias for pushBack used for STL compliance.
Definition list_tpl.h:1494
Exception : operation not allowed.
Defines a discrete random variable over an integer interval.
Size size() const noexcept
Returns the number of elements in the set.
Definition set_tpl.h:607
#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
Size NodeId
Type for node ids.
gum is the global namespace for all aGrUM entities
Definition agrum.h:46