aGrUM  0.20.2
a C++ library for (probabilistic) graphical models
multiDimFunctionGraphProjector_tpl.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 Class used to compute the projection of a function graph
25  *
26  * @author Pierre-Henri WUILLEMIN(@LIP6) and Jean-Christophe MAGNAN and Christophe
27  * GONZALES(@AMU)
28  * @author Christophe GONZALES(@AMU) and Pierre-Henri WUILLEMIN(@LIP6)
29  */
30 
31 #include <agrum/tools/multidim/utils/FunctionGraphUtilities/internalNode.h>
32 #include <agrum/tools/multidim/utils/FunctionGraphUtilities/operators/multiDimFunctionGraphProjector.h>
33 #include <agrum/tools/variables/discreteVariable.h>
34 
35 namespace gum {
36 
37  // CONSTRUCTOR
38  template < typename GUM_SCALAR,
39  template < typename >
40  class FUNCTOR,
41  template < typename >
42  class TerminalNodePolicy >
43  MultiDimFunctionGraphProjector< GUM_SCALAR, FUNCTOR, TerminalNodePolicy >::
44  MultiDimFunctionGraphProjector(
45  const MultiDimFunctionGraph< GUM_SCALAR, TerminalNodePolicy >* src,
46  const Set< const DiscreteVariable* >& delVars,
47  const GUM_SCALAR neutral) :
48  src__(src),
49  delVars__(delVars), function__(), neutral__(neutral) {
50  GUM_CONSTRUCTOR(MultiDimFunctionGraphProjector);
51  rd__ = MultiDimFunctionGraph< GUM_SCALAR >::getReducedAndOrderedInstance();
52  }
53 
54 
55  // DESTRUCTOR
56  template < typename GUM_SCALAR,
57  template < typename >
58  class FUNCTOR,
59  template < typename >
60  class TerminalNodePolicy >
64  }
65 
66 
67  // This function is the main function. To be call every time an Projection
68  // between the two given Function Graphs is required
69  template < typename GUM_SCALAR,
70  template < typename >
71  class FUNCTOR,
72  template < typename >
73  class TerminalNodePolicy >
76  project() {
77  rd__->copy(*src__);
78 
80  = delVars__.beginSafe();
82  ++varIter) {
84 
85  // Tout d'abord, on déplace la variable à projeter en fin de séquence afin
86  // de simplifier la projection
89 
90  // 1er cas spécial : le diagramme est un un simple noeud terminal
91  if (rd__->isTerminalNode(rd__->root())) {
96 
99 
101  continue;
102  }
103 
104  // 2ème cas spécial : la racine du diagramme est associée à la variable
105  // projetée
106  if (rd__->node(rd__->root())->nodeVar() == curVar) {
107  const InternalNode* curVarNode = rd__->node(rd__->root());
110  ++curVarModality)
113 
115 
116  rd__->manager()->eraseNode(rd__->root(), newSonId, false);
117 
119  continue;
120  }
121 
122  // Cas général
123  HashTable< NodeId, NodeId > visitedNode(2 * rd__->realSize(), true, false);
124  std::vector< NodeId > filo;
125  filo.push_back(rd__->root());
126 
127  while (!filo.empty()) {
129  filo.pop_back();
130 
132 
133  for (Idx modality = 0; modality < curNode->nodeVar()->domainSize();
134  ++modality) {
136 
137  if (!visitedNode.exists(oldSonId)) {
139 
140  if (!rd__->isTerminalNode(oldSonId)) {
141  if (rd__->node(oldSonId)->nodeVar() != curVar) {
143  } else {
147  ++curVarModality)
148  newVal = function__(
149  newVal,
151 
153 
154  rd__->manager()->eraseNode(oldSonId, newSonId, false);
156  }
157 
158  } else {
161  ++curVarModality)
163 
166  }
167 
169 
170  } else {
173  }
174  }
175  }
176 
178  }
179 
180  return rd__;
181  }
182 
183 } // namespace gum
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:669