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 >
324 template <
typename GUM_ELEMENT >
333 std::vector< MultiDimFunctionGraph< GUM_ELEMENT >* > qActionsSet;
334 for (
auto actionIter =
fmdp_->beginActions(); actionIter !=
fmdp_->endActions(); ++actionIter) {
336 qActionsSet.push_back(qAction);
347 newVFunction = this->
addReward_(newVFunction);
355 template <
typename GUM_ELEMENT >
370 template <
typename GUM_ELEMENT >
374 qActionsSet.pop_back();
376 while (!qActionsSet.empty()) {
378 qActionsSet.pop_back();
379 newVFunction =
operator_->maximize(newVFunction, qAction);
388 template <
typename GUM_ELEMENT >
392 qActionsSet.pop_back();
394 while (!qActionsSet.empty()) {
396 qActionsSet.pop_back();
397 newVFunction =
operator_->minimize(newVFunction, qAction);
406 template <
typename GUM_ELEMENT >
434 template <
typename GUM_ELEMENT >
445 for (
auto actionIter =
fmdp_->beginActions(); actionIter !=
fmdp_->endActions(); ++actionIter) {
450 argMaxQActionsSet.push_back(
makeArgMax_(qAction, *actionIter));
472 template <
typename GUM_ELEMENT >
478 =
operator_->getArgMaxFunctionInstance();
485 amcpy->
add(**varIter);
498 template <
typename GUM_ELEMENT >
505 if (visitedNodes.
exists(currentNodeId))
return visitedNodes[currentNodeId];
510 nody = argMaxCpy->manager()->addTerminalNode(leaf);
515 for (
Idx moda = 0; moda < currentNode->
nodeVar()->domainSize(); ++moda)
518 nody = argMaxCpy->manager()->addInternalNode(currentNode->
nodeVar(), sonsMap);
520 visitedNodes.
insert(currentNodeId, nody);
527 template <
typename GUM_ELEMENT >
533 = qActionsSet.back();
534 qActionsSet.pop_back();
536 while (!qActionsSet.empty()) {
538 = qActionsSet.back();
539 qActionsSet.pop_back();
540 newVFunction =
operator_->argmaximize(newVFunction, qAction);
551 template <
typename GUM_ELEMENT >
554 argMaxOptimalValueFunction) {
559 = argMaxOptimalValueFunction->variablesSequence().beginSafe();
560 varIter != argMaxOptimalValueFunction->variablesSequence().endSafe();
566 argMaxOptimalValueFunction,
569 delete argMaxOptimalValueFunction;
575 template <
typename GUM_ELEMENT >
581 if (visitedNodes.
exists(currentNodeId))
return visitedNodes[currentNodeId];
584 if (argMaxOptVFunc->isTerminalNode(currentNodeId)) {
589 const InternalNode* currentNode = argMaxOptVFunc->node(currentNodeId);
592 for (
Idx moda = 0; moda < currentNode->
nodeVar()->domainSize(); ++moda)
596 visitedNodes.
insert(currentNodeId, nody);
603 template <
typename GUM_ELEMENT >
611 template <
typename GUM_ELEMENT >
622 template <
typename GUM_ELEMENT >
633 template <
typename GUM_ELEMENT >
638 template <
typename GUM_ELEMENT >
643 template <
typename GUM_ELEMENT >
648 template <
typename GUM_ELEMENT >
654 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.
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.
Link of a chain list allocated using the SmallObjectAllocator.
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 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
Headers of the StructuredPlaner planer class.
Header of the Tensor class.