aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
multiDimProjection.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  *
24  * @author Christophe GONZALES(@AMU) and Pierre-Henri WUILLEMIN(@LIP6)
25  */
26 
27 #ifndef GUM_MULTI_DIM_PROJECTION_H
28 #define GUM_MULTI_DIM_PROJECTION_H
29 
30 #include <utility>
31 
32 #include <agrum/tools/core/sequence.h>
33 #include <agrum/tools/core/set.h>
34 #include <agrum/tools/variables/discreteVariable.h>
35 
36 namespace gum {
37 
38  // clang-format off
39  /**
40  * @class MultiDimProjection
41  * @headerfile multiDimProjection.h <agrum/tools/multidim/operators/multiDimProjection.h>
42  * @ingroup multidim_op_group
43  *
44  * @brief A generic class to project efficiently a MultiDim table over a subset
45  * of its variables
46  *
47  * MultiDimProjection is a generic wrapper designed to project efficiently a
48  * multidimensional object over a subset of its variables.
49  *
50  * By multidimensional objects, we mean of course MultiDimImplementations,
51  * but also more complex objects such as, for instance, pairs of
52  * MultiDimImplementations the first one of which being a utility function
53  * and the second one being a table of instantiations (useful, e.g., for
54  * computing MPE's) but this can also be a pair (Utility,Potential) for the
55  * inference in an Influence Diagram.
56  *
57  * To be quite generic, the MultiDimProjection takes in argument the function
58  * that produces the result of the projection of the multidimensional object.
59  * The following code gives an example of the usage of MultiDimProjection:
60  *
61  * @code
62  * // a function used to project a Potential<float>:
63  * Potential<float>* MinPot ( const Potential<float>& table,
64  * const Set<const DiscreteVariable*>& del_vars ) {
65  * return new Potential<float> (...);
66  * }
67  *
68  * // another function used to project a Potential<float>:
69  * Potential<float>* MaxPot ( const Potential<float>& table,
70  * const Set<const DiscreteVariable*>& del_vars ) {
71  * return new Potential<float> (...);
72  * }
73  *
74  *
75  * Potential<float> t1, t2;
76  * Set<const DiscreteVariable*> set1, set2;
77  * MultiDimProjectionDefault<float,Potential> Proj ( MinPot );
78  * Potential<float>* projected_table = Proj.project ( t1, set1 );
79  *
80  * // change the operator to apply
81  * Proj.setProjectFunction ( MaxPot );
82  * Potential<float>* projected_table2 = Proj.project ( t2, set2 );
83  *
84  * @endcode
85  */
86  // clang-format on
87  template < typename GUM_SCALAR, template < typename > class TABLE >
89  public:
90  // ========================================================================
91  /// @name Constructors / Destructors
92  // ========================================================================
93  /// @{
94 
95  /// Default constructor
96  MultiDimProjection(TABLE< GUM_SCALAR >* (*proj)(const TABLE< GUM_SCALAR >&,
97  const Set< const DiscreteVariable* >&));
98 
99  /// Copy constructor
101 
102  /// Destructor
103  virtual ~MultiDimProjection();
104 
105  /**
106  * @brief virtual constructor
107  *
108  * @return a new fresh MultiDimCombinator with the same projection
109  * function.
110  */
111  virtual MultiDimProjection< GUM_SCALAR, TABLE >* newFactory() const;
112 
113  /// @}
114  // ========================================================================
115  /// @name Accessors/Modifiers
116  // ========================================================================
117  /// @{
118 
119  /**
120  * @brief Creates and returns the projection of the table over a subset of
121  * its vars.
122  *
123  * @return A new freshly created TABLE which is the result of the
124  * projection of the TABLE passed in argument over the set of variables NOT
125  * IN del_vars
126  *
127  * @warning If del_vars is precisely equal to the variables of table, the
128  * result is an empty table.
129  */
130  TABLE< GUM_SCALAR >* project(const TABLE< GUM_SCALAR >& table,
131  const Set< const DiscreteVariable* >& del_vars);
132 
133  void project(TABLE< GUM_SCALAR >& container,
134  const TABLE< GUM_SCALAR >& table,
135  const Set< const TABLE< GUM_SCALAR >* >& del_vars);
136 
137  /// Changes the function used for projecting TABLES
138  void setProjectFunction(TABLE< GUM_SCALAR >* (*proj)(const TABLE< GUM_SCALAR >&,
139  const Set< const DiscreteVariable* >&));
140 
141  /// Returns the projection function currently used by the projector
142  TABLE< GUM_SCALAR >* (*projectFunction())(const TABLE< GUM_SCALAR >&,
143  const Set< const DiscreteVariable* >&);
144 
145  /**
146  * @brief returns a rough estimate of the number of operations that will be
147  * performed to compute the projection.
148  */
149  float nbOperations(const TABLE< GUM_SCALAR >& table,
150  const Set< const DiscreteVariable* >& del_vars) const;
151 
152  /**
153  * @brief returns a rough estimate of the number of operations that will be
154  * performed to compute the projection.
155  */
156  float nbOperations(const Sequence< const DiscreteVariable* >& vars,
157  const Set< const DiscreteVariable* >& del_vars) const;
158 
159  /**
160  * @brief Returns the memory consumption used during the projection.
161  *
162  * Actually, this function does not return a precise account of the memory
163  * used by the multidimProjection but a rough estimate based on the size of
164  * the table involved in the projection.
165  *
166  * @return A pair of memory consumption: the first one is the maximum
167  * amount of memory used during the combination and the second one is the
168  * amount of memory still used at the end of the function ( the memory used
169  * by the resulting table )
170  */
171  std::pair< long, long > memoryUsage(const TABLE< GUM_SCALAR >& table,
172  const Set< const DiscreteVariable* >& del_vars) const;
173 
174  /**
175  * @brief Returns the memory consumption used during the projection.
176  *
177  * Actually, this function does not return a precise account of the memory
178  * used by the multidimProjection but a rough estimate based on the size of
179  * the table involved in the projection.
180  *
181  * @return A pair of memory consumption: the first one is the maximum
182  * amount of memory used during the combination and the second one is the
183  * amount of memory still used at the end of the function ( the memory used
184  * by the resulting table )
185  */
186  std::pair< long, long > memoryUsage(const Sequence< const DiscreteVariable* >& vars,
187  const Set< const DiscreteVariable* >& del_vars) const;
188 
189  /// @}
190 
191  protected:
192  /// The projection function actually used
193  TABLE< GUM_SCALAR >* (*proj_)(const TABLE< GUM_SCALAR >&,
194  const Set< const DiscreteVariable* >&);
195 
196  private:
197  /// Forbid copy operators
200  };
201 
202 } /* namespace gum */
203 
204 // always include the template implementation
205 #include <agrum/tools/multidim/utils/operators/multiDimProjection_tpl.h>
206 
207 #endif /* GUM_MULTI_DIM_PROJECTION_H */
std::pair< long, long > memoryUsage(const Sequence< const DiscreteVariable * > &vars, const Set< const DiscreteVariable * > &del_vars) const
Returns the memory consumption used during the projection.
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:643
MultiDimProjection(TABLE< GUM_SCALAR > *(*proj)(const TABLE< GUM_SCALAR > &, const Set< const DiscreteVariable * > &))
Default constructor.
void project(TABLE< GUM_SCALAR > &container, const TABLE< GUM_SCALAR > &table, const Set< const TABLE< GUM_SCALAR > * > &del_vars)
Creates and returns the projection of the table over a subset of its vars.
std::pair< long, long > memoryUsage(const TABLE< GUM_SCALAR > &table, const Set< const DiscreteVariable * > &del_vars) const
Returns the memory consumption used during the projection.
virtual MultiDimProjection< GUM_SCALAR, TABLE > * newFactory() const
virtual constructor
MultiDimProjection(const MultiDimProjection< GUM_SCALAR, TABLE > &)
Copy constructor.
virtual ~MultiDimProjection()
Destructor.
MultiDimProjection< GUM_SCALAR, TABLE > & operator=(const MultiDimProjection< GUM_SCALAR, TABLE > &)
Forbid copy operators.
float nbOperations(const Sequence< const DiscreteVariable * > &vars, const Set< const DiscreteVariable * > &del_vars) const
returns a rough estimate of the number of operations that will be performed to compute the projection...
float nbOperations(const TABLE< GUM_SCALAR > &table, const Set< const DiscreteVariable * > &del_vars) const
returns a rough estimate of the number of operations that will be performed to compute the projection...
A generic class to project efficiently a MultiDim table over a subset of its variables.
void setProjectFunction(TABLE< GUM_SCALAR > *(*proj)(const TABLE< GUM_SCALAR > &, const Set< const DiscreteVariable * > &))
Changes the function used for projecting TABLES.
TABLE< GUM_SCALAR > * project(const TABLE< GUM_SCALAR > &table, const Set< const DiscreteVariable * > &del_vars)
Creates and returns the projection of the table over a subset of its vars.