aGrUM  0.20.2
a C++ library for (probabilistic) graphical models
scheduleMultiDim.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  * @brief a MultiDimImplementation Wrapper used for scheduling inferences
24  *
25  * @author Christophe GONZALES(@AMU) and Pierre-Henri WUILLEMIN(@LIP6)
26  */
27 #ifndef GUM_SCHEDULE_MULTI_DIM_H
28 #define GUM_SCHEDULE_MULTI_DIM_H
29 
30 #include <string>
31 
32 #include <agrum/agrum.h>
33 
34 #include <agrum/tools/core/hashTable.h>
35 #include <agrum/tools/core/sequence.h>
36 #include <agrum/tools/multidim/implementations/multiDimDecorator.h>
37 #include <agrum/tools/multidim/implementations/multiDimImplementation.h>
38 #include <agrum/tools/variables/discreteVariable.h>
39 
40 namespace gum {
41 
42 #ifndef DOXYGEN_SHOULD_SKIP_THIS
43  // we should grant ScheduleDeleteMultiDim the access to the hashtable actually
44  // containing the MultiDimImplementation: thus, when ScheduleDeleteMultiDim
45  // deletes an implementation, it can remove it from the hashtable and, thus,
46  // the ScheduleMultiDims pointing to it will become abstract.
47  template < typename GUM_SCALAR >
49 #endif
50 
51  /**
52  * @class ScheduleMultiDim
53  * @brief a MultiDimImplementation Wrapper used for scheduling inferences
54  *
55  * A ScheduleMultiDim is a wrapper that contains either a "real" multidim
56  * table or an Id that indicates that the multidim table that should be
57  * contained has not been computed yet (by the scheduler). This Id enables
58  * the scheduler to know which is the operation the result of which will be
59  *the
60  * multidim table that will eventually be contained in the ScheduleMultiDim.
61  * Here is a brief piece of code that should highlight the concept:
62  * @code
63  * // some arbitrary potentials (to be initialized before going on)
64  * gum::Potential<float> pot1, pot2, pot3;
65  *
66  * // we wish to schedule ( pot1 + pot2 ) + pot3
67  * // so, first, create ScheduleMultiDims for wrapping these potentials
68  * gum::ScheduleMultiDim<float> f1 ( pot1 ), f2 ( pot2 ), f3 ( pot3 );
69  *
70  * // now schedule a combination (+) between f1 and f2
71  * gum::ScheduleCombine<float> comb1 ( &f1, &f2, add );
72  *
73  * // get the result and schedule it with f3
74  * const ScheduleMultiDim<float>& result1 = comb1.result ();
75  * gum::ScheduleCombine<float> comb2 ( &result2, &f3,add );
76  *
77  * // get the resulting ScheduleMultiDim
78  * const ScheduleMultiDim<float>& result2 = comb2.result ();
79  *
80  * // here, no addition has been performed yet. We just have a structure
81  * // that indicates which operations we wish to do. So, for the moment,
82  * // result1 and result2 do not contain real multidim tables but just ids.
83  * // As such, they are called abstract and trying to get their "real"
84  * // multiDim table (using method multiDim()) would throw a NotFound
85  *exception.
86  * std::cout << result1.isAbstract ();
87  * std::cout << result2.isAbstract ();
88  * std::cout << ! f1.isAbstract ();
89  *
90  * // now, we can actually perform the operations
91  * comb1.execute ();
92  * std::cout << ! result1.isAbstract ();
93  * comb2.execute ();
94  *
95  * // here, we can display the content of the real multidim table stored
96  * // into result2
97  * std::cout << result2.multiDim ();
98  * @endcode
99  *
100  * So, to summarize the key idea underlying Schedule* classes: these classes
101  * encapsulate operations to perform and multidim tables that should be passed
102  * as argument to these operations. But nothing is actually computed until
103  * the execute() methods of the scheduled operations are executed.
104  */
105  template < typename GUM_SCALAR >
107  public:
108  // ############################################################################
109  /// @name Constructors / Destructors
110  // ############################################################################
111  /// @{
112 
113  /// constructs a ScheduleMultiDim containing an already built implementation
115 
116  /// constructs a ScheduleMultiDim containing an already built implementation
117  explicit ScheduleMultiDim(const MultiDimDecorator< GUM_SCALAR >&);
118 
119  /// construct a ScheduleMultiDim for an implementation yet to be built
120  /** The ScheduleMultiDim created is abstract, i.e., it does not contain a
121  * proper MultiDimImplementation yet. However, the variables of the latter
122  * need be known to optimize inference processes
123  * @warning the sequence of variables is copied into the wrapper. */
124  explicit ScheduleMultiDim(const Sequence< const DiscreteVariable* >& vars);
125 
126  /// copy constructor
127  ScheduleMultiDim(const ScheduleMultiDim< GUM_SCALAR >&);
128 
129  /// destructor
130  ~ScheduleMultiDim();
131 
132  /// @}
133 
134  // ############################################################################
135  /// @name Operators
136  // ############################################################################
137 
138  /// @{
139 
140  /// copy operator
141  ScheduleMultiDim< GUM_SCALAR >&
142  operator=(const ScheduleMultiDim< GUM_SCALAR >&);
143 
144  /// checks whether two ScheduleMultiDim are related to the same table
145  bool operator==(const ScheduleMultiDim< GUM_SCALAR >&) const;
146 
147  /// checks whether two ScheduleMultiDim are related to different tables
148  bool operator!=(const ScheduleMultiDim< GUM_SCALAR >&) const;
149 
150  /// @}
151 
152  // ############################################################################
153  /// @name Accessors/Modifiers
154  // ############################################################################
155  /// @{
156 
157  /// returns whether the ScheduleMultiDim contains a real
158  /// multiDimImplementation
159  /** @returns true if the ScheduleMultiDim is abstract, i.e., it is does not
160  * actually contains a real MultiDimImplementation but rather a ID
161  * indicating
162  * that the real multiDimImplementation is yet to be created as a result of
163  * an
164  * operation on other multiDimImplementations. */
165  bool isAbstract() const;
166 
167  /** @brief returns the multiDimImplementation actually contained in the
168  * ScheduleMultiDim
169  *
170  * @throws NotFound exception is thrown if the multidimImplementation does
171  *not
172  * exist yet (because it has not been computed yet) */
173  const MultiDimImplementation< GUM_SCALAR >& multiDim() const;
174 
175  /// returns the id of the ScheduleMultiDim
176  Idx id() const;
177 
178  /// returns the set of variables involved in the multidim
179  const Sequence< const DiscreteVariable* >& variablesSequence() const;
180 
181  /// returns the domain size of the multidim
182  Size domainSize() const;
183 
184  /// sets a new multiDimImplementation inside the wrapper
185  /** @throws DuplicateElement exception is thrown if the
186  * MultiDimImplementation
187  * has already been wrapped in a ScheduleMultiDim with another id */
189 
190  /// sets a new multiDimDecorator inside the wrapper
191  /** @throws DuplicateElement exception is thrown if the MultiDimDecorator
192  * has already been wrapped in a ScheduleMultiDim with another id */
193  void setMultiDim(const MultiDimDecorator< GUM_SCALAR >&);
194 
195  /// displays the content of the multidim
196  std::string toString() const;
197 
198  /// @}
199 
200  private:
201  /// grant accesses to ScheduleDeleteMultiDim
202  friend class ScheduleDeleteMultiDim< GUM_SCALAR >;
203 
204  /// the unique Id of the ScheduleMultiDim
206 
207  /// returns a new distinct ID for each abtract scheduleMultiDim
208  static Idx newId__();
209 
210  /// returns a mapping from id to multidimImplementations
211  static HashTable< Idx, const MultiDimImplementation< GUM_SCALAR >* >&
212  id2multidim__();
213 
214  /// returns the id corresponding to a given multidim
215  /** useful to assign the same id every time a given MultiDimImplementation
216  * is wrapped into a ScheduleMultiDim */
217  static HashTable< const MultiDimImplementation< GUM_SCALAR >*, Idx >&
218  multidim2id__();
219 
220  /// returns a table indicating how many ScheduleMultiDim have the same id
221  static HashTable< Idx, Idx >& id2refs__();
222 
223  /// returns a table with the variables of the table corresponding to id
224  static HashTable< Idx, const Sequence< const DiscreteVariable* >* >&
225  id2vars__();
226 
227  /// returns a table with the domain size of the table corresponding to id
228  static HashTable< Idx, Size >& id2size__();
229  };
230 
231 } /* namespace gum */
232 
233 // always include the template implementation
234 #include <agrum/tools/graphicalModels/inference/scheduler/scheduleMultiDim_tpl.h>
235 
236 #endif /* GUM_SCHEDULE_MULTI_DIM_H */
const Sequence< const DiscreteVariable *> & variablesSequence() const
returns the set of variables involved in the multidim
std::string toString() const
displays the content of the multidim
bool isAbstract() const
returns whether the ScheduleMultiDim contains a real multiDimImplementation
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:669
Idx id() const
returns the id of the ScheduleMultiDim
ScheduleMultiDim(const ScheduleMultiDim< GUM_SCALAR > &)
copy constructor
Size domainSize() const
returns the domain size of the multidim
static HashTable< Idx, Size > & id2size__()
returns a table with the domain size of the table corresponding to id
bool operator!=(const ScheduleMultiDim< GUM_SCALAR > &) const
checks whether two ScheduleMultiDim are related to different tables
ScheduleMultiDim< GUM_SCALAR > & operator=(const ScheduleMultiDim< GUM_SCALAR > &)
copy operator
Idx id__
the unique Id of the ScheduleMultiDim
static HashTable< const MultiDimImplementation< GUM_SCALAR > *, Idx > & multidim2id__()
returns the id corresponding to a given multidim
~ScheduleMultiDim()
destructor
static HashTable< Idx, Idx > & id2refs__()
returns a table indicating how many ScheduleMultiDim have the same id
void setMultiDim(const MultiDimDecorator< GUM_SCALAR > &)
sets a new multiDimDecorator inside the wrapper
const MultiDimImplementation< GUM_SCALAR > & multiDim() const
returns the multiDimImplementation actually contained in the ScheduleMultiDim
ScheduleMultiDim(const Sequence< const DiscreteVariable * > &vars)
construct a ScheduleMultiDim for an implementation yet to be built
static Idx newId__()
returns a new distinct ID for each abtract scheduleMultiDim
static HashTable< Idx, const Sequence< const DiscreteVariable *> *> & id2vars__()
returns a table with the variables of the table corresponding to id
bool operator==(const ScheduleMultiDim< GUM_SCALAR > &) const
checks whether two ScheduleMultiDim are related to the same table