aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
multiDimCombinationDefault.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  *
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 >
102  public:
103  // ========================================================================
104  /// @name Constructors / Destructors
105  // ========================================================================
106  /// @{
107 
108  /**
109  * @brief Default constructor.
110  *
111  * @param combine a function that takes two tables in input and produces a
112  * new table which is the result of the combination of the two tables
113  * passed in argument.
114  */
115  MultiDimCombinationDefault(TABLE< GUM_SCALAR >* (*combine)(const TABLE< GUM_SCALAR >&,
116  const TABLE< GUM_SCALAR >&));
117 
118  /// Copy constructor
120 
121  /// Destructor
122  virtual ~MultiDimCombinationDefault();
123 
124  /**
125  * @brief virtual constructor
126  *
127  * @return A new fresh MultiDimCombinator with the same combination
128  * function.
129  */
131 
132  /// @}
133  // ========================================================================
134  /// @name Accessors/Modifiers
135  // ========================================================================
136  /// @{
137 
138  /**
139  * @brief Creates and returns the result of the combination of the tables
140  * within set.
141  *
142  * @return a new freshly created TABLE which is the result of the
143  * combination of all the TABLES passed in argument
144  *
145  * @throws InvalidArgumentsNumber exception is thrown if the set passed in
146  * argument contains less than two elements.
147  */
148  virtual TABLE< GUM_SCALAR >* combine(const Set< const TABLE< GUM_SCALAR >* >& set);
149  virtual void combine(TABLE< GUM_SCALAR >& container,
150  const Set< const TABLE< GUM_SCALAR >* >& set);
151 
152  /// Changes the function used for combining two TABLES.
153  virtual void setCombineFunction(TABLE< GUM_SCALAR >* (*combine)(const TABLE< GUM_SCALAR >&,
154  const TABLE< GUM_SCALAR >&));
155 
156  /// Returns the combination function currently used by the combinator.
157  virtual TABLE< GUM_SCALAR >* (*combineFunction())(const TABLE< GUM_SCALAR >&,
158  const TABLE< GUM_SCALAR >&);
159 
160  /**
161  * @brief returns a rough estimate of the number of operations that will be
162  * performed to compute the combination.
163  */
164  virtual float nbOperations(const Set< const TABLE< GUM_SCALAR >* >& set) const;
165  virtual float nbOperations(const Set< const Sequence< const DiscreteVariable* >* >& set) const;
166 
167  /**
168  * @brief Returns the additional memory consumption used during the
169  * combination.
170  *
171  * Actually, this function does not return a precise account of the memory
172  * used by the multidimCombination but a rough estimate based on the sizes
173  * of the tables involved in the combination. @return a pair of memory
174  * consumption: the first one is the maximum amount of memory used during
175  * the combination and the second one is the amount of memory still used at
176  * the end of the function ( the memory used by the resulting table ).
177  */
178  virtual std::pair< long, long > memoryUsage(const Set< const TABLE< GUM_SCALAR >* >& set) const;
179  virtual std::pair< long, long >
180  memoryUsage(const Set< const Sequence< const DiscreteVariable* >* >& set) const;
181 
182  /// @}
183 
184  protected:
185  /// The function used to combine two tables.
186  TABLE< GUM_SCALAR >* (*combine_)(const TABLE< GUM_SCALAR >& t1, const TABLE< GUM_SCALAR >& t2);
187 
188  /**
189  * @brief returns the domain size of the Cartesian product of the union of
190  * all the variables in seq1 and seq2.
191  */
193  const Sequence< const DiscreteVariable* >& seq2) const;
194  };
195 
196 } /* namespace gum */
197 
198 // always include the template implementation
199 #include <agrum/tools/multidim/utils/operators/multiDimCombinationDefault_tpl.h>
200 
201 #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:643
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.