]> ruin.nu Git - popboot.git/blobdiff - planner.cpp
Changed random seed to use usec instead of seconds
[popboot.git] / planner.cpp
index 747f3517760b42634ee5dcb5caa9d428dc93cc83..d29691f95c1d4d620318b2a665f5cebea110d952 100644 (file)
@@ -13,10 +13,12 @@ Planner::Planner(std::vector<Action> actions, Literals init, Literals goal){
        addNode(_start);
 
        for(vector<Action>::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 << "', action: " << action->name() << endl;
-                       _actions[*effect] = *action;
+                       _actionEffects[*effect] = act;
                }
        }
        cerr << "Number of actions: " << _actions.size() << endl;
@@ -24,8 +26,9 @@ Planner::Planner(std::vector<Action> actions, Literals init, Literals goal){
 }
 
 Planner::~Planner(){
+       cerr << "Deleting " << _addedNodes.size() << " nodes" << endl;
        for (vector<Node*>::iterator node = _addedNodes.begin(); node != _addedNodes.end(); ++node){
-               cerr << "Deleting node " << (*node)->action().name() << endl;
+               cerr << "Deleting node " << (*node)->action()->name() << endl;
                delete *node;
        }
 }
@@ -34,8 +37,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;
 
 
@@ -50,8 +53,8 @@ void Planner::makePlan(Node* node){
                                cerr << "Using already added node" << endl;
                                addedNode->second->addChild(node);
                        }else {
-                               hash_map<string, Action>::iterator action = _actions.find(precond->first);
-                               if (action != _actions.end()){
+                               hash_map<string, Action*>::iterator action = _actionEffects.find(precond->first);
+                               if (action != _actionEffects.end()){
                                        cerr << "Adding new node" << endl;
                                        Node* newnode = new Node(action->second);
                                        newnode->addChild(node);
@@ -64,6 +67,7 @@ void Planner::makePlan(Node* node){
                                        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);
                                }
                        }
                }
@@ -71,8 +75,8 @@ void Planner::makePlan(Node* node){
 }
 
 void Planner::addNode(Node* node){
-       cerr << "Adding node for action: " << node->action().name() << endl;
-       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);
 
@@ -85,8 +89,30 @@ void Planner::addNode(Node* node){
 
 void Planner::execute(){
        _start->execute(Literals());
-       /*for (hash_map<string,Node*>::iterator node = _addedNodes.begin(); node != _addedNodes.end(); ++node){
-               if (node->second->executed()){
-       }*/
-       _start->execute(Literals());
+       cerr << "Number of nodes: " << _addedNodes.size() << endl;
+       back_insert_iterator<Literals> ii(_init);
+       copy(_init.begin(), _init.end(), ostream_iterator<string>(cout, " "));
+       cout << endl;
+       _init.clear();
+       for (vector<Node*>::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<Action*>::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<string>(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.
 }