aGrUM 3.3.2
a C++ library for (probabilistic) graphical models
gum::AggregatorDecomposition< GUM_SCALAR > Class Template Reference

<agrum/BN/inference/tools/aggregatorDecomposition.h> More...

#include <aggregatorDecomposition.h>

Public Member Functions

Constructor & destructor.
 AggregatorDecomposition ()
 Default constructor.
virtual ~AggregatorDecomposition ()
 Destructor.
BayesNet< GUM_SCALAR > & getDecomposedAggregator (BayesNet< GUM_SCALAR > &bn)
 Default constructor.
Getters & setters.
virtual std::string name () const
gum::Size getMaximumArity ()
void setMaximumArity (gum::Size arity)

Protected Member Functions

Private evidence handling methods and members.
NodeId addAggregator_ (BayesNet< GUM_SCALAR > &bn, std::string_view aggType, const DiscreteVariable &var, Idx value)
BayesNet< GUM_SCALAR > & decomposeAggregator_ (BayesNet< GUM_SCALAR > &bn, NodeId node)
Set< NodeId > addDepthLayer_ (BayesNet< GUM_SCALAR > &bn, Set< NodeId > nodes, NodeId initialAggregator, int &j)

Static Private Member Functions

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 for min, -infinity for max, 0 otherwise)
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, according to aggType (min/max of the bounds for min/max, sum of the bounds otherwise)

Private Attributes

Size _arity_ = Size(2)

Detailed Description

template<GUM_Numeric GUM_SCALAR>
class gum::AggregatorDecomposition< GUM_SCALAR >

<agrum/BN/inference/tools/aggregatorDecomposition.h>

This class is used to decompose aggregator in PRM<GUM_SCALAR>.

Definition at line 63 of file aggregatorDecomposition.h.

Constructor & Destructor Documentation

◆ AggregatorDecomposition()

template<GUM_Numeric GUM_SCALAR>
gum::AggregatorDecomposition< GUM_SCALAR >::AggregatorDecomposition ( )

Default constructor.

Definition at line 93 of file aggregatorDecomposition_tpl.h.

93 {
95 }
<agrum/BN/inference/tools/aggregatorDecomposition.h>

References AggregatorDecomposition().

Referenced by AggregatorDecomposition(), and ~AggregatorDecomposition().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ ~AggregatorDecomposition()

template<GUM_Numeric GUM_SCALAR>
gum::AggregatorDecomposition< GUM_SCALAR >::~AggregatorDecomposition ( )
virtual

Destructor.

Definition at line 98 of file aggregatorDecomposition_tpl.h.

References AggregatorDecomposition().

Here is the call graph for this function:

Member Function Documentation

◆ _accumulateBounds_()

template<GUM_Numeric GUM_SCALAR>
void gum::AggregatorDecomposition< GUM_SCALAR >::_accumulateBounds_ ( const std::string & aggType,
const DiscreteVariable & v,
long & minVal,
long & maxVal )
staticprivate

updates the bounds of a RANGE intermediate aggregator with those of a new (numerical) parent v, according to aggType (min/max of the bounds for min/max, sum of the bounds otherwise)

Definition at line 71 of file aggregatorDecomposition_tpl.h.

74 {
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") {
83 } else if (aggType == "max") {
86 } else {
87 minVal += lo;
88 maxVal += hi;
89 }
90 }

References gum::DiscreteVariable::domainSize(), gum::DiscreteVariable::isNumerical(), and gum::DiscreteVariable::numerical().

Referenced by addDepthLayer_(), and decomposeAggregator_().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ _resetBounds_()

template<GUM_Numeric GUM_SCALAR>
void gum::AggregatorDecomposition< GUM_SCALAR >::_resetBounds_ ( const std::string & aggType,
long & minVal,
long & maxVal )
staticprivate

bounds of a RANGE intermediate aggregator without parent: the identity element of aggType (+infinity for min, -infinity for max, 0 otherwise)

Definition at line 62 of file aggregatorDecomposition_tpl.h.

64 {
67 else minVal = maxVal = 0;
68 }

Referenced by addDepthLayer_(), and decomposeAggregator_().

Here is the caller graph for this function:

◆ addAggregator_()

template<GUM_Numeric GUM_SCALAR>
NodeId gum::AggregatorDecomposition< GUM_SCALAR >::addAggregator_ ( BayesNet< GUM_SCALAR > & bn,
std::string_view aggType,
const DiscreteVariable & var,
Idx value )
protected

Definition at line 117 of file aggregatorDecomposition_tpl.h.

120 {
121 return bn._addAggregator_(aggType, var, value);
122 }

Referenced by addDepthLayer_(), and decomposeAggregator_().

Here is the caller graph for this function:

◆ addDepthLayer_()

template<GUM_Numeric GUM_SCALAR>
Set< NodeId > gum::AggregatorDecomposition< GUM_SCALAR >::addDepthLayer_ ( BayesNet< GUM_SCALAR > & bn,
Set< NodeId > nodes,
NodeId initialAggregator,
int & j )
protected

