aGrUM 2.3.2
a C++ library for (probabilistic) graphical models
multiDimFunctionGraph.h
Go to the documentation of this file.
1/****************************************************************************
2 * This file is part of the aGrUM/pyAgrum library. *
3 * *
4 * Copyright (c) 2005-2025 by *
5 * - Pierre-Henri WUILLEMIN(_at_LIP6) *
6 * - Christophe GONZALES(_at_AMU) *
7 * *
8 * The aGrUM/pyAgrum library is free software; you can redistribute it *
9 * and/or modify it under the terms of either : *
10 * *
11 * - the GNU Lesser General Public License as published by *
12 * the Free Software Foundation, either version 3 of the License, *
13 * or (at your option) any later version, *
14 * - the MIT license (MIT), *
15 * - or both in dual license, as here. *
16 * *
17 * (see https://agrum.gitlab.io/articles/dual-licenses-lgplv3mit.html) *
18 * *
19 * This aGrUM/pyAgrum library is distributed in the hope that it will be *
20 * useful, but WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, *
21 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES MERCHANTABILITY or FITNESS *
22 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, *
25 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR *
26 * OTHER DEALINGS IN THE SOFTWARE. *
27 * *
28 * See LICENCES for more details. *
29 * *
30 * SPDX-FileCopyrightText: Copyright 2005-2025 *
31 * - Pierre-Henri WUILLEMIN(_at_LIP6) *
32 * - Christophe GONZALES(_at_AMU) *
33 * SPDX-License-Identifier: LGPL-3.0-or-later OR MIT *
34 * *
35 * Contact : info_at_agrum_dot_org *
36 * homepage : http://agrum.gitlab.io *
37 * gitlab : https://gitlab.com/agrumery/agrum *
38 * *
39 ****************************************************************************/
40
41
50#ifndef GUM_MULTI_DIM_FUNCTION_GRAPH_H
51#define GUM_MULTI_DIM_FUNCTION_GRAPH_H
52
58
59namespace gum {
60
61 template < typename GUM_SCALAR, template < typename > class TerminalNodePolicy >
63
64 template < typename GUM_SCALAR, template < typename > class TerminalNodePolicy >
66
67 template < typename GUM_SCALAR, template < typename > class TerminalNodePolicy >
69
88 template < typename GUM_SCALAR,
89 template < typename > class TerminalNodePolicy = ExactTerminalNodePolicy >
91 public MultiDimImplementation< GUM_SCALAR >,
92 public TerminalNodePolicy< GUM_SCALAR > {
93 public:
95 const static GUM_SCALAR defaultValue;
96
97 public:
98 // =========================================================================
100 // =========================================================================
102
103 private:
108 MultiDimFunctionGraph(bool isReduced = true);
109
110 public:
116
124
129
131 // =========================================================================
133 // =========================================================================
135
140 virtual void set(const Instantiation& i, const GUM_SCALAR& value) const;
141
146 virtual void fill(const GUM_SCALAR& d) const;
147
152 virtual void populate(const std::vector< GUM_SCALAR >& v) const;
157 virtual void populate(std::initializer_list< GUM_SCALAR > v) const;
158
163 virtual void copyFrom(const MultiDimContainer< GUM_SCALAR >& src,
164 Instantiation* p_i = (Instantiation*)0) const;
165
170 virtual void copy(const MultiDimContainer< GUM_SCALAR >& src);
171
173 // =========================================================================
175 // =========================================================================
177
179
180 virtual GUM_SCALAR get(const Instantiation& i) const;
181
182 virtual const std::string& name() const;
183
184 virtual void add(const DiscreteVariable& v);
185
186 virtual void erase(const DiscreteVariable& v);
187
188 virtual Size realSize() const;
189
190 virtual void changeNotification(const Instantiation& i,
191 const DiscreteVariable* const var,
192 Idx oldval,
193 Idx newval);
194
195 virtual void setFirstNotification(const Instantiation& i);
196
197 virtual void setLastNotification(const Instantiation& i);
198
199 virtual void setIncNotification(const Instantiation& i);
200
201 virtual void setDecNotification(const Instantiation& i);
202
203 virtual void setChangeNotification(const Instantiation& i);
204
205 virtual std::string toString(const Instantiation* i) const;
206
208 // =========================================================================
210 // =========================================================================
212
220
234 void copyAndReassign(
237
245 GUM_SCALAR gamma);
246
250 void clear();
251
253 // =========================================================================
255 // =========================================================================
257
264 std::string toDot(bool withBackArcs = false) const;
265
270 const NodeGraphPart& model() const;
271
276 MultiDimFunctionGraphManager< GUM_SCALAR, TerminalNodePolicy >* manager();
277
282 const NodeId& root() const;
283
289 bool isTerminalNode(const NodeId& node) const;
290
296 bool isInternalNode(const NodeId& node) const;
297
304 const GUM_SCALAR& nodeValue(NodeId n) const;
305
312 const InternalNode* node(NodeId n) const;
313
320 const LinkedList< NodeId >* varNodeListe(const DiscreteVariable* var) const;
321
326 const std::string& tableName() const;
327
332 void setTableName(const std::string& name);
333
340 bool isReducedAndOrdered() const;
341
353
359
361
362 protected:
367 virtual void replace_(const DiscreteVariable* x, const DiscreteVariable* y);
368
374 GUM_SCALAR& get_(const Instantiation& inst) const;
375
376 private:
378 std::string _name_;
379
381 std::string _tableName_;
382
385
387 MultiDimFunctionGraphManager< GUM_SCALAR, TerminalNodePolicy >* _manager_;
388
391
394
397
400
401 friend class MultiDimFunctionGraphManager< GUM_SCALAR, TerminalNodePolicy >;
402 };
403
404
405#ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
406# ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
407 // extern template class MultiDimFunctionGraph<float>;
408# endif
409#endif
410#ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
411# ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
412 // extern template class MultiDimFunctionGraph<double>;
413# endif
414#endif
415} // namespace gum
416
418
419#endif // GUM_MULTI_DIM_FUNCTION_GRAPH_H
Headers of the ExactTerminalNodePolicy.
Set of pairs of elements with fast search for both elements.
Definition bijection.h:1594
Base class for discrete random variable.
Implementation of a Terminal Node Policy that maps nodeid directly to value.
const GUM_SCALAR & value() const
Returns the value of the current terminal nodes pointed by the constant safe iterator.
The class for generic Hash Tables.
Definition hashTable.h:637
Class for assigning/browsing values to tuples of discrete variables.
Structure used to represent a node internal structure.
Abstract base class for all multi dimensionnal containers.
virtual std::string toString() const
Returns a representation of this MultiDimContainer.
Class implementingting a function graph manager.
MultiDimFunctionGraphManager< GUM_SCALAR, TerminalNodePolicy > * manager()
Returns a const reference to the manager of this diagram.
bool _isReduced_
Wheter the MultiDimFunctionGraphManager is reduced or not.
MultiDimFunctionGraph(bool isReduced=true)
Default constructor.
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.
virtual void setDecNotification(const Instantiation &i)
Listen to increment in each recorded Instantiation.
virtual void set(const Instantiation &i, const GUM_SCALAR &value) const
const NodeId & root() const
Returns the id of the root node from the diagram.
virtual void setChangeNotification(const Instantiation &i)
Listen to an assignment of a value in a Instantiation.
bool isTerminalNode(const NodeId &node) const
Indicates if given node is terminal or not.
void setTableName(const std::string &name)
Sets the name of the table represented by this structure.
bool isInternalNode(const NodeId &node) const
Indicates if given node is terminal or not.
virtual Size realSize() const
Returns the real number of parameters used for this table.
virtual void setIncNotification(const Instantiation &i)
Listen to increment in a given Instantiation.
static MultiDimFunctionGraph< GUM_SCALAR, TerminalNodePolicy > * getTreeInstance()
Returns an arborescent instance.
static MultiDimFunctionGraph< GUM_SCALAR, TerminalNodePolicy > * getReducedAndOrderedInstance()
Returns a reduced and ordered instance.
MultiDimFunctionGraphManager< GUM_SCALAR, TerminalNodePolicy > * _manager_
A reference to the manager that edits this function graph.
const InternalNode * node(NodeId n) const
Returns internalNode structure associated to that nodeId.
virtual void copy(const MultiDimContainer< GUM_SCALAR > &src)
HashTable< NodeId, InternalNode * > _internalNodeMap_
Associates each non-terminal node to a variable.
HashTable< const DiscreteVariable *, LinkedList< NodeId > * > _var2NodeIdMap_
Mapping between var and node.
const GUM_SCALAR & nodeValue(NodeId n) const
Returns value associated to given node.
virtual void setLastNotification(const Instantiation &i)
Listen to setLast in a given Instantiation.
MultiDimFunctionGraph< GUM_SCALAR, TerminalNodePolicy > & operator=(const MultiDimFunctionGraph< GUM_SCALAR, TerminalNodePolicy > &from)
Copy Operator.
const LinkedList< NodeId > * varNodeListe(const DiscreteVariable *var) const
Returns the list of node associated to given variable.
bool isReducedAndOrdered() const
Returns true if this MultiDimFunctionGraph is reduced and Ordered.
static const GUM_SCALAR defaultValue
Only for proper initialization of a certain returned value.
virtual void erase(const DiscreteVariable &v)
Removes a var from the variables of the multidimensional matrix.
std::string _tableName_
The name of the data structure.
GUM_SCALAR & get_(const Instantiation &inst) const
std::string _name_
The name of the data structure.
void copyAndMultiplyByScalar(const MultiDimFunctionGraph< GUM_SCALAR, TerminalNodePolicy > &src, GUM_SCALAR gamma)
Copies src diagrams and multiply every value by the given scalar.
const NodeGraphPart & model() const
Returns a const reference to the manager of this diagram.
void copyAndReassign(const MultiDimFunctionGraph< GUM_SCALAR, TerminalNodePolicy > &src, const Bijection< const DiscreteVariable *, const DiscreteVariable * > &reassign)
Copies src diagrams structure into this diagrams.
virtual void add(const DiscreteVariable &v)
Adds a new var to the variables of the multidimensional matrix.
const std::string & tableName() const
Returns the name of the table represented by this structure.
virtual void populate(const std::vector< GUM_SCALAR > &v) const
NodeId _root_
The root node of the function graph.
virtual void replace_(const DiscreteVariable *x, const DiscreteVariable *y)
virtual void fill(const GUM_SCALAR &d) const
virtual const std::string & name() const
Returns the real name of the multiDim implementation.
virtual void copyFrom(const MultiDimContainer< GUM_SCALAR > &src, Instantiation *p_i=(Instantiation *) 0) const
void clear()
Clears the function graph.
virtual MultiDimContainer< GUM_SCALAR > * newFactory() const
Creates an empty clone of this MultiDimContainer.
NodeGraphPart _model_
Indicates available nodeIds.
virtual GUM_SCALAR get(const Instantiation &i) const
Returns the value pointed by i.
virtual void setFirstNotification(const Instantiation &i)
Listen to setFirst in a given Instantiation.
MultiDimImplementation()
Default constructor.
Class for node sets in graph.
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition types.h:74
Size Idx
Type for indexes.
Definition types.h:79
Size NodeId
Type for node ids.
Headers of MultiDimFunctionGraphManager.
Template methods of MultiDimFunctionGraph.
Headers of gum::MultiDimImplementation.
gum is the global namespace for all aGrUM entities
Definition agrum.h:46
Template trick for efficient development.
Provides basic types used in aGrUM.