#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();
}
#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
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]));
#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{
_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()));
+}
+
#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
#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);
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;
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{
}
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;
void Planner::execute(){
_start->execute(literals());
+ for (hash_map<string,Node*>::iterator node = _addedNodes.begin(); node != _addedNodes.end(); ++node){
}
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