]> ruin.nu Git - popboot.git/blobdiff - planner.cpp
seems to be working now
[popboot.git] / planner.cpp
index 324c2dd73a3c0754d3a4899edbbf739473465d80..10f5e57f2970f1cd7a111d497de7d75c81dc848b 100644 (file)
@@ -5,27 +5,28 @@
 using namespace std;
 using namespace __gnu_cxx;
 
-Planner::Planner(std::vector<Action> actions, literals init, literals goal){
+Planner::Planner(std::vector<Action> actions, Literals init, Literals goal){
        _init = init;
        _goal = goal;
        _start = new StartNode(_init);
-       Node* finish = new EndNode(_goal);
+       _finish = new EndNode(_goal);
+       addNode(_start);
 
        for(vector<Action>::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){
-                       cerr << "Adding effect: " << *effect << endl;
-                       _actions[*effect] = act;
+               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);
+       cerr << "Number of actions: " << _actions.size() << endl;
+       makePlan(_finish);
 }
 
 Planner::~Planner(){
-       for (hash_map<string,Node*>::iterator node = _addedNodes.begin(); node != _addedNodes.end(); ++node){
-               cerr << "Deleting node " << node->second->action().name() << endl;
-               delete node->second;
+       for (vector<Node*>::iterator node = _addedNodes.begin(); node != _addedNodes.end(); ++node){
+               cerr << "Deleting node " << (*node)->action().name() << endl;
+               delete *node;
        }
 }
 
@@ -34,7 +35,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 +43,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<string,Node*>::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<string,Node*>::iterator addedNode = _addedEffects.find(precond->first);
+                       if(addedNode != _addedEffects.end()){
                                cerr << "Using already added node" << endl;
                                addedNode->second->addChild(node);
                        }else {
-                               hash_map<string, Action*>::iterator action = _actions.find(*precond);
+                               hash_map<string, Action>::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,16 +70,22 @@ 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;
+       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(literals());
-       for (hash_map<string,Node*>::iterator node = _addedNodes.begin(); node != _addedNodes.end(); ++node){
+       _start->execute(Literals());
+       /*for (hash_map<string,Node*>::iterator node = _addedNodes.begin(); node != _addedNodes.end(); ++node){
+               if (node->second->executed()){
+       }*/
+       _start->execute(Literals());
 }