aGrUM  0.20.2
a C++ library for (probabilistic) graphical models
multiDimFunctionGraph.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 /**
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
83  public TerminalNodePolicy< GUM_SCALAR > {
84  public:
85  /// Only for proper initialization of a certain returned value
86  const static GUM_SCALAR defaultValue;
87 
88  public:
89  // =========================================================================
90  /// @name Constructors, destructor and copy
91  // =========================================================================
92  /// @{
93 
94  private:
95  /**
96  * @brief Default constructor.
97  * @param isReduced If true, then this MultiDimFunctionGraph is reduced.
98  */
99  MultiDimFunctionGraph(bool isReduced = true);
100 
101  public:
102  /**
103  * @brief Copy constructor.
104  * @param from The MultiDimFunctionGraph to copy.
105  */
107  const MultiDimFunctionGraph< GUM_SCALAR, TerminalNodePolicy >& from);
108 
109  /**
110  * @brief Copy Operator.
111  * @param from The MultiDimFunctionGraph to copy.
112  * @return Returns this MultiDimFunctionGraph.
113  */
116 
117  /**
118  * @brief Class destructor.
119  */
121 
122  /// @}
123  // =========================================================================
124  /// @name Unallowed inherited methods
125  // =========================================================================
126  /// @{
127 
128  /**
129  * @warning This will raise an OperationNotAllowed exceptions. Use the
130  * MultiDimFunctionGraphManager class to edit a MultiDimFunctionGraph.
131  */
132  virtual void set(const Instantiation& i, const GUM_SCALAR& value) const;
133 
134  /**
135  * @warning This will raise an OperationNotAllowed exceptions. Use the
136  * MultiDimFunctionGraphManager class to edit a MultiDimFunctionGraph.
137  */
138  virtual void fill(const GUM_SCALAR& d) const;
139 
140  /**
141  * @warning This will raise an OperationNotAllowed exceptions. Use the
142  * MultiDimFunctionGraphManager class to edit a MultiDimFunctionGraph.
143  */
144  virtual void populate(const std::vector< GUM_SCALAR >& v) const;
145  /**
146  * @warning This will raise an OperationNotAllowed exceptions. Use the
147  * MultiDimFunctionGraphManager class to edit a MultiDimFunctionGraph.
148  */
149  virtual void populate(std::initializer_list< GUM_SCALAR > v) const;
150 
151  /**
152  * @warning This will raise an OperationNotAllowed as MultiDimFunctionGraph
153  * can't copy other multiDim.
154  */
155  virtual void copyFrom(const MultiDimContainer< GUM_SCALAR >& src,
156  Instantiation* p_i = (Instantiation*)0) const;
157 
158  /**
159  * @warning This will raise an OperationNotAllowed as MultiDimFunctionGraph
160  * can't copy other multiDim.
161  */
162  virtual void copy(const MultiDimContainer< GUM_SCALAR >& src);
163 
164  /// @}
165  // =========================================================================
166  /// @name Inherited methods
167  // =========================================================================
168  /// @{
169 
170  virtual MultiDimContainer< GUM_SCALAR >* newFactory() const;
171 
172  virtual GUM_SCALAR get(const Instantiation& i) const;
173 
174  virtual const std::string& name() const;
175 
176  virtual void add(const DiscreteVariable& v);
177 
178  virtual void erase(const DiscreteVariable& v);
179 
180  virtual Size realSize() const;
181 
182  virtual void changeNotification(const Instantiation& i,
183  const DiscreteVariable* const var,
184  Idx oldval,
185  Idx newval);
186 
187  virtual void setFirstNotification(const Instantiation& i);
188 
189  virtual void setLastNotification(const Instantiation& i);
190 
191  virtual void setIncNotification(const Instantiation& i);
192 
193  virtual void setDecNotification(const Instantiation& i);
194 
195  virtual void setChangeNotification(const Instantiation& i);
196 
197  virtual std::string toString(const Instantiation* i) const;
198 
199  /// @}
200  // =========================================================================
201  /// @name Copy methods.
202  // =========================================================================
203  /// @{
204 
205  /**
206  * @brief Removes all variables in this MultiDimFunctionGraph and copy the
207  * content of src, variables included.
208  *
209  * @param src The MultiDimFunctionGraph to copy.
210  */
211  void copy(const MultiDimFunctionGraph< GUM_SCALAR, TerminalNodePolicy >& src);
212 
213  /**
214  * @brief Copies src diagrams structure into this diagrams.
215  *
216  * However it also changes the variables.
217  *
218  * @warning This has two implications:
219  * - First, this is not just a renaming. Pointers are trully changed.
220  * - Second, for each pair of variable, the new variable MUST macth the
221  * number of modalities of the old variable.
222  *
223  * @param src The MultiDimFunctionGraph to copy.
224  * @param reassign A Bijection form variables in src to variables in this.
225  */
226  void copyAndReassign(
227  const MultiDimFunctionGraph< GUM_SCALAR, TerminalNodePolicy >& src,
228  const Bijection< const DiscreteVariable*, const DiscreteVariable* >&
229  reassign);
230 
231  /**
232  * @brief Copies src diagrams and multiply every value by the given scalar.
233  *
234  * @param src The MultiDimFunctionGraph to copy.
235  * @param gamma The scalar used to multiply every value with.
236  */
238  const MultiDimFunctionGraph< GUM_SCALAR, TerminalNodePolicy >& src,
239  GUM_SCALAR gamma);
240 
241  /**
242  * @brief Clears the function graph
243  */
244  void clear();
245 
246  /// @}
247  // =========================================================================
248  /// @name Accessors and modifiers
249  // =========================================================================
250  /// @{
251 
252  /**
253  * @brief Returns a const reference to the manager of this diagram
254  *
255  * @param withBackArcs If true, back arcs will be added.
256  * @return Returns a const reference to the manager of this diagram
257  */
258  std::string toDot(bool withBackArcs = false) const;
259 
260  /**
261  * @brief Returns a const reference to the manager of this diagram
262  * @return Returns a const reference to the manager of this diagram
263  */
264  const NodeGraphPart& model() const;
265 
266  /**
267  * @brief Returns a const reference to the manager of this diagram
268  * @return Returns a const reference to the manager of this diagram
269  */
271 
272  /**
273  * @brief Returns the id of the root node from the diagram
274  * @return Returns the id of the root node from the diagram
275  */
276  const NodeId& root() const;
277 
278  /**
279  * @brief Indicates if given node is terminal or not
280  * @param node The node to test for terminality.
281  * @return Returns true if node is terminal.
282  */
283  bool isTerminalNode(const NodeId& node) const;
284 
285  /**
286  * @brief Indicates if given node is terminal or not
287  * @param node The node to test for being itnernal.
288  * @return Returns true if node is internal.
289  */
290  bool isInternalNode(const NodeId& node) const;
291 
292  /**
293  * @brief Returns value associated to given node.
294  * @param n The node for which the value is returned.
295  * @return Returns value associated to given node.
296  * @throw InvalidNode Raised if node isn't terminal.
297  */
298  const GUM_SCALAR& nodeValue(NodeId n) const;
299 
300  /**
301  * @brief Returns internalNode structure associated to that nodeId
302  * @param n The node for which the internal structure is returned.
303  * @return Returns internalNode structure associated to that nodeId
304  * @throw InvalidNode Raised if node is terminal
305  */
306  const InternalNode* node(NodeId n) const;
307 
308  /**
309  * @brief Returns the list of node associated to given variable
310  * @param var The variable for which the list of associated nodes is
311  * returned.
312  * @return Returns the list of node associated to given variable
313  */
314  const LinkedList< NodeId >* varNodeListe(const DiscreteVariable* var) const;
315 
316  /**
317  * @brief Returns the name of the table represented by this structure.
318  * @return Returns the name of the table represented by this structure.
319  */
320  const std::string& tableName() const;
321 
322  /**
323  * @brief Sets the name of the table represented by this structure.
324  * @param name the new name of this structure.
325  */
326  void setTableName(const std::string& name);
327 
328  /**
329  * @brief Returns true if this MultiDimFunctionGraph is reduced and
330  * Ordered.
331  * @return Returns true if this MultiDimFunctionGraph is reduced and
332  * Ordered.
333  */
334  bool isReducedAndOrdered() const;
335 
336  /**
337  * @brief Returns a reduced and ordered instance.
338  *
339  * Reduced and ordered instance will reduce the size of the graph whenever
340  * it's possible. An inherent order on the variable helps doing so. The
341  * order in which variables will be inserted with function add(const
342  * DiscreteVariable&) specify that order.
343  *
344  * @return Returns a reduced and ordered instance.
345  */
348 
349  /**
350  * @brief Returns an arborescent instance
351  * @return Returns an arborescent instance
352  */
354  getTreeInstance();
355 
356  /// @}
357 
358  protected:
359  /**
360  * @warning This will raise an OperationNotAllowed as this method has not
361  * yet been implemented.
362  */
363  virtual void replace_(const DiscreteVariable* x, const DiscreteVariable* y);
364 
365  /**
366  * @warning This will raise an OperationNotAllowed as you should not use
367  * this method as MultiDimFunctionGraph use its own internal structure for
368  * storing data.
369  */
370  GUM_SCALAR& get_(const Instantiation& inst) const;
371 
372  private:
373  /// The name of the data structure
375 
376  /// The name of the data structure
378 
379  /// Indicates available nodeIds
381 
382  /// A reference to the manager that edits this function graph
384 
385  /// The root node of the function graph
387 
388  /// Associates each non-terminal node to a variable
390 
391  /// Mapping between var and node
393 
394  /// Wheter the MultiDimFunctionGraphManager is reduced or not
396 
398  };
399 
400 
401 #ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
402 # ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
403  // extern template class MultiDimFunctionGraph<float>;
404 # endif
405 #endif
406 #ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
407 # ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
408  // extern template class MultiDimFunctionGraph<double>;
409 # endif
410 #endif
411 } // namespace gum
412 
413 #include <agrum/tools/multidim/implementations/multiDimFunctionGraph_tpl.h>
414 
415 #endif // GUM_MULTI_DIM_FUNCTION_GRAPH_H
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.
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...
MultiDimFunctionGraphManager< GUM_SCALAR, TerminalNodePolicy > * manager__
A reference to the manager that edits this function graph.
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.
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:669
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.
NodeId root__
The root node of the function graph.
MultiDimFunctionGraph< GUM_SCALAR, TerminalNodePolicy > & operator=(const MultiDimFunctionGraph< GUM_SCALAR, TerminalNodePolicy > &from)
Copy Operator.
HashTable< const DiscreteVariable *, LinkedList< NodeId > *> var2NodeIdMap__
Mapping between var and node.
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.
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)
bool isReduced__
Wheter the MultiDimFunctionGraphManager is reduced or not.
const GUM_SCALAR & nodeValue(NodeId n) const
Returns value associated to given node.
HashTable< NodeId, InternalNode *> internalNodeMap__
Associates each non-terminal node to a variable.
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.
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
NodeGraphPart model__
Indicates available nodeIds.
virtual void copyFrom(const MultiDimContainer< GUM_SCALAR > &src, Instantiation *p_i=(Instantiation *) 0) const
MultiDimFunctionGraph(const MultiDimFunctionGraph< GUM_SCALAR, TerminalNodePolicy > &from)
Copy constructor.
std::string tableName__
The name of the data structure.
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.
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.
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.