aGrUM 3.1.1
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-2026 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-2026 *
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
41#pragma once
42
43
50
51#include <climits>
52
63#include <agrum/PRM/elements/PRMAggregate.h> // to ease IDE parser
64
65namespace gum {
66 namespace prm {
67
68 template < GUM_Numeric GUM_SCALAR >
70 AggregateType aggType,
71 const PRMType& rvType) :
72 PRMClassElement< GUM_SCALAR >(name), _agg_type_(aggType), _type_(new PRMType(rvType)),
73 _label_(std::shared_ptr< Idx >(new Idx(INT_MAX))) {
74 GUM_CONSTRUCTOR(PRMAggregate);
75 this->safeName_
76 = (PRMObject::LEFT_CAST() + _type_->name() + PRMObject::RIGHT_CAST()).append(name);
77 this->_type_->variable().setName(name);
78 }
79
80 template < GUM_Numeric GUM_SCALAR >
82 AggregateType aggType,
83 const PRMType& rvType,
84 Idx label) :
85 PRMClassElement< GUM_SCALAR >(name), _agg_type_(aggType), _type_(new PRMType(rvType)),
86 _label_(std::shared_ptr< Idx >(new Idx(label))) {
87 GUM_CONSTRUCTOR(PRMAggregate);
88 this->safeName_
89 = (PRMObject::LEFT_CAST() + _type_->name() + PRMObject::RIGHT_CAST()).append(name);
90 this->_type_->variable().setName(name);
91 }
92
93 template < GUM_Numeric GUM_SCALAR >
95 GUM_DESTRUCTOR(PRMAggregate);
96 delete _type_;
97 }
98
99 template < GUM_Numeric GUM_SCALAR >
101 PRMClassElement< GUM_SCALAR >(source) {
102 GUM_CONS_CPY(PRMAggregate);
103 GUM_ERROR(FatalError, "illegal call to gum::PRMAggregate copy constructor.")
104 }
105
106 template < GUM_Numeric GUM_SCALAR >
109 GUM_ERROR(FatalError, "illegal call to gum::PRMAggregate copy operator.")
110 }
111
112 template < GUM_Numeric GUM_SCALAR >
115 return this->prm_aggregate;
116 }
117
118 template < GUM_Numeric GUM_SCALAR >
123
124 template < GUM_Numeric GUM_SCALAR >
126 if (*_label_ != INT_MAX) return *_label_;
127 GUM_ERROR(OperationNotAllowed, "no label defined for this aggregate")
128 }
129
130 template < GUM_Numeric GUM_SCALAR >
131 const std::string& PRMAggregate< GUM_SCALAR >::labelValue() const {
132 return _label_value_;
133 }
134
135 template < GUM_Numeric GUM_SCALAR >
137 (*_label_) = idx;
138 }
139
140 template < GUM_Numeric GUM_SCALAR >
141 void PRMAggregate< GUM_SCALAR >::setLabel(std::string_view value) {
142 _label_value_ = value;
143 }
144
145 template < GUM_Numeric GUM_SCALAR >
147 return *_label_ != INT_MAX;
148 }
149
150 template < GUM_Numeric GUM_SCALAR >
188
189 template < GUM_Numeric GUM_SCALAR >
193
194 template < GUM_Numeric GUM_SCALAR >
196 return *_type_;
197 }
198
199 template < GUM_Numeric GUM_SCALAR >
200 Tensor< GUM_SCALAR >& PRMAggregate< GUM_SCALAR >::cpf() {
201 GUM_ERROR(OperationNotAllowed, "This is an aggregate.")
202 }
203
204 template < GUM_Numeric GUM_SCALAR >
205 const Tensor< GUM_SCALAR >& PRMAggregate< GUM_SCALAR >::cpf() const {
206 GUM_ERROR(OperationNotAllowed, "This is an aggregate.")
207 }
208
209 template < GUM_Numeric GUM_SCALAR >
211 switch (agg_type()) {
212 case AggregateType::MIN : {
214 }
215 case AggregateType::MAX : {
217 }
218 case AggregateType::OR : {
219 return new aggregator::Or< GUM_SCALAR >();
220 }
221 case AggregateType::AND : {
223 }
226 }
227 case AggregateType::MEDIAN : {
229 }
230 case AggregateType::EXISTS : {
232 }
233 case AggregateType::FORALL : {
235 }
236 case AggregateType::COUNT : {
238 }
239 case AggregateType::SUM : {
241 }
242 default : {
243 GUM_ERROR(OperationNotAllowed, "Unknown aggregator.")
244 }
245 }
246 return nullptr;
247 }
248
249 // See gum::PRMClassElement<GUM_SCALAR>::addParent_().
250 template < GUM_Numeric GUM_SCALAR >
252
253 // See gum::PRMClassElement<GUM_SCALAR>::addChild_().
254 template < GUM_Numeric GUM_SCALAR >
256
257 template < GUM_Numeric GUM_SCALAR >
259 if (!type().isSubType()) {
260 GUM_ERROR(OperationNotAllowed, "this PRMAggregate can not have cast descendant")
261 }
262 auto cast = new PRMScalarAttribute< GUM_SCALAR >(this->name(), type().superType());
263
264 cast->addParent(*this);
265 const DiscreteVariable& my_var = type().variable();
266 DiscreteVariable& cast_var = cast->type().variable();
267 Instantiation inst(cast->cpf());
268
269 for (inst.setFirst(); !inst.end(); inst.inc()) {
270 if (type().label_map()[inst.val(my_var)] == inst.val(cast_var)) {
271 cast->cpf().set(inst, 1);
272 } else {
273 cast->cpf().set(inst, 0);
274 }
275 }
276
277 return cast;
278 }
279
280 template < GUM_Numeric GUM_SCALAR >
281 std::shared_ptr< Idx > PRMAggregate< GUM_SCALAR >::sharedLabel() const {
282 return _label_;
283 }
284
285 template < GUM_Numeric GUM_SCALAR >
286 void PRMAggregate< GUM_SCALAR >::sharedLabel(std::shared_ptr< Idx > label) {
287 this->_label_ = label;
288 }
289
290 template < GUM_Numeric GUM_SCALAR >
293 const auto strLower = toLower(str);
294 if (strLower == "min") {
295 return AggregateType::MIN;
296 } else if (strLower == "max") {
297 return AggregateType::MAX;
298 } else if (strLower == "count") {
300 } else if (strLower == "exists") {
302 } else if (strLower == "or") {
303 return AggregateType::OR;
304 } else if (strLower == "and") {
305 return AggregateType::AND;
306 } else if (strLower == "forall") {
308 } else if (strLower == "amplitude") {
310 } else if (strLower == "median") {
312 } else if (strLower == "sum") {
313 return AggregateType::SUM;
314 } else {
315 std::string msg = "Unknown aggregate: ";
316 msg.append(str);
317 GUM_ERROR(NotFound, msg)
318 }
319 }
320
321 } /* namespace prm */
322} /* namespace gum */
Headers of gum::PRMAggregate.
amplitude aggregator
and aggregator
Base class for discrete random variable.
Exception : fatal (unknown ?) error.
Class for assigning/browsing values to tuples of discrete variables.
bool end() const
Returns true if the Instantiation reached the end.
void inc()
Operator increment.
Idx val(Idx i) const
Returns the current value of the variable at position i.
void setFirst()
Assign the first values to the tuple of the Instantiation.
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
Sum aggregator.
Definition sum.h:69
PRMAttribute< GUM_SCALAR > * getCastDescendant() const override
See gum::PRMClassElement::elt_type().
PRMType & type() override
See gum::PRMClassElement::type().
void addParent(const PRMClassElement< GUM_SCALAR > &elt) override
See gum::PRMClassElement::addParent_().
std::shared_ptr< Idx > sharedLabel() const
Returns the shared_ptr holding this Aggregate label.
void addChild(const PRMClassElement< GUM_SCALAR > &elt) override
See gum::PRMClassElement::addChild_().
PRMAggregate(std::string_view name, AggregateType aggType, const PRMType &rvType)
Default constructor.
AggregateType
The different type of aggregates we can have.
void setLabel(Idx idx)
Set the aggregator's label.
PRMAggregate< GUM_SCALAR > & operator=(const PRMAggregate< GUM_SCALAR > &source)
Copy operator. Don't use it.
bool hasLabel() const
Returns true if the label is defined.
~PRMAggregate() override
Destructor.
AggregateType _agg_type_
The AggregateType of this aggregate.
AggregateType agg_type() const
Returns the aggregate of *this.
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....
MultiDimImplementation< GUM_SCALAR > * buildImpl() const
Returns a pointer over an empty gum::MultiDimImplementation of the good type for this PRMAggregate.
virtual Tensor< GUM_SCALAR > & cpf()
Aggregates don't have Tensor until they are instantiated as PRMAttribute, so this will raise an Opera...
const std::string & labelValue() const
See gum::PRMClassElement::elt_type().
static AggregateType str2enum(std::string_view str)
Static method which returns the AggregateType given its string representation.
PRMType * _type_
The random variable type of this aggregate It is deleted with the aggregate.
PRMClassElement< GUM_SCALAR >::ClassElementType elt_type() const override
See gum::PRMClassElement::elt_type().
Idx label() const
Returns the label's index on which this aggregate applies.
bool isDecomposable() const
Returns true if the aggregator is decomposable.
PRMAttribute is a member of a Class in a PRM.
PRMClassElement(std::string_view name)
Default constructor of a PRMClassElement.
virtual std::string cast(const PRMType &t) const
Returns the name of the cast descendant with PRMType t of this 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 RIGHT_CAST()
Enumeration of the different types of objects handled by a PRM.
static std::string LEFT_CAST()
Enumeration of the different types of objects handled by a PRM.
<agrum/PRM/elements/scalarAttribute.h>
This is a decoration of the DiscreteVariable class.
Definition PRMType.h:78
count aggregator
#define GUM_ERROR(type, msg)
Definition exceptions.h:76
exists aggregator
forall aggregator
Size Idx
Type for indexes.
Definition types.h:79
std::string toLower(std::string_view str)
Returns the lowercase version of str.
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