X-Git-Url: https://ruin.nu/git/?a=blobdiff_plain;f=planner.cpp;h=d29691f95c1d4d620318b2a665f5cebea110d952;hb=36283553b51149fc6d7a704ba4f746310aafa49b;hp=463049473fe16ce62ac9207bce287f270867da0b;hpb=498b277372ebab0008b97399d387285b36d88826;p=popboot.git diff --git a/planner.cpp b/planner.cpp index 4630494..d29691f 100644 --- a/planner.cpp +++ b/planner.cpp @@ -1,49 +1,73 @@ #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("start",literals(), init)); +Planner::Planner(std::vector actions, Literals init, Literals goal){ + _init = init; + _goal = goal; + _start = new StartNode(_init); + _finish = new EndNode(_goal); addNode(_start); - Node* finish = new Node(Action("finish",goal,literals())); for(vector::iterator action = actions.begin(); action != actions.end(); ++action){ - literals effects = action->effects(); - for (literals::iterator effect = effects.begin(); effect != effects.end(); ++effect){ - cerr << "Adding effect: " << *effect << endl; - _actions[*effect] = *action; + Action* act = new Action(*action); + _actions.push_back(act); + const Literals& effects = act->effects(0); + for (Literals::const_iterator effect = effects.begin(); effect != effects.end(); ++effect){ + cerr << "Adding effect: '" << *effect << "', action: " << action->name() << endl; + _actionEffects[*effect] = act; } } - makePlan(finish); + cerr << "Number of actions: " << _actions.size() << endl; + makePlan(_finish); +} + +Planner::~Planner(){ + cerr << "Deleting " << _addedNodes.size() << " nodes" << endl; + for (vector::iterator node = _addedNodes.begin(); node != _addedNodes.end(); ++node){ + cerr << "Deleting node " << (*node)->action()->name() << endl; + delete *node; + } } 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){ - cerr << "Looking for: " << *precond << endl; - hash_map::iterator addedNode = _addedNodes.find(*precond); - if(addedNode != _addedNodes.end()){ + for (Preconditions::const_iterator precond = preconds.begin(); precond != preconds.end(); ++precond){ + cerr << "Looking for: '" << precond->first << "'" << endl; + hash_map::iterator addedNode = _addedEffects.find(precond->first); + if(addedNode != _addedEffects.end()){ cerr << "Using already added node" << endl; addedNode->second->addChild(node); }else { - hash_map::iterator action = _actions.find(*precond); - if (action != _actions.end()){ + hash_map::iterator action = _actionEffects.find(precond->first); + if (action != _actionEffects.end()){ cerr << "Adding new node" << endl; Node* newnode = new Node(action->second); newnode->addChild(node); - addNode(newnode); 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!" << endl; + cerr << "Action with effect: " << precond->first << " not found!" << endl; + cerr << "This is a soft precondition, so we will continue" << endl; + node->satisfyCondition(precond->first); + _start->addChild(node); } } } @@ -51,15 +75,44 @@ 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; + _addedNodes.push_back(node); - for (literals::iterator effect = effects.begin(); effect != effects.end(); ++effect){ - cout << "Adding node for effect: " << *effect << endl; - _addedNodes[*effect] = node; + for (Literals::const_iterator effect = effects.begin(); effect != effects.end(); ++effect){ + cerr << "Adding node for effect: " << *effect << endl; + _addedEffects[*effect] = node; } } void Planner::execute(){ - _start->execute(); + _start->execute(Literals()); + cerr << "Number of nodes: " << _addedNodes.size() << endl; + back_insert_iterator ii(_init); + copy(_init.begin(), _init.end(), ostream_iterator(cout, " ")); + cout << endl; + _init.clear(); + for (vector::iterator node = _addedNodes.begin(); node != _addedNodes.end(); ++node){ + cerr << "Deleting node " << (*node)->action()->name() << endl; + if ((*node)->executed()){ + const Literals& effects = (*node)->effects(); + copy(effects.begin(),effects.end(),ii); + cerr << "Finding action" << endl; + vector::iterator action = find(_actions.begin(), _actions.end(), (*node)->action()); + if (action != _actions.end()){ + cerr << "Removing executed action: " << (*action)->name() << endl; + _actions.erase(action); + //BUG: Sometimes finds the wrong action. + //delete *action; + } + } + delete *node; + } + _addedNodes.clear(); + _actionEffects.clear(); + copy(_init.begin(), _init.end(), ostream_iterator(cout, " ")); + cerr << "Number of nodes left: " << _addedNodes.size() << endl; + //TODO: Fill _actionEffects with the remaining effects, create start end end nodes and create a new plan. }