aGrUM  0.21.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. More...
 
virtual ~AggregatorDecomposition ()
 Destructor. More...
 
BayesNet< GUM_SCALAR > & getDecomposedAggregator (BayesNet< GUM_SCALAR > &bn)
 Default constructor. More...
 
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 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)
 

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 42 of file aggregatorDecomposition.h.

Constructor & Destructor Documentation

◆ AggregatorDecomposition()

template<typename GUM_SCALAR >
INLINE gum::AggregatorDecomposition< GUM_SCALAR >::AggregatorDecomposition ( )

Default constructor.

Definition at line 36 of file aggregatorDecomposition_tpl.h.

References gum::Set< Key, Alloc >::emplace().

36  {
37  _arity_ = 2;
38  GUM_CONSTRUCTOR(AggregatorDecomposition);
39  }
AggregatorDecomposition()
Default constructor.
+ Here is the call graph for this function:

◆ ~AggregatorDecomposition()

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

Destructor.

Definition at line 42 of file aggregatorDecomposition_tpl.h.

References gum::Set< Key, Alloc >::emplace().

42  {
43  GUM_DESTRUCTOR(AggregatorDecomposition);
44  }
AggregatorDecomposition()
Default constructor.
+ 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,
std::string  aggType,
const DiscreteVariable var,
Idx  value 
)
protected

Definition at line 59 of file aggregatorDecomposition_tpl.h.

References gum::Set< Key, Alloc >::emplace().

62  {
63  if (toLower(aggType) == "min") {
64  return bn.addMIN(var);
65  } else if (toLower(aggType) == "max") {
66  return bn.addMAX(var);
67  } else if (toLower(aggType) == "count") {
68  return bn.addCOUNT(var, value);
69  } else if (toLower(aggType) == "exists") {
70  return bn.addEXISTS(var, value);
71  } else if (toLower(aggType) == "or") {
72  return bn.addOR(var);
73  } else if (toLower(aggType) == "and") {
74  return bn.addAND(var);
75  } else if (toLower(aggType) == "forall") {
76  return bn.addFORALL(var);
77  } else if (toLower(aggType) == "amplitude") {
78  return bn.addAMPLITUDE(var);
79  } else if (toLower(aggType) == "median") {
80  return bn.addMEDIAN(var);
81  } else if (toLower(aggType) == "sum") {
82  return bn.addSUM(var);
83  } else {
84  std::string msg = "Unknown aggregate: ";
85  msg.append(aggType);
86  GUM_ERROR(NotFound, msg)
87  }
88  }
std::string toLower(std::string str)
Returns the lowercase version of str.
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51
+ Here is the call 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 225 of file aggregatorDecomposition_tpl.h.

References gum::Set< Key, Alloc >::emplace().

