]> ruin.nu Git - popboot.git/commitdiff
replanning started
authorMichael Andreen <harv@ruin.nu>
Thu, 19 May 2005 13:36:34 +0000 (13:36 +0000)
committerMichael Andreen <harv@ruin.nu>
Thu, 19 May 2005 13:36:34 +0000 (13:36 +0000)
node.cpp
node.h
planner.cpp
planner.h

index fb1c314dcf682a059c3eff9d7235885d214bf2a7..b4d22fb9569c28cea70176abce6ce585c2b8b8c1 100644 (file)
--- a/node.cpp
+++ b/node.cpp
@@ -3,9 +3,9 @@
 #include <iostream>
 using namespace std;
 
-Node::Node(const Action& action){
+Node::Node(const Action* action){
        _action = action;
-       _preconditions = _action.preconditions();
+       _preconditions = _action->preconditions();
        _executed = false;
 }
 Node::Node(){
@@ -18,7 +18,7 @@ Node::Node(const Node& node){
        _executed = node._executed;
 }
 
-const Action& Node::action() const{
+const Action* Node::action() const{
        return _action;
 }
 
@@ -39,7 +39,7 @@ void Node::satisfyCondition(std::string effect){
 }
 
 void Node::execute(const Literals& effects){
-       cerr << "Executing: " << _action.name() << endl;
+       cerr << "Executing: " << _action->name() << endl;
        for (Literals::const_iterator effect = effects.begin(); effect != effects.end(); ++effect){
                cerr << "Satisfied effect: " << *effect << endl;
                _preconditions.erase(_preconditions.find(*effect));
@@ -51,8 +51,8 @@ void Node::execute(const Literals& effects){
                return;
 
        _executed = true;
-       int value = _action.execute();
-       _effects = _action.effects(value);
+       int value = _action->execute();
+       _effects = _action->effects(value);
 
        cerr << "Got returnvalue: " << value << ", number of effects: " << _effects.size() << endl;
 
@@ -64,14 +64,14 @@ void Node::execute(const Literals& effects){
 StartNode::StartNode(const Literals& init){
        EffectsMap initial;
        initial[0] = init;
-       _action = Action("start",Preconditions(),"", initial);
+       _action = new Action("start",Preconditions(),"", initial);
 }
 
 EndNode::EndNode(const Literals& goal){
        Preconditions goalState;
        for(Literals::const_iterator g = goal.begin(); g != goal.end(); ++g)
                goalState[*g] = true;
-       _action = Action("finish",goalState,"",EffectsMap());
-       _preconditions = _action.preconditions();
+       _action = new Action("finish",goalState,"",EffectsMap());
+       _preconditions = _action->preconditions();
 }
 
diff --git a/node.h b/node.h
index 77a9d7bbefce9d7265d870e2f80c9b5fb400dfc7..272bee93bc565977f03a9499f8d02f8255ed7831 100644 (file)
--- a/node.h
+++ b/node.h
@@ -8,19 +8,19 @@
 class Node {
 
        public:
-               Node(const Action& action);
+               Node(const Action* action);
                Node();
                Node(const Node& node);
                virtual ~Node(){}
                void addChild(Node* node);
-               const Action& action() const;
+               const Action* action() const;
                void execute(const Literals& effects);
                bool executed() const;
                const Literals& effects() const;
                void satisfyCondition(std::string effect);
 
        protected:
-               Action _action;
+               const Action* _action;
                std::vector<Node*> _children;
                Preconditions _preconditions;
                bool _executed;
@@ -30,11 +30,13 @@ class Node {
 class StartNode :public Node {
        public:
                StartNode(const Literals& init);
+               ~StartNode(){delete _action;}
 };
 
 class EndNode :public Node {
        public:
                EndNode(const Literals& goal);
+               ~EndNode(){delete _action;}
 };
 
 #endif
index 46cf230d1421c25d4aa28574a79a53987fc8cbba..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;
@@ -26,7 +28,7 @@ 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;
        }
 }
@@ -35,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;
 
 
@@ -51,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);
@@ -65,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);
                                }
                        }
                }
@@ -72,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);
 
@@ -87,12 +90,29 @@ void Planner::addNode(Node* node){
 void Planner::execute(){
        _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()){
-                       //remove stuff
+                       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.
 }
index 79db5420a06cd52e751947013f8eb594af8d7791..f00c61473382884c6c42bf427614278c627c851f 100644 (file)
--- a/planner.h
+++ b/planner.h
@@ -22,9 +22,10 @@ class Planner {
                Node* _start;
                Node* _finish;
                __gnu_cxx::hash_map<std::string,Node*> _addedEffects;
-               __gnu_cxx::hash_map<std::string,Action> _actions;
+               __gnu_cxx::hash_map<std::string,Action*> _actionEffects;
                Literals _init;
                Literals _goal;
                std::vector<Node*> _addedNodes;
+               std::vector<Action*> _actions;
 };
 #endif