aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
PRMAggregate_tpl.h
Go to the documentation of this file.
1 /**
2  *
3  * Copyright (c) 2005-2021 by 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() + PRMObject::RIGHT_CAST() + name;
54  this->_type_->variable().setName(name);
55  }
56 
57  template < typename GUM_SCALAR >
60  const PRMType& rvType,
61  Idx label) :
64  _label_(std::shared_ptr< Idx >(new Idx(label))) {
67  this->_type_->variable().setName(name);
68  }
69 
70  template < typename GUM_SCALAR >
73  delete _type_;
74  }
75 
76  template < typename GUM_SCALAR >
80  GUM_ERROR(FatalError, "illegal call to gum::PRMAggregate copy constructor.")
81  }
82 
83  template < typename GUM_SCALAR >
86  GUM_ERROR(FatalError, "illegal call to gum::PRMAggregate copy operator.")
87  }
88 
89  template < typename GUM_SCALAR >
92  return this->prm_aggregate;
93  }
94 
95  template < typename GUM_SCALAR >
98  return _agg_type_;
99  }
100 
101  template < typename GUM_SCALAR >
103  if (*_label_ != INT_MAX) return *_label_;
104  GUM_ERROR(OperationNotAllowed, "no label defined for this aggregate")
105  }
106 
107  template < typename GUM_SCALAR >
109  return _label_value_;
110  }
111 
112  template < typename GUM_SCALAR >
114  (*_label_) = idx;
115  }
116 
117  template < typename GUM_SCALAR >
120  }
121 
122  template < typename GUM_SCALAR >
124  return *_label_ != INT_MAX;
125  }
126 
127  template < typename GUM_SCALAR >
129  switch (agg_type()) {
130  case AggregateType::MIN: {
131  return aggregator::Min< GUM_SCALAR >().isDecomposable();
132  }
133  case AggregateType::MAX: {
134  return aggregator::Max< GUM_SCALAR >().isDecomposable();
135  }
136  case AggregateType::OR: {
137  return aggregator::Or< GUM_SCALAR >().isDecomposable();
138  }
139  case AggregateType::AND: {
140  return aggregator::And< GUM_SCALAR >().isDecomposable();
141  }
142  case AggregateType::AMPLITUDE: {
144  }
145  case AggregateType::MEDIAN: {
147  }
148  case AggregateType::EXISTS: {
150  }
151  case AggregateType::FORALL: {
153  }
154  case AggregateType::COUNT: {
156  }
157  case AggregateType::SUM: {
158  return aggregator::Sum< GUM_SCALAR >().isDecomposable();
159  }
160  default: {
161  GUM_ERROR(OperationNotAllowed, "Unknown aggregator.")
162  }
163  }
164  }
165 
166  template < typename GUM_SCALAR >
168  return *_type_;
169  }
170 
171  template < typename GUM_SCALAR >
173  return *_type_;
174  }
175 
176  template < typename GUM_SCALAR >
178  GUM_ERROR(OperationNotAllowed, "This is an aggregate.")
179  }
180 
181  template < typename GUM_SCALAR >
183  GUM_ERROR(OperationNotAllowed, "This is an aggregate.")
184  }
185 
186  template < typename GUM_SCALAR >
188  switch (agg_type()) {
189  case AggregateType::MIN: {
190  return new aggregator::Min< GUM_SCALAR >();
191  }
192  case AggregateType::MAX: {
193  return new aggregator::Max< GUM_SCALAR >();
194  }
195  case AggregateType::OR: {
196  return new aggregator::Or< GUM_SCALAR >();
197  }
198  case AggregateType::AND: {
199  return new aggregator::And< GUM_SCALAR >();
200  }
201  case AggregateType::AMPLITUDE: {
202  return new aggregator::Amplitude< GUM_SCALAR >();
203  }
204  case AggregateType::MEDIAN: {
205  return new aggregator::Median< GUM_SCALAR >();
206  }
207  case AggregateType::EXISTS: {
208  return new aggregator::Exists< GUM_SCALAR >(label());
209  }
210  case AggregateType::FORALL: {
211  return new aggregator::Forall< GUM_SCALAR >(label());
212  }
213  case AggregateType::COUNT: {
214  return new aggregator::Count< GUM_SCALAR >(label());
215  }
216  case AggregateType::SUM: {
217  return new aggregator::Sum< GUM_SCALAR >();
218  }
219  default: {
220  GUM_ERROR(OperationNotAllowed, "Unknown aggregator.")
221  }
222  }
223  return nullptr;
224  }
225 
226 
227  // See gum::PRMClassElement<GUM_SCALAR>::addParent_().
228  template < typename GUM_SCALAR >
230 
231  // See gum::PRMClassElement<GUM_SCALAR>::addChild_().
232  template < typename GUM_SCALAR >
234 
235  template < typename GUM_SCALAR >
238 
239  try {
240  cast = new PRMScalarAttribute< GUM_SCALAR >(this->name(), type().superType());
241  } catch (NotFound&) {
242  GUM_ERROR(OperationNotAllowed, "this PRMAggregate can not have cast descendant")
243  }
244 
245  cast->addParent(*this);
246  const DiscreteVariable& my_var = type().variable();
249 
250  for (inst.setFirst(); !inst.end(); inst.inc()) {
251  if (type().label_map()[inst.val(my_var)] == inst.val(cast_var)) {
252  cast->cpf().set(inst, 1);
253  } else {
254  cast->cpf().set(inst, 0);
255  }
256  }
257 
258  return cast;
259  }
260 
261  template < typename GUM_SCALAR >
263  return _label_;
264  }
265 
266  template < typename GUM_SCALAR >
268  this->_label_ = label;
269  }
270 
271  } /* namespace prm */
272 } /* namespace gum */
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:643
ParamScopeData(const std::string &s, const PRMReferenceSlot< GUM_SCALAR > &ref, Idx d)