From 716fc4e282ce52fe953867171c046acd3b614d3b Mon Sep 17 00:00:00 2001 From: Michael Andreen Date: Tue, 17 May 2005 12:35:24 +0000 Subject: [PATCH] some done, need parser --- action.cpp | 26 ++++++++------------------ action.h | 49 +++++++++++++++++++++++++++++++++++++------------ node.cpp | 38 ++++++++++++++++++++++---------------- node.h | 16 ++++++++-------- planner.cpp | 41 ++++++++++++++++++++++++----------------- planner.h | 38 +++++--------------------------------- 6 files changed, 104 insertions(+), 104 deletions(-) diff --git a/action.cpp b/action.cpp index 7b7a588..87947ca 100644 --- a/action.cpp +++ b/action.cpp @@ -3,13 +3,13 @@ using namespace std; using namespace __gnu_cxx; -const literals Action::_empty; +const Literals Action::_empty; -Action::Action(std::string name, preconditionsVector preconditions, effectsMap effects){ +Action::Action(std::string name,const Preconditions& preconditions, std::string executable,const EffectsMap& effects){ _name = name; + _executable = executable; _preconditions = preconditions; _effects = effects; - _currentPrecondition = _preconditions.begin(); } Action::Action(const Action& action){ @@ -18,32 +18,22 @@ Action::Action(const Action& action){ _effects = action._effects; } -const literals& Action::effects(int value) const{ - effectsMap::const_iterator effects = _effects.find(value); +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 _currentPrecondition->second; +const Preconditions& Action::preconditions() const{ + return _preconditions; } int Action::execute() const{ - cout << "Executing: " << _currentPrecondition->first << endl; + cout << "Executing: " << _executable << endl; return 0; } 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 dd14e28..606857d 100644 --- a/action.h +++ b/action.h @@ -5,28 +5,53 @@ #include #include -typedef std::vector literals; -typedef std::vector > preconditionsVector; -typedef __gnu_cxx::hash_map effectsMap; +typedef std::vector Literals; +typedef __gnu_cxx::hash_map Preconditions; +typedef __gnu_cxx::hash_map EffectsMap; +namespace __gnu_cxx { + + template< typename CharT, typename Traits, typename Alloc > + struct hash< std::basic_string > { + size_t operator()(const std::basic_string& s) const { + + const std::collate& c = std::use_facet< std::collate >(std::locale()); + + return c.hash(s.c_str(), s.c_str() + s.size()); + + } + + }; + + template< typename CharT, typename Traits, typename Alloc > + struct hash< const std::basic_string > { //yes you need this version aswell! + + size_t operator()(const std::basic_string& s) const { + + const std::collate& c = std::use_facet< std::collate >(std::locale()); + + return c.hash(s.c_str(), s.c_str() + s.size()); + } + + }; +}; class Action { public: - Action(std::string name, preconditionsVector preconditions, effectsMap effects); + Action(std::string name, const Preconditions& preconditions, std::string executable, const EffectsMap& effects); Action(const Action& action); Action(){}; - const literals& effects(int value) const; - const literals& preconditions() const; + const Literals& effects(int value) const; + const Preconditions& preconditions() const; const std::string& name() const; - bool nextExecutable(); int execute() const; - void reset(); protected: - preconditionsVector::const_iterator _currentPrecondition; std::string _name; - preconditionsVector _preconditions; - effectsMap _effects; - static const literals _empty; + std::string _executable; + Preconditions _preconditions; + EffectsMap _effects; + static const Literals _empty; }; + #endif diff --git a/node.cpp b/node.cpp index acc5bd7..9bedf38 100644 --- a/node.cpp +++ b/node.cpp @@ -30,38 +30,44 @@ bool Node::executed() const{ return _executed; } -const literals& Node::effects() const{ +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)); +void Node::execute(bool strict, const Literals& effects){ + for (Literals::const_iterator effect = effects.begin(); effect != effects.end(); ++effect){ + _preconditions.erase(_preconditions.find(*effect)); } - if (_preconditions.size() != 0 || _executed) + if ((_preconditions.size() != 0 && strict) || _executed) return; + if (_preconditions.size() != 0){ + for (Preconditions::iterator precond = _preconditions.begin(); precond != _preconditions.end(); ++precond){ + if (precond->second) + return; + } + } + _executed = true; int value = _action.execute(); _effects = _action.effects(value); for(vector::iterator child = _children.begin(); child != _children.end(); ++child){ - (*child)->execute(effects); + (*child)->execute(strict,effects); } } -StartNode::StartNode(const literals& init){ - effectsMap initial; +StartNode::StartNode(const Literals& init){ + EffectsMap initial; initial[0] = init; - Node(Action("start",preconditionsVector(), initial)); + _action = Action("start",Preconditions(),"", initial); } -EndNode::EndNode(const literals& goal){ - preconditionsVector goalState; - pair goalPair; - goalPair.first = ""; - goalPair.second = goal; - goalState.push_back(goalPair); - Node(Action("finish",goalState,effectsMap())); +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(); } diff --git a/node.h b/node.h index cfa0222..28a8287 100644 --- a/node.h +++ b/node.h @@ -14,26 +14,26 @@ class Node { virtual ~Node(){} void addChild(Node* node); const Action& action() const; - void execute(const literals& effects); + void execute(bool strict, const Literals& effects); bool executed() const; - const literals& effects() const; + const Literals& effects() const; protected: Action _action; std::vector _children; - literals _preconditions; + Preconditions _preconditions; bool _executed; - literals _effects; + Literals _effects; }; -class StartNode : Node { +class StartNode :public Node { public: - StartNode(const literals& init); + StartNode(const Literals& init); }; -class EndNode : Node { +class EndNode :public Node { public: - EndNode(const literals& goal); + EndNode(const Literals& goal); }; #endif diff --git a/planner.cpp b/planner.cpp index 324c2dd..15fc248 100644 --- a/planner.cpp +++ b/planner.cpp @@ -5,21 +5,20 @@ using namespace std; using namespace __gnu_cxx; -Planner::Planner(std::vector actions, literals init, literals goal){ +Planner::Planner(std::vector actions, Literals init, Literals goal){ _init = init; _goal = goal; _start = new StartNode(_init); - Node* finish = new EndNode(_goal); + _finish = new EndNode(_goal); for(vector::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){ + const Literals& effects = action->effects(0); + for (Literals::const_iterator effect = effects.begin(); effect != effects.end(); ++effect){ cerr << "Adding effect: " << *effect << endl; - _actions[*effect] = act; + _actions[*effect] = *action; } } - makePlan(finish); + makePlan(_finish); } Planner::~Planner(){ @@ -34,7 +33,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 +41,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::iterator addedNode = _addedNodes.find(*precond); + for (Preconditions::const_iterator precond = preconds.begin(); precond != preconds.end(); ++precond){ + cerr << "Looking for: " << precond->first << endl; + hash_map::iterator addedNode = _addedNodes.find(precond->first); if(addedNode != _addedNodes.end()){ 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->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,9 +68,9 @@ void Planner::makePlan(Node* node){ } void Planner::addNode(Node* node){ - const literals& effects = node->action().effects(0); + const Literals& effects = node->action().effects(0); - for (literals::const_iterator effect = effects.begin(); effect != effects.end(); ++effect){ + for (Literals::const_iterator effect = effects.begin(); effect != effects.end(); ++effect){ cout << "Adding node for effect: " << *effect << endl; _addedNodes[*effect] = node; } @@ -74,6 +78,9 @@ void Planner::addNode(Node* node){ void Planner::execute(){ - _start->execute(literals()); + _start->execute(true,Literals()); for (hash_map::iterator node = _addedNodes.begin(); node != _addedNodes.end(); ++node){ + if (!node->second->executed()) + node->second->execute(false,Literals()); + } } diff --git a/planner.h b/planner.h index bb6836a..3faa6fb 100644 --- a/planner.h +++ b/planner.h @@ -7,38 +7,9 @@ class Node; - - -namespace __gnu_cxx { - - template< typename CharT, typename Traits, typename Alloc > - struct hash< std::basic_string > { - size_t operator()(const std::basic_string& s) const { - - const std::collate& c = std::use_facet< std::collate >(std::locale()); - - return c.hash(s.c_str(), s.c_str() + s.size()); - -} - - }; - - template< typename CharT, typename Traits, typename Alloc > - struct hash< const std::basic_string > { //yes you need this version aswell! - -size_t operator()(const std::basic_string& s) const { - - const std::collate& c = std::use_facet< std::collate >(std::locale()); - - return c.hash(s.c_str(), s.c_str() + s.size()); - } - - }; -}; - class Planner { public: - Planner(std::vector actions, literals init, literals goal); + Planner(std::vector actions, Literals init, Literals goal); ~Planner(); void execute(); @@ -49,9 +20,10 @@ class Planner { void addNode(Node* node); Node* _start; + Node* _finish; __gnu_cxx::hash_map _addedNodes; - __gnu_cxx::hash_map _actions; - literals _init; - literals _goal; + __gnu_cxx::hash_map _actions; + Literals _init; + Literals _goal; }; #endif -- 2.39.2