228  {
229  auto p = static_cast< const gum::aggregator::MultiDimAggregator< GUM_SCALAR >* >(
230  bn.cpt(initialAggregator).content());
231 
232  gum::Size arity = getMaximumArity();
233  std::string aggType = p->aggregatorName();
234 
235  if (nodes.size() <= arity) {
236  return nodes;
237  } else {
238  auto newAgg = bn.variable(initialAggregator).clone();
239 
240  Set< NodeId > newAggs = Set< NodeId >();
241 
242  List< NodeId > newAggParents;
243 
244  std::list< NodeId > orderedParents = {};
245 
246  for (const auto& elt: nodes) {
247  orderedParents.push_back(elt);
248  }
249 
250  orderedParents.sort();
251 
252  gum::Size i = 0;
253  gum::Size q = 0;
254  long minVal = 0;
255  long maxVal = 0;
256 
257  j++;
258 
259  std::string newName
260  = std::string(bn.variable(initialAggregator).name()) + "_" + std::to_string(j);
261 
262  newAgg->setName(newName);
263  newAgg->setDescription(aggType);
264 
265  // for(Set<NodeId>::iterator it = nodes.begin(); it!= nodes.end(); ++it){
266  for (auto it = orderedParents.begin(); it != orderedParents.end(); ++it) {
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);
277  addAggregator_(bn, aggType, *newAgg, 0);
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 
293  newName = std::string(bn.variable(initialAggregator).name()) + "_" + std::to_string(j);
294 
295  delete (newAgg);
296  newAgg = bn.variable(initialAggregator).clone();
297  newAgg->setName(newName);
298  newAgg->setDescription(aggType);
299 
300  if (bn.variable(*it).varType() == VarType::Range) {
301  minVal += static_cast< const RangeVariable& >(bn.variable(*it)).minVal();
302  maxVal += static_cast< const RangeVariable& >(bn.variable(*it)).maxVal();
303  }
304 
305  newAggParents.push_back(*it);
306  i++;
307  } else {
308  if (bn.variable(*it).varType() == VarType::Range) {
309  minVal += static_cast< const RangeVariable& >(bn.variable(*it)).minVal();
310  maxVal += static_cast< const RangeVariable& >(bn.variable(*it)).maxVal();
311  }
312 
313  newAggParents.push_back(*it);
314  i++;
315  }
316  } else {
317  newAggs.insert(*it);
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);
327  addAggregator_(bn, aggType, *newAgg, 0);
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);
339  return addDepthLayer_(bn, newAggs, initialAggregator, j);
340  }
341  }
<agrum/tools/multidim/aggregators/multiDimAggregator.h>
std::string to_string(const Formula &f)
Definition: formula_inl.h:474
Set< NodeId > addDepthLayer_(BayesNet< GUM_SCALAR > &bn, Set< NodeId > nodes, NodeId initialAggregator, int &j)
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:47
Size size() const noexcept
Returns the number of elements in the set.
Definition: set_tpl.h:694
Size NodeId
Type for node ids.
Definition: graphElements.h:97
void insert(const Key &k)
Inserts a new element into the set.
Definition: set_tpl.h:606
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51
NodeId addAggregator_(BayesNet< GUM_SCALAR > &bn, std::string aggType, const DiscreteVariable &var, Idx value)
+ Here is the call 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 92 of file aggregatorDecomposition_tpl.h.

References gum::Set< Key, Alloc >::emplace().

