X-Git-Url: https://ruin.nu/git/?p=popboot.git;a=blobdiff_plain;f=planner.cpp;h=514277668c0e378a9a32a0473ba1fc6105012c11;hp=15fc248372361b188b63f3e67dce49f628dc3fa5;hb=d385ba13a2ded8ce22b83c82d8a50c89a5d1d63e;hpb=716fc4e282ce52fe953867171c046acd3b614d3b diff --git a/planner.cpp b/planner.cpp index 15fc248..5142776 100644 --- a/planner.cpp +++ b/planner.cpp @@ -10,21 +10,30 @@ Planner::Planner(std::vector actions, Literals init, Literals goal){ _goal = goal; _start = new StartNode(_init); _finish = new EndNode(_goal); + addNode(_start); for(vector::iterator action = actions.begin(); action != actions.end(); ++action){ - const Literals& effects = action->effects(0); + 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 << endl; - _actions[*effect] = *action; + cerr << "Adding effect: '" << *effect << "', action: " << action->name() << endl; + _actionEffects[*effect] = act; } } + cerr << "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; + 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; + } + for (vector::iterator action = _actions.begin(); action != _actions.end(); ++action){ + cerr << "Deleting action " << (*action)->name() << endl; + delete *action; } } @@ -32,8 +41,8 @@ Planner::~Planner(){ void Planner::makePlan(Node* node){ addNode(node); - cerr << "Fetching preconditions for action: " << node->action().name() << ".. "; - const Preconditions& preconds = node->action().preconditions(); + cerr << "Fetching preconditions for action: " << node->action()->name() << ".. "; + const Preconditions& preconds = node->action()->preconditions(); cerr << "done" << endl; @@ -42,14 +51,14 @@ void Planner::makePlan(Node* node){ _start->addChild(node); }else{ 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 << "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->first); - 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); @@ -61,6 +70,8 @@ void Planner::makePlan(Node* node){ }else{ 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); } } } @@ -68,19 +79,70 @@ void Planner::makePlan(Node* node){ } void Planner::addNode(Node* node){ - const Literals& effects = node->action().effects(0); + 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::const_iterator effect = effects.begin(); effect != effects.end(); ++effect){ - cout << "Adding node for effect: " << *effect << endl; - _addedNodes[*effect] = node; + cerr << "Adding node for effect: " << *effect << endl; + _addedEffects[*effect] = node; } } void Planner::execute(){ - _start->execute(true,Literals()); - for (hash_map::iterator node = _addedNodes.begin(); node != _addedNodes.end(); ++node){ - if (!node->second->executed()) - node->second->execute(false,Literals()); + _start->execute(Literals()); + cerr << "Number of nodes: " << _addedNodes.size() << endl; + back_insert_iterator ii(_init); + _init.clear(); + int executions = 0; + for (vector::iterator node = _addedNodes.begin(); node != _addedNodes.end(); ++node){ + cerr << "Deleting node " << (*node)->action()->name() << endl; + if ((*node)->executed()){ + executions++; + 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; + delete *action; + _actions.erase(action); + } + } + delete *node; + } + _addedNodes.clear(); + _addedEffects.clear(); + _actionEffects.clear(); + copy(_init.begin(), _init.end(), ostream_iterator(cerr, " ")); + cerr << endl; + cerr << "Number of actions left: " << _actions.size() << endl; + //TODO: Fill _actionEffects with the remaining effects, create start end end nodes and create a new plan. + if (executions <= 1){ + cerr << "Non of the remaining actions could be executed, quiting." << endl; + return; + } + if (_actions.size() == 0){ + cerr << "No remaining actions, quiting." << endl; + return; + } + for (vector::iterator action = _actions.begin(); action != _actions.end(); ++action){ + 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; + _actionEffects[*effect] = *action; + } + } + _start = new StartNode(_init); + _finish = new EndNode(_goal); + addNode(_start); + makePlan(_finish); + if (_addedNodes.size() <= 2){ + cerr << "No actions to execute, quiting." << endl; + return; } + execute(); + }