aGrUM 2.3.2
a C++ library for (probabilistic) graphical models
multiDimFunctionGraphProjector_tpl.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#pragma once
41
42
51
55
56namespace gum {
57
58 // CONSTRUCTOR
59 template < typename GUM_SCALAR,
60 template < typename > class FUNCTOR,
61 template < typename > class TerminalNodePolicy >
71
72 // DESTRUCTOR
73 template < typename GUM_SCALAR,
74 template < typename > class FUNCTOR,
75 template < typename > class TerminalNodePolicy >
80
81 // This function is the main function. To be call every time an Projection
82 // between the two given Function Graphs is required
83 template < typename GUM_SCALAR,
84 template < typename > class FUNCTOR,
85 template < typename > class TerminalNodePolicy >
88 _rd_->copy(*_src_);
89
91 varIter != _delVars_.endSafe();
92 ++varIter) {
93 const DiscreteVariable* curVar = *varIter;
94
95 // Tout d'abord, on déplace la variable à projeter en fin de séquence afin
96 // de simplifier la projection
97 if (_rd_->variablesSequence().exists(curVar))
98 _rd_->manager()->moveTo(curVar, _rd_->variablesSequence().size() - 1);
99
100 // 1er cas spécial : le diagramme est un un simple noeud terminal
101 if (_rd_->isTerminalNode(_rd_->root())) {
102 GUM_SCALAR newVal = _neutral_, oldVal = _rd_->nodeValue(_rd_->root());
103 for (Idx curVarModality = 0; curVarModality < curVar->domainSize(); ++curVarModality)
104 newVal = _function_(newVal, oldVal);
105
106 NodeId newSonId = _rd_->manager()->addTerminalNode(newVal);
107 _rd_->manager()->setRootNode(newSonId);
108
109 if (_rd_->variablesSequence().exists(curVar)) _rd_->erase(*curVar);
110 continue;
111 }
112
113 // 2ème cas spécial : la racine du diagramme est associée à la variable
114 // projetée
115 if (_rd_->node(_rd_->root())->nodeVar() == curVar) {
116 const InternalNode* curVarNode = _rd_->node(_rd_->root());
117 GUM_SCALAR newVal = _neutral_;
118 for (Idx curVarModality = 0; curVarModality < curVar->domainSize(); ++curVarModality)
119 newVal = _function_(newVal, _rd_->nodeValue(curVarNode->son(curVarModality)));
120
121 NodeId newSonId = _rd_->manager()->addTerminalNode(newVal);
122
123 _rd_->manager()->eraseNode(_rd_->root(), newSonId, false);
124
125 if (_rd_->variablesSequence().exists(curVar)) _rd_->erase(*curVar);
126 continue;
127 }
128
129 // Cas général
130 HashTable< NodeId, NodeId > visitedNode(2 * _rd_->realSize(), true, false);
131 std::vector< NodeId > filo;
132 filo.push_back(_rd_->root());
133
134 while (!filo.empty()) {
135 NodeId curNodeId = filo.back();
136 filo.pop_back();
137
138 const InternalNode* curNode = _rd_->node(curNodeId);
139
140 for (Idx modality = 0; modality < curNode->nodeVar()->domainSize(); ++modality) {
141 NodeId oldSonId = curNode->son(modality);
142
143 if (!visitedNode.exists(oldSonId)) {
144 NodeId newSonId = oldSonId;
145
146 if (!_rd_->isTerminalNode(oldSonId)) {
147 if (_rd_->node(oldSonId)->nodeVar() != curVar) {
148 filo.push_back(oldSonId);
149 } else {
150 const InternalNode* curVarNode = _rd_->node(oldSonId);
151 GUM_SCALAR newVal = _neutral_;
152 for (Idx curVarModality = 0; curVarModality < curVar->domainSize();
153 ++curVarModality)
154 newVal = _function_(newVal, _rd_->nodeValue(curVarNode->son(curVarModality)));
155
156 newSonId = _rd_->manager()->addTerminalNode(newVal);
157
158 _rd_->manager()->eraseNode(oldSonId, newSonId, false);
159 _rd_->manager()->setSon(curNodeId, modality, newSonId);
160 }
161
162 } else {
163 GUM_SCALAR newVal = _neutral_, oldVal = _rd_->nodeValue(oldSonId);
164 for (Idx curVarModality = 0; curVarModality < curVar->domainSize(); ++curVarModality)
165 newVal = _function_(newVal, oldVal);
166
167 newSonId = _rd_->manager()->addTerminalNode(newVal);
168 _rd_->manager()->setSon(curNodeId, modality, newSonId);
169 }
170
171 visitedNode.insert(oldSonId, newSonId);
172
173 } else {
174 if (_rd_->node(curNodeId)->son(modality) != visitedNode[oldSonId])
175 _rd_->manager()->setSon(curNodeId, modality, visitedNode[oldSonId]);
176 }
177 }
178 }
179
180 if (_rd_->variablesSequence().exists(curVar)) _rd_->erase(*curVar);
181 }
182
183 return _rd_;
184 }
185
186} // namespace gum
Base class for discrete random variable.
virtual Size domainSize() const =0
bool exists(const Key &key) const
Checks whether there exists an element with a given key in the hashtable.
value_type & insert(const Key &key, const Val &val)
Adds a new element (actually a copy of this element) into the hash table.
Structure used to represent a node internal structure.
const DiscreteVariable * nodeVar() const
Returns the node variable.
NodeId son(Idx modality) const
Returns the son at a given index.
MultiDimFunctionGraphProjector(const MultiDimFunctionGraph< GUM_SCALAR, TerminalNodePolicy > *src, const gum::VariableSet &delVars, const GUM_SCALAR neutral)
Default constructor.
const gum::VariableSet & _delVars_
The list of variables on which the projection is performed.
const FUNCTOR< GUM_SCALAR > _function_
The function to be performed on the leaves.
MultiDimFunctionGraph< GUM_SCALAR, TerminalNodePolicy > * _rd_
The resulting function graph.
MultiDimFunctionGraph< GUM_SCALAR, TerminalNodePolicy > * project()
Computes and builds the Function Graph that is the result of the Projection.
const MultiDimFunctionGraph< GUM_SCALAR, TerminalNodePolicy > * _src_
One of the two function graphs used for the Projection.
const GUM_SCALAR _neutral_
The function to be performed on the leaves.
static MultiDimFunctionGraph< GUM_SCALAR, TerminalNodePolicy > * getReducedAndOrderedInstance()
Returns a reduced and ordered instance.
Safe iterators for the Set class.
Definition set.h:601
Base class for discrete random variable.
Size Idx
Type for indexes.
Definition types.h:79
Size NodeId
Type for node ids.
Headers of the InternalNode class.
Class used to compute the projection of a function graph.
gum is the global namespace for all aGrUM entities
Definition agrum.h:46
Set< const DiscreteVariable * > VariableSet