aGrUM  0.14.2
treeRegress_tpl.h
Go to the documentation of this file.
1 /****************************************************************************
2  * Copyright (C) 2005 by Pierre-Henri WUILLEMIN et Christophe GONZALES *
3  * {prenom.nom}_at_lip6.fr *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19  ****************************************************************************/
31 
32 #define ALLOCATE(x) SmallObjectAllocator::instance().allocate(x)
33 #define DEALLOCATE(x, y) SmallObjectAllocator::instance().deallocate(x, y)
34 
35 namespace gum {
36 
37  template < typename GUM_SCALAR,
38  template < typename >
39  class COMBINEOPERATOR,
40  template < typename >
41  class PROJECTOPERATOR,
42  template < typename >
43  class TerminalNodePolicy >
47  const Bijection<
48  const DiscreteVariable*,
50  __vFunc(qAction),
51  __pxi(pxi) {
52  GUM_CONSTRUCTOR(TreeRegress);
53  }
54 
55  template < typename GUM_SCALAR,
56  template < typename >
57  class COMBINEOPERATOR,
58  template < typename >
59  class PROJECTOPERATOR,
60  template < typename >
61  class TerminalNodePolicy >
64  GUM_DESTRUCTOR(TreeRegress);
65  }
66 
67  // This function is the main function. To be call every time an operation
68  // between the two given Function Graphs is required
69  template < typename GUM_SCALAR,
70  template < typename >
71  class COMBINEOPERATOR,
72  template < typename >
73  class PROJECTOPERATOR,
74  template < typename >
75  class TerminalNodePolicy >
77  TreeRegress< GUM_SCALAR,
78  COMBINEOPERATOR,
79  PROJECTOPERATOR,
80  TerminalNodePolicy >::compute() {
82  if (__vFunc->isTerminalNode(__vFunc->root())) {
83  ret = MultiDimFunctionGraph< GUM_SCALAR,
84  TerminalNodePolicy >::getTreeInstance();
85  ret->copy(*__vFunc);
86  } else
87  ret = __xPloreVFunc(__vFunc->root());
88  return ret;
89  }
90 
91 
92  template < typename GUM_SCALAR,
93  template < typename >
94  class COMBINEOPERATOR,
95  template < typename >
96  class PROJECTOPERATOR,
97  template < typename >
98  class TerminalNodePolicy >
100  TreeRegress< GUM_SCALAR,
101  COMBINEOPERATOR,
102  PROJECTOPERATOR,
103  TerminalNodePolicy >::__xPloreVFunc(NodeId currentNodeId) {
104  const InternalNode* currentNode = __vFunc->node(currentNodeId);
105 
106  std::vector< MultiDimFunctionGraph< GUM_SCALAR, TerminalNodePolicy >* >
107  varbucket;
108 
109  for (Idx moda = 0; moda < currentNode->nodeVar()->domainSize(); ++moda) {
111  __context.insert(currentNode->nodeVar(), moda);
112  if (__vFunc->isTerminalNode(currentNode->son(moda))) {
113  GUM_SCALAR value = __vFunc->nodeValue(currentNode->son(moda));
114  if (value) {
115  vpxi = MultiDimFunctionGraph< GUM_SCALAR,
116  TerminalNodePolicy >::getTreeInstance();
117  vpxi->manager()->setRootNode(vpxi->manager()->addTerminalNode(value));
118  }
119  } else {
120  vpxi = __xPloreVFunc(currentNode->son(moda));
121  }
122 
123  if (vpxi != nullptr) {
125  vpxi, __pxi.second(currentNode->nodeVar()), __context);
126  varbucket.push_back(combinope.compute());
127  }
128  delete vpxi;
129  __context.erase(currentNode->nodeVar());
130  }
131 
132  if (varbucket.empty()) return nullptr;
133 
135  varbucket.pop_back();
136  while (!varbucket.empty()) {
138  TreeOperator< GUM_SCALAR, PROJECTOPERATOR > projope(vp, varbucket.back());
139  vp = projope.compute();
140  delete temp;
141  temp = varbucket.back();
142  varbucket.pop_back();
143  delete temp;
144  }
145 
146  return vp;
147  }
148 
149 } // namespace gum
Class used to perform Decision Tree Regression in the FMDP Framework.
Definition: treeRegress.h:52
Class used to compute the operation between two decision diagrams.
const DiscreteVariable * nodeVar() const
Returns the node variable.
NodeId son(Idx modality) const
Returns the son at a given index.
TreeRegress(const MultiDimFunctionGraph< GUM_SCALAR, TerminalNodePolicy > *qAction, const Bijection< const DiscreteVariable *, const MultiDimFunctionGraph< GUM_SCALAR, TerminalNodePolicy > * > pxi)
Default constructor.
Headers of the InternalNode class.
Base class for discrete random variable.
gum is the global namespace for all aGrUM entities
Definition: agrum.h:25
Class used to perform Decision Tree Operation in the FMDP Framework.
Definition: treeOperator.h:50
~TreeRegress()
Default destructor.
const MultiDimFunctionGraph< GUM_SCALAR, TerminalNodePolicy > * __vFunc
The function graphs used for the operation.
Definition: treeRegress.h:87
virtual Size domainSize() const =0
MultiDimFunctionGraph< GUM_SCALAR, TerminalNodePolicy > * compute()
Computes and builds the Function Graph that is the result of the operation.
Structure used to represent a node internal structure.
Definition: internalNode.h:100
Class used to compute the operation between two decision diagrams.
MultiDimFunctionGraph< GUM_SCALAR, TerminalNodePolicy > * compute()
Computes and builds the Function Graph that is the result of the operation.
Set of pairs of elements with fast search for both elements.
Definition: bijection.h:1803
Class implementingting a function graph.
const Bijection< const DiscreteVariable *, const MultiDimFunctionGraph< GUM_SCALAR, TerminalNodePolicy > *> __pxi
Definition: treeRegress.h:91
Size Idx
Type for indexes.
Definition: types.h:50
HashTable< const DiscreteVariable *, Idx > __context
Definition: treeRegress.h:92
MultiDimFunctionGraph< GUM_SCALAR, TerminalNodePolicy > * __xPloreVFunc(NodeId currentNodeId)
The whatever 1.
Size NodeId
Type for node ids.
Definition: graphElements.h:97