aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
multiDimFunctionGraph.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  * @brief Headers of MultiDimFunctionGraph.
25  *
26  * @author Pierre-Henri WUILLEMIN(@LIP6) & Christophe GONZALES(@AMU)
27  * @author Pierre-Henri WUILLEMIN(@LIP6) and Jean-Christophe MAGNAN and Christophe
28  * GONZALES(@AMU)
29  */
30 #ifndef GUM_MULTI_DIM_FUNCTION_GRAPH_H
31 #define GUM_MULTI_DIM_FUNCTION_GRAPH_H
32 
33 #include <agrum/tools/core/bijection.h>
34 #include <agrum/tools/core/list.h>
35 #include <agrum/tools/core/smallobjectallocator/smallObjectAllocator.h>
36 #include <agrum/tools/core/types.h>
37 
38 #include <agrum/FMDP/learning/core/templateStrategy.h>
39 
40 #include <agrum/tools/graphs/graphElements.h>
41 
42 #include <agrum/tools/multidim/implementations/multiDimFunctionGraphManager.h>
43 #include <agrum/tools/multidim/implementations/multiDimImplementation.h>
44 #include <agrum/tools/multidim/utils/FunctionGraphUtilities/internalNode.h>
45 #include <agrum/tools/multidim/utils/FunctionGraphUtilities/link.h>
46 #include <agrum/tools/multidim/utils/FunctionGraphUtilities/terminalNodePolicies/ExactTerminalNodePolicy.h>
47 #include <agrum/tools/multidim/utils/FunctionGraphUtilities/terminalNodePolicies/ITerminalNodePolicy.h>
48 
49 namespace gum {
50 
51  template < typename GUM_SCALAR, template < typename > class TerminalNodePolicy >
53 
54  template < typename GUM_SCALAR, template < typename > class TerminalNodePolicy >
56 
57  template < typename GUM_SCALAR, template < typename > class TerminalNodePolicy >
59 
60  /**
61  * @class MultiDimFunctionGraph
62  * @ingroup multidim_group
63  *
64  * @warning Doxygen does not like spanning command on multiple line, so we
65  * could not configure it with the correct include directive. Use the
66  * following code snippet to include this file.
67  * @code
68  * #include <agrum/tools/multidim/implementations/multiDimFunctionGraph.h>
69  * @endcode
70  *
71  * @brief Class implementingting a function graph.
72  *
73  * @tparam GUM_SCALAR The type of scalars stored in this multidimensional
74  * table.
75  * @tparam TerminalNodePolicy The terminal node policy to use in this
76  * MultiDimFunctionGraph.
77  */
78  template < typename GUM_SCALAR,
79  template < typename > class TerminalNodePolicy = ExactTerminalNodePolicy >
82  public TerminalNodePolicy< GUM_SCALAR > {
83  public:
84  /// Only for proper initialization of a certain returned value
85  const static GUM_SCALAR defaultValue;
86 
87  public:
88  // =========================================================================
89  /// @name Constructors, destructor and copy
90  // =========================================================================
91  /// @{
92 
93  private:
94  /**
95  * @brief Default constructor.
96  * @param isReduced If true, then this MultiDimFunctionGraph is reduced.
97  */
98  MultiDimFunctionGraph(bool isReduced = true);
99 
100  public:
101  /**
102  * @brief Copy constructor.
103  * @param from The MultiDimFunctionGraph to copy.
104  */
105  MultiDimFunctionGraph(const MultiDimFunctionGraph< GUM_SCALAR, TerminalNodePolicy >& from);
106 
107  /**
108  * @brief Copy Operator.
109  * @param from The MultiDimFunctionGraph to copy.
110  * @return Returns this MultiDimFunctionGraph.
111  */
114 
115  /**
116  * @brief Class destructor.
117  */
119 
120  /// @}
121  // =========================================================================
122  /// @name Unallowed inherited methods
123  // =========================================================================
124  /// @{
125 
126  /**
127  * @warning This will raise an OperationNotAllowed exceptions. Use the
128  * MultiDimFunctionGraphManager class to edit a MultiDimFunctionGraph.
129  */
130  virtual void set(const Instantiation& i, const GUM_SCALAR& value) const;
131 
132  /**
133  * @warning This will raise an OperationNotAllowed exceptions. Use the
134  * MultiDimFunctionGraphManager class to edit a MultiDimFunctionGraph.
135  */
136  virtual void fill(const GUM_SCALAR& d) const;
137 
138  /**
139  * @warning This will raise an OperationNotAllowed exceptions. Use the
140  * MultiDimFunctionGraphManager class to edit a MultiDimFunctionGraph.
141  */
142  virtual void populate(const std::vector< GUM_SCALAR >& v) const;
143  /**
144  * @warning This will raise an OperationNotAllowed exceptions. Use the
145  * MultiDimFunctionGraphManager class to edit a MultiDimFunctionGraph.
146  */
147  virtual void populate(std::initializer_list< GUM_SCALAR > v) const;
148 
149  /**
150  * @warning This will raise an OperationNotAllowed as MultiDimFunctionGraph
151  * can't copy other multiDim.
152  */
153  virtual void copyFrom(const MultiDimContainer< GUM_SCALAR >& src,
154  Instantiation* p_i = (Instantiation*)0) const;
155 
156  /**
157  * @warning This will raise an OperationNotAllowed as MultiDimFunctionGraph
158  * can't copy other multiDim.
159  */
160  virtual void copy(const MultiDimContainer< GUM_SCALAR >& src);
161 
162  /// @}
163  // =========================================================================
164  /// @name Inherited methods
165  // =========================================================================
166  /// @{
167 
168  virtual MultiDimContainer< GUM_SCALAR >* newFactory() const;
169 
170  virtual GUM_SCALAR get(const Instantiation& i) const;
171 
172  virtual const std::string& name() const;
173 
174  virtual void add(const DiscreteVariable& v);
175 
176  virtual void erase(const DiscreteVariable& v);
177 
178  virtual Size realSize() const;
179 
180  virtual void changeNotification(const Instantiation& i,
181  const DiscreteVariable* const var,
182  Idx oldval,
183  Idx newval);
184 
185  virtual void setFirstNotification(const Instantiation& i);
186 
187  virtual void setLastNotification(const Instantiation& i);
188 
189  virtual void setIncNotification(const Instantiation& i);
190 
191  virtual void setDecNotification(const Instantiation& i);
192 
193  virtual void setChangeNotification(const Instantiation& i);
194 
195  virtual std::string toString(const Instantiation* i) const;
196 
197  /// @}
198  // =========================================================================
199  /// @name Copy methods.
200  // =========================================================================
201  /// @{
202 
203  /**
204  * @brief Removes all variables in this MultiDimFunctionGraph and copy the
205  * content of src, variables included.
206  *
207  * @param src The MultiDimFunctionGraph to copy.
208  */
209  void copy(const MultiDimFunctionGraph< GUM_SCALAR, TerminalNodePolicy >& src);
210 
211  /**
212  * @brief Copies src diagrams structure into this diagrams.
213  *
214  * However it also changes the variables.
215  *
216  * @warning This has two implications:
217  * - First, this is not just a renaming. Pointers are trully changed.
218  * - Second, for each pair of variable, the new variable MUST macth the
219  * number of modalities of the old variable.
220  *
221  * @param src The MultiDimFunctionGraph to copy.
222  * @param reassign A Bijection form variables in src to variables in this.
223  */
224  void copyAndReassign(
225  const MultiDimFunctionGraph< GUM_SCALAR, TerminalNodePolicy >& src,
226  const Bijection< const DiscreteVariable*, const DiscreteVariable* >& reassign);
227 
228  /**
229  * @brief Copies src diagrams and multiply every value by the given scalar.
230  *
231  * @param src The MultiDimFunctionGraph to copy.
232  * @param gamma The scalar used to multiply every value with.
233  */
234  void copyAndMultiplyByScalar(const MultiDimFunctionGraph< GUM_SCALAR, TerminalNodePolicy >& src,
235  GUM_SCALAR gamma);
236 
237  /**
238  * @brief Clears the function graph
239  */
240  void clear();
241 
242  /// @}
243  // =========================================================================
244  /// @name Accessors and modifiers
245  // =========================================================================
246  /// @{
247 
248  /**
249  * @brief Returns a const reference to the manager of this diagram
250  *
251  * @param withBackArcs If true, back arcs will be added.
252  * @return Returns a const reference to the manager of this diagram
253  */
254  std::string toDot(bool withBackArcs = false) const;
255 
256  /**
257  * @brief Returns a const reference to the manager of this diagram
258  * @return Returns a const reference to the manager of this diagram
259  */
260  const NodeGraphPart& model() const;
261 
262  /**
263  * @brief Returns a const reference to the manager of this diagram
264  * @return Returns a const reference to the manager of this diagram
265  */
267 
268  /**
269  * @brief Returns the id of the root node from the diagram
270  * @return Returns the id of the root node from the diagram
271  */
272  const NodeId& root() const;
273 
274  /**
275  * @brief Indicates if given node is terminal or not
276  * @param node The node to test for terminality.
277  * @return Returns true if node is terminal.
278  */
279  bool isTerminalNode(const NodeId& node) const;
280 
281  /**
282  * @brief Indicates if given node is terminal or not
283  * @param node The node to test for being itnernal.
284  * @return Returns true if node is internal.
285  */
286  bool isInternalNode(const NodeId& node) const;
287 
288  /**
289  * @brief Returns value associated to given node.
290  * @param n The node for which the value is returned.
291  * @return Returns value associated to given node.
292  * @throw InvalidNode Raised if node isn't terminal.
293  */
294  const GUM_SCALAR& nodeValue(NodeId n) const;
295 
296  /**
297  * @brief Returns internalNode structure associated to that nodeId
298  * @param n The node for which the internal structure is returned.
299  * @return Returns internalNode structure associated to that nodeId
300  * @throw InvalidNode Raised if node is terminal
301  */
302  const InternalNode* node(NodeId n) const;
303 
304  /**
305  * @brief Returns the list of node associated to given variable
306  * @param var The variable for which the list of associated nodes is
307  * returned.
308  * @return Returns the list of node associated to given variable
309  */
310  const LinkedList< NodeId >* varNodeListe(const DiscreteVariable* var) const;
311 
312  /**
313  * @brief Returns the name of the table represented by this structure.
314  * @return Returns the name of the table represented by this structure.
315  */
316  const std::string& tableName() const;
317 
318  /**
319  * @brief Sets the name of the table represented by this structure.
320  * @param name the new name of this structure.
321  */
322  void setTableName(const std::string& name);
323 
324  /**
325  * @brief Returns true if this MultiDimFunctionGraph is reduced and
326  * Ordered.
327  * @return Returns true if this MultiDimFunctionGraph is reduced and
328  * Ordered.
329  */
330  bool isReducedAndOrdered() const;
331 
332  /**
333  * @brief Returns a reduced and ordered instance.
334  *
335  * Reduced and ordered instance will reduce the size of the graph whenever
336  * it's possible. An inherent order on the variable helps doing so. The
337  * order in which variables will be inserted with function add(const
338  * DiscreteVariable&) specify that order.
339  *
340  * @return Returns a reduced and ordered instance.
341  */
343 
344  /**
345  * @brief Returns an arborescent instance
346  * @return Returns an arborescent instance
347  */
349 
350  /// @}
351 
352  protected:
353  /**
354  * @warning This will raise an OperationNotAllowed as this method has not
355  * yet been implemented.
356  */
357  virtual void replace_(const DiscreteVariable* x, const DiscreteVariable* y);
358 
359  /**
360  * @warning This will raise an OperationNotAllowed as you should not use
361  * this method as MultiDimFunctionGraph use its own internal structure for
362  * storing data.
363  */
364  GUM_SCALAR& get_(const Instantiation& inst) const;
365 
366  private:
367  /// The name of the data structure
369 
370  /// The name of the data structure
372 
373  /// Indicates available nodeIds
375 
376  /// A reference to the manager that edits this function graph
378 
379  /// The root node of the function graph
381 
382  /// Associates each non-terminal node to a variable
384 
385  /// Mapping between var and node
387 
388  /// Wheter the MultiDimFunctionGraphManager is reduced or not
390 
392  };
393 
394 
395 #ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
396 # ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
397  // extern template class MultiDimFunctionGraph<float>;
398 # endif
399 #endif
400 #ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
401 # ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
402  // extern template class MultiDimFunctionGraph<double>;
403 # endif
404 #endif
405 } // namespace gum
406 
407 #include <agrum/tools/multidim/implementations/multiDimFunctionGraph_tpl.h>
408 
409 #endif // GUM_MULTI_DIM_FUNCTION_GRAPH_H
MultiDimFunctionGraphManager< GUM_SCALAR, TerminalNodePolicy > * _manager_
A reference to the manager that edits this function graph.
friend MultiDimFunctionGraphManager< GUM_SCALAR, TerminalNodePolicy > * MultiDimFunctionGraph()
This friend methods from is the only way to get an instance of a manager.
NodeId _root_
The root node of the function graph.
bool isInternalNode(const NodeId &node) const
Indicates if given node is terminal or not.
bool isTerminalNode(const NodeId &node) const
Indicates if given node is terminal or not.
void copy(const MultiDimFunctionGraph< GUM_SCALAR, TerminalNodePolicy > &src)
Removes all variables in this MultiDimFunctionGraph and copy the content of src, variables included...
virtual void populate(const std::vector< GUM_SCALAR > &v) const
static const GUM_SCALAR defaultValue
Only for proper initialization of a certain returned value.
void copyAndMultiplyByScalar(const MultiDimFunctionGraph< GUM_SCALAR, TerminalNodePolicy > &src, GUM_SCALAR gamma)
Copies src diagrams and multiply every value by the given scalar.
virtual void setFirstNotification(const Instantiation &i)
Listen to setFirst in a given Instantiation.
virtual Size realSize() const
Returns the real number of parameters used for this table.
const InternalNode * node(NodeId n) const
Returns internalNode structure associated to that nodeId.
HashTable< const DiscreteVariable *, LinkedList< NodeId > *> _var2NodeIdMap_
Mapping between var and node.
virtual void setChangeNotification(const Instantiation &i)
Listen to an assignment of a value in a Instantiation.
friend MultiDimFunctionGraphManager< GUM_SCALAR, TerminalNodePolicy > * MultiDimFunctionGraph()
This friend methods from is the only way to get an instance of a manager.
~MultiDimFunctionGraph()
Class destructor.
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:643
void copyAndReassign(const MultiDimFunctionGraph< GUM_SCALAR, TerminalNodePolicy > &src, const Bijection< const DiscreteVariable *, const DiscreteVariable * > &reassign)
Copies src diagrams structure into this diagrams.
MultiDimFunctionGraph(bool isReduced=true)
Default constructor.
MultiDimFunctionGraph< GUM_SCALAR, TerminalNodePolicy > & operator=(const MultiDimFunctionGraph< GUM_SCALAR, TerminalNodePolicy > &from)
Copy Operator.
std::string _name_
The name of the data structure.
friend MultiDimFunctionGraphManager< GUM_SCALAR, TerminalNodePolicy > * MultiDimFunctionGraph()
This friend methods from is the only way to get an instance of a manager.
virtual void erase(const DiscreteVariable &v)
Removes a var from the variables of the multidimensional matrix.
NodeGraphPart _model_
Indicates available nodeIds.
virtual void setDecNotification(const Instantiation &i)
Listen to increment in each recorded Instantiation.
virtual void set(const Instantiation &i, const GUM_SCALAR &value) const
virtual void add(const DiscreteVariable &v)
Adds a new var to the variables of the multidimensional matrix.
virtual void replace_(const DiscreteVariable *x, const DiscreteVariable *y)
const GUM_SCALAR & nodeValue(NodeId n) const
Returns value associated to given node.
const NodeId & root() const
Returns the id of the root node from the diagram.
const LinkedList< NodeId > * varNodeListe(const DiscreteVariable *var) const
Returns the list of node associated to given variable.
virtual const std::string & name() const
Returns the real name of the multiDim implementation.
virtual std::string toString(const Instantiation *i) const
Display the internal representation of i.
HashTable< NodeId, InternalNode *> _internalNodeMap_
Associates each non-terminal node to a variable.
virtual MultiDimContainer< GUM_SCALAR > * newFactory() const
Creates an empty clone of this MultiDimContainer.
virtual void setLastNotification(const Instantiation &i)
Listen to setLast in a given Instantiation.
GUM_SCALAR & get_(const Instantiation &inst) const
virtual void copyFrom(const MultiDimContainer< GUM_SCALAR > &src, Instantiation *p_i=(Instantiation *) 0) const
MultiDimFunctionGraph(const MultiDimFunctionGraph< GUM_SCALAR, TerminalNodePolicy > &from)
Copy constructor.
static MultiDimFunctionGraph< GUM_SCALAR, TerminalNodePolicy > * getTreeInstance()
Returns an arborescent instance.
const std::string & tableName() const
Returns the name of the table represented by this structure.
virtual void changeNotification(const Instantiation &i, const DiscreteVariable *const var, Idx oldval, Idx newval)
Listen to changes in a given Instantiation.
bool _isReduced_
Wheter the MultiDimFunctionGraphManager is reduced or not.
std::string toDot(bool withBackArcs=false) const
Returns a const reference to the manager of this diagram.
MultiDimFunctionGraphManager< GUM_SCALAR, TerminalNodePolicy > * manager()
Returns a const reference to the manager of this diagram.
virtual void populate(std::initializer_list< GUM_SCALAR > v) const
void setTableName(const std::string &name)
Sets the name of the table represented by this structure.
bool isReducedAndOrdered() const
Returns true if this MultiDimFunctionGraph is reduced and Ordered.
virtual void setIncNotification(const Instantiation &i)
Listen to increment in a given Instantiation.
virtual GUM_SCALAR get(const Instantiation &i) const
Returns the value pointed by i.
const NodeGraphPart & model() const
Returns a const reference to the manager of this diagram.
std::string _tableName_
The name of the data structure.
static MultiDimFunctionGraph< GUM_SCALAR, TerminalNodePolicy > * getReducedAndOrderedInstance()
Returns a reduced and ordered instance.
virtual void fill(const GUM_SCALAR &d) const
void clear()
Clears the function graph.