aGrUM 3.1.1
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 return bn._addAggregator_(aggType, var, value);
88 }

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

227 {
228 auto p = static_cast< const gum::aggregator::MultiDimAggregator< GUM_SCALAR >* >(
230
232 std::string aggType = p->aggregatorName();
233
234 if (nodes.size() <= arity) {
235 return nodes;
236 } else {
237 auto newAgg = bn.variable(initialAggregator).clone();
238
239 auto newAggs = Set< NodeId >();
240
242
244
245 for (const auto& elt: nodes) {
246 orderedParents.push_back(elt);
247 }
248
249 orderedParents.sort();
250
251 gum::Size i = 0;
252 gum::Size q = 0;
253 long minVal = 0;
254 long maxVal = 0;
255
256 j++;
257
259 = std::string(bn.variable(initialAggregator).name()) + "_" + std::to_string(j);
260
261 newAgg->setName(newName);
262 newAgg->setDescription(aggType);
263
264 // for(Set<NodeId>::iterator it = nodes.begin(); it!= nodes.end(); ++it){
265 // for (auto it = orderedParents.begin(); it != orderedParents.end(); ++it) {
266 for (const auto parent: orderedParents) {
267 if (q < nodes.size() - nodes.size() % arity) {
268 if (i == arity) {
269 i = 0;
270 j++;
271
272 if (newAgg->varType() == VarType::LABELIZED) {
273 addAggregator_(bn, aggType, *newAgg, p->domainSize());
274 } else if (newAgg->varType() == VarType::RANGE) {
275 static_cast< RangeVariable* >(newAgg)->setMinVal(minVal);
276 static_cast< RangeVariable* >(newAgg)->setMaxVal(maxVal);
278 } else {
279 GUM_ERROR(OperationNotAllowed, "Decomposition is not available for type : " + aggType)
280 }
281
282 for (NodeId node: newAggParents) {
283 bn.addArc(node, bn.idFromName(newName));
284 }
285
286 newAggs.insert(bn.idFromName(newName));
287
288 newAggParents.clear();
289
290 minVal = 0;
291 maxVal = 0;
292
294
295 delete newAgg;
296 newAgg = bn.variable(initialAggregator).clone();
297 newAgg->setName(newName);
298 newAgg->setDescription(aggType);
299
300 if (bn.variable(parent).varType() == VarType::RANGE) {
301 minVal += static_cast< const RangeVariable& >(bn.variable(parent)).minVal();
302 maxVal += static_cast< const RangeVariable& >(bn.variable(parent)).maxVal();
303 }
304
305 newAggParents.push_back(parent);
306 i++;
307 } else {
308 if (bn.variable(parent).varType() == VarType::RANGE) {
309 minVal += static_cast< const RangeVariable& >(bn.variable(parent)).minVal();
310 maxVal += static_cast< const RangeVariable& >(bn.variable(parent)).maxVal();
311 }
312
313 newAggParents.push_back(parent);
314 i++;
315 }
316 } else {
317 newAggs.insert(parent);
318 }
319 q++;
320 }
321
322 if (newAgg->varType() == VarType::LABELIZED) {
323 addAggregator_(bn, aggType, *newAgg, p->domainSize());
324 } else if (newAgg->varType() == VarType::RANGE) {
325 static_cast< RangeVariable* >(newAgg)->setMinVal(minVal);
326 static_cast< RangeVariable* >(newAgg)->setMaxVal(maxVal);
328 } else {
329 GUM_ERROR(OperationNotAllowed, "Decomposition is not available for type : " + aggType)
330 }
331
332 newAggs.insert(bn.idFromName(newName));
333
334 for (NodeId node: newAggParents) {
335 bn.addArc(node, bn.idFromName(newName));
336 }
337
338 delete newAgg;
340 }
341 }
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)
#define GUM_ERROR(type, msg)
Definition exceptions.h:76

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

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

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

350 {
351 return _arity_;
352 }

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

355 {
356 return "aggregator decomposition";
357 }

◆ setMaximumArity()

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

Definition at line 344 of file aggregatorDecomposition_tpl.h.

344 {
345 if (arity < 2) { GUM_ERROR(OperationNotAllowed, "Maximum arity should be at least 2") }
346 _arity_ = arity;
347 }

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: