aGrUM 3.1.1
a C++ library for (probabilistic) graphical models
fmdpLearner_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
50
51// =========================================================================
53
54// =========================================================================
55
56namespace gum {
57
58 // ==========================================================================
59 // Constructor & destructor.
60 // ==========================================================================
61
62 // ###################################################################
63 // Default constructor
64 // ###################################################################
65 template < TESTNAME VariableAttributeSelection,
66 TESTNAME RewardAttributeSelection,
67 LEARNERNAME LearnerSelection >
69 FMDPLearner(double lT, bool actionReward, double sT) :
71 GUM_CONSTRUCTOR(FMDPLearner);
72 _rewardLearner_ = nullptr;
73 }
74
75 // ###################################################################
76 // Default destructor
77 // ###################################################################
78 template < TESTNAME VariableAttributeSelection,
79 TESTNAME RewardAttributeSelection,
80 LEARNERNAME LearnerSelection >
83 for (auto actionIter = _actionLearners_.beginSafe(); actionIter != _actionLearners_.endSafe();
84 ++actionIter) {
85 for (auto learnerIter = actionIter.val()->beginSafe();
86 learnerIter != actionIter.val()->endSafe();
87 ++learnerIter)
88 delete learnerIter.val();
89 delete actionIter.val();
90 if (_actionRewardLearners_.exists(actionIter.key()))
91 delete _actionRewardLearners_[actionIter.key()];
92 }
93
95
96 GUM_DESTRUCTOR(FMDPLearner);
97 }
98
99 // ==========================================================================
100 //
101 // ==========================================================================
102
103 // ###################################################################
104 //
105 // ###################################################################
106 template < TESTNAME VariableAttributeSelection,
107 TESTNAME RewardAttributeSelection,
108 LEARNERNAME LearnerSelection >
111 _fmdp_ = fmdp;
112
113 _modaMax_ = 0;
114 _rmax_ = 0.0;
115
116 gum::VariableSet mainVariables;
117 for (auto varIter = _fmdp_->beginVariables(); varIter != _fmdp_->endVariables(); ++varIter) {
118 mainVariables.insert(*varIter);
119 _modaMax_ = _modaMax_ < (*varIter)->domainSize() ? (*varIter)->domainSize() : _modaMax_;
120 }
121
122 for (auto actionIter = _fmdp_->beginActions(); actionIter != _fmdp_->endActions();
123 ++actionIter) {
124 // Adding a Hashtable for the action
125 _actionLearners_.insert(*actionIter, new VarLearnerTable());
126
127 // Adding a learner for each variable
128 for (auto varIter = _fmdp_->beginVariables(); varIter != _fmdp_->endVariables(); ++varIter) {
130 varTrans->setTableName("ACTION : " + _fmdp_->actionName(*actionIter)
131 + " - VARIABLE : " + (*varIter)->name());
132 _fmdp_->addTransitionForAction(*actionIter, *varIter, varTrans);
133 _actionLearners_[*actionIter]->insert(
134 (*varIter),
135 _instantiateVarLearner_(varTrans, mainVariables, _fmdp_->main2prime(*varIter)));
136 }
137
138 if (_actionReward_) {
140 reward->setTableName("REWARD - ACTION : " + _fmdp_->actionName(*actionIter));
141 _fmdp_->addRewardForAction(*actionIter, reward);
142 _actionRewardLearners_.insert(*actionIter,
143 _instantiateRewardLearner_(reward, mainVariables));
144 }
145 }
146
147 if (!_actionReward_) {
149 reward->setTableName("REWARD");
150 _fmdp_->addReward(reward);
151 _rewardLearner_ = _instantiateRewardLearner_(reward, mainVariables);
152 }
153 }
154
155 // ###################################################################
156 //
157 // ###################################################################
158 template < TESTNAME VariableAttributeSelection,
159 TESTNAME RewardAttributeSelection,
160 LEARNERNAME LearnerSelection >
162 addObservation(Idx actionId, const Observation* newObs) {
163 for (SequenceIteratorSafe< const DiscreteVariable* > varIter = _fmdp_->beginVariables();
164 varIter != _fmdp_->endVariables();
165 ++varIter) {
166 _actionLearners_[actionId]->getWithDefault(*varIter, nullptr)->addObservation(newObs);
167 _actionLearners_[actionId]->getWithDefault(*varIter, nullptr)->updateGraph();
168 }
169
170 if (_actionReward_) {
171 _actionRewardLearners_[actionId]->addObservation(newObs);
172 _actionRewardLearners_[actionId]->updateGraph();
173 } else {
174 _rewardLearner_->addObservation(newObs);
175 _rewardLearner_->updateGraph();
176 }
177
178 _rmax_ = _rmax_ < std::abs(newObs->reward()) ? std::abs(newObs->reward()) : _rmax_;
179
180 return false;
181 }
182
183 // ###################################################################
184 //
185 // ###################################################################
186 template < TESTNAME VariableAttributeSelection,
187 TESTNAME RewardAttributeSelection,
188 LEARNERNAME LearnerSelection >
190 size() {
191 Size s = 0;
192 for (SequenceIteratorSafe< Idx > actionIter = _fmdp_->beginActions();
193 actionIter != _fmdp_->endActions();
194 ++actionIter) {
195 for (SequenceIteratorSafe< const DiscreteVariable* > varIter = _fmdp_->beginVariables();
196 varIter != _fmdp_->endVariables();
197 ++varIter)
198 s += _actionLearners_[*actionIter]->getWithDefault(*varIter, nullptr)->size();
199 if (_actionReward_) s += _actionRewardLearners_[*actionIter]->size();
200 }
201
202 if (!_actionReward_) s += _rewardLearner_->size();
203
204 return s;
205 }
206
207 // ###################################################################
208 //
209 // ###################################################################
210 template < TESTNAME VariableAttributeSelection,
211 TESTNAME RewardAttributeSelection,
212 LEARNERNAME LearnerSelection >
214 updateFMDP() {
215 for (SequenceIteratorSafe< Idx > actionIter = _fmdp_->beginActions();
216 actionIter != _fmdp_->endActions();
217 ++actionIter) {
218 for (SequenceIteratorSafe< const DiscreteVariable* > varIter = _fmdp_->beginVariables();
219 varIter != _fmdp_->endVariables();
220 ++varIter)
221 _actionLearners_[*actionIter]->getWithDefault(*varIter, nullptr)->updateFunctionGraph();
222 if (_actionReward_) _actionRewardLearners_[*actionIter]->updateFunctionGraph();
223 }
224
225 if (!_actionReward_) _rewardLearner_->updateFunctionGraph();
226 }
227
228 // ==========================================================================
229 // Instantiation methods
230 // ==========================================================================
231
232 template < TESTNAME VariableAttributeSelection,
233 TESTNAME RewardAttributeSelection,
234 LEARNERNAME LearnerSelection >
235 MultiDimFunctionGraph< double >* FMDPLearner< VariableAttributeSelection,
236 RewardAttributeSelection,
240
241 template < TESTNAME VariableAttributeSelection,
242 TESTNAME RewardAttributeSelection,
243 LEARNERNAME LearnerSelection >
249
250 template < TESTNAME VariableAttributeSelection,
251 TESTNAME RewardAttributeSelection,
252 LEARNERNAME LearnerSelection >
258
259 template < TESTNAME VariableAttributeSelection,
260 TESTNAME RewardAttributeSelection,
261 LEARNERNAME LearnerSelection >
271
272 template < TESTNAME VariableAttributeSelection,
273 TESTNAME RewardAttributeSelection,
274 LEARNERNAME LearnerSelection >
286
287 template < TESTNAME VariableAttributeSelection,
288 TESTNAME RewardAttributeSelection,
289 LEARNERNAME LearnerSelection >
297
298 template < TESTNAME VariableAttributeSelection,
299 TESTNAME RewardAttributeSelection,
300 LEARNERNAME LearnerSelection >
306
307 template < TESTNAME VariableAttributeSelection,
308 TESTNAME RewardAttributeSelection,
309 LEARNERNAME LearnerSelection >
316
317 template < TESTNAME VariableAttributeSelection,
318 TESTNAME RewardAttributeSelection,
319 LEARNERNAME LearnerSelection >
326
327 template < TESTNAME VariableAttributeSelection,
328 TESTNAME RewardAttributeSelection,
329 LEARNERNAME LearnerSelection >
332 varLearner(Idx actionId, const DiscreteVariable* var) const {
333 return _actionLearners_[actionId]->getWithDefault(var, nullptr);
334 }
335
336 template < TESTNAME VariableAttributeSelection,
337 TESTNAME RewardAttributeSelection,
338 LEARNERNAME LearnerSelection >
339 double
344
345 template < TESTNAME VariableAttributeSelection,
346 TESTNAME RewardAttributeSelection,
347 LEARNERNAME LearnerSelection >
352
353} // End of namespace gum
Base class for discrete random variable.
double rMax() const override
learnerSize
RewardLearnerType * _instantiateRewardLearner_(MultiDimFunctionGraph< double > *target, gum::VariableSet &mainVariables)
Initializes the learner.
typename LearnerSelect< LearnerSelection, IMDDI< RewardAttributeSelection, true >, ITI< RewardAttributeSelection, true > >::type RewardLearnerType
Definition fmdpLearner.h:82
HashTable< Idx, RewardLearnerType * > _actionRewardLearners_
const double _similarityThreshold_
void initialize(FMDP< double > *fmdp) override
Initializes the learner.
MultiDimFunctionGraph< double > * _instantiateFunctionGraph_()
Initializes the learner.
double _modaMax_
learnerSize
FMDP< double > * _fmdp_
The FMDP to store the learned model.
double _rmax_
learnerSize
bool addObservation(Idx actionId, const Observation *obs) override
Gives to the learner a new transition.
HashTable< const DiscreteVariable *, VariableLearnerType * > VarLearnerTable
Definition fmdpLearner.h:86
~FMDPLearner() override
Default destructor.
const double _learningThreshold_
RewardLearnerType * _rewardLearner_
VariableLearnerType * _instantiateVarLearner_(MultiDimFunctionGraph< double > *target, gum::VariableSet &mainVariables, const DiscreteVariable *learnedVar)
Initializes the learner.
FMDPLearner(double learningThreshold, bool actionReward, double similarityThreshold=0.05)
Default constructor.
void updateFMDP() override
Starts an update of datastructure in the associated FMDP.
HashTable< Idx, VarLearnerTable * > _actionLearners_
typename LearnerSelect< LearnerSelection, IMDDI< VariableAttributeSelection, false >, ITI< VariableAttributeSelection, false > >::type VariableLearnerType
Definition fmdpLearner.h:77
const IVisitableGraphLearner * varLearner(Idx actionId, const DiscreteVariable *var) const override
extractCount
Size size() override
learnerSize
double modaMax() const override
learnerSize
<agrum/FMDP/SDyna/IVisitableGraphLearner.h>
static MultiDimFunctionGraph< GUM_ELEMENT, TerminalNodePolicy > * getTreeInstance()
Returns an arborescent instance.
void setTableName(std::string_view name)
Sets the name of the table represented by this structure.
static MultiDimFunctionGraph< GUM_ELEMENT, TerminalNodePolicy > * getReducedAndOrderedInstance()
Returns a reduced and ordered instance.
double reward() const
Returns the modality assumed by the given variable in this observation.
Safe iterators for Sequence.
Definition sequence.h:1148
void insert(const Key &k)
Inserts a new element into the set.
Definition set_tpl.h:510
Headers of the FMDPLearner class.
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
gum is the global namespace for all aGrUM entities
Definition agrum.h:46
Set< const DiscreteVariable * > VariableSet