aGrUM  0.20.2
a C++ library for (probabilistic) graphical models
adaptiveRMaxPlaner.cpp
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 Template implementation of AdaptiveRMaxPlaner classes.
25  *
26  * @author Pierre-Henri WUILLEMIN(@LIP6) and Jean-Christophe MAGNAN and Christophe
27  * GONZALES(@AMU)
28  */
29 
30 // =========================================================================
31 #include <queue>
32 #include <vector>
33 //#include <algorithm>
34 //#include <utility>
35 // =========================================================================
36 #include <agrum/tools/core/math/math_utils.h>
37 #include <agrum/tools/core/functors.h>
38 #include <agrum/tools/core/smallobjectallocator/smallObjectAllocator.h>
39 // =========================================================================
40 #include <agrum/tools/multidim/implementations/multiDimFunctionGraph.h>
41 #include <agrum/tools/multidim/instantiation.h>
42 #include <agrum/tools/multidim/potential.h>
43 // =========================================================================
44 #include <agrum/FMDP/planning/adaptiveRMaxPlaner.h>
45 // =========================================================================
46 
47 /// For shorter line and hence more comprehensive code purposes only
48 #define RECASTED(x) reinterpret_cast< const MultiDimFunctionGraph< double >* >(x)
49 
50 namespace gum {
51 
52  /* **************************************************************************************************
53  * **/
54  /* ** **/
55  /* ** Constructors / Destructors **/
56  /* ** **/
57  /* **************************************************************************************************
58  * **/
59 
60  // ===========================================================================
61  // Default constructor
62  // ===========================================================================
64  double discountFactor,
65  double epsilon,
67  bool verbose) :
71  }
72 
73  // ===========================================================================
74  // Default destructor
75  // ===========================================================================
78 
82  ++scIter)
83  delete scIter.val();
84  }
85 
86  /* **************************************************************************************************
87  * **/
88  /* ** **/
89  /* ** Planning Methods **/
90  /* ** **/
91  /* **************************************************************************************************
92  * **/
93 
94  // ==========================================================================
95  // Initializes data structure needed for making the planning
96  // ==========================================================================
97  void AdaptiveRMaxPlaner::initialize(const FMDP< double >* fmdp) {
98  if (!initialized__) {
101  for (auto actionIter = fmdp->beginActions();
102  actionIter != fmdp->endActions();
103  ++actionIter) {
106  }
107  initialized__ = true;
108  }
109  }
110 
111  // ===========================================================================
112  // Performs a value iteration
113  // ===========================================================================
116 
118 
119  clearTables__();
120  }
121 
122  /* **************************************************************************************************
123  * **/
124  /* ** **/
125  /* ** Value Iteration Methods **/
126  /* ** **/
127  /* **************************************************************************************************
128  * **/
129 
130  // ===========================================================================
131  // Performs a single step of value iteration
132  // ===========================================================================
136  for (auto actionIter = fmdp_->beginActions();
138  ++actionIter)
140  RECASTED(this->fmdp_->reward(*actionIter)),
141  1);
142  }
143 
144  // ===========================================================================
145  // Performs a single step of value iteration
146  // ===========================================================================
148  // *****************************************************************************************
149  // Loop reset
153 
154  // *****************************************************************************************
155  // For each action
157  for (auto actionIter = fmdp_->beginActions();
159  ++actionIter) {
160  MultiDimFunctionGraph< double >* qAction
162 
163  // *******************************************************************************************
164  // Next, we add the reward
166 
167  qAction = this->operator_->maximize(
170  2);
171 
173  }
174  delete newVFunction;
175 
176  // *****************************************************************************************
177  // Next to evaluate main value function, we take maximise over all action
178  // value, ...
180 
181  return newVFunction;
182  }
183 
184  /* **************************************************************************************************
185  * **/
186  /* ** **/
187  /* ** Optimal Policy Evaluation Methods **/
188  /* ** **/
189  /* **************************************************************************************************
190  * **/
191 
192  // ===========================================================================
193  // Evals the policy corresponding to the given value function
194  // ===========================================================================
196  // *****************************************************************************************
197  // Loop reset
201 
202  std::vector<
205  // *****************************************************************************************
206  // For each action
207  for (auto actionIter = fmdp_->beginActions();
209  ++actionIter) {
210  MultiDimFunctionGraph< double >* qAction
212 
214 
215  qAction = this->operator_->maximize(
218  2);
219 
221  }
222  delete newVFunction;
223 
224  // *****************************************************************************************
225  // Next to evaluate main value function, we take maximise over all action
226  // value, ...
230 
231  // *****************************************************************************************
232  // Next to evaluate main value function, we take maximise over all action
233  // value, ...
235  }
236 
237  // ===========================================================================
238  //
239  // ===========================================================================
242  = fmdpLearner__->modaMax() * 5 > 30 ? fmdpLearner__->modaMax() * 5 : 30;
243  rmax__ = fmdpLearner__->rMax() / (1.0 - this->discountFactor_);
244 
245  for (auto actionIter = this->fmdp()->beginActions();
246  actionIter != this->fmdp()->endActions();
247  ++actionIter) {
248  std::vector< MultiDimFunctionGraph< double >* > rmaxs;
249  std::vector< MultiDimFunctionGraph< double >* > boolQs;
250 
251  for (auto varIter = this->fmdp()->beginVariables();
252  varIter != this->fmdp()->endVariables();
253  ++varIter) {
255 
256  MultiDimFunctionGraph< double >* varRMax
257  = this->operator_->getFunctionInstance();
259  = this->operator_->getFunctionInstance();
260 
263 
264  std::pair< NodeId, NodeId > rooty
267  varRMax->manager()->reduce();
268  varRMax->manager()->clean();
270  varBoolQ->manager()->reduce();
271  varBoolQ->manager()->clean();
272 
275 
276  // std::cout << RECASTED(this->fmdp_->transition(*actionIter,
277  // *varIter))->toDot() << std::endl;
278  // for( auto varIter2 =
279  // RECASTED(this->fmdp_->transition(*actionIter,
280  // *varIter))->variablesSequence().beginSafe(); varIter2 !=
281  // RECASTED(this->fmdp_->transition(*actionIter,
282  // *varIter))->variablesSequence().endSafe(); ++varIter2 )
283  // std::cout << (*varIter2)->name() << " | ";
284  // std::cout << std::endl;
285 
286  // std::cout << varRMax->toDot() << std::endl;
287  // for( auto varIter =
288  // varRMax->variablesSequence().beginSafe(); varIter !=
289  // varRMax->variablesSequence().endSafe(); ++varIter )
290  // std::cout << (*varIter)->name() << " | ";
291  // std::cout << std::endl;
292 
293  // std::cout << varBoolQ->toDot() << std::endl;
294  // for( auto varIter =
295  // varBoolQ->variablesSequence().beginSafe(); varIter !=
296  // varBoolQ->variablesSequence().endSafe(); ++varIter )
297  // std::cout << (*varIter)->name() << " | ";
298  // std::cout << std::endl;
299  }
300 
301  // std::cout << "Maximising" << std::endl;
304  }
305  }
306 
307  // ===========================================================================
308  //
309  // ===========================================================================
310  std::pair< NodeId, NodeId >
313  MultiDimFunctionGraph< double >* rmax,
314  MultiDimFunctionGraph< double >* boolQ) {
315  std::pair< NodeId, NodeId > rep;
321  return rep;
322  }
323 
324  NodeId* rmaxsons = static_cast< NodeId* >(SOA_ALLOCATE(
325  sizeof(NodeId) * visited->nodeVar(currentNodeId)->domainSize()));
326  NodeId* bqsons = static_cast< NodeId* >(SOA_ALLOCATE(
327  sizeof(NodeId) * visited->nodeVar(currentNodeId)->domainSize()));
328 
330  ++moda) {
331  std::pair< NodeId, NodeId > sonp
334  rmax,
335  boolQ);
336  rmaxsons[moda] = sonp.first;
337  bqsons[moda] = sonp.second;
338  }
339 
341  rmaxsons);
343  bqsons);
344  return rep;
345  }
346 
347  // ===========================================================================
348  //
349  // ===========================================================================
351  for (auto actionIter = this->fmdp()->beginActions();
352  actionIter != this->fmdp()->endActions();
353  ++actionIter) {
356  }
359  }
360 
361 } // end of namespace gum
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:669
#define RECASTED(x)
For shorter line and hence more comprehensive code purposes only.