Definition at line 246 of file aggregatorDecomposition_tpl.h.

249 {
250 auto p = static_cast< const gum::aggregator::MultiDimAggregator< GUM_SCALAR >* >(
252
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
264
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;
278
279 j++;
280
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);
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
314
316
317 delete newAgg;
318 newAgg = bn.variable(initialAggregator).clone();
319 newAgg->setName(newName);
320 newAgg->setDescription(aggType);
321 }
322
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);
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;
350 }
351 }
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)
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,...
NodeId addAggregator_(BayesNet< GUM_SCALAR > &bn, std::string_view aggType, const DiscreteVariable &var, Idx value)

References _accumulateBounds_(), _resetBounds_(), addAggregator_(), addDepthLayer_(), getMaximumArity(), GUM_ERROR, gum::LABELIZED, gum::List< Val >::push_back(), gum::RANGE, and gum::Set< Key >::size().

Referenced by addDepthLayer_(), and decomposeAggregator_().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ decomposeAggregator_()

template<GUM_Numeric GUM_SCALAR>
BayesNet< GUM_SCALAR > & gum::AggregatorDecomposition< GUM_SCALAR >::decomposeAggregator_ ( BayesNet< GUM_SCALAR > & bn,
NodeId node )
protected

Definition at line 126 of file aggregatorDecomposition_tpl.h.

127 {
128 auto p = static_cast< const gum::aggregator::MultiDimAggregator< GUM_SCALAR >* >(
130 auto newAgg = bn.variable(initialAggregator).clone();
131
133
135
136 for (const auto& elt: parents) {
137 orderedParents.push_back(elt);
138 }
139
140 orderedParents.sort();
141
142 auto newAggs = Set< NodeId >();
144
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
155 = std::string(bn.variable(initialAggregator).name()) + "_" + std::to_string(j);
156 const std::string aggType = p->aggregatorName();
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);
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
203
205
206 delete (newAgg);
207 newAgg = bn.variable(initialAggregator).clone();
208 newAgg->setName(newName);
209 newAgg->setDescription(aggType);
210 }
211
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);
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
238 bn.addArc(agg, initialAggregator);
239 }
240
241 delete (newAgg);
242 return bn;
243 }

References _accumulateBounds_(), _resetBounds_(), addAggregator_(), addDepthLayer_(), getMaximumArity(), GUM_ERROR, gum::LABELIZED, gum::List< Val >::push_back(), gum::RANGE, and gum::Set< Key >::size().

Referenced by getDecomposedAggregator().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ getDecomposedAggregator()

template<GUM_Numeric GUM_SCALAR>
BayesNet< GUM_SCALAR > & gum::AggregatorDecomposition< GUM_SCALAR >::getDecomposedAggregator ( BayesNet< GUM_SCALAR > & bn)

Default constructor.

Definition at line 104 of file aggregatorDecomposition_tpl.h.

104 {
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()) {
111 }
112 }
113 return bn;
114 }
BayesNet< GUM_SCALAR > & decomposeAggregator_(BayesNet< GUM_SCALAR > &bn, NodeId node)

References decomposeAggregator_(), and gum::aggregator::MultiDimAggregator< GUM_ELEMENT >::isDecomposable().

Here is the call graph for this function:

◆ getMaximumArity()

template<GUM_Numeric GUM_SCALAR>
gum::Size gum::AggregatorDecomposition< GUM_SCALAR >::getMaximumArity ( )

Definition at line 360 of file aggregatorDecomposition_tpl.h.

360 {
361 return _arity_;
362 }

References _arity_.

Referenced by addDepthLayer_(), and decomposeAggregator_().

Here is the caller graph for this function:

◆ name()

template<GUM_Numeric GUM_SCALAR>
std::string gum::AggregatorDecomposition< GUM_SCALAR >::name ( ) const
virtual

Definition at line 365 of file aggregatorDecomposition_tpl.h.

365 {
366 return "aggregator decomposition";
367 }

◆ setMaximumArity()

template<GUM_Numeric GUM_SCALAR>
void gum::AggregatorDecomposition< GUM_SCALAR >::setMaximumArity ( gum::Size arity)

Definition at line 354 of file aggregatorDecomposition_tpl.h.

354 {
355 if (arity < 2) { GUM_ERROR(OperationNotAllowed, "Maximum arity should be at least 2") }
356 _arity_ = arity;
357 }

References _arity_, and GUM_ERROR.

Member Data Documentation

◆ _arity_

template<GUM_Numeric GUM_SCALAR>
Size gum::AggregatorDecomposition< GUM_SCALAR >::_arity_ = Size(2)
private

Definition at line 111 of file aggregatorDecomposition.h.

Referenced by getMaximumArity(), and setMaximumArity().


The documentation for this class was generated from the following files: