aGrUM  0.20.2
a C++ library for (probabilistic) graphical models
PRMAggregate_tpl.h
Go to the documentation of this file.
1 /**
2  *
3  * Copyright 2005-2020 Pierre-Henri WUILLEMIN(@LIP6) & Christophe GONZALES(@AMU)
4  * info_at_agrum_dot_org
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 gum::PRMAggregate
25  *
26  * @author Lionel TORTI and Pierre-Henri WUILLEMIN(@LIP6)
27  */
28 
29 #include <climits>
30 
31 #include <agrum/tools/multidim/aggregators/amplitude.h>
32 #include <agrum/tools/multidim/aggregators/and.h>
33 #include <agrum/tools/multidim/aggregators/count.h>
34 #include <agrum/tools/multidim/aggregators/or.h>
35 #include <agrum/tools/multidim/aggregators/exists.h>
36 #include <agrum/tools/multidim/aggregators/forall.h>
37 #include <agrum/tools/multidim/aggregators/max.h>
38 #include <agrum/tools/multidim/aggregators/median.h>
39 #include <agrum/tools/multidim/aggregators/min.h>
40 #include <agrum/tools/multidim/aggregators/sum.h>
41 
42 namespace gum {
43  namespace prm {
44 
45  template < typename GUM_SCALAR >
46  PRMAggregate< GUM_SCALAR >::PRMAggregate(const std::string& name,
47  AggregateType aggType,
48  const PRMType& rvType) :
49  PRMClassElement< GUM_SCALAR >(name),
50  agg_type__(aggType), type__(new PRMType(rvType)),
51  label__(std::shared_ptr< Idx >(new Idx(INT_MAX))) {
52  GUM_CONSTRUCTOR(PRMAggregate);
53  this->safeName_ = PRMObject::LEFT_CAST() + type__->name()
54  + PRMObject::RIGHT_CAST() + name;
55  this->type__->variable().setName(name);
56  }
57 
58  template < typename GUM_SCALAR >
61  const PRMType& rvType,
62  Idx label) :
65  label__(std::shared_ptr< Idx >(new Idx(label))) {
67  this->safeName_ = PRMObject::LEFT_CAST() + type__->name()
68  + PRMObject::RIGHT_CAST() + name;
69  this->type__->variable().setName(name);
70  }
71 
72  template < typename GUM_SCALAR >
75  delete type__;
76  }
77 
78  template < typename GUM_SCALAR >
80  const PRMAggregate< GUM_SCALAR >& source) :
83  GUM_ERROR(FatalError, "illegal call to gum::PRMAggregate copy constructor.");
84  }
85 
86  template < typename GUM_SCALAR >
88  const PRMAggregate< GUM_SCALAR >& source) {
89  GUM_ERROR(FatalError, "illegal call to gum::PRMAggregate copy operator.");
90  }
91 
92  template < typename GUM_SCALAR >
95  return this->prm_aggregate;
96  }
97 
98  template < typename GUM_SCALAR >
101  return agg_type__;
102  }
103 
104  template < typename GUM_SCALAR >
106  if (*label__ != INT_MAX) return *label__;
107  GUM_ERROR(OperationNotAllowed, "no label defined for this aggregate");
108  }
109 
110  template < typename GUM_SCALAR >
112  return label_value__;
113  }
114 
115  template < typename GUM_SCALAR >
117  (*label__) = idx;
118  }
119 
120  template < typename GUM_SCALAR >
123  }
124 
125  template < typename GUM_SCALAR >
127  return *label__ != INT_MAX;
128  }
129 
130  template < typename GUM_SCALAR >
132  switch (agg_type()) {
133  case AggregateType::MIN: {
134  return aggregator::Min< GUM_SCALAR >().isDecomposable();
135  }
136  case AggregateType::MAX: {
137  return aggregator::Max< GUM_SCALAR >().isDecomposable();
138  }
139  case AggregateType::OR: {
140  return aggregator::Or< GUM_SCALAR >().isDecomposable();
141  }
142  case AggregateType::AND: {
143  return aggregator::And< GUM_SCALAR >().isDecomposable();
144  }
145  case AggregateType::AMPLITUDE: {
147  }
148  case AggregateType::MEDIAN: {
150  }
151  case AggregateType::EXISTS: {
153  }
154  case AggregateType::FORALL: {
156  }
157  case AggregateType::COUNT: {
159  }
160  case AggregateType::SUM: {
161  return aggregator::Sum< GUM_SCALAR >().isDecomposable();
162  }
163  default: {
164  GUM_ERROR(OperationNotAllowed, "Unknown aggregator.");
165  }
166  }
167  }
168 
169  template < typename GUM_SCALAR >
171  return *type__;
172  }
173 
174  template < typename GUM_SCALAR >
176  return *type__;
177  }
178 
179  template < typename GUM_SCALAR >
181  GUM_ERROR(OperationNotAllowed, "This is an aggregate.");
182  }
183 
184  template < typename GUM_SCALAR >
186  GUM_ERROR(OperationNotAllowed, "This is an aggregate.");
187  }
188 
189  template < typename GUM_SCALAR >
192  switch (agg_type()) {
193  case AggregateType::MIN: {
194  return new aggregator::Min< GUM_SCALAR >();
195  }
196  case AggregateType::MAX: {
197  return new aggregator::Max< GUM_SCALAR >();
198  }
199  case AggregateType::OR: {
200  return new aggregator::Or< GUM_SCALAR >();
201  }
202  case AggregateType::AND: {
203  return new aggregator::And< GUM_SCALAR >();
204  }
205  case AggregateType::AMPLITUDE: {
206  return new aggregator::Amplitude< GUM_SCALAR >();
207  }
208  case AggregateType::MEDIAN: {
209  return new aggregator::Median< GUM_SCALAR >();
210  }
211  case AggregateType::EXISTS: {
212  return new aggregator::Exists< GUM_SCALAR >(label());
213  }
214  case AggregateType::FORALL: {
215  return new aggregator::Forall< GUM_SCALAR >(label());
216  }
217  case AggregateType::COUNT: {
218  return new aggregator::Count< GUM_SCALAR >(label());
219  }
220  case AggregateType::SUM: {
221  return new aggregator::Sum< GUM_SCALAR >();
222  }
223  default: {
224  GUM_ERROR(OperationNotAllowed, "Unknown aggregator.");
225  }
226  }
227  return nullptr;
228  }
229 
230 
231  // See gum::PRMClassElement<GUM_SCALAR>::addParent_().
232  template < typename GUM_SCALAR >
234  const PRMClassElement< GUM_SCALAR >& elt) {}
235 
236  // See gum::PRMClassElement<GUM_SCALAR>::addChild_().
237  template < typename GUM_SCALAR >
239  const PRMClassElement< GUM_SCALAR >& elt) {}
240 
241  template < typename GUM_SCALAR >
245 
246  try {
247  cast = new PRMScalarAttribute< GUM_SCALAR >(this->name(),
248  type().superType());
249  } catch (NotFound&) {
251  "this PRMAggregate can not have cast descendant");
252  }
253 
254  cast->addParent(*this);
255  const DiscreteVariable& my_var = type().variable();
258 
259  for (inst.setFirst(); !inst.end(); inst.inc()) {
260  if (type().label_map()[inst.val(my_var)] == inst.val(cast_var)) {
261  cast->cpf().set(inst, 1);
262  } else {
263  cast->cpf().set(inst, 0);
264  }
265  }
266 
267  return cast;
268  }
269 
270  template < typename GUM_SCALAR >
272  return label__;
273  }
274 
275  template < typename GUM_SCALAR >
276  INLINE void
278  this->label__ = label;
279  }
280 
281  } /* namespace prm */
282 } /* namespace gum */
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:669
ParamScopeData(const std::string &s, const PRMReferenceSlot< GUM_SCALAR > &ref, Idx d)