aGrUM 3.0.0
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< NodeIdaddDepthLayer_ (BayesNet< GUM_SCALAR > &bn, Set< NodeId > nodes, NodeId initialAggregator, int &j)

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 59 of file aggregatorDecomposition_tpl.h.

59 {
61 }
<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 64 of file aggregatorDecomposition_tpl.h.

References AggregatorDecomposition().

Here is the call graph for this function:

Member Function Documentation

◆ 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 83 of file aggregatorDecomposition_tpl.h.

86 {
87 const auto aggLower = toLower(aggType);
88 if (aggLower == "min") {
89 return bn.addMIN(var);
90 } else if (aggLower == "max") {
91 return bn.addMAX(var);
92 } else if (aggLower == "count") {
93 return bn.addCOUNT(var, value);
94 } else if (aggLower == "exists") {
95 return bn.addEXISTS(var, value);
96 } else if (aggLower == "or") {
97 return bn.addOR(var);
98 } else if (aggLower == "and") {
99 return bn.addAND(var);
100 } else if (aggLower == "forall") {
101 return bn.addFORALL(var);
102 } else if (aggLower == "amplitude") {
103 return bn.addAMPLITUDE(var);
104 } else if (aggLower == "median") {
105 return bn.addMEDIAN(var);
106 } else if (aggLower == "sum") {
107 return bn.addSUM(var);
108 } else {
109 std::string msg = "Unknown aggregate: ";
110 msg.append(aggType);
112 }
113 }
#define GUM_ERROR(type, msg)
Definition exceptions.h:76
std::string toLower(std::string_view str)
Returns the lowercase version of str.

References GUM_ERROR, and gum::toLower().

Referenced by addDepthLayer_(), and decomposeAggregator_().

Here is the call graph for this function:
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 249 of file aggregatorDecomposition_tpl.h.

