33 #define RECAST(x) reinterpret_cast< const MultiDimFunctionGraph< GUM_SCALAR >* >(x) 49 template <
typename GUM_SCALAR >
51 GUM_CONSTRUCTOR(
FMDP);
52 __onDestructionDeleteVars = onDestructionDeleteVar;
55 __actionMap.insert(0,
new std::string(
"DEFAULT"));
57 __actionCostTable.insert(0,
nullptr);
58 __actionRewardTable.insert(0,
nullptr);
64 template <
typename GUM_SCALAR >
67 for (
auto iterA = __actionTransitionTable.beginSafe();
68 iterA != __actionTransitionTable.endSafe();
71 for (
auto iterH = (iterA.val())->beginSafe();
72 iterH != (iterA.val())->endSafe();
74 if (iterH.val())
delete iterH.val();
80 for (
auto iterA = __actionCostTable.beginSafe();
81 iterA != __actionCostTable.endSafe();
83 if (iterA.val())
delete iterA.val();
86 for (
auto iterA = __actionRewardTable.beginSafe();
87 iterA != __actionRewardTable.endSafe();
89 if (iterA.val())
delete iterA.val();
92 for (
auto iterId = __actionMap.beginSafe(); iterId != __actionMap.endSafe();
94 delete iterId.second();
97 for (
auto varIter = __main2primed.beginSafe();
98 varIter != __main2primed.endSafe();
100 delete varIter.second();
101 if (__onDestructionDeleteVars)
delete varIter.first();
104 GUM_DESTRUCTOR(
FMDP);
120 template <
typename GUM_SCALAR >
122 if (__varSeq.exists(var))
124 " Variable " << var->
name()
125 <<
" has already been inserted in FMDP.");
128 __varSeq.insert(var);
133 __main2primed.insert(var, primeVar);
149 template <
typename GUM_SCALAR >
151 const std::string& action) {
155 __actionMap.beginSafe();
156 actIter != __actionMap.endSafe();
158 if (*(actIter.second()) == action)
162 <<
" has already been inserted in FMDP with this name.");
164 if (__actionMap.existsFirst(actionId))
166 " An action with same id (" << actionId
167 <<
") has already been inserted.");
169 __actionMap.insert(actionId,
new std::string(action));
171 __actionTransitionTable.insert(actionId,
173 __actionCostTable.insert(actionId,
nullptr);
174 __actionRewardTable.insert(actionId,
nullptr);
176 __actionSeq.insert(actionId);
194 template <
typename GUM_SCALAR >
199 if (!__varSeq.exists(var))
201 " Variable " << var->
name() <<
" has not been declared before.");
203 if (!__actionTransitionTable.exists(actionId))
205 " Action " << actionName(actionId)
206 <<
" has not been declared before.");
208 if (__actionTransitionTable[actionId]->exists(var))
210 " Variable " << var->
name()
211 <<
" already has a transition table in " << actionId
214 __actionTransitionTable[actionId]->insert(var, transition);
222 template <
typename GUM_SCALAR >
226 if (!__actionTransitionTable.exists(actionId))
228 " Action " << actionName(actionId)
229 <<
" has not been declared before.");
231 if (__actionTransitionTable[actionId]->exists(v))
232 return (*__actionTransitionTable[actionId])[v];
234 return (*__actionTransitionTable[0]).exists(v)
235 ? (*__actionTransitionTable[0])[v]
253 template <
typename GUM_SCALAR >
256 if (!__actionCostTable.exists(actionId))
258 " Action " << actionName(actionId)
259 <<
" has not been declared before.");
261 if (__actionCostTable[actionId] !=
nullptr)
263 " Action " << actionName(actionId) <<
" already has a cost table");
265 __actionCostTable[actionId] = cost;
273 template <
typename GUM_SCALAR >
276 if (!__actionCostTable.exists(actionId))
278 " Action " << actionName(actionId)
279 <<
" has not been declared before.");
281 if (__actionCostTable[actionId])
return __actionCostTable[actionId];
282 return __actionCostTable[0];
298 template <
typename GUM_SCALAR >
301 if (!__actionRewardTable.exists(actionId))
303 " Action " << actionName(actionId)
304 <<
" has not been declared before.");
306 if (__actionRewardTable[actionId] !=
nullptr)
308 " Action " << actionName(actionId)
309 <<
" already has a reward table");
311 __actionRewardTable[actionId] = reward;
319 template <
typename GUM_SCALAR >
322 if (!__actionRewardTable.exists(actionId))
324 " Action " << actionName(actionId)
325 <<
" has not been declared before.");
327 if (__actionRewardTable[actionId])
return __actionRewardTable[actionId];
328 return __actionRewardTable[0];
343 template <
typename GUM_SCALAR >
345 if (!__actionMap.existsFirst(actionId))
348 return *(__actionMap.second(actionId));
354 template <
typename GUM_SCALAR >
358 actIter != __actionMap.end();
360 if (*(actIter.second()) == action) {
return actIter.first(); }
366 template <
typename GUM_SCALAR >
368 std::stringstream fmdpCore;
370 for (
auto actionIter = beginActions(); actionIter != endActions();
372 for (
auto varIter = beginVariables(); varIter != endVariables(); ++varIter)
373 if (this->transition(*actionIter, *varIter))
374 fmdpCore <<
RECAST(this->transition(*actionIter, *varIter))->toDot()
376 if (this->reward(*actionIter))
377 fmdpCore <<
RECAST(this->reward(*actionIter))->toDot() << std::endl;
380 for (
auto varIter = beginVariables(); varIter != endVariables(); ++varIter)
381 if (this->transition(0, *varIter))
382 fmdpCore <<
RECAST(this->transition(0, *varIter))->toDot() << std::endl;
383 if (this->reward()) fmdpCore <<
RECAST(this->reward())->toDot() << std::endl;
384 return fmdpCore.str();
388 template <
typename GUM_SCALAR >
391 for (
auto actionIter = beginActions(); actionIter != endActions();
393 for (
auto varIter = beginVariables(); varIter != endVariables(); ++varIter)
394 if (this->transition(*actionIter, *varIter))
395 s += this->transition(*actionIter, *varIter)->realSize();
396 if (this->reward(*actionIter)) s += this->reward(*actionIter)->realSize();
399 for (
auto varIter = beginVariables(); varIter != endVariables(); ++varIter)
400 if (this->transition(0, *varIter))
401 s += this->transition(0, *varIter)->realSize();
402 if (this->reward()) s += this->reward()->realSize();
void setName(const std::string &theValue)
sets the name of the variable
~FMDP()
Default destructor.
Idx actionId(const std::string &) const
Returns action id.
Safe iterators for bijectionIterator.
const std::string & actionName(Idx actionId) const
Returns name of action given in parameter.
virtual DiscreteVariable * clone() const =0
Copy Factory.
void addCostForAction(Idx actionId, const MultiDimImplementation< GUM_SCALAR > *cost)
Adds a cost table to specified action.
This class is used to implement factored decision process.
Base class for discrete random variable.
gum is the global namespace for all aGrUM entities
The class for generic Hash Tables.
Class for implementation of factored markov decision process.
void addVariable(const DiscreteVariable *var)
Adds a variable to FMDP description.
void addTransitionForAction(Idx actionId, const DiscreteVariable *var, const MultiDimImplementation< GUM_SCALAR > *transition)
Adds a variable transition table to specified action.
const MultiDimImplementation< GUM_SCALAR > * transition(Idx actionId, const DiscreteVariable *v) const
Returns transition associated to given in parameter variable and the given action.
FMDP(bool onDestructionDeleteVar=false)
Default constructor.
Size size() const
Returns the map binding main variables and prime variables.
const MultiDimImplementation< GUM_SCALAR > * reward(Idx actionId=0) const
Returns the reward table of mdp.
void addRewardForAction(Idx actionId, const MultiDimImplementation< GUM_SCALAR > *reward)
Adds a default variable reward.
Unsafe iterators for bijection.
const MultiDimImplementation< GUM_SCALAR > * cost(Idx actionId=0) const
Returns the reward table of mdp.
<agrum/multidim/multiDimImplementation.h>
Size Idx
Type for indexes.
std::size_t Size
In aGrUM, hashed values are unsigned long int.
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.
#define GUM_ERROR(type, msg)
std::string toString() const
Displays the FMDP in a Dot format.