93  {
94  auto p = static_cast< const gum::aggregator::MultiDimAggregator< GUM_SCALAR >* >(
95  bn.cpt(initialAggregator).content());
96  auto newAgg = bn.variable(initialAggregator).clone();
97 
98  Set< NodeId > parents = bn.parents(initialAggregator);
99 
100  std::list< NodeId > orderedParents = {};
101 
102  for (const auto& elt: parents) {
103  orderedParents.push_back(elt);
104  }
105 
106  orderedParents.sort();
107 
108  Set< NodeId > newAggs = Set< NodeId >();
109  List< NodeId > newAggParents;
110 
111  gum::Size arity = getMaximumArity();
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 
120  std::string newName
121  = std::string(bn.variable(initialAggregator).name()) + "_" + std::to_string(j);
122  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  if (q < parents.size() - parents.size() % arity) {
137  if (i == arity) {
138  i = 0;
139  j++;
140 
141  if (newAgg->varType() == VarType::Labelized) {
142  addAggregator_(bn, aggType, *newAgg, p->domainSize());
143  } else if (newAgg->varType() == VarType::Range) {
144  static_cast< RangeVariable* >(newAgg)->setMinVal(minVal);
145  static_cast< RangeVariable* >(newAgg)->setMaxVal(maxVal);
146  addAggregator_(bn, aggType, *newAgg, 0);
147  } else {
148  GUM_ERROR(OperationNotAllowed, "Decomposition is not available for type : " + aggType)
149  }
150 
151  /*
152  * Adding arcs in the new node from its parents and adding thoses into
153  * the temporary potential
154  */
155  for (NodeId node: newAggParents) {
156  bn.addArc(node, bn.idFromName(newName));
157  }
158 
159  /*
160  * Adding the new aggregator in t
161  */
162  newAggs.insert(bn.idFromName(newName));
163 
164  newAggParents.clear();
165 
166  minVal = 0;
167  maxVal = 0;
168 
169  newName = std::string(bn.variable(initialAggregator).name()) + "_" + std::to_string(j);
170 
171  delete (newAgg);
172  newAgg = bn.variable(initialAggregator).clone();
173  newAgg->setName(newName);
174  newAgg->setDescription(aggType);
175 
176  if (bn.variable(*it).varType() == VarType::Range) {
177  minVal += static_cast< const RangeVariable& >(bn.variable(*it)).minVal();
178  maxVal += static_cast< const RangeVariable& >(bn.variable(*it)).maxVal();
179  }
180 
181  newAggParents.push_back(*it);
182  i++;
183  } else {
184  if (bn.variable(*it).varType() == VarType::Range) {
185  minVal += static_cast< const RangeVariable& >(bn.variable(*it)).minVal();
186  maxVal += static_cast< const RangeVariable& >(bn.variable(*it)).maxVal();
187  }
188 
189  newAggParents.push_back(*it);
190  i++;
191  }
192  } else {
193  newAggs.insert(*it);
194  }
195  q++;
196  }
197 
198  if (newAgg->varType() == VarType::Labelized) {
199  addAggregator_(bn, aggType, *newAgg, p->domainSize());
200  } else if (newAgg->varType() == VarType::Range) {
201  static_cast< RangeVariable* >(newAgg)->setMinVal(minVal);
202  static_cast< RangeVariable* >(newAgg)->setMaxVal(maxVal);
203  addAggregator_(bn, aggType, *newAgg, 0);
204  } else {
205  GUM_ERROR(OperationNotAllowed, "Decomposition is not available for type : " + aggType)
206  }
207 
208  newAggs.insert(bn.idFromName(newName));
209 
210  for (NodeId node: newAggParents) {
211  bn.addArc(node, bn.idFromName(newName));
212  }
213 
214  Set< NodeId > final = addDepthLayer_(bn, newAggs, initialAggregator, j);
215 
216  for (auto agg: final) {
217  bn.addArc(agg, initialAggregator);
218  }
219 
220  delete (newAgg);
221  return bn;
222  }
virtual DiscreteVariable * clone() const =0
Copy Factory.
const DiscreteVariable & variable(Idx i) const override
Returns a const ref to the ith var.
<agrum/tools/multidim/aggregators/multiDimAggregator.h>
std::string to_string(const Formula &f)
Definition: formula_inl.h:474
Set< NodeId > addDepthLayer_(BayesNet< GUM_SCALAR > &bn, Set< NodeId > nodes, NodeId initialAggregator, int &j)
virtual Size domainSize() const override
Returns the product of the variables domain size.
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:47
Size NodeId
Type for node ids.
Definition: graphElements.h:97
void insert(const Key &k)
Inserts a new element into the set.
Definition: set_tpl.h:606
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51
NodeId addAggregator_(BayesNet< GUM_SCALAR > &bn, std::string aggType, const DiscreteVariable &var, Idx value)
+ Here is the call 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 48 of file aggregatorDecomposition_tpl.h.

References gum::Set< Key, Alloc >::emplace().

48  {
49  for (NodeId node: bn.nodes().asNodeSet()) {
50  std::string description = bn.cpt(node).toString();
51  auto p = dynamic_cast< const gum::aggregator::MultiDimAggregator< GUM_SCALAR >* >(
52  bn.cpt(node).content());
53  if (p != nullptr && p->isDecomposable()) { decomposeAggregator_(bn, node); }
54  }
55  return bn;
56  }
<agrum/tools/multidim/aggregators/multiDimAggregator.h>
BayesNet< GUM_SCALAR > & decomposeAggregator_(BayesNet< GUM_SCALAR > &bn, NodeId node)
Size NodeId
Type for node ids.
Definition: graphElements.h:97
+ Here is the call graph for this function:

◆ getMaximumArity()

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

Definition at line 351 of file aggregatorDecomposition_tpl.h.

References gum::Set< Key, Alloc >::emplace().

351  {
352  return _arity_;
353  }
+ Here is the call graph for this function:

◆ name()

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

Definition at line 356 of file aggregatorDecomposition_tpl.h.

356  {
357  return "aggregator decomposition";
358  }

◆ setMaximumArity()

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

Definition at line 345 of file aggregatorDecomposition_tpl.h.

References gum::Set< Key, Alloc >::emplace().

345  {
346  if (arity < 2) { GUM_ERROR(OperationNotAllowed, "Maximum arity should be at least 2") }
347  _arity_ = arity;
348  }
#define GUM_ERROR(type, msg)
Definition: exceptions.h:51
+ Here is the call graph for this function:

Member Data Documentation

◆ _arity_

template<typename GUM_SCALAR >
gum::Size gum::AggregatorDecomposition< GUM_SCALAR >::_arity_
private

Definition at line 89 of file aggregatorDecomposition.h.


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