aGrUM 2.3.2
a C++ library for (probabilistic) graphical models
PRMAggregate_tpl.h
Go to the documentation of this file.
1/****************************************************************************
2 * This file is part of the aGrUM/pyAgrum library. *
3 * *
4 * Copyright (c) 2005-2025 by *
5 * - Pierre-Henri WUILLEMIN(_at_LIP6) *
6 * - Christophe GONZALES(_at_AMU) *
7 * *
8 * The aGrUM/pyAgrum library is free software; you can redistribute it *
9 * and/or modify it under the terms of either : *
10 * *
11 * - the GNU Lesser General Public License as published by *
12 * the Free Software Foundation, either version 3 of the License, *
13 * or (at your option) any later version, *
14 * - the MIT license (MIT), *
15 * - or both in dual license, as here. *
16 * *
17 * (see https://agrum.gitlab.io/articles/dual-licenses-lgplv3mit.html) *
18 * *
19 * This aGrUM/pyAgrum library is distributed in the hope that it will be *
20 * useful, but WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, *
21 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES MERCHANTABILITY or FITNESS *
22 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, *
25 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR *
26 * OTHER DEALINGS IN THE SOFTWARE. *
27 * *
28 * See LICENCES for more details. *
29 * *
30 * SPDX-FileCopyrightText: Copyright 2005-2025 *
31 * - Pierre-Henri WUILLEMIN(_at_LIP6) *
32 * - Christophe GONZALES(_at_AMU) *
33 * SPDX-License-Identifier: LGPL-3.0-or-later OR MIT *
34 * *
35 * Contact : info_at_agrum_dot_org *
36 * homepage : http://agrum.gitlab.io *
37 * gitlab : https://gitlab.com/agrumery/agrum *
38 * *
39 ****************************************************************************/
40#pragma once
41
42
49
50#include <climits>
51
62
63namespace gum {
64 namespace prm {
65
66 template < typename GUM_SCALAR >
68 AggregateType aggType,
69 const PRMType& rvType) :
70 PRMClassElement< GUM_SCALAR >(name), _agg_type_(aggType), _type_(new PRMType(rvType)),
71 _label_(std::shared_ptr< Idx >(new Idx(INT_MAX))) {
72 GUM_CONSTRUCTOR(PRMAggregate);
74 this->_type_->variable().setName(name);
75 }
76
77 template < typename GUM_SCALAR >
79 AggregateType aggType,
80 const PRMType& rvType,
81 Idx label) :
82 PRMClassElement< GUM_SCALAR >(name), _agg_type_(aggType), _type_(new PRMType(rvType)),
83 _label_(std::shared_ptr< Idx >(new Idx(label))) {
84 GUM_CONSTRUCTOR(PRMAggregate);
86 this->_type_->variable().setName(name);
87 }
88
89 template < typename GUM_SCALAR >
91 GUM_DESTRUCTOR(PRMAggregate);
92 delete _type_;
93 }
94
95 template < typename GUM_SCALAR >
97 PRMClassElement< GUM_SCALAR >(source) {
98 GUM_CONS_CPY(PRMAggregate);
99 GUM_ERROR(FatalError, "illegal call to gum::PRMAggregate copy constructor.")
100 }
101
102 template < typename GUM_SCALAR >
105 GUM_ERROR(FatalError, "illegal call to gum::PRMAggregate copy operator.")
106 }
107
108 template < typename GUM_SCALAR >
111 return this->prm_aggregate;
112 }
113
114 template < typename GUM_SCALAR >
119
120 template < typename GUM_SCALAR >
122 if (*_label_ != INT_MAX) return *_label_;
123 GUM_ERROR(OperationNotAllowed, "no label defined for this aggregate")
124 }
125
126 template < typename GUM_SCALAR >
127 INLINE const std::string& PRMAggregate< GUM_SCALAR >::labelValue() const {
128 return _label_value_;
129 }
130
131 template < typename GUM_SCALAR >
133 (*_label_) = idx;
134 }
135
136 template < typename GUM_SCALAR >
137 INLINE void PRMAggregate< GUM_SCALAR >::setLabel(const std::string& value) {
138 _label_value_ = value;
139 }
140
141 template < typename GUM_SCALAR >
143 return *_label_ != INT_MAX;
144 }
145
146 template < typename GUM_SCALAR >
148 switch (agg_type()) {
149 case AggregateType::MIN : {
151 }
152 case AggregateType::MAX : {
154 }
155 case AggregateType::OR : {
157 }
158 case AggregateType::AND : {
160 }
161 case AggregateType::AMPLITUDE : {
162 return aggregator::Amplitude< GUM_SCALAR >().isDecomposable();
163 }
164 case AggregateType::MEDIAN : {
165 return aggregator::Median< GUM_SCALAR >().isDecomposable();
166 }
167 case AggregateType::EXISTS : {
168 return aggregator::Exists< GUM_SCALAR >(label()).isDecomposable();
170 case AggregateType::FORALL : {
172 }
173 case AggregateType::COUNT : {
175 }
176 case AggregateType::SUM : {
177 return aggregator::Sum< GUM_SCALAR >().isDecomposable();
179 default : {
180 GUM_ERROR(OperationNotAllowed, "Unknown aggregator.")
182 }
183 }
184
185 template < typename GUM_SCALAR >
190 template < typename GUM_SCALAR >
192 return *_type_;
193 }
194
195 template < typename GUM_SCALAR >
196 INLINE Tensor< GUM_SCALAR >& PRMAggregate< GUM_SCALAR >::cpf() {
197 GUM_ERROR(OperationNotAllowed, "This is an aggregate.")
198 }
199
200 template < typename GUM_SCALAR >
201 INLINE const Tensor< GUM_SCALAR >& PRMAggregate< GUM_SCALAR >::cpf() const {
202 GUM_ERROR(OperationNotAllowed, "This is an aggregate.")
203 }
204
205 template < typename GUM_SCALAR >
207 switch (agg_type()) {
208 case AggregateType::MIN : {
210 }
211 case AggregateType::MAX : {
213 }
214 case AggregateType::OR : {
215 return new aggregator::Or< GUM_SCALAR >();
216 }
217 case AggregateType::AND : {
220 case AggregateType::AMPLITUDE : {
223 case AggregateType::MEDIAN : {
226 case AggregateType::EXISTS : {
227 return new aggregator::Exists< GUM_SCALAR >(label());
229 case AggregateType::FORALL : {
230 return new aggregator::Forall< GUM_SCALAR >(label());
231 }
232 case AggregateType::COUNT : {
233 return new aggregator::Count< GUM_SCALAR >(label());
234 }
235 case AggregateType::SUM : {
236 return new aggregator::Sum< GUM_SCALAR >();
237 }
238 default : {
239 GUM_ERROR(OperationNotAllowed, "Unknown aggregator.")
240 }
241 }
242 return nullptr;
243 }
244
245 // See gum::PRMClassElement<GUM_SCALAR>::addParent_().
246 template < typename GUM_SCALAR >
248
249 // See gum::PRMClassElement<GUM_SCALAR>::addChild_().
250 template < typename GUM_SCALAR >
252
253 template < typename GUM_SCALAR >
256
257 try {
258 cast = new PRMScalarAttribute< GUM_SCALAR >(this->name(), type().superType());
259 } catch (NotFound const&) {
260 GUM_ERROR(OperationNotAllowed, "this PRMAggregate can not have cast descendant")
261 }
262
263 cast->addParent(*this);
264 const DiscreteVariable& my_var = type().variable();
265 DiscreteVariable& cast_var = cast->type().variable();
266 Instantiation inst(cast->cpf());
267
268 for (inst.setFirst(); !inst.end(); inst.inc()) {
269 if (type().label_map()[inst.val(my_var)] == inst.val(cast_var)) {
270 cast->cpf().set(inst, 1);
271 } else {
272 cast->cpf().set(inst, 0);
273 }
274 }
275
276 return cast;
277 }
278
279 template < typename GUM_SCALAR >
280 INLINE std::shared_ptr< Idx > PRMAggregate< GUM_SCALAR >::sharedLabel() const {
281 return _label_;
282 }
283
284 template < typename GUM_SCALAR >
285 INLINE void PRMAggregate< GUM_SCALAR >::sharedLabel(std::shared_ptr< Idx > label) {
286 this->_label_ = label;
287 }
288
289 } /* namespace prm */
290} /* namespace gum */
amplitude aggregator
and aggregator
Base class for discrete random variable.
Exception : fatal (unknown ?) error.
Class for assigning/browsing values to tuples of discrete variables.
<agrum/base/multidim/multiDimImplementation.h>
Exception : the element we looked for cannot be found.
Exception : operation not allowed.
amplitude aggregator
Definition amplitude.h:74
And aggregator.
Definition and.h:74
count aggregator
Definition count.h:76
exists aggregator
Definition exists.h:73
forall aggregator
Definition forall.h:74
max aggregator
Definition max.h:73
median aggregator
Definition median.h:79
min aggregator
Definition min.h:72
or aggregator
Definition or.h:75
Idx label() const
Returns the label's index on which this aggregate applies.
virtual void addParent(const PRMClassElement< GUM_SCALAR > &elt)
See gum::PRMClassElement::addParent_().
virtual void addChild(const PRMClassElement< GUM_SCALAR > &elt)
See gum::PRMClassElement::addChild_().
virtual PRMType & type()
See gum::PRMClassElement::type().
AggregateType
The different type of aggregates we can have.
PRMAggregate< GUM_SCALAR > & operator=(const PRMAggregate< GUM_SCALAR > &source)
Copy operator. Don't use it.
virtual PRMAttribute< GUM_SCALAR > * getCastDescendant() const
See gum::PRMClassElement::elt_type().
PRMAggregate(const std::string &name, AggregateType aggType, const PRMType &rvType)
Default constructor.
AggregateType agg_type() const
Returns the aggregate of *this.
AggregateType _agg_type_
The AggregateType of this aggregate.
bool hasLabel() const
Returns true if the label is defined.
std::shared_ptr< Idx > sharedLabel() const
Returns the shared_ptr holding this Aggregate label.
void setLabel(Idx idx)
Set the aggregator's label.
std::string _label_value_
The AggregateType of this aggregate.
std::shared_ptr< Idx > _label_
Some aggregators applies only on a given label. This attribute must have the concerned Idx....
virtual Tensor< GUM_SCALAR > & cpf()
Aggregates don't have Tensor until they are instantiated as PRMAttribute, so this will raise an Opera...
virtual PRMClassElement< GUM_SCALAR >::ClassElementType elt_type() const
See gum::PRMClassElement::elt_type().
MultiDimImplementation< GUM_SCALAR > * buildImpl() const
Returns a pointer over an empty gum::MultiDimImplementation of the good type for this PRMAggregate.
PRMType * _type_
The random variable type of this aggregate It is deleted with the aggregate.
virtual ~PRMAggregate()
Destructor.
const std::string & labelValue() const
See gum::PRMClassElement::elt_type().
bool isDecomposable() const
Returns true if the aggregator is decomposable.
PRMAttribute is a member of a Class in a PRM.
virtual std::string cast(const PRMType &t) const
PRMClassElement(const std::string &name)
Default constructor of a PRMClassElement.
ClassElementType
Returns true if obj_ptr is of type PRMReferenceSlot.
std::string safeName_
The safe name of this PRMClassElement.
const std::string & name() const
Returns the name of this object.
static std::string LEFT_CAST()
Enumeration of the different types of objects handled by a PRM.
Definition PRMObject.h:90
static std::string RIGHT_CAST()
Enumeration of the different types of objects handled by a PRM.
Definition PRMObject.h:92
<agrum/PRM/elements/scalarAttribute.h>
virtual const Tensor< GUM_SCALAR > & cpf() const
See gum::PRMClassElement::cpf().
virtual PRMType & type()
See gum::PRMClassElement::type().
virtual void addParent(const PRMClassElement< GUM_SCALAR > &elt)
See gum::PRMClassElement::addParent_().
This is a decoration of the DiscreteVariable class.
Definition PRMType.h:78
DiscreteVariable & variable()
Return a reference on the DiscreteVariable contained in this.
Definition PRMType_inl.h:64
count aggregator
#define GUM_ERROR(type, msg)
Definition exceptions.h:72
exists aggregator
forall aggregator
Size Idx
Type for indexes.
Definition types.h:79
max aggregator
median aggregator
min aggregator
namespace for all probabilistic relational models entities
Definition agrum.h:68
gum is the global namespace for all aGrUM entities
Definition agrum.h:46
STL namespace.
or aggregator
sum aggregator