aGrUM 2.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, const std::string &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<typename 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<typename GUM_SCALAR>
INLINE gum::AggregatorDecomposition< GUM_SCALAR >::AggregatorDecomposition ( )

Default constructor.

Definition at line 58 of file aggregatorDecomposition_tpl.h.

58 {
60 }
<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<typename GUM_SCALAR>
gum::AggregatorDecomposition< GUM_SCALAR >::~AggregatorDecomposition ( )
virtual

Destructor.

Definition at line 63 of file aggregatorDecomposition_tpl.h.

References AggregatorDecomposition().

Here is the call graph for this function:

Member Function Documentation

◆ addAggregator_()

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

Definition at line 80 of file aggregatorDecomposition_tpl.h.

83 {
84 if (toLower(aggType) == "min") {
85 return bn.addMIN(var);
86 } else if (toLower(aggType) == "max") {
87 return bn.addMAX(var);
88 } else if (toLower(aggType) == "count") {
89 return bn.addCOUNT(var, value);
90 } else if (toLower(aggType) == "exists") {
91 return bn.addEXISTS(var, value);
92 } else if (toLower(aggType) == "or") {
93 return bn.addOR(var);
94 } else if (toLower(aggType) == "and") {
95 return bn.addAND(var);
96 } else if (toLower(aggType) == "forall") {
97 return bn.addFORALL(var);
98 } else if (toLower(aggType) == "amplitude") {
99 return bn.addAMPLITUDE(var);
100 } else if (toLower(aggType) == "median") {
101 return bn.addMEDIAN(var);
102 } else if (toLower(aggType) == "sum") {
103 return bn.addSUM(var);
104 } else {
105 std::string msg = "Unknown aggregate: ";
106 msg.append(aggType);
108 }
109 }
#define GUM_ERROR(type, msg)
Definition exceptions.h:72
std::string toLower(std::string 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<typename GUM_SCALAR>
Set< NodeId > gum::AggregatorDecomposition< GUM_SCALAR >::addDepthLayer_ ( BayesNet< GUM_SCALAR > & bn,
Set< NodeId > nodes,
NodeId initialAggregator,
int & j )
protected

Definition at line 245 of file aggregatorDecomposition_tpl.h.

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

Definition at line 113 of file aggregatorDecomposition_tpl.h.

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

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<typename GUM_SCALAR>
BayesNet< GUM_SCALAR > & gum::AggregatorDecomposition< GUM_SCALAR >::getDecomposedAggregator ( BayesNet< GUM_SCALAR > & bn)

Default constructor.

Definition at line 69 of file aggregatorDecomposition_tpl.h.

69 {
70 for (NodeId node: bn.nodes().asNodeSet()) {
71 std::string description = bn.cpt(node).toString();
72 auto p = dynamic_cast< const gum::aggregator::MultiDimAggregator< GUM_SCALAR >* >(
73 bn.cpt(node).content());
74 if (p != nullptr && p->isDecomposable()) { decomposeAggregator_(bn, node); }
75 }
76 return bn;
77 }
BayesNet< GUM_SCALAR > & decomposeAggregator_(BayesNet< GUM_SCALAR > &bn, NodeId node)

References decomposeAggregator_().

Here is the call graph for this function:

◆ getMaximumArity()

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

Definition at line 371 of file aggregatorDecomposition_tpl.h.

371 {
372 return _arity_;
373 }

References _arity_.

Referenced by addDepthLayer_(), and decomposeAggregator_().

Here is the caller graph for this function:

◆ name()

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

Definition at line 376 of file aggregatorDecomposition_tpl.h.

376 {
377 return "aggregator decomposition";
378 }

◆ setMaximumArity()

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

Definition at line 365 of file aggregatorDecomposition_tpl.h.

365 {
366 if (arity < 2) { GUM_ERROR(OperationNotAllowed, "Maximum arity should be at least 2") }
367 _arity_ = arity;
368 }

References _arity_, and GUM_ERROR.

Member Data Documentation

◆ _arity_

template<typename 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: