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