aGrUM  0.20.2
a C++ library for (probabilistic) graphical models
aggregatorDecomposition_tpl.h
Go to the documentation of this file.
1 /**
2  *
3  * Copyright 2005-2019 Pierre-Henri WUILLEMIN & Christophe GONZALES(@AMU)
4  * {prenom.nom}_at_lip6.fr
5  *
6  * This library is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this library. If not, see <http://www.gnu.org/licenses/>.
18  *
19  */
20 
21 
22 /**
23  * @file
24  * @brief Inline implementation of AggregatorDecomposition.
25  *
26  * @author Gaspard Ducamp
27  *
28  */
29 #include <agrum/BN/inference/tools/aggregatorDecomposition.h>
30 #include <typeinfo>
31 #include <list>
32 
33 namespace gum {
34 
35  template < typename GUM_SCALAR >
36  INLINE AggregatorDecomposition< GUM_SCALAR >::AggregatorDecomposition() {
37  arity__ = 2;
39  }
40 
41  template < typename GUM_SCALAR >
44  }
45 
46  template < typename GUM_SCALAR >
49  BayesNet< GUM_SCALAR >& bn) {
50  for (NodeId node: bn.nodes().asNodeSet()) {
52  auto p = dynamic_cast<
54  bn.cpt(node).content());
55  if (p != nullptr && p->isDecomposable()) { decomposeAggregator_(bn, node); }
56  }
57  return bn;
58  }
59 
60  template < typename GUM_SCALAR >
64  const DiscreteVariable& var,
65  Idx value) {
66  if (toLower(aggType) == "min") {
67  return bn.addMIN(var);
68  } else if (toLower(aggType) == "max") {
69  return bn.addMAX(var);
70  } else if (toLower(aggType) == "count") {
71  return bn.addCOUNT(var, value);
72  } else if (toLower(aggType) == "exists") {
73  return bn.addEXISTS(var, value);
74  } else if (toLower(aggType) == "or") {
75  return bn.addOR(var);
76  } else if (toLower(aggType) == "and") {
77  return bn.addAND(var);
78  } else if (toLower(aggType) == "forall") {
79  return bn.addFORALL(var);
80  } else if (toLower(aggType) == "amplitude") {
81  return bn.addAMPLITUDE(var);
82  } else if (toLower(aggType) == "median") {
83  return bn.addMEDIAN(var);
84  } else if (toLower(aggType) == "sum") {
85  return bn.addSUM(var);
86  } else {
87  std::string msg = "Unknown aggregate: ";
90  }
91  }
92 
93  template < typename GUM_SCALAR >
98  auto p
99  = static_cast< const gum::aggregator::MultiDimAggregator< GUM_SCALAR >* >(
102 
104 
105  std::list< NodeId > orderedParents = {};
106 
107  for (const auto& elt: parents) {
109  }
110 
112 
113  Set< NodeId > newAggs = Set< NodeId >();
115 
117  gum::Size q = 0;
118  gum::Size i = 0;
119 
120  long minVal = 0;
121  long maxVal = 0;
122 
123  int j = 1;
124 
126  + std::to_string(j);
128 
129  for (auto parent: parents) {
131  }
132 
133  /*
134  * We are constructing the new aggregator with a clone of the former
135  */
138 
139  // for(Set<NodeId>::iterator it = parents.begin(); it!= parents.end(); ++it){
140  for (auto it = orderedParents.begin(); it != orderedParents.end(); ++it) {
141  if (q < parents.size() - parents.size() % arity) {
142  if (i == arity) {
143  i = 0;
144  j++;
145 
146  if (newAgg->varType() == VarType::Labelized) {
148  } else if (newAgg->varType() == VarType::Range) {
149  static_cast< RangeVariable* >(newAgg)->setMinVal(minVal);
150  static_cast< RangeVariable* >(newAgg)->setMaxVal(maxVal);
152  } else {
154  "Decomposition is not available for type : " + aggType);
155  }
156 
157  /*
158  * Adding arcs in the new node from its parents and adding thoses into
159  * the temporary potential
160  */
161  for (NodeId node: newAggParents) {
163  }
164 
165  /*
166  * Adding the new aggregator in t
167  */
169 
171 
172  minVal = 0;
173  maxVal = 0;
174 
176  + std::to_string(j);
177 
178  delete (newAgg);
182 
183  if (bn.variable(*it).varType() == VarType::Range) {
184  minVal
185  += static_cast< const RangeVariable& >(bn.variable(*it)).minVal();
186  maxVal
187  += static_cast< const RangeVariable& >(bn.variable(*it)).maxVal();
188  }
189 
191  i++;
192  } else {
193  if (bn.variable(*it).varType() == VarType::Range) {
194  minVal
195  += static_cast< const RangeVariable& >(bn.variable(*it)).minVal();
196  maxVal
197  += static_cast< const RangeVariable& >(bn.variable(*it)).maxVal();
198  }
199 
201  i++;
202  }
203  } else {
204  newAggs.insert(*it);
205  }
206  q++;
207  }
208 
209  if (newAgg->varType() == VarType::Labelized) {
211  } else if (newAgg->varType() == VarType::Range) {
212  static_cast< RangeVariable* >(newAgg)->setMinVal(minVal);
213  static_cast< RangeVariable* >(newAgg)->setMaxVal(maxVal);
215  } else {
217  "Decomposition is not available for type : " + aggType);
218  }
219 
221 
222  for (NodeId node: newAggParents) {
224  }
225 
227 
228  for (auto agg: final) {
230  }
231 
232  delete (newAgg);
233  return bn;
234  }
235 
236  template < typename GUM_SCALAR >
238  BayesNet< GUM_SCALAR >& bn,
239  Set< NodeId > nodes,
241  int& j) {
242  auto p
243  = static_cast< const gum::aggregator::MultiDimAggregator< GUM_SCALAR >* >(
245 
248 
249  if (nodes.size() <= arity) {
250  return nodes;
251  } else {
253 
254  Set< NodeId > newAggs = Set< NodeId >();
255 
257 
258  std::list< NodeId > orderedParents = {};
259 
260  for (const auto& elt: nodes) {
262  }
263 
265 
266  gum::Size i = 0;
267  gum::Size q = 0;
268  long minVal = 0;
269  long maxVal = 0;
270 
271  j++;
272 
274  + "_" + std::to_string(j);
275 
278 
279  // for(Set<NodeId>::iterator it = nodes.begin(); it!= nodes.end(); ++it){
280  for (auto it = orderedParents.begin(); it != orderedParents.end(); ++it) {
281  if (q < nodes.size() - nodes.size() % arity) {
282  if (i == arity) {
283  i = 0;
284  j++;
285 
286  if (newAgg->varType() == VarType::Labelized) {
288  } else if (newAgg->varType() == VarType::Range) {
289  static_cast< RangeVariable* >(newAgg)->setMinVal(minVal);
290  static_cast< RangeVariable* >(newAgg)->setMaxVal(maxVal);
292  } else {
294  "Decomposition is not available for type : " + aggType);
295  }
296 
297  for (NodeId node: newAggParents) {
299  }
300 
302 
304 
305  minVal = 0;
306  maxVal = 0;
307 
309  + std::to_string(j);
310 
311  delete (newAgg);
315 
316  if (bn.variable(*it).varType() == VarType::Range) {
317  minVal
318  += static_cast< const RangeVariable& >(bn.variable(*it)).minVal();
319  maxVal
320  += static_cast< const RangeVariable& >(bn.variable(*it)).maxVal();
321  }
322 
324  i++;
325  } else {
326  if (bn.variable(*it).varType() == VarType::Range) {
327  minVal
328  += static_cast< const RangeVariable& >(bn.variable(*it)).minVal();
329  maxVal
330  += static_cast< const RangeVariable& >(bn.variable(*it)).maxVal();
331  }
332 
334  i++;
335  }
336  } else {
337  newAggs.insert(*it);
338  }
339  q++;
340  }
341 
342  if (newAgg->varType() == VarType::Labelized) {
344  } else if (newAgg->varType() == VarType::Range) {
345  static_cast< RangeVariable* >(newAgg)->setMinVal(minVal);
346  static_cast< RangeVariable* >(newAgg)->setMaxVal(maxVal);
348  } else {
350  "Decomposition is not available for type : " + aggType);
351  }
352 
354 
355  for (NodeId node: newAggParents) {
357  }
358 
359  delete (newAgg);
361  }
362  }
363 
364 
365  template < typename GUM_SCALAR >
366  INLINE void
368  if (arity < 2) {
369  GUM_ERROR(OperationNotAllowed, "Maximum arity should be at least 2");
370  }
371  arity__ = arity;
372  }
373 
374  template < typename GUM_SCALAR >
376  return arity__;
377  }
378 
379  template < typename GUM_SCALAR >
381  return "aggregator decomposition";
382  }
383 
384 } /* namespace gum */
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:669