]> ruin.nu Git - popboot.git/blob - planner.cpp
delete remaining actions
[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                 Action* act = new Action(*action);
17                 _actions.push_back(act);
18                 const Literals& effects = act->effects(0);
19                 for (Literals::const_iterator effect = effects.begin(); effect != effects.end(); ++effect){
20                         cerr << "Adding effect: '" << *effect << "', action: " << action->name() << endl;
21                         _actionEffects[*effect] = act;
22                 }
23         }
24         cerr << "Number of actions: " << _actions.size() << endl;
25         makePlan(_finish);
26 }
27
28 Planner::~Planner(){
29         cerr << "Deleting " << _addedNodes.size() << " nodes" << endl;
30         for (vector<Node*>::iterator node = _addedNodes.begin(); node != _addedNodes.end(); ++node){
31                 cerr << "Deleting node " << (*node)->action()->name() << endl;
32                 delete *node;
33         }
34         for (vector<Action*>::iterator action = _actions.begin(); action != _actions.end(); ++action){
35                 cerr << "Deleting action " << (*action)->name() << endl;
36                 delete *action;
37         }
38 }
39
40
41 void Planner::makePlan(Node* node){
42         addNode(node);
43
44         cerr << "Fetching preconditions for action: " << node->action()->name() << ".. ";
45         const Preconditions& preconds = node->action()->preconditions();
46         cerr << "done" << endl;
47
48
49         if (preconds.size() == 0){
50                 cerr << "Found no preconds" << endl;
51                 _start->addChild(node);
52         }else{
53                 for (Preconditions::const_iterator precond = preconds.begin(); precond != preconds.end(); ++precond){
54                         cerr << "Looking for: '" << precond->first << "'" <<  endl;
55                         hash_map<string,Node*>::iterator addedNode = _addedEffects.find(precond->first);
56                         if(addedNode != _addedEffects.end()){
57                                 cerr << "Using already added node" << endl;
58                                 addedNode->second->addChild(node);
59                         }else {
60                                 hash_map<string, Action*>::iterator action = _actionEffects.find(precond->first);
61                                 if (action != _actionEffects.end()){
62                                         cerr << "Adding new node" << endl;
63                                         Node* newnode = new Node(action->second);
64                                         newnode->addChild(node);
65                                         makePlan(newnode);
66                                 }else if (precond->second){
67                                         cerr << "Action with effect: " << precond->first << " not found!" << endl;
68                                         cerr << "This is a hard precondition, so this action and the children can't be executed." << endl;
69                                         return;
70                                 }else{
71                                         cerr << "Action with effect: " << precond->first << " not found!" << endl;
72                                         cerr << "This is a soft precondition, so we will continue" << endl;
73                                         node->satisfyCondition(precond->first);
74                                         _start->addChild(node);
75                                 }
76                         }
77                 }
78         }
79 }
80
81 void Planner::addNode(Node* node){
82         cerr << "Adding node for action: " << node->action()->name() << endl;
83         const Literals& effects = node->action()->effects(0);
84         cerr << "Number of effects: " << effects.size() << endl;
85         _addedNodes.push_back(node);
86
87         for (Literals::const_iterator effect = effects.begin(); effect != effects.end(); ++effect){
88                 cerr << "Adding node for effect: " << *effect << endl;
89                 _addedEffects[*effect] = node;
90         }
91 }
92
93
94 void Planner::execute(){
95         _start->execute(Literals());
96         cerr << "Number of nodes: " << _addedNodes.size() << endl;
97         back_insert_iterator<Literals> ii(_init);
98         _init.clear();
99         int executions = 0;
100         for (vector<Node*>::iterator node = _addedNodes.begin(); node != _addedNodes.end(); ++node){
101                 cerr << "Deleting node " << (*node)->action()->name() << endl;
102                 if ((*node)->executed()){
103                         executions++;
104                         const Literals& effects = (*node)->effects();
105                         copy(effects.begin(),effects.end(),ii);
106                         cerr << "Finding action" << endl;
107                         vector<Action*>::iterator action = find(_actions.begin(), _actions.end(), (*node)->action());
108                         if (action != _actions.end()){
109                                 cerr << "Removing executed action: " << (*action)->name() << endl;
110                                 delete *action;
111                                 _actions.erase(action);
112                         }
113                 }
114                 delete *node;
115         }
116         _addedNodes.clear();
117         _addedEffects.clear();
118         _actionEffects.clear();
119         copy(_init.begin(), _init.end(), ostream_iterator<string>(cerr, " "));
120         cerr << endl;
121         cerr << "Number of actions left: " << _actions.size() << endl;
122         //TODO: Fill _actionEffects with the remaining effects, create start end end nodes and create a new plan.
123         if (executions <= 1){
124                 cerr << "Non of the remaining actions could be executed, quiting." << endl;
125                 return;
126         }
127         if (_actions.size() == 0){
128                 cerr << "No remaining actions, quiting." << endl;
129                 return;
130         }
131         for (vector<Action*>::iterator action = _actions.begin(); action != _actions.end(); ++action){
132                 const Literals& effects = (*action)->effects(0);
133                 for (Literals::const_iterator effect = effects.begin(); effect != effects.end(); ++effect){
134                         cerr << "Adding effect: '" << *effect << "', action: " << (*action)->name() << endl;
135                         _actionEffects[*effect] = *action;
136                 }
137         }
138         _start = new StartNode(_init);
139         _finish = new EndNode(_goal);
140         addNode(_start);
141         makePlan(_finish);
142         if (_addedNodes.size() <= 2){
143                 cerr << "No actions to execute, quiting." << endl;
144                 return;
145         }
146         execute();
147         
148 }