aGrUM  0.20.2
a C++ library for (probabilistic) graphical models
multiDimCombinationDefault.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  *
25  * @author Christophe GONZALES(@AMU) and Pierre-Henri WUILLEMIN(@LIP6)
26  */
27 
28 #ifndef GUM_MULTI_DIM_COMBINATION_DEFAULT_H
29 #define GUM_MULTI_DIM_COMBINATION_DEFAULT_H
30 
31 #include <agrum/tools/core/sequence.h>
32 #include <agrum/tools/multidim/utils/operators/multiDimCombination.h>
33 #include <agrum/tools/variables/discreteVariable.h>
34 
35 namespace gum {
36 
37  // clang-format off
38  /**
39  * @class MultiDimCombinationDefault
40  * @headerfile multiDimCombinationDefault.h <agrum/tools/multidim/operators/multiDimCombinationDefault.h>
41  * @brief A class to combine efficiently several MultiDim tables
42  * @ingroup multidim_op_group
43  *
44  * MultiDimCombinationDefault is a class designed to combine efficiently
45  * several multidimensional objects, that is, to compute expressions like T1
46  * op T2 op T3 op .... op Tn, where the Ti's are the multidimensional objects
47  * and op is an operator or a function taking in argument two such objects
48  * and producing a new (combined) Ti object. Note that the
49  * MultiDimCombinationDefault determines itself in which order the objects
50  * should be combined. As such, the combination operation to perform should
51  * thus be COMMUTATIVE and ASSOCIATIVE.
52  *
53  * By multidimensional objects, we mean of course MultiDimImplementations,
54  * but also more complex objects such as, for instance, pairs of
55  * MultiDimImplementations the first one of which being a utility function
56  * and the second one being a table of instantiations (useful, e.g., for
57  * computing MPE's) but this can also be a pair (Utility,Potential) for the
58  * inference in an Influence Diagram. Actually, the important point for a
59  * multidimensional object to be eligible to be combined by the
60  * MultiDimCombinationDefault is:
61  * # that the object contains a method variablesSequence that returns a
62  * sequence of Discrete variables that represent the dimensions of the
63  * multidimensional object
64  * # that there exists a function taking in arguments two such
65  * multidimensional objects and producing a new object of the same type,
66  * which is the so-called combined result of these two objects.
67  *
68  * To be quite generic, the MultiDimCombinationDefault takes in argument the
69  * function that produces the result of the combination of two
70  * multidimensional objects. The following code gives an example of the usage
71  * of MultiDimCombinations:
72  *
73  * @code
74  * // a function used to combine two Potential<float>'s:
75  * Potential<float>* addPotential ( const Potential<float>& t1,
76  * const Potential<float>& t2 ) {
77  * return new Potential<float> (t1 + t2);
78  * }
79  *
80  * // another function used to combine two Potential<float>'s:
81  * Potential<float>* multPotential ( const Potential<float>& t1,
82  * const Potential<float>& t2 ) {
83  * return new Potential<float> (t1 * t2);
84  * }
85  *
86  *
87  * Potential<float> t1, t2, t3;
88  * Set<const Potential<float>*> set;
89  * set << &table1 << &table2 << &table3;
90  * MultiDimCombinationDefault<float,Potential> Comb ( addPotential );
91  * Potential<float>* combined_table = Comb.combine ( set );
92  *
93  * // change the operator to apply
94  * Comb.setCombineFunction ( multPotential );
95  * Potential<float>* combined_table2 = Comb.combine ( set );
96  *
97  * @endcode
98  */
99  // clang-format on
100  template < typename GUM_SCALAR, template < typename > class TABLE >
103  public:
104  // ========================================================================
105  /// @name Constructors / Destructors
106  // ========================================================================
107  /// @{
108 
109  /**
110  * @brief Default constructor.
111  *
112  * @param combine a function that takes two tables in input and produces a
113  * new table which is the result of the combination of the two tables
114  * passed in argument.
115  */
116  MultiDimCombinationDefault(TABLE< GUM_SCALAR >* (
117  *combine)(const TABLE< GUM_SCALAR >&, const TABLE< GUM_SCALAR >&));
118 
119  /// Copy constructor
122 
123  /// Destructor
124  virtual ~MultiDimCombinationDefault();
125 
126  /**
127  * @brief virtual constructor
128  *
129  * @return A new fresh MultiDimCombinator with the same combination
130  * function.
131  */
133 
134  /// @}
135  // ========================================================================
136  /// @name Accessors/Modifiers
137  // ========================================================================
138  /// @{
139 
140  /**
141  * @brief Creates and returns the result of the combination of the tables
142  * within set.
143  *
144  * @return a new freshly created TABLE which is the result of the
145  * combination of all the TABLES passed in argument
146  *
147  * @throws InvalidArgumentsNumber exception is thrown if the set passed in
148  * argument contains less than two elements.
149  */
150  virtual TABLE< GUM_SCALAR >*
151  combine(const Set< const TABLE< GUM_SCALAR >* >& set);
152  virtual void combine(TABLE< GUM_SCALAR >& container,
153  const Set< const TABLE< GUM_SCALAR >* >& set);
154 
155  /// Changes the function used for combining two TABLES.
156  virtual void setCombineFunction(TABLE< GUM_SCALAR >* (
157  *combine)(const TABLE< GUM_SCALAR >&, const TABLE< GUM_SCALAR >&));
158 
159  /// Returns the combination function currently used by the combinator.
160  virtual TABLE< GUM_SCALAR >* (*combineFunction())(const TABLE< GUM_SCALAR >&,
161  const TABLE< GUM_SCALAR >&);
162 
163  /**
164  * @brief returns a rough estimate of the number of operations that will be
165  * performed to compute the combination.
166  */
167  virtual float nbOperations(const Set< const TABLE< GUM_SCALAR >* >& set) const;
168  virtual float nbOperations(
169  const Set< const Sequence< const DiscreteVariable* >* >& set) const;
170 
171  /**
172  * @brief Returns the additional memory consumption used during the
173  * combination.
174  *
175  * Actually, this function does not return a precise account of the memory
176  * used by the multidimCombination but a rough estimate based on the sizes
177  * of the tables involved in the combination. @return a pair of memory
178  * consumption: the first one is the maximum amount of memory used during
179  * the combination and the second one is the amount of memory still used at
180  * the end of the function ( the memory used by the resulting table ).
181  */
182  virtual std::pair< long, long >
183  memoryUsage(const Set< const TABLE< GUM_SCALAR >* >& set) const;
184  virtual std::pair< long, long > memoryUsage(
185  const Set< const Sequence< const DiscreteVariable* >* >& set) const;
186 
187  /// @}
188 
189  protected:
190  /// The function used to combine two tables.
191  TABLE< GUM_SCALAR >* (*combine_)(const TABLE< GUM_SCALAR >& t1,
192  const TABLE< GUM_SCALAR >& t2);
193 
194  /**
195  * @brief returns the domain size of the Cartesian product of the union of
196  * all the variables in seq1 and seq2.
197  */
199  const Sequence< const DiscreteVariable* >& seq2) const;
200  };
201 
202 } /* namespace gum */
203 
204 // always include the template implementation
205 #include <agrum/tools/multidim/utils/operators/multiDimCombinationDefault_tpl.h>
206 
207 #endif /* GUM_MULTI_DIM_COMBINATION_DEFAULT_H */
A class to combine efficiently several MultiDim tablesMultiDimCombinationDefault is a class designed ...
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:669
virtual float nbOperations(const Set< const Sequence< const DiscreteVariable * > * > &set) const
Creates and returns the result of the combination of the tables within set.
virtual void setCombineFunction(TABLE< GUM_SCALAR > *(*combine)(const TABLE< GUM_SCALAR > &, const TABLE< GUM_SCALAR > &))
Changes the function used for combining two TABLES.
virtual std::pair< long, long > memoryUsage(const Set< const Sequence< const DiscreteVariable * > * > &set) const
Creates and returns the result of the combination of the tables within set.
virtual MultiDimCombinationDefault< GUM_SCALAR, TABLE > * newFactory() const
virtual constructor
Size combinedSize_(const Sequence< const DiscreteVariable * > &seq1, const Sequence< const DiscreteVariable * > &seq2) const
returns the domain size of the Cartesian product of the union of all the variables in seq1 and seq2...
MultiDimCombinationDefault(const MultiDimCombinationDefault< GUM_SCALAR, TABLE > &)
Copy constructor.
virtual void combine(TABLE< GUM_SCALAR > &container, const Set< const TABLE< GUM_SCALAR > * > &set)
Creates and returns the result of the combination of the tables within set.
MultiDimCombinationDefault(TABLE< GUM_SCALAR > *(*combine)(const TABLE< GUM_SCALAR > &, const TABLE< GUM_SCALAR > &))
Default constructor.
virtual ~MultiDimCombinationDefault()
Destructor.
virtual TABLE< GUM_SCALAR > * combine(const Set< const TABLE< GUM_SCALAR > * > &set)
Creates and returns the result of the combination of the tables within set.