]> ruin.nu Git - popboot.git/commitdiff
not working
authorMichael Andreen <harv@ruin.nu>
Tue, 17 May 2005 10:43:33 +0000 (10:43 +0000)
committerMichael Andreen <harv@ruin.nu>
Tue, 17 May 2005 10:43:33 +0000 (10:43 +0000)
action.cpp
action.h
main.cpp
node.cpp
node.h
planner.cpp
planner.h

index 626eb37475bb5487756381dafcd823fd2188380f..7b7a58807f5536e436e615a82ac19b2a2373831d 100644 (file)
@@ -1,32 +1,49 @@
 #include "action.h"
 #include <iostream>
 using namespace std;
+using namespace __gnu_cxx;
 
-Action::Action(std::string executable, literals preconditions, literals effects){
-       _executable = executable;
+const literals Action::_empty;
+
+Action::Action(std::string name, preconditionsVector preconditions, effectsMap effects){
+       _name = name;
        _preconditions = preconditions;
        _effects = effects;
+       _currentPrecondition = _preconditions.begin();
 }
 
 Action::Action(const Action& action){
-       _executable = action._executable;
+       _name = action._name;
        _preconditions = action._preconditions;
        _effects = action._effects;
 }
 
-const literals& Action::effects() const{
-       return _effects;
+const literals& Action::effects(int value) const{
+       effectsMap::const_iterator effects = _effects.find(value);
+       if (effects != _effects.end())
+               return effects->second;
+       return _empty;
 }
 
 const literals& Action::preconditions() const{
-       return _preconditions;
+       return _currentPrecondition->second;
 }
 
 int Action::execute() const{
-       cout << "Executing: " << _executable << endl;
+       cout << "Executing: " << _currentPrecondition->first << endl;
        return 0;
 }
 
-const string& Action::executable() const{
-       return _executable;
+const string& Action::name() const{
+       return _name;
+}
+
+bool Action::nextExecutable(){
+       if (++_currentPrecondition != _preconditions.end())
+               return true;
+       return false;
+}
+
+void Action::reset(){
+       _currentPrecondition = _preconditions.begin();
 }
index 9b3c9276a105ab8366324c63839ffde92d34d70f..dd14e28798d0ab83b9370901085e983b1d570195 100644 (file)
--- a/action.h
+++ b/action.h
@@ -3,23 +3,30 @@
 
 #include <vector>
 #include <string>
+#include <ext/hash_map>
 
 typedef std::vector<std::string> literals;
+typedef std::vector<std::pair<std::string,literals> > preconditionsVector;
+typedef __gnu_cxx::hash_map<int,literals> effectsMap;
 
 class Action {
        public:
-               Action(std::string executable, literals preconditions, literals effects);
+               Action(std::string name, preconditionsVector preconditions, effectsMap effects);
                Action(const Action& action);
                Action(){};
-               const literals& effects() const;
+               const literals& effects(int value) const;
                const literals& preconditions() const;
-               const std::string& executable() const;
+               const std::string& name() const;
+               bool nextExecutable();
                int execute() const;
+               void reset();
 
        protected:
-               std::string _executable;
-               literals _preconditions;
-               literals _effects;
+               preconditionsVector::const_iterator _currentPrecondition;
+               std::string _name;
+               preconditionsVector _preconditions;
+               effectsMap _effects;
+               static const literals _empty;
 };
 
 #endif
index 4463ff26d4d0e49f2c249c17594ca7340a53337f..4eb71ea59c4bd5f54fecf514cb4443b03232de45 100644 (file)
--- a/main.cpp
+++ b/main.cpp
@@ -40,7 +40,7 @@ int main(int argc, char** argv){
                getline(file,precond);
                cout << exec << ":" << effects << ":" << precond << endl;
                if (effects == "") continue;
-               actions.push_back(Action(exec, stringToVector(precond), stringToVector(effects)));
+               //actions.push_back(Action(exec, stringToVector(precond), stringToVector(effects)));
        }
        Planner p(actions, stringToVector(argv[2]), stringToVector(argv[3]));
 
index 1fb2b2e3ee9448eeb50a7f0657e23f1082f72895..acc5bd7090144cf31ffd4b536e2a5a68f1a72f18 100644 (file)
--- a/node.cpp
+++ b/node.cpp
@@ -2,9 +2,19 @@
 #include <algorithm>
 using namespace std;
 
-Node::Node(Action action){
+Node::Node(const Action& action){
        _action = action;
        _preconditions = _action.preconditions();
+       _executed = false;
+}
+Node::Node(){
+       _executed = false;
+}
+
+Node::Node(const Node& node){
+       _action = node._action;
+       _preconditions = node._preconditions;
+       _executed = node._executed;
 }
 
 const Action& Node::action() const{
@@ -16,16 +26,42 @@ void Node::addChild(Node* node){
        _children.push_back(node);
 }
 
+bool Node::executed() const{
+       return _executed;
+}
+
+const literals& Node::effects() const{
+       return _effects;
+}
 
 void Node::execute(const literals& effects){
        for (literals::const_iterator effect = effects.begin(); effect != effects.end(); ++effect){
                _preconditions.erase(find(_preconditions.begin(),_preconditions.end(), *effect));
        }
-       if (_preconditions.size() != 0)
+       if (_preconditions.size() != 0 || _executed)
                return;
-       _action.execute();
-       
+
+       _executed = true;
+       int value = _action.execute();
+       _effects = _action.effects(value);
+
        for(vector<Node*>::iterator child = _children.begin(); child != _children.end(); ++child){
-               (*child)->execute(_action.effects());
+               (*child)->execute(effects);
        }
 }
+
+StartNode::StartNode(const literals& init){
+       effectsMap initial;
+       initial[0] = init;
+       Node(Action("start",preconditionsVector(), initial));
+}
+
+EndNode::EndNode(const literals& goal){
+       preconditionsVector goalState;
+       pair<string,literals> goalPair;
+       goalPair.first = "";
+       goalPair.second = goal;
+       goalState.push_back(goalPair);
+       Node(Action("finish",goalState,effectsMap()));
+}
+
diff --git a/node.h b/node.h
index 7bcf3a8c371338375df7d42604ce5d0ddf98ce74..cfa02223854a9d66dfad039fc2fcd0deb57e2a76 100644 (file)
--- a/node.h
+++ b/node.h
@@ -2,20 +2,38 @@
 #define __node_h__
 
 #include <vector>
+#include <utility>
 #include "action.h"
 
 class Node {
 
        public:
-               Node(Action action);
+               Node(const Action& action);
+               Node();
+               Node(const Node& node);
+               virtual ~Node(){}
                void addChild(Node* node);
                const Action& action() const;
                void execute(const literals& effects);
+               bool executed() const;
+               const literals& effects() const;
 
        protected:
                Action _action;
                std::vector<Node*> _children;
                literals _preconditions;
+               bool _executed;
+               literals _effects;
+};
+
+class StartNode : Node {
+       public:
+               StartNode(const literals& init);
+};
+
+class EndNode : Node {
+       public:
+               EndNode(const literals& goal);
 };
 
 #endif
index 1210d0479309df2ed3af1a2fe0b8f783fa5d1a33..324c2dd73a3c0754d3a4899edbbf739473465d80 100644 (file)
@@ -1,19 +1,22 @@
 #include "planner.h"
 #include "node.h"
 #include <iostream>
+#include <vector>
 using namespace std;
 using namespace __gnu_cxx;
 
 Planner::Planner(std::vector<Action> actions, literals init, literals goal){
-
-       _start = new Node(Action("start",literals(), init));
-       Node* finish = new Node(Action("finish",goal,literals()));
+       _init = init;
+       _goal = goal;
+       _start = new StartNode(_init);
+       Node* finish = new EndNode(_goal);
 
        for(vector<Action>::iterator action = actions.begin(); action != actions.end(); ++action){
-               const literals& effects = action->effects();
+               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] = *action;
+                       _actions[*effect] = act;
                }
        }
        makePlan(finish);
@@ -21,18 +24,19 @@ Planner::Planner(std::vector<Action> actions, literals init, literals goal){
 
 Planner::~Planner(){
        for (hash_map<string,Node*>::iterator node = _addedNodes.begin(); node != _addedNodes.end(); ++node){
-               cerr << "Deleting node " << node->second->action().executable() << endl;
+               cerr << "Deleting node " << node->second->action().name() << endl;
                delete node->second;
        }
 }
 
 
 void Planner::makePlan(Node* node){
-       cerr << "Fetching preconditions for action: " << node->action().executable() << ".. ";
+       addNode(node);
+
+       cerr << "Fetching preconditions for action: " << node->action().name() << ".. ";
        const literals& preconds = node->action().preconditions();
        cerr << "done" << endl;
 
-       addNode(node);
 
        if (preconds.size() == 0){
                cerr << "Found no preconds" << endl;
@@ -45,10 +49,10 @@ 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);
+                               hash_map<string, Action*>::iterator action = _actions.find(*precond);
                                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{
@@ -60,7 +64,7 @@ void Planner::makePlan(Node* node){
 }
 
 void Planner::addNode(Node* node){
-       const literals& effects = node->action().effects();
+       const literals& effects = node->action().effects(0);
 
        for (literals::const_iterator effect = effects.begin(); effect != effects.end(); ++effect){
                cout << "Adding node for effect: " << *effect << endl;
@@ -71,4 +75,5 @@ void Planner::addNode(Node* node){
 
 void Planner::execute(){
        _start->execute(literals());
+       for (hash_map<string,Node*>::iterator node = _addedNodes.begin(); node != _addedNodes.end(); ++node){
 }
index ab8f4ee27774f4df9e13283a67b1c744619a18f5..bb6836a269d23bf0ff8a85c9451dca6d40143abe 100644 (file)
--- a/planner.h
+++ b/planner.h
@@ -50,6 +50,8 @@ class Planner {
                
                Node* _start;
                __gnu_cxx::hash_map<std::string,Node*> _addedNodes;
-               __gnu_cxx::hash_map<std::string,Action> _actions;
+               __gnu_cxx::hash_map<std::string,Action*> _actions;
+               literals _init;
+               literals _goal;
 };
 #endif