aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
iti.h
Go to the documentation of this file.
1 /**
2  *
3  * Copyright (c) 2005-2021 by 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() {
105  GUM_DESTRUCTOR(ITI);
106  ;
107  }
108 
109  /// @}
110 
111  // ###################################################################
112  /// @name New Observation insertion methods
113  // ###################################################################
114  /// @{
115  public:
116  // ==========================================================================
117  /**
118  * Inserts a new observation
119  * @param obs the new observation to learn
120  */
121  // ==========================================================================
122  void addObservation(const Observation* obs);
123 
124  protected:
125  // ==========================================================================
126  /**
127  * Will update internal graph's NodeDatabase of given node with the new
128  * observation
129  * @param newObs
130  * @param currentNodeId
131  */
132  // ==========================================================================
133  void updateNodeWithObservation_(const Observation* newObs, NodeId currentNodeId);
134 
135  /// @}
136 
137  // ###################################################################
138  /// @name Graph Structure update methods
139  // ###################################################################
140  /// @{
141  public:
142  // ==========================================================================
143  /// Updates the internal graph after a new observation has been added
144  // ==========================================================================
145  void updateGraph();
146 
147  protected:
148  // ==========================================================================
149  /**
150  * inserts a new node in internal graph
151  * @param nDB : the associated database
152  * @param boundVar : the associated variable
153  * @return the newly created node's id
154  */
155  // ==========================================================================
157  const DiscreteVariable* boundVar);
158 
159  // ==========================================================================
160  /**
161  * Changes the associated variable of a node
162  * @param chgedNodeId : the node to change
163  * @param desiredVar : its new associated variable
164  */
165  // ==========================================================================
166  void chgNodeBoundVar_(NodeId chgedNodeId, const DiscreteVariable* desiredVar);
167 
168  // ==========================================================================
169  /**
170  * Removes a node from the internal graph
171  * @param removedNodeId : the node to remove
172  */
173  // ==========================================================================
174  void removeNode_(NodeId removedNodeId);
175 
176  /// @}
177 
178  public:
179  // ###################################################################
180  /// @name Function Graph Updating methods
181  // ###################################################################
182  /// @{
183 
184  // ==========================================================================
185  /// Updates target to currently learned graph structure
186  // ==========================================================================
187  void updateFunctionGraph();
188 
189  private:
190  // ==========================================================================
191  /**
192  * Inserts an internal node in the target
193  * @param src the source node in internal graph
194  * @return the mathcing node id in the target
195  */
196  // ==========================================================================
198 
199  // ==========================================================================
200  /**
201  * Insert a terminal node in the target.
202  * This function is a dispatcher that will call the right function according
203  * to
204  * the value of the template isScalar
205  * @param src the source node in the learned graph
206  * @return the matching node in the target
207  */
208  // ==========================================================================
210  return _insertTerminalNode_(src, Int2Type< isScalar >());
211  }
212 
213  // ==========================================================================
214  /**
215  * Insert a terminal node in the target.
216  * This function is called if we're learning a real value function.
217  * Inserts then a single value in target.
218  * @param src the source node in the learned graph
219  * @return the matching node in the target
220  */
221  // ==========================================================================
223 
224  // ==========================================================================
225  /**
226  * Insert a terminal node in the target.
227  * This function is called if we're learning the behaviour of a variable.
228  * Inserts then this variable and the relevant value beneath into target.
229  * @param src the source node in the learned graph
230  * @return the matching node in the target
231  */
232  // ==========================================================================
234 
235  /// @}
236 
237 
238  protected:
239  // ==========================================================================
240  /**
241  * @brief insertSetOfVars_
242  * @param ret
243  */
244  // ==========================================================================
245  void insertSetOfVars_(MultiDimFunctionGraph< double >* ret) {
246  for (SetIteratorSafe< const DiscreteVariable* > varIter = this->setOfVars_.beginSafe();
247  varIter != this->setOfVars_.endSafe();
248  ++varIter)
249  ret->add(**varIter);
250  }
251 
252  private:
253  /// Hashtable indicating if given node has been modified (upon receiving new
254  /// exemple or through a transpose)
255  /// The aim is not if we have revise the installed variable on that node
257 
258  /// The total number of observation added to this tree
260 
261  /// The threshold above which we consider variables to be dependant
263  };
264 
265 
266 } /* namespace gum */
267 
268 #include <agrum/FMDP/learning/datastructure/iti_tpl.h>
269 
270 #endif // GUM_ITI_H
void updateFunctionGraph()
Updates target to currently learned graph structure.
Definition: iti_tpl.h:251
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:84
~ITI()
Default destructor.
Definition: iti.h:104
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:643
NodeId _insertTerminalNode_(NodeId src, Int2Type< false >)
Insert a terminal node in the target.
Definition: iti_tpl.h:295
void updateGraph()
Updates the internal graph after a new observation has been added.
Definition: iti_tpl.h:139
double _attributeSelectionThreshold_
The threshold above which we consider variables to be dependant.
Definition: iti.h:262
void removeNode_(NodeId removedNodeId)
Removes a node from the internal graph.
Definition: iti_tpl.h:237
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
Idx _nbTotalObservation_
The total number of observation added to this tree.
Definition: iti.h:259
void insertSetOfVars_(MultiDimFunctionGraph< double > *ret)
insertSetOfVars_
Definition: iti.h:245
NodeId insertNode_(NodeDatabase< AttributeSelection, isScalar > *nDB, const DiscreteVariable *boundVar)
inserts a new node in internal graph
Definition: iti_tpl.h:203
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:122
HashTable< NodeId, bool > _staleTable_
Hashtable indicating if given node has been modified (upon receiving new exemple or through a transpo...
Definition: iti.h:256
void chgNodeBoundVar_(NodeId chgedNodeId, const DiscreteVariable *desiredVar)
Changes the associated variable of a node.
Definition: iti_tpl.h:220
NodeId _insertTerminalNode_(NodeId src)
Insert a terminal node in the target.
Definition: iti.h:209
NodeId _insertNodeInFunctionGraph_(NodeId src)
Inserts an internal node in the target.
Definition: iti_tpl.h:265
void addObservation(const Observation *obs)
Inserts a new observation.
Definition: iti_tpl.h:108