X-Git-Url: https://ruin.nu/git/?p=popboot.git;a=blobdiff_plain;f=planner.cpp;h=15fc248372361b188b63f3e67dce49f628dc3fa5;hp=324c2dd73a3c0754d3a4899edbbf739473465d80;hb=716fc4e282ce52fe953867171c046acd3b614d3b;hpb=3dadaa088d9fff7ca05cbb297f3d7e88179faccb diff --git a/planner.cpp b/planner.cpp index 324c2dd..15fc248 100644 --- a/planner.cpp +++ b/planner.cpp @@ -5,21 +5,20 @@ using namespace std; using namespace __gnu_cxx; -Planner::Planner(std::vector actions, literals init, literals goal){ +Planner::Planner(std::vector actions, Literals init, Literals goal){ _init = init; _goal = goal; _start = new StartNode(_init); - Node* finish = new EndNode(_goal); + _finish = new EndNode(_goal); for(vector::iterator action = actions.begin(); action != actions.end(); ++action){ - Action* act = new Action(*action); - const literals& effects = act->effects(0); - for (literals::const_iterator effect = effects.begin(); effect != effects.end(); ++effect){ + const Literals& effects = action->effects(0); + for (Literals::const_iterator effect = effects.begin(); effect != effects.end(); ++effect){ cerr << "Adding effect: " << *effect << endl; - _actions[*effect] = act; + _actions[*effect] = *action; } } - makePlan(finish); + makePlan(_finish); } Planner::~Planner(){ @@ -34,7 +33,7 @@ void Planner::makePlan(Node* node){ addNode(node); cerr << "Fetching preconditions for action: " << node->action().name() << ".. "; - const literals& preconds = node->action().preconditions(); + const Preconditions& preconds = node->action().preconditions(); cerr << "done" << endl; @@ -42,21 +41,26 @@ void Planner::makePlan(Node* node){ cerr << "Found no preconds" << endl; _start->addChild(node); }else{ - for (literals::const_iterator precond = preconds.begin(); precond != preconds.end(); ++precond){ - cerr << "Looking for: " << *precond << endl; - 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); + 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!" << endl; + cerr << "Action with effect: " << precond->first << " not found!" << endl; + cerr << "This is a soft precondition, so we will continue" << endl; } } } @@ -64,9 +68,9 @@ void Planner::makePlan(Node* node){ } void Planner::addNode(Node* node){ - const literals& effects = node->action().effects(0); + const Literals& effects = node->action().effects(0); - for (literals::const_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; } @@ -74,6 +78,9 @@ void Planner::addNode(Node* node){ void Planner::execute(){ - _start->execute(literals()); + _start->execute(true,Literals()); for (hash_map::iterator node = _addedNodes.begin(); node != _addedNodes.end(); ++node){ + if (!node->second->executed()) + node->second->execute(false,Literals()); + } }