aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
scheduleCombinationBasic.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 /** @file
23  * @brief A class to combine efficiently several ScheduleMultiDims
24  *
25  * MultiDimCombinationBasic is a class designed to combine efficiently
26  * several ScheduleMultiDims, that is, to compute expressions like
27  * T1 op T2 op T3 op .... op Tn, where the Ti's are either the ScheduleMultiDims
28  * or just MultiDimImplementations that will be later wrapped into
29  * ScheduleMultiDims and op is an operator or a function taking in argument two
30  * such objects and producing a new (combined) Ti object. By default, the
31  * combination operation "op" is assumed to be COMMUTATIVE and ASSOCIATIVE.
32  *
33  * To be quite generic, the MultiDimCombinationBasic takes in argument the
34  * function that produces the result of the combination of two multidimensional
35  * objects. The following code gives an example of the usage of
36  * MultiDimCombinationBasics:
37  * @code
38  * // a function used to combine two MultiDimImplementation<float>'s:
39  * MultiDimImplementation<float>* add ( const MultiDimImplementation<float>& t1,
40  * const MultiDimImplementation<float>& t2
41  *) {
42  * return new MultiDimImplementation<float> (t1 + t2);
43  * }
44  *
45  * // another function used to combine two MultiDimImplementation<float>'s:
46  * MultiDimImplementation<float>*
47  * mult ( const MultiDimImplementation<float>& t1,
48  * const MultiDimImplementation<float>& t2 ) {
49  * return new MultiDimImplementation<float> (t1 * t2);
50  * }
51  *
52  *
53  * MultiDimImplementation<float> t1, t2, t3;
54  * Set<const MultiDimImplementation<float>*> set;
55  * set << &t1 << &t2 << &t3;
56  * ScheduleCombinationBasic<float> Comb ( add );
57  * ScheduleMultiDim<float> combined_table = Comb.combine ( set );
58  *
59  * // do the same thing only with ScheduleMultiDims
60  * MultiDimImplementation<float> tt4, tt5;
61  * ScheduleMultiDim<float> t4 ( &tt4 ), t5 ( &tt5 );
62  * Set<const ScheduleMultiDim<float>*> set;
63  * set << &t4 << &t5;
64  * ScheduleCombinationBasic<float> Comb ( add );
65  * ScheduleMultiDim<float> combined_table2 = Comb.combine ( set );
66  *
67  * // change the operator to apply
68  * Comb.setCombineFunction ( mult );
69  * ScheduleMultiDim<float> combined_table3 = Comb.combine ( set );
70  *
71  * @endcode
72  *
73  * @author Christophe GONZALES(@AMU) and Pierre-Henri WUILLEMIN(@LIP6)
74  */
75 
76 #ifndef GUM_SCHEDULE_COMBINATION_BASIC_H
77 #define GUM_SCHEDULE_COMBINATION_BASIC_H
78 
79 #include <agrum/agrum.h>
80 
81 #include <agrum/tools/core/priorityQueue.h>
82 #include <agrum/tools/core/sequence.h>
83 #include <agrum/tools/graphicalModels/inference/scheduler/scheduleCombination.h>
84 #include <agrum/tools/variables/discreteVariable.h>
85 
86 namespace gum {
87 
88  template < typename GUM_SCALAR >
90  public:
91  // ############################################################################
92  /// @name Constructors / Destructors
93  // ############################################################################
94  /// @{
95 
96  /// default constructor
100 
101  /// copy constructor
103 
104  /// destructor
105  virtual ~ScheduleCombinationBasic();
106 
107  /// virtual constructor
108  /** @return a new fresh ScheduleCombinator with the same combination
109  * function. */
110  virtual ScheduleCombinationBasic< GUM_SCALAR >* newFactory() const;
111 
112  /// @}
113 
114  // ############################################################################
115  /// @name Accessors/Modifiers
116  // ############################################################################
117  /// @{
118 
119  // adds to a given schedule the operations necessary to perform a
120  // combination
121  /** @returns the ScheduleMultiDim resulting from the combination
122  * @throws InvalidArgumentsNumber exception is thrown if the set passed in
123  * argument contains less than two elements */
124  virtual ScheduleMultiDim< GUM_SCALAR >
125  combine(const Set< const ScheduleMultiDim< GUM_SCALAR >* >& set, Schedule< GUM_SCALAR >&);
127  combine(const Set< const MultiDimImplementation< GUM_SCALAR >* >& set,
128  Schedule< GUM_SCALAR >&);
129  template < template < typename > class TABLE >
130  ScheduleMultiDim< GUM_SCALAR > combine(const Set< const TABLE< GUM_SCALAR >* >& set,
131  Schedule< GUM_SCALAR >&);
132 
133  /// changes the function used for combining two TABLES
137 
138  /// returns the combination function currently used by the combinator
139  virtual MultiDimImplementation< GUM_SCALAR >* (*combineFunction())(
140  const MultiDimImplementation< GUM_SCALAR >&,
141  const MultiDimImplementation< GUM_SCALAR >&);
142 
143  /** @brief returns a rough estimate of the number of operations that will be
144  * performed to compute the combination */
145  virtual float nbOperations(const Set< const ScheduleMultiDim< GUM_SCALAR >* >& set,
146  const Schedule< GUM_SCALAR >& schedule);
147  float nbOperations(const Set< const MultiDimImplementation< GUM_SCALAR >* >& set,
148  const Schedule< GUM_SCALAR >& schedule);
149  template < template < typename > class TABLE >
150  float nbOperations(const Set< const TABLE< GUM_SCALAR >* >& set,
151  const Schedule< GUM_SCALAR >& schedule);
152 
153  /// returns the memory consumption used during the combination
154  /** Actually, this function does not return a precise account of the memory
155  * used by the multidimCombination but a rough estimate based on the sizes
156  * of the tables involved in the combination.
157  * @return a pair of memory consumption: the first one is the maximum
158  * amount of memory used during the combination and the second one is the
159  * amount of memory still used at the end of the function ( the memory used
160  * by
161  * the resulting table ) */
162  virtual std::pair< long, long >
163  memoryUsage(const Set< const ScheduleMultiDim< GUM_SCALAR >* >& set,
164  const Schedule< GUM_SCALAR >& schedule);
165  std::pair< long, long >
167  const Schedule< GUM_SCALAR >& schedule);
168  template < template < typename > class TABLE >
169  std::pair< long, long > memoryUsage(const Set< const TABLE< GUM_SCALAR >* >& set,
170  const Schedule< GUM_SCALAR >& schedule);
171 
172  /// @}
173 
174  protected:
175  /// the function used to combine two tables
179 
180  /** @brief returns the domain size of the Cartesian product of the union of
181  * all the variables in seq1 and seq2 */
183  const Sequence< const DiscreteVariable* >& seq2) const;
184  };
185 
186 } /* namespace gum */
187 
188 // always include the template implementation
189 #include <agrum/tools/graphicalModels/inference/scheduler/scheduleCombinationBasic_tpl.h>
190 
191 #endif /* GUM_SCHEDULE_COMBINATION_BASIC_H */
virtual ~ScheduleCombinationBasic()
destructor
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:643
std::pair< long, long > memoryUsage(const Set< const TABLE< GUM_SCALAR > * > &set, const Schedule< GUM_SCALAR > &schedule)
virtual ScheduleCombinationBasic< GUM_SCALAR > * newFactory() const
virtual constructor
float nbOperations(const Set< const TABLE< GUM_SCALAR > * > &set, const Schedule< GUM_SCALAR > &schedule)
float nbOperations(const Set< const MultiDimImplementation< GUM_SCALAR > * > &set, const Schedule< GUM_SCALAR > &schedule)
ScheduleMultiDim< GUM_SCALAR > combine(const Set< const TABLE< GUM_SCALAR > * > &set, Schedule< GUM_SCALAR > &)
ScheduleCombinationBasic(MultiDimImplementation< GUM_SCALAR > *(*combine)(const MultiDimImplementation< GUM_SCALAR > &, const MultiDimImplementation< GUM_SCALAR > &))
default constructor
std::pair< long, long > memoryUsage(const Set< const MultiDimImplementation< GUM_SCALAR > * > &set, const Schedule< GUM_SCALAR > &schedule)
virtual void setCombineFunction(MultiDimImplementation< GUM_SCALAR > *(*combine)(const MultiDimImplementation< GUM_SCALAR > &, const MultiDimImplementation< GUM_SCALAR > &))
changes the function used for combining two TABLES
ScheduleMultiDim< GUM_SCALAR > combine(const Set< const MultiDimImplementation< GUM_SCALAR > * > &set, Schedule< GUM_SCALAR > &)
ScheduleCombinationBasic(const ScheduleCombinationBasic< GUM_SCALAR > &)
copy 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 ...