From: Michael Andreen Date: Tue, 17 May 2005 10:43:33 +0000 (+0000) Subject: not working X-Git-Url: https://ruin.nu/git/?p=popboot.git;a=commitdiff_plain;h=3dadaa088d9fff7ca05cbb297f3d7e88179faccb not working --- diff --git a/action.cpp b/action.cpp index 626eb37..7b7a588 100644 --- a/action.cpp +++ b/action.cpp @@ -1,32 +1,49 @@ #include "action.h" #include 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(); } diff --git a/action.h b/action.h index 9b3c927..dd14e28 100644 --- a/action.h +++ b/action.h @@ -3,23 +3,30 @@ #include #include +#include typedef std::vector literals; +typedef std::vector > preconditionsVector; +typedef __gnu_cxx::hash_map 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 diff --git a/main.cpp b/main.cpp index 4463ff2..4eb71ea 100644 --- 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])); diff --git a/node.cpp b/node.cpp index 1fb2b2e..acc5bd7 100644 --- a/node.cpp +++ b/node.cpp @@ -2,9 +2,19 @@ #include 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::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 goalPair; + goalPair.first = ""; + goalPair.second = goal; + goalState.push_back(goalPair); + Node(Action("finish",goalState,effectsMap())); +} + diff --git a/node.h b/node.h index 7bcf3a8..cfa0222 100644 --- a/node.h +++ b/node.h @@ -2,20 +2,38 @@ #define __node_h__ #include +#include #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 _children; literals _preconditions; + bool _executed; + literals _effects; +}; + +class StartNode : Node { + public: + StartNode(const literals& init); +}; + +class EndNode : Node { + public: + EndNode(const literals& goal); }; #endif diff --git a/planner.cpp b/planner.cpp index 1210d04..324c2dd 100644 --- a/planner.cpp +++ b/planner.cpp @@ -1,19 +1,22 @@ #include "planner.h" #include "node.h" #include +#include using namespace std; using namespace __gnu_cxx; Planner::Planner(std::vector 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::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 actions, literals init, literals goal){ Planner::~Planner(){ for (hash_map::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::iterator action = _actions.find(*precond); + hash_map::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::iterator node = _addedNodes.begin(); node != _addedNodes.end(); ++node){ } diff --git a/planner.h b/planner.h index ab8f4ee..bb6836a 100644 --- a/planner.h +++ b/planner.h @@ -50,6 +50,8 @@ class Planner { Node* _start; __gnu_cxx::hash_map _addedNodes; - __gnu_cxx::hash_map _actions; + __gnu_cxx::hash_map _actions; + literals _init; + literals _goal; }; #endif