aGrUM 2.3.2
a C++ library for (probabilistic) graphical models
structuredPlaner.h
Go to the documentation of this file.
1/****************************************************************************
2 * This file is part of the aGrUM/pyAgrum library. *
3 * *
4 * Copyright (c) 2005-2025 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-2025 *
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
49
50// =========================================================================
51#ifndef GUM_STRUCTURED_PLANNING_H
52#define GUM_STRUCTURED_PLANNING_H
53// =========================================================================
54#include <thread>
55// =========================================================================
56// =========================================================================
57// =========================================================================
61
62// =========================================================================
63
64namespace gum {
65
81 template < typename GUM_SCALAR >
82 class StructuredPlaner: public IPlanningStrategy< GUM_SCALAR > {
83 // ###################################################################
85 // ###################################################################
87
88 public:
89 // ==========================================================================
91 // ==========================================================================
92 static StructuredPlaner< GUM_SCALAR >* spumddInstance(GUM_SCALAR discountFactor = 0.9,
93 GUM_SCALAR epsilon = 0.00001,
94 bool verbose = true) {
96 discountFactor,
97 epsilon,
98 verbose);
99 }
100
101 // ==========================================================================
103 // ==========================================================================
104 static StructuredPlaner< GUM_SCALAR >* sviInstance(GUM_SCALAR discountFactor = 0.9,
105 GUM_SCALAR epsilon = 0.00001,
106 bool verbose = true) {
108 discountFactor,
109 epsilon,
110 verbose);
111 }
112
114
115 // ###################################################################
117 // ###################################################################
119
120 protected:
121 // ==========================================================================
123 // ==========================================================================
125 GUM_SCALAR discountFactor,
126 GUM_SCALAR epsilon,
127 bool verbose);
128
129 // ==========================================================================
131 // ==========================================================================
132
133 public:
134 virtual ~StructuredPlaner();
135
137
138 // ###################################################################
140 // ###################################################################
142
143 public:
144 // ==========================================================================
147 // ==========================================================================
148 INLINE const FMDP< GUM_SCALAR >* fmdp() { return fmdp_; }
149
150 // ==========================================================================
152 // ==========================================================================
154
155 // ==========================================================================
157 // ==========================================================================
158 virtual Size vFunctionSize() { return vFunction_ != nullptr ? vFunction_->realSize() : 0; }
159
160 // ==========================================================================
162 // ==========================================================================
166
167 // ==========================================================================
169 // ==========================================================================
171 return optimalPolicy_ != nullptr ? optimalPolicy_->realSize() : 0;
172 }
173
174 // ==========================================================================
178 // ==========================================================================
179 std::string optimalPolicy2String();
180
182
183
184 // ###################################################################
186 // ###################################################################
188
189 public:
190 // ==========================================================================
196 // ==========================================================================
197 virtual void initialize(const FMDP< GUM_SCALAR >* fmdp);
198
199
200 // ==========================================================================
209 // ==========================================================================
210 virtual void makePlanning(Idx nbStep = 1000000);
211
213
214
215 // ###################################################################
217 // ###################################################################
219
220 protected:
221 // ==========================================================================
223 // ==========================================================================
224 virtual void initVFunction_();
225
226 // ==========================================================================
228 // ==========================================================================
230
231 // ==========================================================================
233 // ==========================================================================
236
237 // ==========================================================================
240 // ==========================================================================
243
244 // ==========================================================================
247 // ==========================================================================
250
251 // ==========================================================================
254 // ==========================================================================
256 addReward_(MultiDimFunctionGraph< GUM_SCALAR >* function, Idx actionId = 0);
257
259
260
261 // ###################################################################
263 // ###################################################################
265
266 protected:
267 // ==========================================================================
269 // ==========================================================================
270 virtual void evalPolicy_();
271
272 // ==========================================================================
283 // ==========================================================================
285 makeArgMax_(const MultiDimFunctionGraph< GUM_SCALAR >* Qaction, Idx actionId);
286
287 private:
288 // ==========================================================================
290 // ==========================================================================
292 NodeId,
293 Idx,
297
298 protected:
299 // ==========================================================================
302 // ==========================================================================
306
307 // ==========================================================================
312 // ==========================================================================
315 optimalValueFunction);
316
317 private:
318 // ==========================================================================
320 // ==========================================================================
322 NodeId,
325
326 // ==========================================================================
328 // ==========================================================================
330
331
333
334 protected:
335 // ==========================================================================
339 // ==========================================================================
341
342 // ==========================================================================
344 // ==========================================================================
346
347 // ==========================================================================
354 // ==========================================================================
356
357 // ==========================================================================
359 // ==========================================================================
361
362 // ==========================================================================
364 // ==========================================================================
365 GUM_SCALAR discountFactor_;
366
368
369 // ==========================================================================
372 // ==========================================================================
374
375
376 private:
377 // ==========================================================================
380 // ==========================================================================
381 GUM_SCALAR _threshold_;
383 };
384
385} /* namespace gum */
386
388
389#endif // GUM_STRUCTURED_PLANNING_H
Headers of the Planning Strategy interface.
A class to store the optimal actions.
Definition actionSet.h:105
Class to handle efficiently argMaxSet.
Definition argMaxSet.h:78
This class is used to implement factored decision process.
Definition fmdp.h:73
<agrum/FMDP/SDyna/IOperatorStrategy.h>
<agrum/FMDP/SDyna/IPlanningStrategy.h>
<agrum/FMDP/planning/mddOperatorStrategy.h>
Class implementingting a function graph.
Implementation of a Terminal Node Policy that maps nodeid to a set of value.
virtual void evalPolicy_()
Perform the required tasks to extract an optimal policy.
NodeId _recurExtractOptPol_(NodeId, const MultiDimFunctionGraph< ArgMaxSet< GUM_SCALAR, Idx >, SetTerminalNodePolicy > *, HashTable< NodeId, NodeId > &)
Recursion part for the createArgMaxCopy.
GUM_SCALAR discountFactor_
Discount Factor used for infinite horizon planning.
virtual void initialize(const FMDP< GUM_SCALAR > *fmdp)
Initializes data structure needed for making the planning.
NodeId _recurArgMaxCopy_(NodeId, Idx, const MultiDimFunctionGraph< GUM_SCALAR > *, MultiDimFunctionGraph< ArgMaxSet< GUM_SCALAR, Idx >, SetTerminalNodePolicy > *, HashTable< NodeId, NodeId > &)
Recursion part for the createArgMaxCopy.
bool verbose_
Boolean used to indcates whether or not iteration informations should be displayed on terminal.
virtual ~StructuredPlaner()
Default destructor.
virtual void makePlanning(Idx nbStep=1000000)
Performs a value iteration.
static StructuredPlaner< GUM_SCALAR > * sviInstance(GUM_SCALAR discountFactor=0.9, GUM_SCALAR epsilon=0.00001, bool verbose=true)
void _transferActionIds_(const ArgMaxSet< GUM_SCALAR, Idx > &, ActionSet &)
Extract from an ArgMaxSet the associated ActionSet.
virtual Size vFunctionSize()
Returns vFunction computed so far current size.
virtual MultiDimFunctionGraph< GUM_SCALAR > * minimiseFunctions_(std::vector< MultiDimFunctionGraph< GUM_SCALAR > * > &)
Performs min_i F_i.
virtual MultiDimFunctionGraph< ArgMaxSet< GUM_SCALAR, Idx >, SetTerminalNodePolicy > * argmaximiseQactions_(std::vector< MultiDimFunctionGraph< ArgMaxSet< GUM_SCALAR, Idx >, SetTerminalNodePolicy > * > &)
Performs argmax_a Q(s,a).
void extractOptimalPolicy_(const MultiDimFunctionGraph< ArgMaxSet< GUM_SCALAR, Idx >, SetTerminalNodePolicy > *optimalValueFunction)
From V(s)* = argmax_a Q*(s,a), this function extract pi*(s) This function mainly consists in extracti...
MultiDimFunctionGraph< ActionSet, SetTerminalNodePolicy > * optimalPolicy_
The associated optimal policy.
virtual MultiDimFunctionGraph< GUM_SCALAR > * addReward_(MultiDimFunctionGraph< GUM_SCALAR > *function, Idx actionId=0)
Perform the R(s) + gamma . function.
IOperatorStrategy< GUM_SCALAR > * operator_
const FMDP< GUM_SCALAR > * fmdp_
The Factored Markov Decision Process describing our planning situation (NB : this one must have funct...
std::string optimalPolicy2String()
Provide a better toDot for the optimal policy where the leaves have the action name instead of its id...
virtual Size optimalPolicySize()
Returns optimalPolicy computed so far current size.
virtual MultiDimFunctionGraph< GUM_SCALAR > * valueIteration_()
Performs a single step of value iteration.
INLINE const MultiDimFunctionGraph< GUM_SCALAR > * vFunction()
Returns a const ptr on the value function computed so far.
GUM_SCALAR _threshold_
The threshold value Whenever | V^{n} - V^{n+1} | < threshold, we consider that V ~ V*.
gum::VariableSet elVarSeq_
A Set to eleminate primed variables.
StructuredPlaner(IOperatorStrategy< GUM_SCALAR > *opi, GUM_SCALAR discountFactor, GUM_SCALAR epsilon, bool verbose)
Default constructor.
virtual void initVFunction_()
Performs a single step of value iteration.
INLINE const FMDP< GUM_SCALAR > * fmdp()
Returns a const ptr on the Factored Markov Decision Process on which we're planning.
MultiDimFunctionGraph< ArgMaxSet< GUM_SCALAR, Idx >, SetTerminalNodePolicy > * makeArgMax_(const MultiDimFunctionGraph< GUM_SCALAR > *Qaction, Idx actionId)
Creates a copy of given Qaction that can be exploit by a Argmax.
static StructuredPlaner< GUM_SCALAR > * spumddInstance(GUM_SCALAR discountFactor=0.9, GUM_SCALAR epsilon=0.00001, bool verbose=true)
virtual MultiDimFunctionGraph< GUM_SCALAR > * maximiseQactions_(std::vector< MultiDimFunctionGraph< GUM_SCALAR > * > &)
Performs max_a Q(s,a).
MultiDimFunctionGraph< GUM_SCALAR > * vFunction_
The Value Function computed iteratively.
INLINE MultiDimFunctionGraph< ActionSet, SetTerminalNodePolicy > * optimalPolicy()
Returns the best policy obtained so far.
virtual MultiDimFunctionGraph< GUM_SCALAR > * evalQaction_(const MultiDimFunctionGraph< GUM_SCALAR > *, Idx)
Performs the P(s'|s,a).V^{t-1}(s') part of the value itération.
<agrum/FMDP/planning/treeOperatorStrategy.h>
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 MDDOperatorStrategy planer class.
gum is the global namespace for all aGrUM entities
Definition agrum.h:46
Set< const DiscreteVariable * > VariableSet
Template implementation of FMDP/planning/StructuredPlaner.h classes.
Headers of the TreeOperatorStrategy planer class.