aGrUM 3.1.1
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-2026 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-2026 *
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_ELEMENT, template < typename > class TerminalNodePolicy >
63
64 template < typename GUM_ELEMENT, template < typename > class TerminalNodePolicy >
66
67 template < typename GUM_ELEMENT, template < typename > class TerminalNodePolicy >
69
88 template < typename GUM_ELEMENT,
89 template < typename > class TerminalNodePolicy = ExactTerminalNodePolicy >
91 public MultiDimImplementation< GUM_ELEMENT >,
92 public TerminalNodePolicy< GUM_ELEMENT > {
93 public:
95 const static GUM_ELEMENT defaultValue;
96
97 public:
98 // =========================================================================
100 // =========================================================================
102
103 private:
108 MultiDimFunctionGraph(bool isReduced = true);
109
110 public:
116
124
128 ~MultiDimFunctionGraph() override;
129
131 // =========================================================================
133 // =========================================================================
135
140 void set(const Instantiation& i, const GUM_ELEMENT& value) const override;
141
146 void fill(const GUM_ELEMENT& d) const override;
147
152 void populate(const std::vector< GUM_ELEMENT >& v) const override;
157 void populate(std::initializer_list< GUM_ELEMENT > v) const override;
158
164 Instantiation* p_i = (Instantiation*)0) const override;
165
170 void copy(const MultiDimContainer< GUM_ELEMENT >& src) override;
171
173 // =========================================================================
175 // =========================================================================
177
178 [[nodiscard]] MultiDimContainer< GUM_ELEMENT >* newFactory() const override;
179
180 GUM_ELEMENT get(const Instantiation& i) const override;
181
182 const std::string& name() const override;
183
184 void add(const DiscreteVariable& v) override;
185
186 void erase(const DiscreteVariable& v) override;
187
188 Size realSize() const override;
189
190 void changeNotification(const Instantiation& i,
191 const DiscreteVariable* const var,
192 Idx oldval,
193 Idx newval) override;
194
195 void setFirstNotification(const Instantiation& i) override;
196
197 void setLastNotification(const Instantiation& i) override;
198
199 void setIncNotification(const Instantiation& i) override;
200
201 void setDecNotification(const Instantiation& i) override;
202
203 void setChangeNotification(const Instantiation& i) override;
204
205 std::string toString(const Instantiation* i) const override;
206
208 // =========================================================================
210 // =========================================================================
212
220
234 void copyAndReassign(
237
244 void
246 GUM_ELEMENT gamma);
247
251 void clear();
252
254 // =========================================================================
256 // =========================================================================
258
265 std::string toDot(bool withBackArcs = false) const;
266
271 const NodeGraphPart& model() const;
272
277 MultiDimFunctionGraphManager< GUM_ELEMENT, TerminalNodePolicy >* manager();
278
283 const NodeId& root() const;
284
290 bool isTerminalNode(const NodeId& node) const;
291
297 bool isInternalNode(const NodeId& node) const;
298
305 const GUM_ELEMENT& nodeValue(NodeId n) const;
306
313 const InternalNode* node(NodeId n) const;
314
321 const LinkedList< NodeId >* varNodeListe(const DiscreteVariable* var) const;
322
327 const std::string& tableName() const;
328
333 void setTableName(std::string_view name);
334
341 bool isReducedAndOrdered() const;
342
355
362
364
365 protected:
370 void replace_(const DiscreteVariable* x, const DiscreteVariable* y) override;
371
377 GUM_ELEMENT& get_(const Instantiation& inst) const override;
378
379 private:
381 std::string _name_;
382
384 std::string _tableName_;
385
388
390 MultiDimFunctionGraphManager< GUM_ELEMENT, TerminalNodePolicy >* _manager_;
391
394
397
400
403
404 friend class MultiDimFunctionGraphManager< GUM_ELEMENT, TerminalNodePolicy >;
405 };
406
407
408#ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
409# ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
410 // extern template class MultiDimFunctionGraph<float>;
411# endif
412#endif
413#ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
414# ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
415 // extern template class MultiDimFunctionGraph<double>;
416# endif
417#endif
418} // namespace gum
419
421
422#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:1640
Base class for discrete random variable.
Implementation of a Terminal Node Policy that maps nodeid directly to value.
const GUM_ELEMENT & value() const override
Returns the value of the current terminal nodes pointed by the constant safe iterator.
The class for generic Hash Tables.
Definition hashTable.h:640
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.
NodeGraphPart _model_
Indicates available nodeIds.
NodeId _root_
The root node of the function graph.
Size realSize() const override
Returns the real number of parameters used for this table.
HashTable< NodeId, InternalNode * > _internalNodeMap_
Associates each non-terminal node to a variable.
~MultiDimFunctionGraph() override
Class destructor.
void clear()
Clears the function graph.
std::string toDot(bool withBackArcs=false) const
Returns a const reference to the manager of this diagram.
static MultiDimFunctionGraph< GUM_ELEMENT, TerminalNodePolicy > * getTreeInstance()
Returns an arborescent instance.
HashTable< const DiscreteVariable *, LinkedList< NodeId > * > _var2NodeIdMap_
Mapping between var and node.
void copyFrom(const MultiDimContainer< GUM_ELEMENT > &src, Instantiation *p_i=(Instantiation *) 0) const override
void setFirstNotification(const Instantiation &i) override
Listen to setFirst in a given Instantiation.
void setChangeNotification(const Instantiation &i) override
Listen to an assignment of a value in a Instantiation.
MultiDimFunctionGraph(bool isReduced=true)
Default constructor.
void add(const DiscreteVariable &v) override
Adds a new var to the variables of the multidimensional matrix.
void replace_(const DiscreteVariable *x, const DiscreteVariable *y) override
void setTableName(std::string_view name)
Sets the name of the table represented by this structure.
void copy(const MultiDimContainer< GUM_ELEMENT > &src) override
MultiDimContainer< GUM_ELEMENT > * newFactory() const override
Creates an empty clone of this MultiDimContainer.
MultiDimFunctionGraph< GUM_ELEMENT, TerminalNodePolicy > & operator=(const MultiDimFunctionGraph< GUM_ELEMENT, TerminalNodePolicy > &from)
Copy Operator.
bool isInternalNode(const NodeId &node) const
Indicates if given node is terminal or not.
const LinkedList< NodeId > * varNodeListe(const DiscreteVariable *var) const
Returns the list of node associated to given variable.
MultiDimFunctionGraphManager< GUM_ELEMENT, TerminalNodePolicy > * _manager_
A reference to the manager that edits this function graph.
const NodeId & root() const
Returns the id of the root node from the diagram.
void copyAndMultiplyByScalar(const MultiDimFunctionGraph< GUM_ELEMENT, TerminalNodePolicy > &src, GUM_ELEMENT gamma)
Copies src diagrams and multiply every value by the given scalar.
void set(const Instantiation &i, const GUM_ELEMENT &value) const override
void setDecNotification(const Instantiation &i) override
Listen to increment in each recorded Instantiation.
void setLastNotification(const Instantiation &i) override
Listen to setLast in a given Instantiation.
void changeNotification(const Instantiation &i, const DiscreteVariable *const var, Idx oldval, Idx newval) override
Listen to changes in a given Instantiation.
const std::string & tableName() const
Returns the name of the table represented by this structure.
const std::string & name() const override
Returns the real name of the multiDim implementation.
bool isReducedAndOrdered() const
Returns true if this MultiDimFunctionGraph is reduced and Ordered.
std::string _name_
The name of the data structure.
bool isTerminalNode(const NodeId &node) const
Indicates if given node is terminal or not.
GUM_ELEMENT & get_(const Instantiation &inst) const override
void copyAndReassign(const MultiDimFunctionGraph< GUM_ELEMENT, TerminalNodePolicy > &src, const Bijection< const DiscreteVariable *, const DiscreteVariable * > &reassign)
Copies src diagrams structure into this diagrams.
bool _isReduced_
Wheter the MultiDimFunctionGraphManager is reduced or not.
const GUM_ELEMENT & nodeValue(NodeId n) const
Returns value associated to given node.
void fill(const GUM_ELEMENT &d) const override
void erase(const DiscreteVariable &v) override
Removes a var from the variables of the multidimensional matrix.
const NodeGraphPart & model() const
Returns a const reference to the manager of this diagram.
void populate(const std::vector< GUM_ELEMENT > &v) const override
void setIncNotification(const Instantiation &i) override
Listen to increment in a given Instantiation.
std::string _tableName_
The name of the data structure.
GUM_ELEMENT get(const Instantiation &i) const override
Returns the value pointed by i.
const InternalNode * node(NodeId n) const
Returns internalNode structure associated to that nodeId.
MultiDimFunctionGraphManager< GUM_ELEMENT, TerminalNodePolicy > * manager()
Returns a const reference to the manager of this diagram.
static const GUM_ELEMENT defaultValue
Only for proper initialization of a certain returned value.
static MultiDimFunctionGraph< GUM_ELEMENT, TerminalNodePolicy > * getReducedAndOrderedInstance()
Returns a reduced and ordered instance.
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.