From 9811871585fa6028362c8910bfac18159c286323 Mon Sep 17 00:00:00 2001 From: Michael Andreen Date: Fri, 6 May 2005 21:06:02 +0000 Subject: [PATCH] seems to work --- actions | 30 ++++++++++++++++++++++++++++++ main.cpp | 39 +++++++++++++++++++++++++++++++++++++++ planner.cpp | 17 ++++++++++++----- src.pro | 2 ++ 4 files changed, 83 insertions(+), 5 deletions(-) create mode 100644 actions diff --git a/actions b/actions new file mode 100644 index 0000000..db94321 --- /dev/null +++ b/actions @@ -0,0 +1,30 @@ +1 +A + +2 +B + +3 +C +A +4 +D +B +5 +E +A D +6 +F +C B +7 +G +D +8 +H +G +9 +I +D E +10 +J +I G diff --git a/main.cpp b/main.cpp index 15bca93..740c889 100644 --- a/main.cpp +++ b/main.cpp @@ -1,7 +1,46 @@ #include +#include +#include using namespace std; +#include "planner.h" +#include "action.h" + +vector stringToVector(string str){ + vector strings; + + istringstream ist(str); + while (ist >> str){ + strings.push_back(str); + } + + return strings; +} + int main(int argc, char** argv){ + + if (argc != 4){ + cout << "Syntax: " << argv[0] << " \"init state\" \"goal state\"" << endl; + return 1; + } + + ifstream file(argv[1]); + if (!file){ + cerr << "Cannot open input file: " << argv[1] << endl; + exit(2); + } + vector actions; + while (!file.eof()){ + string exec; + string precond; + string effects; + getline(file,exec); + getline(file,effects); + getline(file,precond); + cout << exec << ":" << effects << ":" << precond << endl; + actions.push_back(Action(exec, stringToVector(precond), stringToVector(effects))); + } + Planner p(actions, stringToVector(argv[2]), stringToVector(argv[3])); return 0; } diff --git a/planner.cpp b/planner.cpp index 3e8d5de..5e1719e 100644 --- a/planner.cpp +++ b/planner.cpp @@ -6,12 +6,13 @@ using namespace __gnu_cxx; Planner::Planner(std::vector actions, literals init, literals goal){ - _start = new Node(Action("",init, literals())); - Node* finish = new Node(Action("",literals(),goal)); + _start = new Node(Action("",literals(), init)); + Node* finish = new Node(Action("",goal,literals())); for(vector::iterator action = actions.begin(); action != actions.end(); ++action){ - literals preconds = action->preconditions(); - for (literals::iterator effect = preconds.begin(); effect != preconds.end(); ++effect){ + literals effects = action->effects(); + for (literals::iterator effect = effects.begin(); effect != effects.end(); ++effect){ + cerr << "Adding effect: " << *effect << endl; _actions[*effect] = *action; } } @@ -23,20 +24,25 @@ void Planner::makePlan(Node* node){ literals preconds = node->action().preconditions(); if (preconds.size() == 0){ + cerr << "Found no preconds" << endl; _start->addChild(node); }else{ for (literals::iterator precond = preconds.begin(); precond != preconds.end(); ++precond){ + cerr << "Looking for: " << *precond << endl; hash_map::iterator addedNode = _addedNodes.find(*precond); if(addedNode != _addedNodes.end()){ + cerr << "Using already added node" << endl; addedNode->second->addChild(node); }else { hash_map::iterator action = _actions.find(*precond); if (action != _actions.end()){ + cerr << "Adding new node" << endl; Node* newnode = new Node(action->second); newnode->addChild(node); + addNode(newnode); makePlan(newnode); }else{ - cerr << "Action with effect: " << *precond << " not found!"; + cerr << "Action with effect: " << *precond << " not found!" << endl; } } } @@ -47,6 +53,7 @@ void Planner::addNode(Node* node){ literals effects = node->action().effects(); for (literals::iterator effect = effects.begin(); effect != effects.end(); ++effect){ + cout << "Adding node for effect: " << *effect << endl; _addedNodes[*effect] = node; } } diff --git a/src.pro b/src.pro index 7446f02..c5ed145 100644 --- a/src.pro +++ b/src.pro @@ -3,6 +3,8 @@ ###################################################################### TEMPLATE = app +CONFIG -= qt +CONFIG += debug INCLUDEPATH += . # Input -- 2.39.2