aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
multiDimAggregator.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 MultiDimAggregator
25  *
26  * @author Pierre-Henri WUILLEMIN(@LIP6) & Christophe GONZALES(@AMU)
27  */
28 
29 #ifndef GUM_MULTI_DIM_AGGREGATOR_H
30 #define GUM_MULTI_DIM_AGGREGATOR_H
31 
32 #include <agrum/tools/multidim/implementations/multiDimReadOnly.h>
33 
34 namespace gum {
35  namespace aggregator {
36  // =========================================================================
37  // === GUM_MULTI_DIM_AGGREGATOR ===
38  // =========================================================================
39 
40  /**
41  * @class MultiDimAggregator
42  * @headerfile multiDimAggregator.h
43  * <agrum/tools/multidim/aggregators/multiDimAggregator.h>
44  * @ingroup multidim_agg_group
45  *
46  * @brief Abstract base class for all multi dimensionnal aggregator.
47  *
48  * The principle of a deterministic aggregator is that
49  * \f$P[i,J_1,\cdots,J_n]=1 \iff
50  * i==f(J_1,f(J_2,\cdots,f(J_n,NeutraElement)\cdots))\f$ and 0 otherwise.
51  * f is a virtual function called the folder function (like folder_left or
52  * folder_right in OCaml). NeutralElement is an Idx
53  *
54  * @warning
55  * - the way aggregators are implemented assumed that the FIRST variable
56  * in the multiDim is the aggregator variable.
57  * - the way aggregators are implemented does not check types and domain
58  * size (e.g domain(folder function)==domain(aggregator variable)).
59  * However, \f$f(J_1,f(J_2,\cdots,f(J_n,NeutraElement)\cdots))\f$ is
60  * truncated in order to fit in domain(aggregator variable).
61  */
62  template < typename GUM_SCALAR >
64  public:
65  // =======================================================================
66  /// @name Constructors / Destructors
67  // =======================================================================
68  /// @{
69 
70  /**
71  * Default constructor.
72  */
74 
75  /**
76  * Copy constructor.
77  */
78  MultiDimAggregator(const MultiDimAggregator< GUM_SCALAR >& from);
79 
80  /**
81  * Class destructor.
82  */
83  virtual ~MultiDimAggregator();
84 
85  /// @}
86 
87  /**
88  * This method creates a clone of this object, withouth its content
89  * (including variable), you must use this method if you want to ensure
90  * that the generated object has the same type than the object containing
91  * the called newFactory()
92  *
93  * For example :
94  * @code
95  * MultiDimArray<double> y;
96  * MultiDimContainer<double>* x = y.newFactory();
97  * @endcode
98  *
99  * Then x is a MultiDimArray<double>*
100  *
101  * @warning you must desallocate by yourself the memory
102  * @return an empty clone of this object with the same type
103  */
104  virtual MultiDimContainer< GUM_SCALAR >* newFactory() const override = 0;
105 
106  // =======================================================================
107  /// @name Accessors / Modifiers
108  // =======================================================================
109  /// @{
110 
111  public:
112  virtual GUM_SCALAR get(const Instantiation& i) const override;
113 
114  virtual std::string aggregatorName() const = 0;
115  std::string toString() const override;
116 
118  const gum::DiscreteVariable* const,
119  gum::Idx,
120  gum::Idx) override{};
121 
122  void setFirstNotification(const gum::Instantiation&) override{};
123 
124  void setLastNotification(const gum::Instantiation&) override{};
125 
126  void setIncNotification(const gum::Instantiation&) override{};
127 
128  void setDecNotification(const gum::Instantiation&) override{};
129 
130  void setChangeNotification(const gum::Instantiation&) override{};
131 
132  std::string toString(const gum::Instantiation* i) const override { return i->toString(); };
133 
134  /**
135  * @return the real number of parameters used for this table.
136  *
137  * This function is used for compute @see compressionRatio()
138  */
139  Size realSize() const override { return 0; };
140 
141  /**
142  * @brief Returns the real name of the multiDimArray.
143  *
144  * In aGrUM, all the types of multi-dimensional arrays/functionals have a
145  * name that describes what they are in reality. For instance, a table
146  * stored in extension is a "MultiDimArray", one that stores only non
147  * zero elements is a "MultiDimSparseArray", and so on. These names are
148  * unique for each type of implementation and is used by the system to
149  * determine which is the best functions to use, say, when we wish to use
150  * operators such as operator+ on two MultiDimImplementations.
151  */
152  const std::string& name() const override;
153 
154  /**
155  * @brief Copy of a multiDimICIModel.
156  *
157  * This method is virtual because it should be optimized in certain
158  * MultiDimContainer.
159  *
160  * @throw OperationNotAllowed Raised if src does not have the same domain
161  * size than this MultiDimContainer.
162  **/
163  void copyFrom(const MultiDimContainer< GUM_SCALAR >& src) const override;
164 
165  /**
166  * @return true if the aggregator is decomposable.
167  */
168  bool isDecomposable() const;
169 
170  /// @}
171  protected:
172  /// by default, buildValue_ uses a "fold" scheme and the user has to
173  /// implement neutralElt_ and fold_
174  /// but if necessary (as for @ref Median), buildValue_ can be
175  /// reimplemented.
176  virtual Idx buildValue_(const gum::Instantiation& i) const;
177 
178  /// decomposable_ indicates if the aggregator can be decomposed
180 
181  /// neutralElt_() is the result value for the first application of fold_
182  virtual Idx neutralElt_() const = 0;
183 
184  /// fold_ is applied on value i1 for variable v. the actual result for
185  /// precedent applications is i2.
186  /// @return the new result for applications up to v.
187  virtual Idx fold_(const DiscreteVariable& v, Idx i1, Idx i2, bool& stop_iteration) const = 0;
188  };
189 
190 
191 #ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
192  extern template class MultiDimAggregator< double >;
193 #endif
194 
195  /// For friendly displaying the content of the array.
196  template < typename GUM_SCALAR >
198 
199  } /* namespace aggregator */
200 } /* namespace gum */
201 
202 #include <agrum/tools/multidim/aggregators/multiDimAggregator_tpl.h>
203 
204 #endif /* GUM_MULTI_DIM_AGGREGATOR_H */
virtual ~MultiDimAggregator()
Class destructor.
MultiDimAggregator(const MultiDimAggregator< GUM_SCALAR > &from)
Copy constructor.
virtual Idx buildValue_(const gum::Instantiation &i) const
by default, buildValue_ uses a "fold" scheme and the user has to implement neutralElt_ and fold_ but ...
const std::string & name() const override
Returns the real name of the multiDimArray.
void setChangeNotification(const gum::Instantiation &) override
void setDecNotification(const gum::Instantiation &) override
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:643
virtual std::string aggregatorName() const =0
void copyFrom(const MultiDimContainer< GUM_SCALAR > &src) const override
Copy of a multiDimICIModel.
std::string toString() const override
void changeNotification(const gum::Instantiation &, const gum::DiscreteVariable *const, gum::Idx, gum::Idx) override
<agrum/tools/multidim/aggregators/multiDimAggregator.h>
void setIncNotification(const gum::Instantiation &) override
void setFirstNotification(const gum::Instantiation &) override
virtual Idx neutralElt_() const =0
neutralElt_() is the result value for the first application of fold_
void setLastNotification(const gum::Instantiation &) override
virtual Idx fold_(const DiscreteVariable &v, Idx i1, Idx i2, bool &stop_iteration) const =0
fold_ is applied on value i1 for variable v. the actual result for precedent applications is i2...
virtual MultiDimContainer< GUM_SCALAR > * newFactory() const override=0
This method creates a clone of this object, withouth its content (including variable), you must use this method if you want to ensure that the generated object has the same type than the object containing the called newFactory()
bool decomposable_
decomposable_ indicates if the aggregator can be decomposed
virtual GUM_SCALAR get(const Instantiation &i) const override
std::ostream & operator<<(std::ostream &s, const MultiDimAggregator< GUM_SCALAR > &ag)
For friendly displaying the content of the array.
INLINE std::ostream & operator<<(std::ostream &s, const MultiDimAggregator< GUM_SCALAR > &ag)
For friendly displaying the content of the array.
std::string toString(const gum::Instantiation *i) const override