X-Git-Url: https://ruin.nu/git/?a=blobdiff_plain;f=planner.cpp;h=3e8d5de60af18b35416301943000b20d1768094c;hb=b177aeec472c3941b39b874a83883ff87a41919c;hp=159cb528d68f15e5e0d5667facef30455042867d;hpb=c0744e66fecd39c1606727858e7fa05990e84402;p=popboot.git diff --git a/planner.cpp b/planner.cpp index 159cb52..3e8d5de 100644 --- a/planner.cpp +++ b/planner.cpp @@ -1,3 +1,52 @@ #include "planner.h" +#include "node.h" +#include +using namespace std; +using namespace __gnu_cxx; +Planner::Planner(std::vector actions, literals init, literals goal){ + _start = new Node(Action("",init, literals())); + Node* finish = new Node(Action("",literals(),goal)); + + for(vector::iterator action = actions.begin(); action != actions.end(); ++action){ + literals preconds = action->preconditions(); + for (literals::iterator effect = preconds.begin(); effect != preconds.end(); ++effect){ + _actions[*effect] = *action; + } + } + makePlan(finish); +} + + +void Planner::makePlan(Node* node){ + literals preconds = node->action().preconditions(); + + if (preconds.size() == 0){ + _start->addChild(node); + }else{ + for (literals::iterator precond = preconds.begin(); precond != preconds.end(); ++precond){ + hash_map::iterator addedNode = _addedNodes.find(*precond); + if(addedNode != _addedNodes.end()){ + addedNode->second->addChild(node); + }else { + hash_map::iterator action = _actions.find(*precond); + if (action != _actions.end()){ + Node* newnode = new Node(action->second); + newnode->addChild(node); + makePlan(newnode); + }else{ + cerr << "Action with effect: " << *precond << " not found!"; + } + } + } + } +} + +void Planner::addNode(Node* node){ + literals effects = node->action().effects(); + + for (literals::iterator effect = effects.begin(); effect != effects.end(); ++effect){ + _addedNodes[*effect] = node; + } +}