70#define RECAST(x) reinterpret_cast< const MultiDimFunctionGraph< GUM_ELEMENT >* >(x)
86 template <
typename GUM_ELEMENT >
88 GUM_ELEMENT discountFactor,
102 template <
typename GUM_ELEMENT >
124 template <
typename GUM_ELEMENT >
135 std::string terminalStream;
136 std::string nonTerminalStream;
137 std::string arcstream;
140 output +=
"\ndigraph \" OPTIMAL POLICY \" {\n";
143 terminalStream +=
"node [shape = box];\n";
144 nonTerminalStream +=
"node [shape = ellipse];\n";
147 std::string tab =
"\t";
153 std::queue< NodeId > fifo;
162 while (!fifo.empty()) {
164 NodeId currentNodeId = fifo.front();
173 terminalStream += std::format(
"{0}{1};{0}{1} [label=\"{1} - ", tab, currentNodeId);
178 terminalStream +=
fmdp_->actionName(*valIter);
179 terminalStream +=
' ';
183 terminalStream +=
"\"];\n";
190 const InternalNode* currentNode = optimalPolicy_->node(currentNodeId);
193 nonTerminalStream += std::format(
"{0}{1};{0}{1} [label=\"{1} - {2}\"];\n",
200 for (
Idx sonIter = 0; sonIter < currentNode->nbSons(); ++sonIter) {
201 if (!visited.
exists(currentNode->son(sonIter))) {
202 fifo.push(currentNode->son(sonIter));
203 visited << currentNode->son(sonIter);
205 if (!sonMap.
exists(currentNode->son(sonIter)))
207 sonMap[currentNode->son(sonIter)]->addLink(sonIter);
211 for (
auto sonIter = sonMap.
beginSafe(); sonIter != sonMap.
endSafe(); ++sonIter) {
212 arcstream += std::format(
"{}{} -> {} [label=\" ", tab, currentNodeId, sonIter.key());
215 arcstream += currentNode->nodeVar()->label(modaIter->
element());
216 if (modaIter->
nextLink()) arcstream +=
", ";
219 arcstream +=
"\",color=\"#00ff00\"];\n";
220 delete sonIter.val();
226 output += terminalStream +
'\n' + nonTerminalStream +
'\n' + arcstream +
"\n}\n";
242 template <
typename GUM_ELEMENT >
250 for (
auto varIter =
fmdp_->beginVariables(); varIter !=
fmdp_->endVariables(); ++varIter)
262 template <
typename GUM_ELEMENT >
285 if (gap < fabs(deltaV->
value())) gap = fabs(deltaV->
value());
289 std::cout <<
" ------------------- Fin itération n° " << nbIte << std::endl
307 template <
typename GUM_ELEMENT >
323 template <
typename GUM_ELEMENT >
332 std::vector< MultiDimFunctionGraph< GUM_ELEMENT >* > qActionsSet;
333 for (
auto actionIter =
fmdp_->beginActions(); actionIter !=
fmdp_->endActions(); ++actionIter) {
335 qActionsSet.push_back(qAction);
346 newVFunction = this->
addReward_(newVFunction);
354 template <
typename GUM_ELEMENT >
369 template <
typename GUM_ELEMENT >
373 qActionsSet.pop_back();
375 while (!qActionsSet.empty()) {
377 qActionsSet.pop_back();
378 newVFunction =
operator_->maximize(newVFunction, qAction);
387 template <
typename GUM_ELEMENT >
391 qActionsSet.pop_back();
393 while (!qActionsSet.empty()) {
395 qActionsSet.pop_back();
396 newVFunction =
operator_->minimize(newVFunction, qAction);
405 template <
typename GUM_ELEMENT >
433 template <
typename GUM_ELEMENT >
444 for (
auto actionIter =
fmdp_->beginActions(); actionIter !=
fmdp_->endActions(); ++actionIter) {
449 argMaxQActionsSet.push_back(
makeArgMax_(qAction, *actionIter));
471 template <
typename GUM_ELEMENT >
477 =
operator_->getArgMaxFunctionInstance();
484 amcpy->
add(**varIter);
497 template <
typename GUM_ELEMENT >
504 if (visitedNodes.
exists(currentNodeId))
return visitedNodes[currentNodeId];
509 nody = argMaxCpy->manager()->addTerminalNode(leaf);
514 for (
Idx moda = 0; moda < currentNode->
nodeVar()->domainSize(); ++moda)
517 nody = argMaxCpy->manager()->addInternalNode(currentNode->
nodeVar(), sonsMap);
519 visitedNodes.
insert(currentNodeId, nody);
526 template <
typename GUM_ELEMENT >
532 = qActionsSet.back();
533 qActionsSet.pop_back();
535 while (!qActionsSet.empty()) {
537 = qActionsSet.back();
538 qActionsSet.pop_back();
539 newVFunction =
operator_->argmaximize(newVFunction, qAction);
550 template <
typename GUM_ELEMENT >
553 argMaxOptimalValueFunction) {
558 = argMaxOptimalValueFunction->variablesSequence().beginSafe();
559 varIter != argMaxOptimalValueFunction->variablesSequence().endSafe();
565 argMaxOptimalValueFunction,
568 delete argMaxOptimalValueFunction;
574 template <
typename GUM_ELEMENT >
580 if (visitedNodes.
exists(currentNodeId))
return visitedNodes[currentNodeId];
583 if (argMaxOptVFunc->isTerminalNode(currentNodeId)) {
588 const InternalNode* currentNode = argMaxOptVFunc->node(currentNodeId);
591 for (
Idx moda = 0; moda < currentNode->
nodeVar()->domainSize(); ++moda)
595 visitedNodes.
insert(currentNodeId, nody);
602 template <
typename GUM_ELEMENT >
610 template <
typename GUM_ELEMENT >
621 template <
typename GUM_ELEMENT >
632 template <
typename GUM_ELEMENT >
637 template <
typename GUM_ELEMENT >
642 template <
typename GUM_ELEMENT >
647 template <
typename GUM_ELEMENT >
653 template <
typename GUM_ELEMENT >
A class to store the optimal actions.
SequenceIteratorSafe< Idx > beginSafe() const
Iterator beginning.
SequenceIteratorSafe< Idx > endSafe() const
Iterator end.
Class to handle efficiently argMaxSet.
SequenceIteratorSafe< GUM_SCALAR_SEQ > beginSafe() const
Iterator beginning.
SequenceIteratorSafe< GUM_SCALAR_SEQ > endSafe() const
Iterator end.
virtual Size domainSize() const =0
bool hasValue() const override
Indicates if constant safe iterator has reach end of terminal nodes list.
void nextValue() const override
Increments the constant safe iterator.
void beginValues() const override
Initializes the constant safe iterator on terminal nodes.
const GUM_ELEMENT & value() const override
Returns the value of the current terminal nodes pointed by the constant safe iterator.
This class is used to implement factored decision process.
const DiscreteVariable * main2prime(const DiscreteVariable *mainVar) const
Returns the primed variable associate to the given main variable.
The class for generic Hash Tables.
iterator_safe beginSafe()
Returns the safe iterator pointing to the beginning of the hashtable.
value_type & insert(const Key &key, const Val &val)
Adds a new element (actually a copy of this element) into the hash table.
const iterator_safe & endSafe() noexcept
Returns the safe iterator pointing to the end of the hashtable.
bool exists(const Key &key) const
Checks whether there exists an element with a given key in the hashtable.
<agrum/FMDP/SDyna/IOperatorStrategy.h>
Structure used to represent a node internal structure.
const DiscreteVariable * nodeVar() const
Returns the node variable.
NodeId son(Idx modality) const
Returns the son at a given index.
const T & element() const
Returns the element stored in this link.
const Link< T > * nextLink() const
Returns next link.
Chain list allocated using the SmallObjectAllocator.
<agrum/FMDP/planning/mddOperatorStrategy.h>
Class implementingting a function graph.
void clear()
Clears the function graph.
void add(const DiscreteVariable &v) override
Adds a new var to the variables of the multidimensional matrix.
const NodeId & root() const
Returns the id of the root node from the diagram.
void copyAndMultiplyByScalar(const MultiDimFunctionGraph< GUM_ELEMENT, TerminalNodePolicy > &src, GUM_ELEMENT gamma)
Copies src diagrams and multiply every value by the given scalar.
bool isTerminalNode(const NodeId &node) const
Indicates if given node is terminal or not.
void copyAndReassign(const MultiDimFunctionGraph< GUM_ELEMENT, TerminalNodePolicy > &src, const Bijection< const DiscreteVariable *, const DiscreteVariable * > &reassign)
Copies src diagrams structure into this diagrams.
const GUM_ELEMENT & nodeValue(NodeId n) const
Returns value associated to given node.
const InternalNode * node(NodeId n) const
Returns internalNode structure associated to that nodeId.
MultiDimFunctionGraphManager< GUM_ELEMENT, TerminalNodePolicy > * manager()
Returns a const reference to the manager of this diagram.
const Sequence< const DiscreteVariable * > & variablesSequence() const override
Returns a const ref to the sequence of DiscreteVariable*.
Implementation of a Terminal Node Policy that maps nodeid to a set of value.
bool exists(const Key &k) const
Indicates whether a given elements belong to the set.
<agrum/FMDP/planning/structuredPlaner.h>
virtual MultiDimFunctionGraph< ArgMaxSet< GUM_ELEMENT, Idx >, SetTerminalNodePolicy > * argmaximiseQactions_(std::vector< MultiDimFunctionGraph< ArgMaxSet< GUM_ELEMENT, Idx >, SetTerminalNodePolicy > * > &)
Performs argmax_a Q(s,a).
Size optimalPolicySize() override
Returns optimalPolicy computed so far current size.
const FMDP< GUM_ELEMENT > * fmdp()
Returns a const ptr on the Factored Markov Decision Process on which we're planning.
NodeId _recurExtractOptPol_(NodeId, const MultiDimFunctionGraph< ArgMaxSet< GUM_ELEMENT, Idx >, SetTerminalNodePolicy > *, HashTable< NodeId, NodeId > &)
Recursion part for the createArgMaxCopy.
gum::VariableSet elVarSeq_
A Set to eleminate primed variables.
void initialize(const FMDP< GUM_ELEMENT > *fmdp) override
Initializes data structure needed for making the planning.
GUM_ELEMENT discountFactor_
Discount Factor used for infinite horizon planning.
void _transferActionIds_(const ArgMaxSet< GUM_ELEMENT, Idx > &, ActionSet &)
Extract from an ArgMaxSet the associated ActionSet.
virtual void evalPolicy_()
IOperatorStrategy< GUM_ELEMENT > * operator_
void extractOptimalPolicy_(const MultiDimFunctionGraph< ArgMaxSet< GUM_ELEMENT, Idx >, SetTerminalNodePolicy > *optimalValueFunction)
From V(s)* = argmax_a Q*(s,a), this function extract pi*(s) This function mainly consists in extracti...
void makePlanning(Idx nbStep=1000000) override
Performs a value iteration.
virtual MultiDimFunctionGraph< GUM_ELEMENT > * addReward_(MultiDimFunctionGraph< GUM_ELEMENT > *function, Idx actionId=0)
Perform the R(s) + gamma . function.
MultiDimFunctionGraph< ArgMaxSet< GUM_ELEMENT, Idx >, SetTerminalNodePolicy > * makeArgMax_(const MultiDimFunctionGraph< GUM_ELEMENT > *Qaction, Idx actionId)
Creates a copy of given Qaction that can be exploit by a Argmax.
std::string optimalPolicy2String() override
Provide a better toDot for the optimal policy where the leaves have the action name instead of its id...
const MultiDimFunctionGraph< GUM_ELEMENT > * vFunction()
Returns a const ptr on the value function computed so far.
MultiDimFunctionGraph< ActionSet, SetTerminalNodePolicy > * optimalPolicy() override
Returns the best policy obtained so far.
bool verbose_
Boolean used to indcates whether or not iteration informations should be displayed on terminal.
const FMDP< double > * fmdp_
static StructuredPlaner< GUM_ELEMENT > * spumddInstance(GUM_ELEMENT discountFactor=0.9, GUM_ELEMENT epsilon=0.00001, bool verbose=true)
static StructuredPlaner< GUM_ELEMENT > * sviInstance(GUM_ELEMENT discountFactor=0.9, GUM_ELEMENT epsilon=0.00001, bool verbose=true)
virtual MultiDimFunctionGraph< GUM_ELEMENT > * minimiseFunctions_(std::vector< MultiDimFunctionGraph< GUM_ELEMENT > * > &)
Performs min_i F_i.
StructuredPlaner(IOperatorStrategy< GUM_ELEMENT > *opi, GUM_ELEMENT discountFactor, GUM_ELEMENT epsilon, bool verbose)
Default constructor.
Size vFunctionSize() override
Returns vFunction computed so far current size.
MultiDimFunctionGraph< ActionSet, SetTerminalNodePolicy > * optimalPolicy_
MultiDimFunctionGraph< double > * vFunction_
virtual MultiDimFunctionGraph< GUM_ELEMENT > * evalQaction_(const MultiDimFunctionGraph< GUM_ELEMENT > *, Idx)
Performs the P(s'|s,a).V^{t-1}(s') part of the value itération.
virtual void initVFunction_()
Performs a single step of value iteration.
virtual MultiDimFunctionGraph< GUM_ELEMENT > * maximiseQactions_(std::vector< MultiDimFunctionGraph< GUM_ELEMENT > * > &)
Performs max_a Q(s,a).
virtual MultiDimFunctionGraph< double > * valueIteration_()
NodeId _recurArgMaxCopy_(NodeId, Idx, const MultiDimFunctionGraph< GUM_ELEMENT > *, MultiDimFunctionGraph< ArgMaxSet< GUM_ELEMENT, Idx >, SetTerminalNodePolicy > *, HashTable< NodeId, NodeId > &)
Recursion part for the createArgMaxCopy.
~StructuredPlaner() override
Default destructor.
<agrum/FMDP/planning/treeOperatorStrategy.h>
const std::string & name() const
returns the name of the variable
This files contains several function objects that are not (yet) defined in the STL.
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Size Idx
Type for indexes.
Size NodeId
Type for node ids.
Header files of gum::Instantiation.
#define RECAST(x)
For shorter line and hence more comprehensive code only.
Headers of MultiDimFunctionGraph.
gum is the global namespace for all aGrUM entities
template class GUM_SHARED_PUBLIC Link< Idx >
Headers of the StructuredPlaner planer class.
Header of the Tensor class.