aGrUM 3.1.1
a C++ library for (probabilistic) graphical models
iti_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
51// =======================================================
54
56// =======================================================
59// =======================================================
61
62// =======================================================
63
64
65namespace gum {
66
67 // ==========================================================================
69 // ==========================================================================
70
71 // ###################################################################
83 // ###################################################################
84 template < TESTNAME AttributeSelection, bool isScalar >
86 double attributeSelectionThreshold,
87 gum::VariableSet attributeListe,
88 const DiscreteVariable* learnedValue) :
89 IncrementalGraphLearner< AttributeSelection, isScalar >(target, attributeListe, learnedValue),
90 _nbTotalObservation_(0), _attributeSelectionThreshold_(attributeSelectionThreshold) {
91 GUM_CONSTRUCTOR(ITI);
92 _staleTable_.insert(this->root_, false);
93 }
94
95 // ###################################################################
106 // ###################################################################
107 template < TESTNAME AttributeSelection, bool isScalar >
109 double attributeSelectionThreshold,
110 gum::VariableSet attributeListe) :
111 IncrementalGraphLearner< AttributeSelection, isScalar >(
112 target,
113 attributeListe,
114 new LabelizedVariable("Reward", "", 2)),
115 _nbTotalObservation_(0), _attributeSelectionThreshold_(attributeSelectionThreshold) {
116 GUM_CONSTRUCTOR(ITI);
117 _staleTable_.insert(this->root_, false);
118 }
119
120 // ==========================================================================
122 // ==========================================================================
123
124 // ############################################################################
129 // ############################################################################
130 template < TESTNAME AttributeSelection, bool isScalar >
135
136 // ############################################################################
143 // ############################################################################
144 template < TESTNAME AttributeSelection, bool isScalar >
152
153 // ============================================================================
155 // ============================================================================
156
157 // ############################################################################
159 // ############################################################################
160 template < TESTNAME AttributeSelection, bool isScalar >
162 std::vector< NodeId > filo;
163 filo.push_back(this->root_);
165 tensorVars.insert(this->root_, new gum::VariableSet(this->setOfVars_));
166
167
168 while (!filo.empty()) {
169 NodeId currentNodeId = filo.back();
170 filo.pop_back();
171
172 // First we look for the best var to install on the node
173 double bestValue = _attributeSelectionThreshold_;
174 gum::VariableSet bestVars;
175
176 for (auto varIter = tensorVars[currentNodeId]->cbeginSafe();
177 varIter != tensorVars[currentNodeId]->cendSafe();
178 ++varIter)
179 if (this->nodeId2Database_[currentNodeId]->isTestRelevant(*varIter)) {
180 double varValue = this->nodeId2Database_[currentNodeId]->testValue(*varIter);
181 if (varValue >= bestValue) {
182 if (varValue > bestValue) {
183 bestValue = varValue;
184 bestVars.clear();
185 }
186 bestVars.insert(*varIter);
187 }
188 }
189
190 // Then We installed Variable a test on that node
191 this->updateNode_(currentNodeId, bestVars);
192
193 // The we move on the children if needed
194 if (this->nodeVarMap_[currentNodeId] != this->value_) {
195 for (Idx moda = 0; moda < this->nodeVarMap_[currentNodeId]->domainSize(); moda++) {
196 gum::VariableSet* itsTensorVars = new gum::VariableSet(*tensorVars[currentNodeId]);
197 itsTensorVars->erase(this->nodeVarMap_[currentNodeId]);
198 NodeId sonId = this->nodeSonsMap_[currentNodeId][moda];
199 if (_staleTable_[sonId]) {
200 filo.push_back(sonId);
201 tensorVars.insert(sonId, itsTensorVars);
202 }
203 }
204 }
205 }
206
208 nodeIter != tensorVars.endSafe();
209 ++nodeIter)
210 delete nodeIter.val();
211 }
212
213 // ############################################################################
220 // ############################################################################
221 template < TESTNAME AttributeSelection, bool isScalar >
229
230 // ############################################################################
236 // ############################################################################
237 template < TESTNAME AttributeSelection, bool isScalar >
239 const DiscreteVariable* desiredVar) {
240 if (this->nodeVarMap_[currentNodeId] != desiredVar) {
241 _staleTable_[currentNodeId] = true;
243 desiredVar);
244 }
245 }
246
247 // ############################################################################
252 // ############################################################################
253 template < TESTNAME AttributeSelection, bool isScalar >
258
259 // ============================================================================
261 // ============================================================================
262
263 // ############################################################################
265 // ############################################################################
266 template < TESTNAME AttributeSelection, bool isScalar >
268 this->target_->clear();
269 this->target_->manager()->setRootNode(this->_insertNodeInFunctionGraph_(this->root_));
270 }
271
272 // ############################################################################
278 // ############################################################################
279 template < TESTNAME AttributeSelection, bool isScalar >
281 if (this->nodeVarMap_[currentNodeId] == this->value_) {
282 NodeId nody = _insertTerminalNode_(currentNodeId);
283 return nody;
284 }
285
286 if (!this->target_->variablesSequence().exists(this->nodeVarMap_[currentNodeId])) {
287 this->target_->add(*(this->nodeVarMap_[currentNodeId]));
288 }
289
290 NodeId nody = this->target_->manager()->addInternalNode(this->nodeVarMap_[currentNodeId]);
291 for (Idx moda = 0; moda < this->nodeVarMap_[currentNodeId]->domainSize(); ++moda) {
292 NodeId son = this->_insertNodeInFunctionGraph_(this->nodeSonsMap_[currentNodeId][moda]);
293 this->target_->manager()->setSon(nody, moda, son);
294 }
295
296 return nody;
297 }
298
299 // ############################################################################
307 // ############################################################################
308 template < TESTNAME AttributeSelection, bool isScalar >
311 if (!this->target_->variablesSequence().exists(this->value_))
312 this->target_->add(*(this->value_));
313
314 Size tot = this->nodeId2Database_[currentNodeId]->nbObservation();
315 if (tot == Size(0)) return this->target_->manager()->addTerminalNode(0.0);
316
317 NodeId* sonsMap
318 = static_cast< NodeId* >(SOA_ALLOCATE(sizeof(NodeId) * this->value_->domainSize()));
319 for (Idx modality = 0; modality < this->value_->domainSize(); ++modality) {
320 double newVal = 0.0;
321 newVal = (double)this->nodeId2Database_[currentNodeId]->effectif(modality) / (double)tot;
322 sonsMap[modality] = this->target_->manager()->addTerminalNode(newVal);
323 }
324 NodeId nody = this->target_->manager()->addInternalNode(this->value_, sonsMap);
325 return nody;
326 }
327
328 // ############################################################################
336 // ############################################################################
337 template < TESTNAME AttributeSelection, bool isScalar >
340 double value = 0.0;
341 for (auto valIter = this->nodeId2Database_[currentNodeId]->cbeginValues();
342 valIter != this->nodeId2Database_[currentNodeId]->cendValues();
343 ++valIter) {
344 value += (double)valIter.key() * valIter.val();
345 }
346 if (this->nodeId2Database_[currentNodeId]->nbObservation())
347 value /= (double)this->nodeId2Database_[currentNodeId]->nbObservation();
348 NodeId nody = this->target_->manager()->addTerminalNode(value);
349 return nody;
350 }
351
352 template < TESTNAME AttributeSelection, bool isScalar >
354 GUM_DESTRUCTOR(ITI);
355 ;
356 }
357
358 template < TESTNAME AttributeSelection, bool isScalar >
362
363 template < TESTNAME AttributeSelection, bool isScalar >
365 for (SetIteratorSafe< const DiscreteVariable* > varIter = this->setOfVars_.beginSafe();
366 varIter != this->setOfVars_.endSafe();
367 ++varIter)
368 ret->add(**varIter);
369 }
370
371} // namespace gum
Headers of the ChiSquare class.
Safe Iterators for hashtables.
Base class for discrete random variable.
The class for generic Hash Tables.
Definition hashTable.h:640
iterator_safe beginSafe()
Returns the safe iterator pointing to the beginning of the hashtable.
value_type & insert(const Key &key, const Val &val)
Adds a new element (actually a copy of this element) into the hash table.
const iterator_safe & endSafe() noexcept
Returns the safe iterator pointing to the end of the hashtable.
const const_iterator_safe & cendSafe() const noexcept
Returns the safe const_iterator pointing to the end of the hashtable.
void updateFunctionGraph() override
Updates target to currently learned graph structure.
Definition iti_tpl.h:267
NodeId _insertTerminalNode_(NodeId src)
Insert a terminal node in the target.
Definition iti_tpl.h:359
double _attributeSelectionThreshold_
The threshold above which we consider variables to be dependant.
Definition iti.h:272
void removeNode_(NodeId removedNodeId) override
Removes a node from the internal graph.
Definition iti_tpl.h:254
HashTable< NodeId, bool > _staleTable_
Hashtable indicating if given node has been modified (upon receiving new exemple or through a transpo...
Definition iti.h:266
void updateGraph() override
Updates the internal graph after a new observation has been added.
Definition iti_tpl.h:161
NodeId _insertNodeInFunctionGraph_(NodeId src)
Inserts an internal node in the target.
Definition iti_tpl.h:280
Idx _nbTotalObservation_
The total number of observation added to this tree.
Definition iti.h:269
ITI(MultiDimFunctionGraph< double > *target, double attributeSelectionThreshold, gum::VariableSet 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:85
NodeId insertNode_(NodeDatabase< AttributeSelection, isScalar > *nDB, const DiscreteVariable *boundVar) override
inserts a new node in internal graph
Definition iti_tpl.h:222
void insertSetOfVars_(MultiDimFunctionGraph< double > *ret)
insertSetOfVars_
Definition iti_tpl.h:364
void addObservation(const Observation *obs) override
Inserts a new observation.
Definition iti_tpl.h:131
void chgNodeBoundVar_(NodeId chgedNodeId, const DiscreteVariable *desiredVar) override
Changes the associated variable of a node.
Definition iti_tpl.h:238
~ITI() override
Default destructor.
Definition iti_tpl.h:353
void updateNodeWithObservation_(const Observation *newObs, NodeId currentNodeId) override
Will update internal graph's NodeDatabase of given node with the new observation.
Definition iti_tpl.h:145
virtual void updateNodeWithObservation_(const Observation *newObs, NodeId currentNodeId)
Will update internal graph's NodeDatabase of given node with the new observation.
HashTable< NodeId, NodeDatabase< AttributeSelection, isScalar > * > nodeId2Database_
virtual void addObservation(const Observation *obs)
Inserts a new observation.
virtual NodeId insertNode_(NodeDatabase< AttributeSelection, isScalar > *nDB, const DiscreteVariable *boundVar)
inserts a new node in internal graph
HashTable< NodeId, const DiscreteVariable * > nodeVarMap_
IncrementalGraphLearner(MultiDimFunctionGraph< double > *target, gum::VariableSet attributesSet, const DiscreteVariable *learnVariable)
virtual void removeNode_(NodeId removedNodeId)
Removes a node from the internal graph.
virtual void chgNodeBoundVar_(NodeId chgedNodeId, const DiscreteVariable *desiredVar)
Changes the associated variable of a node.
class LabelizedVariable
void add(const DiscreteVariable &v) override
Adds a new var to the variables of the multidimensional matrix.
<agrum/FMDP/learning/datastructure/nodeDatabase.h>
Safe iterators for the Set class.
Definition set.h:592
void clear()
Removes all the elements, if any, from the set.
Definition set_tpl.h:315
void insert(const Key &k)
Inserts a new element into the set.
Definition set_tpl.h:510
void erase(const Key &k)
Erases an element from the set.
Definition set_tpl.h:553
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition types.h:74
Size Idx
Type for indexes.
Definition types.h:79
Size NodeId
Type for node ids.
Headers of the ITI class.
Base class for labelized discrete random variables.
Useful macros for maths.
gum is the global namespace for all aGrUM entities
Definition agrum.h:46
Set< const DiscreteVariable * > VariableSet
priority queues (in which an element cannot appear more than once)
#define SOA_ALLOCATE(x)
Provides basic types used in aGrUM.