252 {
253 auto p = static_cast< const gum::aggregator::MultiDimAggregator< GUM_SCALAR >* >(
255
257 std::string aggType = p->aggregatorName();
258
259 if (nodes.size() <= arity) {
260 return nodes;
261 } else {
262 auto newAgg = bn.variable(initialAggregator).clone();
263
264 auto newAggs = Set< NodeId >();
265
267
269
270 for (const auto& elt: nodes) {
271 orderedParents.push_back(elt);
272 }
273
274 orderedParents.sort();
275
276 gum::Size i = 0;
277 gum::Size q = 0;
278 long minVal = 0;
279 long maxVal = 0;
280
281 j++;
282
284 = std::string(bn.variable(initialAggregator).name()) + "_" + std::to_string(j);
285
286 newAgg->setName(newName);
287 newAgg->setDescription(aggType);
288
289 // for(Set<NodeId>::iterator it = nodes.begin(); it!= nodes.end(); ++it){
290 // for (auto it = orderedParents.begin(); it != orderedParents.end(); ++it) {
291 for (const auto parent: orderedParents) {
292 if (q < nodes.size() - nodes.size() % arity) {
293 if (i == arity) {
294 i = 0;
295 j++;
296
297 if (newAgg->varType() == VarType::LABELIZED) {
298 addAggregator_(bn, aggType, *newAgg, p->domainSize());
299 } else if (newAgg->varType() == VarType::RANGE) {
300 static_cast< RangeVariable* >(newAgg)->setMinVal(minVal);
301 static_cast< RangeVariable* >(newAgg)->setMaxVal(maxVal);
303 } else {
304 GUM_ERROR(OperationNotAllowed, "Decomposition is not available for type : " + aggType)
305 }
306
307 for (NodeId node: newAggParents) {
308 bn.addArc(node, bn.idFromName(newName));
309 }
310
311 newAggs.insert(bn.idFromName(newName));
312
313 newAggParents.clear();
314
315 minVal = 0;
316 maxVal = 0;
317
319
320 delete newAgg;
321 newAgg = bn.variable(initialAggregator).clone();
322 newAgg->setName(newName);
323 newAgg->setDescription(aggType);
324
325 if (bn.variable(parent).varType() == VarType::RANGE) {
326 minVal += static_cast< const RangeVariable& >(bn.variable(parent)).minVal();
327 maxVal += static_cast< const RangeVariable& >(bn.variable(parent)).maxVal();
328 }
329
330 newAggParents.push_back(parent);
331 i++;
332 } else {
333 if (bn.variable(parent).varType() == VarType::RANGE) {
334 minVal += static_cast< const RangeVariable& >(bn.variable(parent)).minVal();
335 maxVal += static_cast< const RangeVariable& >(bn.variable(parent)).maxVal();
336 }
337
338 newAggParents.push_back(parent);
339 i++;
340 }
341 } else {
342 newAggs.insert(parent);
343 }
344 q++;
345 }
346
347 if (newAgg->varType() == VarType::LABELIZED) {
348 addAggregator_(bn, aggType, *newAgg, p->domainSize());
349 } else if (newAgg->varType() == VarType::RANGE) {
350 static_cast< RangeVariable* >(newAgg)->setMinVal(minVal);
351 static_cast< RangeVariable* >(newAgg)->setMaxVal(maxVal);
353 } else {
354 GUM_ERROR(OperationNotAllowed, "Decomposition is not available for type : " + aggType)
355 }
356
357 newAggs.insert(bn.idFromName(newName));
358
359 for (NodeId node: newAggParents) {
360 bn.addArc(node, bn.idFromName(newName));
361 }
362
363 delete newAgg;
365 }
366 }
Set< NodeId > addDepthLayer_(BayesNet< GUM_SCALAR > &bn, Set< NodeId > nodes, NodeId initialAggregator, int &j)
NodeId addAggregator_(BayesNet< GUM_SCALAR > &bn, std::string_view aggType, const DiscreteVariable &var, Idx value)

References addAggregator_(), addDepthLayer_(), getMaximumArity(), GUM_ERROR, gum::LABELIZED, gum::RangeVariable::maxVal(), gum::RangeVariable::minVal(), 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 117 of file aggregatorDecomposition_tpl.h.

118 {
119 auto p = static_cast< const gum::aggregator::MultiDimAggregator< GUM_SCALAR >* >(
121 auto newAgg = bn.variable(initialAggregator).clone();
122
124
126
127 for (const auto& elt: parents) {
128 orderedParents.push_back(elt);
129 }
130
131 orderedParents.sort();
132
133 auto newAggs = Set< NodeId >();
135
137 gum::Size q = 0;
138 gum::Size i = 0;
139
140 long minVal = 0;
141 long maxVal = 0;
142
143 int j = 1;
144
146 = std::string(bn.variable(initialAggregator).name()) + "_" + std::to_string(j);
147 const std::string aggType = p->aggregatorName();
148
149 for (auto parent: parents) {
150 bn.eraseArc(parent, initialAggregator);
151 }
152
153 /*
154 * We are constructing the new aggregator with a clone of the former
155 */
156 newAgg->setName(newName);
157 newAgg->setDescription(aggType);
158
159 // for(Set<NodeId>::iterator it = parents.begin(); it!= parents.end(); ++it){
160 // for (auto it = orderedParents.begin(); it != orderedParents.end(); ++it) {
161 for (const auto& parent: orderedParents) {
162 if (q < parents.size() - parents.size() % arity) {
163 if (i == arity) {
164 i = 0;
165 j++;
166
167 if (newAgg->varType() == VarType::LABELIZED) {
168 addAggregator_(bn, aggType, *newAgg, p->domainSize());
169 } else if (newAgg->varType() == VarType::RANGE) {
170 static_cast< RangeVariable* >(newAgg)->setMinVal(minVal);
171 static_cast< RangeVariable* >(newAgg)->setMaxVal(maxVal);
173 } else {
174 GUM_ERROR(OperationNotAllowed, "Decomposition is not available for type : " + aggType)
175 }
176
177 /*
178 * Adding arcs in the new node from its parents and adding thoses into
179 * the temporary tensor
180 */
181 for (NodeId node: newAggParents) {
182 bn.addArc(node, bn.idFromName(newName));
183 }
184
185 /*
186 * Adding the new aggregator in t
187 */
188 newAggs.insert(bn.idFromName(newName));
189
190 newAggParents.clear();
191
192 minVal = 0;
193 maxVal = 0;
194
196
197 delete (newAgg);
198 newAgg = bn.variable(initialAggregator).clone();
199 newAgg->setName(newName);
200 newAgg->setDescription(aggType);
201
202 if (bn.variable(parent).varType() == VarType::RANGE) {
203 minVal += static_cast< const RangeVariable& >(bn.variable(parent)).minVal();
204 maxVal += static_cast< const RangeVariable& >(bn.variable(parent)).maxVal();
205 }
206
207 newAggParents.push_back(parent);
208 i++;
209 } else {
210 if (bn.variable(parent).varType() == VarType::RANGE) {
211 minVal += static_cast< const RangeVariable& >(bn.variable(parent)).minVal();
212 maxVal += static_cast< const RangeVariable& >(bn.variable(parent)).maxVal();
213 }
214
215 newAggParents.push_back(parent);
216 i++;
217 }
218 } else {
219 newAggs.insert(parent);
220 }
221 q++;
222 }
223
224 if (newAgg->varType() == VarType::LABELIZED) {
225 addAggregator_(bn, aggType, *newAgg, p->domainSize());
226 } else if (newAgg->varType() == VarType::RANGE) {
227 static_cast< RangeVariable* >(newAgg)->setMinVal(minVal);
228 static_cast< RangeVariable* >(newAgg)->setMaxVal(maxVal);
230 } else {
231 GUM_ERROR(OperationNotAllowed, "Decomposition is not available for type : " + aggType)
232 }
233
234 newAggs.insert(bn.idFromName(newName));
235
236 for (NodeId node: newAggParents) {
237 bn.addArc(node, bn.idFromName(newName));
238 }
239
241 bn.addArc(agg, initialAggregator);
242 }
243
244 delete (newAgg);
245 return bn;
246 }

References addAggregator_(), addDepthLayer_(), getMaximumArity(), GUM_ERROR, gum::LABELIZED, gum::RangeVariable::maxVal(), gum::RangeVariable::minVal(), 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 70 of file aggregatorDecomposition_tpl.h.

70 {
71 for (NodeId node: bn.nodes().asNodeSet()) {
72 std::string description = bn.cpt(node).toString();
73 if (auto p = dynamic_cast< const gum::aggregator::MultiDimAggregator< GUM_SCALAR >* >(
74 bn.cpt(node).content());
75 p != nullptr && p->isDecomposable()) {
77 }
78 }
79 return bn;
80 }
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 375 of file aggregatorDecomposition_tpl.h.

375 {
376 return _arity_;
377 }

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 380 of file aggregatorDecomposition_tpl.h.

380 {
381 return "aggregator decomposition";
382 }

◆ setMaximumArity()

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

Definition at line 369 of file aggregatorDecomposition_tpl.h.

369 {
370 if (arity < 2) { GUM_ERROR(OperationNotAllowed, "Maximum arity should be at least 2") }
371 _arity_ = arity;
372 }

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: