aGrUM  0.20.2
a C++ library for (probabilistic) graphical models
iti.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 Headers of the ITI class.
25  *
26  * @author Jean-Christophe MAGNAN
27  */
28 
29 // =========================================================================
30 #ifndef GUM_ITI_H
31 #define GUM_ITI_H
32 // =========================================================================
33 #include <agrum/tools/core/multiPriorityQueue.h>
34 // =========================================================================
35 #include <agrum/tools/multidim/implementations/multiDimFunctionGraph.h>
36 // =========================================================================
37 #include <agrum/FMDP/learning/core/templateStrategy.h>
38 #include <agrum/FMDP/learning/datastructure/incrementalGraphLearner.h>
39 // =========================================================================
40 #include <agrum/tools/variables/discreteVariable.h>
41 // =========================================================================
42 
43 namespace gum {
44 
45  /**
46  * @class ITI
47  * @headerfile iti.h <agrum/FMDP/planning/FunctionGraph/iti.h>
48  * @brief
49  * @ingroup fmdp_group
50  *
51  * Learn a graphical representation of a function as a decision tree.
52  * This learning is done incrementaly.
53  * Hence first observation are add to the structure and then
54  * the structure is updated.
55  * Maintains two graph function : one internal for the learning
56  * and a target which is updated on demand.
57  *
58  */
59 
60  template < TESTNAME AttributeSelection, bool isScalar = false >
62  public:
63  // ###################################################################
64  /// @name Constructor & destructor.
65  // ###################################################################
66  /// @{
67 
68  // ==========================================================================
69  /**
70  * ITI constructor for functions describing the behaviour of one variable
71  * according to a set of other variable such as conditionnal probabilities
72  * @param target : the MultiDimFunctionGraph in which we load the structure
73  * @param attributeSelectionThreshold : threshold under which a node is not
74  * installed (pe-pruning)
75  * @param attributeListe : Set of vars on which we rely to explain the
76  * behaviour of learned variable
77  * @param learnedValue : the variable from which we try to learn the
78  * behaviour
79  */
80  // ==========================================================================
81  ITI(MultiDimFunctionGraph< double >* target,
82  double attributeSelectionThreshold,
83  Set< const DiscreteVariable* > attributeListe,
84  const DiscreteVariable* learnedValue);
85 
86  // ==========================================================================
87  /**
88  * ITI constructeur for real functions. We try to predict the output of a
89  * function f given a set of variable
90  * @param target : the MultiDimFunctionGraph in which we load the structure
91  * @param attributeSelectionThreshold : threshold under which a node is not
92  * installed (pe-pruning)
93  * @param attributeListe : Set of vars on which we rely to explain the
94  * behaviour of learned function
95  */
96  // ==========================================================================
97  ITI(MultiDimFunctionGraph< double >* target,
98  double attributeSelectionThreshold,
99  Set< const DiscreteVariable* > attributeListe);
100 
101  // ==========================================================================
102  /// Default destructor
103  // ==========================================================================
104  ~ITI() { GUM_DESTRUCTOR(ITI); }
105 
106  /// @}
107 
108  // ###################################################################
109  /// @name New Observation insertion methods
110  // ###################################################################
111  /// @{
112  public:
113  // ==========================================================================
114  /**
115  * Inserts a new observation
116  * @param obs the new observation to learn
117  */
118  // ==========================================================================
119  void addObservation(const Observation* obs);
120 
121  protected:
122  // ==========================================================================
123  /**
124  * Will update internal graph's NodeDatabase of given node with the new
125  * observation
126  * @param newObs
127  * @param currentNodeId
128  */
129  // ==========================================================================
130  void updateNodeWithObservation_(const Observation* newObs,
131  NodeId currentNodeId);
132 
133  /// @}
134 
135  // ###################################################################
136  /// @name Graph Structure update methods
137  // ###################################################################
138  /// @{
139  public:
140  // ==========================================================================
141  /// Updates the internal graph after a new observation has been added
142  // ==========================================================================
143  void updateGraph();
144 
145  protected:
146  // ==========================================================================
147  /**
148  * inserts a new node in internal graph
149  * @param nDB : the associated database
150  * @param boundVar : the associated variable
151  * @return the newly created node's id
152  */
153  // ==========================================================================
155  const DiscreteVariable* boundVar);
156 
157  // ==========================================================================
158  /**
159  * Changes the associated variable of a node
160  * @param chgedNodeId : the node to change
161  * @param desiredVar : its new associated variable
162  */
163  // ==========================================================================
164  void chgNodeBoundVar_(NodeId chgedNodeId, const DiscreteVariable* desiredVar);
165 
166  // ==========================================================================
167  /**
168  * Removes a node from the internal graph
169  * @param removedNodeId : the node to remove
170  */
171  // ==========================================================================
172  void removeNode_(NodeId removedNodeId);
173 
174  /// @}
175 
176  public:
177  // ###################################################################
178  /// @name Function Graph Updating methods
179  // ###################################################################
180  /// @{
181 
182  // ==========================================================================
183  /// Updates target to currently learned graph structure
184  // ==========================================================================
185  void updateFunctionGraph();
186 
187  private:
188  // ==========================================================================
189  /**
190  * Inserts an internal node in the target
191  * @param src the source node in internal graph
192  * @return the mathcing node id in the target
193  */
194  // ==========================================================================
196 
197  // ==========================================================================
198  /**
199  * Insert a terminal node in the target.
200  * This function is a dispatcher that will call the right function according
201  * to
202  * the value of the template isScalar
203  * @param src the source node in the learned graph
204  * @return the matching node in the target
205  */
206  // ==========================================================================
208  return insertTerminalNode__(src, Int2Type< isScalar >());
209  }
210 
211  // ==========================================================================
212  /**
213  * Insert a terminal node in the target.
214  * This function is called if we're learning a real value function.
215  * Inserts then a single value in target.
216  * @param src the source node in the learned graph
217  * @return the matching node in the target
218  */
219  // ==========================================================================
221 
222  // ==========================================================================
223  /**
224  * Insert a terminal node in the target.
225  * This function is called if we're learning the behaviour of a variable.
226  * Inserts then this variable and the relevant value beneath into target.
227  * @param src the source node in the learned graph
228  * @return the matching node in the target
229  */
230  // ==========================================================================
232 
233  /// @}
234 
235 
236  protected:
237  // ==========================================================================
238  /**
239  * @brief insertSetOfVars_
240  * @param ret
241  */
242  // ==========================================================================
243  void insertSetOfVars_(MultiDimFunctionGraph< double >* ret) {
244  for (SetIteratorSafe< const DiscreteVariable* > varIter
245  = this->setOfVars_.beginSafe();
246  varIter != this->setOfVars_.endSafe();
247  ++varIter)
248  ret->add(**varIter);
249  }
250 
251  private:
252  /// Hashtable indicating if given node has been modified (upon receiving new
253  /// exemple or through a transpose)
254  /// The aim is not if we have revise the installed variable on that node
256 
257  /// The total number of observation added to this tree
259 
260  /// The threshold above which we consider variables to be dependant
262  };
263 
264 
265 } /* namespace gum */
266 
267 #include <agrum/FMDP/learning/datastructure/iti_tpl.h>
268 
269 #endif // GUM_ITI_H
void updateFunctionGraph()
Updates target to currently learned graph structure.
Definition: iti_tpl.h:268
Learn a graphical representation of a function as a decision tree.
Definition: iti.h:61
ITI(MultiDimFunctionGraph< double > *target, double attributeSelectionThreshold, Set< const DiscreteVariable * > attributeListe)
ITI constructeur for real functions.
Definition: iti_tpl.h:88
~ITI()
Default destructor.
Definition: iti.h:104
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:669
void updateGraph()
Updates the internal graph after a new observation has been added.
Definition: iti_tpl.h:147
void removeNode_(NodeId removedNodeId)
Removes a node from the internal graph.
Definition: iti_tpl.h:253
double attributeSelectionThreshold__
The threshold above which we consider variables to be dependant.
Definition: iti.h:261
ITI(MultiDimFunctionGraph< double > *target, double attributeSelectionThreshold, Set< const DiscreteVariable * > attributeListe, const DiscreteVariable *learnedValue)
ITI constructor for functions describing the behaviour of one variable according to a set of other va...
Definition: iti_tpl.h:61
NodeId insertTerminalNode__(NodeId src, Int2Type< false >)
Insert a terminal node in the target.
Definition: iti_tpl.h:318
void insertSetOfVars_(MultiDimFunctionGraph< double > *ret)
insertSetOfVars_
Definition: iti.h:243
NodeId insertNode_(NodeDatabase< AttributeSelection, isScalar > *nDB, const DiscreteVariable *boundVar)
inserts a new node in internal graph
Definition: iti_tpl.h:214
NodeId insertNodeInFunctionGraph__(NodeId src)
Inserts an internal node in the target.
Definition: iti_tpl.h:283
void updateNodeWithObservation_(const Observation *newObs, NodeId currentNodeId)
Will update internal graph&#39;s NodeDatabase of given node with the new observation. ...
Definition: iti_tpl.h:129
NodeId insertTerminalNode__(NodeId src)
Insert a terminal node in the target.
Definition: iti.h:207
HashTable< NodeId, bool > staleTable__
Hashtable indicating if given node has been modified (upon receiving new exemple or through a transpo...
Definition: iti.h:255
Idx nbTotalObservation__
The total number of observation added to this tree.
Definition: iti.h:258
void chgNodeBoundVar_(NodeId chgedNodeId, const DiscreteVariable *desiredVar)
Changes the associated variable of a node.
Definition: iti_tpl.h:234
void addObservation(const Observation *obs)
Inserts a new observation.
Definition: iti_tpl.h:115