aGrUM  0.20.2
a C++ library for (probabilistic) graphical models
multiDimProjection.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 /** @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 >* (
97  *proj)(const TABLE< GUM_SCALAR >&, 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 >* (
139  *proj)(const TABLE< GUM_SCALAR >&, const Set< const DiscreteVariable* >&));
140 
141  /// Returns the projection function currently used by the projector
142  TABLE< GUM_SCALAR >* (*projectFunction())(
143  const TABLE< GUM_SCALAR >&,
144  const Set< const DiscreteVariable* >&);
145 
146  /**
147  * @brief returns a rough estimate of the number of operations that will be
148  * performed to compute the projection.
149  */
150  float nbOperations(const TABLE< GUM_SCALAR >& table,
151  const Set< const DiscreteVariable* >& del_vars) const;
152 
153  /**
154  * @brief returns a rough estimate of the number of operations that will be
155  * performed to compute the projection.
156  */
157  float nbOperations(const Sequence< const DiscreteVariable* >& vars,
158  const Set< const DiscreteVariable* >& del_vars) const;
159 
160  /**
161  * @brief Returns the memory consumption used during the projection.
162  *
163  * Actually, this function does not return a precise account of the memory
164  * used by the multidimProjection but a rough estimate based on the size of
165  * the table involved in the projection.
166  *
167  * @return A pair of memory consumption: the first one is the maximum
168  * amount of memory used during the combination and the second one is the
169  * amount of memory still used at the end of the function ( the memory used
170  * by the resulting table )
171  */
172  std::pair< long, long >
173  memoryUsage(const TABLE< GUM_SCALAR >& table,
174  const Set< const DiscreteVariable* >& del_vars) const;
175 
176  /**
177  * @brief Returns the memory consumption used during the projection.
178  *
179  * Actually, this function does not return a precise account of the memory
180  * used by the multidimProjection but a rough estimate based on the size of
181  * the table involved in the projection.
182  *
183  * @return A pair of memory consumption: the first one is the maximum
184  * amount of memory used during the combination and the second one is the
185  * amount of memory still used at the end of the function ( the memory used
186  * by the resulting table )
187  */
188  std::pair< long, long >
189  memoryUsage(const Sequence< const DiscreteVariable* >& vars,
190  const Set< const DiscreteVariable* >& del_vars) const;
191 
192  /// @}
193 
194  protected:
195  /// The projection function actually used
196  TABLE< GUM_SCALAR >* (*proj_)(const TABLE< GUM_SCALAR >&,
197  const Set< const DiscreteVariable* >&);
198 
199  private:
200  /// Forbid copy operators
203  };
204 
205 } /* namespace gum */
206 
207 // always include the template implementation
208 #include <agrum/tools/multidim/utils/operators/multiDimProjection_tpl.h>
209 
210 #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:669
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.