X-Git-Url: https://ruin.nu/git/?p=popboot.git;a=blobdiff_plain;f=planner.cpp;h=d49fb911beb267aa1b7005a8bdd9a729b84f2e22;hp=3e8d5de60af18b35416301943000b20d1768094c;hb=c9c72023d15decebe700ad0bfe3e8cbac9a85248;hpb=b177aeec472c3941b39b874a83883ff87a41919c diff --git a/planner.cpp b/planner.cpp index 3e8d5de..d49fb91 100644 --- a/planner.cpp +++ b/planner.cpp @@ -1,42 +1,68 @@ #include "planner.h" #include "node.h" #include +#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)); +Planner::Planner(std::vector actions, Literals init, Literals goal){ + _init = init; + _goal = goal; + _start = new StartNode(_init); + _finish = new EndNode(_goal); + addNode(_start); for(vector::iterator action = actions.begin(); action != actions.end(); ++action){ - literals preconds = action->preconditions(); - for (literals::iterator effect = preconds.begin(); effect != preconds.end(); ++effect){ + const Literals& effects = action->effects(0); + for (Literals::const_iterator effect = effects.begin(); effect != effects.end(); ++effect){ + cerr << "Adding effect: '" << *effect << "', action: " << action->name() << endl; _actions[*effect] = *action; } } - makePlan(finish); + cout << "Number of actions: " << _actions.size() << endl; + makePlan(_finish); +} + +Planner::~Planner(){ + for (hash_map::iterator node = _addedNodes.begin(); node != _addedNodes.end(); ++node){ + cerr << "Deleting node " << node->second->action().name() << endl; + delete node->second; + } } void Planner::makePlan(Node* node){ - literals preconds = node->action().preconditions(); + addNode(node); + + cerr << "Fetching preconditions for action: " << node->action().name() << ".. "; + const Preconditions& preconds = node->action().preconditions(); + cerr << "done" << endl; + if (preconds.size() == 0){ + cerr << "Found no preconds" << endl; _start->addChild(node); }else{ - for (literals::iterator precond = preconds.begin(); precond != preconds.end(); ++precond){ - hash_map::iterator addedNode = _addedNodes.find(*precond); + for (Preconditions::const_iterator precond = preconds.begin(); precond != preconds.end(); ++precond){ + cerr << "Looking for: '" << precond->first << "'" << endl; + hash_map::iterator addedNode = _addedNodes.find(precond->first); if(addedNode != _addedNodes.end()){ + cerr << "Using already added node" << endl; addedNode->second->addChild(node); }else { - hash_map::iterator action = _actions.find(*precond); + hash_map::iterator action = _actions.find(precond->first); if (action != _actions.end()){ + cerr << "Adding new node" << endl; Node* newnode = new Node(action->second); newnode->addChild(node); makePlan(newnode); + }else if (precond->second){ + cerr << "Action with effect: " << precond->first << " not found!" << endl; + cerr << "This is a hard precondition, so this action and the children can't be executed." << endl; + return; }else{ - cerr << "Action with effect: " << *precond << " not found!"; + cerr << "Action with effect: " << precond->first << " not found!" << endl; + cerr << "This is a soft precondition, so we will continue" << endl; } } } @@ -44,9 +70,21 @@ void Planner::makePlan(Node* node){ } void Planner::addNode(Node* node){ - literals effects = node->action().effects(); + cerr << "Adding node for action: " << node->action().name() << endl; + const Literals& effects = node->action().effects(0); + cerr << "Number of effects: " << effects.size() << endl; - for (literals::iterator effect = effects.begin(); effect != effects.end(); ++effect){ + for (Literals::const_iterator effect = effects.begin(); effect != effects.end(); ++effect){ + cout << "Adding node for effect: " << *effect << endl; _addedNodes[*effect] = node; } } + + +void Planner::execute(){ + _start->execute(Literals()); + /*for (hash_map::iterator node = _addedNodes.begin(); node != _addedNodes.end(); ++node){ + if (node->second->executed()){ + }*/ + _start->execute(Literals()); +}