aGrUM  0.16.0
fmdp_tpl.h
Go to the documentation of this file.
1 
30 //======================================================================
31 #include <cstdio>
32 #include <iostream>
33 //======================================================================
34 #include <agrum/FMDP/fmdp.h>
35 //======================================================================
36 #define RECAST(x) reinterpret_cast< const MultiDimFunctionGraph< GUM_SCALAR >* >(x)
37 
38 namespace gum {
39 
40 
41  /* **************************************************************************************************
42  * **/
43  /* ** **/
44  /* ** Constructors / Destructors **/
45  /* ** **/
46  /* **************************************************************************************************
47  * **/
48 
49  // ===========================================================================
50  // Default constructor.
51  // ===========================================================================
52  template < typename GUM_SCALAR >
53  INLINE FMDP< GUM_SCALAR >::FMDP(bool onDestructionDeleteVar) {
54  GUM_CONSTRUCTOR(FMDP);
55  __onDestructionDeleteVars = onDestructionDeleteVar;
56 
57  // Default Action initialisation
58  __actionMap.insert(0, new std::string("DEFAULT"));
59  __actionTransitionTable.insert(0, new VarTransitionTable< GUM_SCALAR >());
60  __actionCostTable.insert(0, nullptr);
61  __actionRewardTable.insert(0, nullptr);
62  }
63 
64  // ===========================================================================
65  // Destructor.
66  // ===========================================================================
67  template < typename GUM_SCALAR >
69  // Action Transition Graph deletion
70  for (auto iterA = __actionTransitionTable.beginSafe();
71  iterA != __actionTransitionTable.endSafe();
72  ++iterA) {
73  if (iterA.val()) {
74  for (auto iterH = (iterA.val())->beginSafe();
75  iterH != (iterA.val())->endSafe();
76  ++iterH)
77  if (iterH.val()) delete iterH.val();
78  delete iterA.val();
79  }
80  }
81 
82  // Action cost graph deletion
83  for (auto iterA = __actionCostTable.beginSafe();
84  iterA != __actionCostTable.endSafe();
85  ++iterA)
86  if (iterA.val()) delete iterA.val();
87 
88  // Action reward graph deletion
89  for (auto iterA = __actionRewardTable.beginSafe();
90  iterA != __actionRewardTable.endSafe();
91  ++iterA)
92  if (iterA.val()) delete iterA.val();
93 
94  // Action Name deletion
95  for (auto iterId = __actionMap.beginSafe(); iterId != __actionMap.endSafe();
96  ++iterId)
97  delete iterId.second();
98 
99  // Primed Variables deletion
100  for (auto varIter = __main2primed.beginSafe();
101  varIter != __main2primed.endSafe();
102  ++varIter) {
103  delete varIter.second();
104  if (__onDestructionDeleteVars) delete varIter.first();
105  }
106 
107  GUM_DESTRUCTOR(FMDP);
108  }
109 
110 
111  /* **************************************************************************************************
112  * **/
113  /* ** **/
114  /* ** Variable Handling Methods. **/
115  /* ** **/
116  /* **************************************************************************************************
117  * **/
118 
119  // ===========================================================================
120  // Adds a variable to FMDP description
121  // @throw DuplicateElement if a similar variable already exists
122  // ===========================================================================
123  template < typename GUM_SCALAR >
125  if (__varSeq.exists(var))
127  " Variable " << var->name()
128  << " has already been inserted in FMDP.");
129 
130 
131  __varSeq.insert(var);
132 
133  // Prime version creation
134  DiscreteVariable* primeVar = var->clone();
135  primeVar->setName(var->name() + "'");
136  __main2primed.insert(var, primeVar);
137  }
138 
139 
140  /* **************************************************************************************************
141  * **/
142  /* ** **/
143  /* ** Variable Handling Methods. **/
144  /* ** **/
145  /* **************************************************************************************************
146  * **/
147 
148  // ===========================================================================
149  // Adds an action to FMDP description
150  // @throw DuplicateElement if an action with same name already exists
151  // ===========================================================================
152  template < typename GUM_SCALAR >
153  INLINE void FMDP< GUM_SCALAR >::addAction(Idx actionId,
154  const std::string& action) {
155  if (actionId == 0) GUM_ERROR(DuplicateElement, " Action Id 0 is reserved.");
156 
158  __actionMap.beginSafe();
159  actIter != __actionMap.endSafe();
160  ++actIter)
161  if (*(actIter.second()) == action)
163  " Action "
164  << action
165  << " has already been inserted in FMDP with this name.");
166 
167  if (__actionMap.existsFirst(actionId))
169  " An action with same id (" << actionId
170  << ") has already been inserted.");
171 
172  __actionMap.insert(actionId, new std::string(action));
173 
174  __actionTransitionTable.insert(actionId,
176  __actionCostTable.insert(actionId, nullptr);
177  __actionRewardTable.insert(actionId, nullptr);
178 
179  __actionSeq.insert(actionId);
180  }
181 
182 
183  /* **************************************************************************************************
184  * **/
185  /* ** **/
186  /* ** Transition methods. **/
187  /* ** **/
188  /* **************************************************************************************************
189  * **/
190 
191  // ===========================================================================
192  // Adds a variable transition table to specified action
193  // @throw NotFound if action or var does not exists
194  // @throw DuplicateElement if variable already has a transition for this
195  // action
196  // ===========================================================================
197  template < typename GUM_SCALAR >
199  Idx actionId,
200  const DiscreteVariable* var,
201  const MultiDimImplementation< GUM_SCALAR >* transition) {
202  if (!__varSeq.exists(var))
204  " Variable " << var->name() << " has not been declared before.");
205 
206  if (!__actionTransitionTable.exists(actionId))
208  " Action " << actionName(actionId)
209  << " has not been declared before.");
210 
211  if (__actionTransitionTable[actionId]->exists(var))
213  " Variable " << var->name()
214  << " already has a transition table in " << actionId
215  << " table.");
216 
217  __actionTransitionTable[actionId]->insert(var, transition);
218  }
219 
220 
221  // ===========================================================================
222  // Returns transition associated to given in parameter variable and given
223  // action
224  // ===========================================================================
225  template < typename GUM_SCALAR >
228  const DiscreteVariable* v) const {
229  if (!__actionTransitionTable.exists(actionId))
231  " Action " << actionName(actionId)
232  << " has not been declared before.");
233 
234  if (__actionTransitionTable[actionId]->exists(v))
235  return (*__actionTransitionTable[actionId])[v];
236  else
237  return (*__actionTransitionTable[0]).exists(v)
238  ? (*__actionTransitionTable[0])[v]
239  : nullptr;
240  }
241 
242 
243  /* **************************************************************************************************
244  * **/
245  /* ** **/
246  /* ** Cost methods. **/
247  /* ** **/
248  /* **************************************************************************************************
249  * **/
250 
251  // ===========================================================================
252  // Adds a cost table to specified action
253  // @throw NotFound if action does not exists
254  // @throw DuplicateElement if action already has a cost
255  // ===========================================================================
256  template < typename GUM_SCALAR >
258  Idx actionId, const MultiDimImplementation< GUM_SCALAR >* cost) {
259  if (!__actionCostTable.exists(actionId))
261  " Action " << actionName(actionId)
262  << " has not been declared before.");
263 
264  if (__actionCostTable[actionId] != nullptr)
266  " Action " << actionName(actionId) << " already has a cost table");
267 
268  __actionCostTable[actionId] = cost;
269  }
270 
271 
272  // ===========================================================================
273  // Returns transition associated to given in parameter variable and given
274  // action
275  // ===========================================================================
276  template < typename GUM_SCALAR >
278  FMDP< GUM_SCALAR >::cost(Idx actionId) const {
279  if (!__actionCostTable.exists(actionId))
281  " Action " << actionName(actionId)
282  << " has not been declared before.");
283 
284  if (__actionCostTable[actionId]) return __actionCostTable[actionId];
285  return __actionCostTable[0];
286  }
287 
288 
289  /* **************************************************************************************************
290  * **/
291  /* ** **/
292  /* ** Cost methods. **/
293  /* ** **/
294  /* **************************************************************************************************
295  * **/
296 
297  // ===========================================================================
298  // Adds a default variable reward
299  // @throw DuplicateElement if a default reward exists already
300  // ===========================================================================
301  template < typename GUM_SCALAR >
303  Idx actionId, const MultiDimImplementation< GUM_SCALAR >* reward) {
304  if (!__actionRewardTable.exists(actionId))
306  " Action " << actionName(actionId)
307  << " has not been declared before.");
308 
309  if (__actionRewardTable[actionId] != nullptr)
311  " Action " << actionName(actionId)
312  << " already has a reward table");
313 
314  __actionRewardTable[actionId] = reward;
315  }
316 
317 
318  // ===========================================================================
319  // Returns transition associated to given in parameter variable and given
320  // action
321  // ===========================================================================
322  template < typename GUM_SCALAR >
324  FMDP< GUM_SCALAR >::reward(Idx actionId) const {
325  if (!__actionRewardTable.exists(actionId))
327  " Action " << actionName(actionId)
328  << " has not been declared before.");
329 
330  if (__actionRewardTable[actionId]) return __actionRewardTable[actionId];
331  return __actionRewardTable[0];
332  }
333 
334 
335  /* **************************************************************************************************
336  * **/
337  /* ** **/
338  /* ** Miscelleanous methods. **/
339  /* ** **/
340  /* **************************************************************************************************
341  * **/
342 
343  // ===========================================================================
344  // Returns name of action given in parameter
345  // ===========================================================================
346  template < typename GUM_SCALAR >
347  INLINE const std::string& FMDP< GUM_SCALAR >::actionName(Idx actionId) const {
348  if (!__actionMap.existsFirst(actionId))
349  GUM_ERROR(NotFound, "No action with " << actionId << " as identifiant.");
350 
351  return *(__actionMap.second(actionId));
352  }
353 
354  // ===========================================================================
355  // Returns action id
356  // ===========================================================================
357  template < typename GUM_SCALAR >
358  INLINE Idx FMDP< GUM_SCALAR >::actionId(const std::string& action) const {
360  __actionMap.begin();
361  actIter != __actionMap.end();
362  ++actIter)
363  if (*(actIter.second()) == action) { return actIter.first(); }
364 
365  GUM_ERROR(NotFound, " Action " << action << " has not been declared before.");
366  }
367 
368 
369  template < typename GUM_SCALAR >
370  INLINE std::string FMDP< GUM_SCALAR >::toString() const {
371  std::stringstream fmdpCore;
372 
373  for (auto actionIter = beginActions(); actionIter != endActions();
374  ++actionIter) {
375  for (auto varIter = beginVariables(); varIter != endVariables(); ++varIter)
376  if (this->transition(*actionIter, *varIter))
377  fmdpCore << RECAST(this->transition(*actionIter, *varIter))->toDot()
378  << std::endl;
379  if (this->reward(*actionIter))
380  fmdpCore << RECAST(this->reward(*actionIter))->toDot() << std::endl;
381  }
382 
383  for (auto varIter = beginVariables(); varIter != endVariables(); ++varIter)
384  if (this->transition(0, *varIter))
385  fmdpCore << RECAST(this->transition(0, *varIter))->toDot() << std::endl;
386  if (this->reward()) fmdpCore << RECAST(this->reward())->toDot() << std::endl;
387  return fmdpCore.str();
388  }
389 
390 
391  template < typename GUM_SCALAR >
393  Size s = 0;
394  for (auto actionIter = beginActions(); actionIter != endActions();
395  ++actionIter) {
396  for (auto varIter = beginVariables(); varIter != endVariables(); ++varIter)
397  if (this->transition(*actionIter, *varIter))
398  s += this->transition(*actionIter, *varIter)->realSize();
399  if (this->reward(*actionIter)) s += this->reward(*actionIter)->realSize();
400  }
401 
402  for (auto varIter = beginVariables(); varIter != endVariables(); ++varIter)
403  if (this->transition(0, *varIter))
404  s += this->transition(0, *varIter)->realSize();
405  if (this->reward()) s += this->reward()->realSize();
406  return s;
407  }
408 } // namespace gum
void setName(const std::string &theValue)
sets the name of the variable
~FMDP()
Default destructor.
Definition: fmdp_tpl.h:68
Idx actionId(const std::string &) const
Returns action id.
Definition: fmdp_tpl.h:358
Safe iterators for bijectionIterator.
Definition: bijection.h:1411
const std::string & actionName(Idx actionId) const
Returns name of action given in parameter.
Definition: fmdp_tpl.h:347
virtual DiscreteVariable * clone() const =0
Copy Factory.
void addCostForAction(Idx actionId, const MultiDimImplementation< GUM_SCALAR > *cost)
Adds a cost table to specified action.
Definition: fmdp_tpl.h:257
This class is used to implement factored decision process.
Definition: fmdp.h:57
Base class for discrete random variable.
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Definition: agrum.h:25
The class for generic Hash Tables.
Definition: hashTable.h:679
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
void addVariable(const DiscreteVariable *var)
Adds a variable to FMDP description.
Definition: fmdp_tpl.h:124
void addTransitionForAction(Idx actionId, const DiscreteVariable *var, const MultiDimImplementation< GUM_SCALAR > *transition)
Adds a variable transition table to specified action.
Definition: fmdp_tpl.h:198
const MultiDimImplementation< GUM_SCALAR > * transition(Idx actionId, const DiscreteVariable *v) const
Returns transition associated to given in parameter variable and the given action.
Definition: fmdp_tpl.h:227
FMDP(bool onDestructionDeleteVar=false)
Default constructor.
Definition: fmdp_tpl.h:53
Size size() const
Returns the map binding main variables and prime variables.
Definition: fmdp_tpl.h:392
const MultiDimImplementation< GUM_SCALAR > * reward(Idx actionId=0) const
Returns the reward table of mdp.
Definition: fmdp_tpl.h:324
void addRewardForAction(Idx actionId, const MultiDimImplementation< GUM_SCALAR > *reward)
Adds a default variable reward.
Definition: fmdp_tpl.h:302
Unsafe iterators for bijection.
Definition: bijection.h:1609
#define RECAST(x)
Definition: fmdp_tpl.h:36
const MultiDimImplementation< GUM_SCALAR > * cost(Idx actionId=0) const
Returns the reward table of mdp.
Definition: fmdp_tpl.h:278
<agrum/multidim/multiDimImplementation.h>
Size Idx
Type for indexes.
Definition: types.h:53
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:48
const std::string & name() const
returns the name of the variable
void addAction(Idx actionId, const std::string &action)
Adds an action to FMDP description.
Definition: fmdp_tpl.h:153
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55
std::string toString() const
Displays the FMDP in a Dot format.
Definition: fmdp_tpl.h:370