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){
_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();
-}
#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;
+typedef std::vector<std::string> Literals;
+typedef __gnu_cxx::hash_map<std::string,bool> Preconditions;
+typedef __gnu_cxx::hash_map<int,Literals> EffectsMap;
+namespace __gnu_cxx {
+
+ template< typename CharT, typename Traits, typename Alloc >
+ struct hash< std::basic_string<CharT, Traits, Alloc> > {
+ size_t operator()(const std::basic_string<CharT, Traits, Alloc>& s) const {
+
+ const std::collate<CharT>& c = std::use_facet< std::collate<CharT> >(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<CharT, Traits, Alloc> > { //yes you need this version aswell!
+
+ size_t operator()(const std::basic_string<CharT, Traits, Alloc>& s) const {
+
+ const std::collate<CharT>& c = std::use_facet< std::collate<CharT> >(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
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<Node*>::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<string,literals> 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();
}
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<Node*> _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
using namespace std;
using namespace __gnu_cxx;
-Planner::Planner(std::vector<Action> actions, literals init, literals goal){
+Planner::Planner(std::vector<Action> actions, Literals init, Literals goal){
_init = init;
_goal = goal;
_start = new StartNode(_init);
- Node* finish = new EndNode(_goal);
+ _finish = new EndNode(_goal);
for(vector<Action>::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(){
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;
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<string,Node*>::iterator addedNode = _addedNodes.find(*precond);
+ for (Preconditions::const_iterator precond = preconds.begin(); precond != preconds.end(); ++precond){
+ cerr << "Looking for: " << precond->first << endl;
+ hash_map<string,Node*>::iterator addedNode = _addedNodes.find(precond->first);
if(addedNode != _addedNodes.end()){
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->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;
}
}
}
}
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;
}
void Planner::execute(){
- _start->execute(literals());
+ _start->execute(true,Literals());
for (hash_map<string,Node*>::iterator node = _addedNodes.begin(); node != _addedNodes.end(); ++node){
+ if (!node->second->executed())
+ node->second->execute(false,Literals());
+ }
}
class Node;
-
-
-namespace __gnu_cxx {
-
- template< typename CharT, typename Traits, typename Alloc >
- struct hash< std::basic_string<CharT, Traits, Alloc> > {
- size_t operator()(const std::basic_string<CharT, Traits, Alloc>& s) const {
-
- const std::collate<CharT>& c = std::use_facet< std::collate<CharT> >(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<CharT, Traits, Alloc> > { //yes you need this version aswell!
-
-size_t operator()(const std::basic_string<CharT, Traits, Alloc>& s) const {
-
- const std::collate<CharT>& c = std::use_facet< std::collate<CharT> >(std::locale());
-
- return c.hash(s.c_str(), s.c_str() + s.size());
- }
-
- };
-};
-
class Planner {
public:
- Planner(std::vector<Action> actions, literals init, literals goal);
+ Planner(std::vector<Action> actions, Literals init, Literals goal);
~Planner();
void execute();
void addNode(Node* node);
Node* _start;
+ Node* _finish;
__gnu_cxx::hash_map<std::string,Node*> _addedNodes;
- __gnu_cxx::hash_map<std::string,Action*> _actions;
- literals _init;
- literals _goal;
+ __gnu_cxx::hash_map<std::string,Action> _actions;
+ Literals _init;
+ Literals _goal;
};
#endif