]> ruin.nu Git - popboot.git/blob - planner.cpp
planning and execution seems to work now
[popboot.git] / planner.cpp
1 #include "planner.h"
2 #include "node.h"
3 #include <iostream>
4 #include <vector>
5 using namespace std;
6 using namespace __gnu_cxx;
7
8 Planner::Planner(std::vector<Action> actions, Literals init, Literals goal){
9         _init = init;
10         _goal = goal;
11         _start = new StartNode(_init);
12         _finish = new EndNode(_goal);
13         addNode(_start);
14
15         for(vector<Action>::iterator action = actions.begin(); action != actions.end(); ++action){
16                 const Literals& effects = action->effects(0);
17                 for (Literals::const_iterator effect = effects.begin(); effect != effects.end(); ++effect){
18                         cerr << "Adding effect: '" << *effect << "', action: " << action->name() << endl;
19                         _actions[*effect] = *action;
20                 }
21         }
22         cerr << "Number of actions: " << _actions.size() << endl;
23         makePlan(_finish);
24 }
25
26 Planner::~Planner(){
27         cerr << "Deleting " << _addedNodes.size() << " nodes" << endl;
28         for (vector<Node*>::iterator node = _addedNodes.begin(); node != _addedNodes.end(); ++node){
29                 cerr << "Deleting node " << (*node)->action().name() << endl;
30                 delete *node;
31         }
32 }
33
34
35 void Planner::makePlan(Node* node){
36         addNode(node);
37
38         cerr << "Fetching preconditions for action: " << node->action().name() << ".. ";
39         const Preconditions& preconds = node->action().preconditions();
40         cerr << "done" << endl;
41
42
43         if (preconds.size() == 0){
44                 cerr << "Found no preconds" << endl;
45                 _start->addChild(node);
46         }else{
47                 for (Preconditions::const_iterator precond = preconds.begin(); precond != preconds.end(); ++precond){
48                         cerr << "Looking for: '" << precond->first << "'" <<  endl;
49                         hash_map<string,Node*>::iterator addedNode = _addedEffects.find(precond->first);
50                         if(addedNode != _addedEffects.end()){
51                                 cerr << "Using already added node" << endl;
52                                 addedNode->second->addChild(node);
53                         }else {
54                                 hash_map<string, Action>::iterator action = _actions.find(precond->first);
55                                 if (action != _actions.end()){
56                                         cerr << "Adding new node" << endl;
57                                         Node* newnode = new Node(action->second);
58                                         newnode->addChild(node);
59                                         makePlan(newnode);
60                                 }else if (precond->second){
61                                         cerr << "Action with effect: " << precond->first << " not found!" << endl;
62                                         cerr << "This is a hard precondition, so this action and the children can't be executed." << endl;
63                                         return;
64                                 }else{
65                                         cerr << "Action with effect: " << precond->first << " not found!" << endl;
66                                         cerr << "This is a soft precondition, so we will continue" << endl;
67                                         node->satisfyCondition(precond->first);
68                                 }
69                         }
70                 }
71         }
72 }
73
74 void Planner::addNode(Node* node){
75         cerr << "Adding node for action: " << node->action().name() << endl;
76         const Literals& effects = node->action().effects(0);
77         cerr << "Number of effects: " << effects.size() << endl;
78         _addedNodes.push_back(node);
79
80         for (Literals::const_iterator effect = effects.begin(); effect != effects.end(); ++effect){
81                 cerr << "Adding node for effect: " << *effect << endl;
82                 _addedEffects[*effect] = node;
83         }
84 }
85
86
87 void Planner::execute(){
88         _start->execute(Literals());
89         cerr << "Number of nodes: " << _addedNodes.size() << endl;
90         for (vector<Node*>::iterator node = _addedNodes.begin(); node != _addedNodes.end(); ++node){
91                 if ((*node)->executed()){
92                         //remove stuff
93                 }
94                 delete *node;
95         }
96         _addedNodes.clear();
97         cerr << "Number of nodes left: " << _addedNodes.size() << endl;
98 }