]> ruin.nu Git - popboot.git/blob - planner.cpp
including iterator
[popboot.git] / planner.cpp
1 #include "planner.h"
2 #include "node.h"
3 #include <iostream>
4 #include <iterator>
5 using namespace std;
6 using namespace __gnu_cxx;
7
8 extern "C" void* executeNode(void* arg);
9
10 struct ExecutionStuff {
11         sem_t* nodes;
12         sem_t* list;
13         Node* node;
14         queue<Node*>* execQueue;
15 };
16
17 Planner::Planner(std::vector<Action> actions, Literals init, Literals goal){
18         _init = init;
19         _goal = goal;
20         _start = new StartNode(_init);
21         _finish = new EndNode(_goal);
22         addNode(_start);
23
24         for(vector<Action>::iterator action = actions.begin(); action != actions.end(); ++action){
25                 Action* act = new Action(*action);
26                 _actions.push_back(act);
27                 const Literals& effects = act->effects(0);
28                 for (Literals::const_iterator effect = effects.begin(); effect != effects.end(); ++effect){
29                         _actionEffects[*effect] = act;
30                 }
31         }
32         makePlan(_finish);
33 }
34
35 Planner::~Planner(){
36         //Iterating over the remaining nodes and deleting them
37         for (vector<Node*>::iterator node = _addedNodes.begin(); node != _addedNodes.end(); ++node){
38                 delete *node;
39         }
40         //iterating over the the remaining actions and deleting them
41         for (vector<Action*>::iterator action = _actions.begin(); action != _actions.end(); ++action){
42                 delete *action;
43         }
44 }
45
46
47 void Planner::makePlan(Node* node){
48         addNode(node);
49
50         const Preconditions& preconds = node->action()->preconditions();
51
52         if (preconds.size() == 0){
53                 //Add the node as a child to start if there are no preconditions
54                 _start->addChild(node);
55         }else{
56                 //iterate over the preconditions
57                 for (Preconditions::const_iterator precond = preconds.begin(); precond != preconds.end(); ++precond){
58                         //Check if there is a node with this precondition as an effect
59                         hash_map<string,Node*>::iterator addedNode = _addedEffects.find(precond->first);
60                         if(addedNode != _addedEffects.end()){
61                                 //Use this node if there is one
62                                 //cerr << "Using already added node for effect " << precond->first << ", on node: " << node->action()->name() << endl;
63                                 addedNode->second->addChild(node);
64                         }else {
65                                 //Check if there is an action which satisfies this effect
66                                 hash_map<string, Action*>::iterator action = _actionEffects.find(precond->first);
67                                 if (action != _actionEffects.end()){
68                                         //Create a new node for the found effect and add the current
69                                         //one as a child
70                                         Node* newnode = new Node(action->second);
71                                         newnode->addChild(node);
72                                         makePlan(newnode);
73                                 }else if (precond->second){
74                                         //No such action found, and since it was a hard preconition
75                                         //we need to stop here.
76                                         cerr << "Could not satisfy the effect: " << precond->first << " for: " << node->action()->name() << endl;
77                                         return;
78                                 }else{
79                                         //No action found, but it was a soft precondition, so we can
80                                         //satisfy it and continue.
81                                         //cerr << "Could not satisfy the effect: " << precond->first << " for: " << node->action()->name() << endl;
82                                         node->satisfyCondition(precond->first);
83                                         _start->addChild(node);
84                                 }
85                         }
86                 }
87         }
88 }
89
90 void Planner::addNode(Node* node){
91         const Literals& effects = node->action()->effects(0);
92         _addedNodes.push_back(node);
93
94         //Iterate over the effects for this node and add them to the map.
95         for (Literals::const_iterator effect = effects.begin(); effect != effects.end(); ++effect){
96                 _addedEffects[*effect] = node;
97         }
98 }
99
100
101 void Planner::execute(){
102         sem_init(&_nodes, 0, 1);
103         sem_init(&_list, 0, 1);
104
105         //We've "executed" the start node.
106         _executedNodes.push(_start);
107         int executions = 1;
108
109         //As long as there executed nodes in the queue.
110         while (executions > 0){
111                 //Wait for a node to be added to the queue
112                 sem_wait(&_nodes);
113                 --executions;
114
115                 //Pop the first node from the queue.
116                 sem_wait(&_list);
117                 Node* node = _executedNodes.front();
118                 _executedNodes.pop();
119                 sem_post(&_list);
120
121                 //We don't need to continue if the end node was executed.
122                 if (node == _finish)
123                         return;
124
125                 vector<Node*> children = node->children();
126
127                 //Iterate over the children for this node
128                 for(vector<Node*>::iterator child = children.begin(); child != children.end(); ++child){
129                         //Satisfy the preconditions the current node had as effect.
130                         if ((*child)->satisfyConditions(node->effects())){
131                                 //If all preconditions were satisified we can create a new thread
132                                 //and execute this child.
133                                 ++executions;
134                                 pthread_attr_t tattr;
135                                 pthread_t tid;
136                                 pthread_attr_init(&tattr);
137                                 pthread_attr_setscope(&tattr, PTHREAD_SCOPE_SYSTEM);
138                                 //Create a struct object for the thread and add the
139                                 //needed members to it.
140                                 ExecutionStuff* es = new ExecutionStuff;
141                                 es->nodes = &_nodes;
142                                 es->list = &_list;
143                                 es->node = *child;
144                                 es->execQueue = &_executedNodes;
145                                 pthread_create(&tid, &tattr, executeNode, es);
146                         }
147                 }
148
149         }
150         //Clearing the init and goal vectors, new effects will be added below.
151         _init.clear();
152         _goal.clear();
153         Preconditions goal = _finish->preconditions();
154         for (Preconditions::const_iterator precond = goal.begin(); precond != goal.end(); ++precond){
155                 _goal.push_back(precond->first);
156         }
157
158         cout << "Unsatisfied preconditions so far: " << _goal.size() << ": ";
159         copy(_goal.begin(), _goal.end(), ostream_iterator<string>(cout, " "));
160         cout << endl;
161
162         //iterator for inserting effects at the end of _init
163         back_insert_iterator<Literals> ii(_init);
164
165         //Iterate throu the nodes, to see what's been done
166         for (vector<Node*>::iterator node = _addedNodes.begin(); node != _addedNodes.end(); ++node){
167                 if ((*node)->executed()){
168                         //Node was executed, adding the effects to _init.
169                         executions++;
170                         const Literals& effects = (*node)->effects();
171                         copy(effects.begin(),effects.end(),ii);
172
173                         vector<Action*>::iterator action = find(_actions.begin(), _actions.end(), (*node)->action());
174                         if (action != _actions.end()){
175                                 //The action can be deleted, since it's already been executed.
176                                 delete *action;
177                                 _actions.erase(action);
178                         }
179                 }
180                 //The node is not needed anymore
181                 delete *node;
182         }
183         //Clearing the vectors and maps, since they are obsolete now.
184         _addedNodes.clear();
185         _addedEffects.clear();
186         _actionEffects.clear();
187
188         cout << "Effects achieved so far: " << _init.size() << ": ";
189         copy(_init.begin(), _init.end(), ostream_iterator<string>(cout, " "));
190         cout << endl;
191
192         if (executions <= 1){
193                 cerr << "Non of the remaining actions could be executed, quiting." << endl;
194                 return;
195         }
196         if (_actions.size() == 0){
197                 //Nothing left to do, quitting.
198                 return;
199         }
200         //REPLANNING
201         cout << "Replanning..." << endl;
202         
203         //Adding the effecs for the remaining actions.
204         for (vector<Action*>::iterator action = _actions.begin(); action != _actions.end(); ++action){
205                 const Literals& effects = (*action)->effects(0);
206                 for (Literals::const_iterator effect = effects.begin(); effect != effects.end(); ++effect){
207                         _actionEffects[*effect] = *action;
208                 }
209         }
210         _start = new StartNode(_init);
211         _finish = new EndNode(_goal);
212         addNode(_start);
213         makePlan(_finish);
214         if (_addedNodes.size() <= 2){
215                 cerr << "No actions to execute, quiting." << endl;
216                 return;
217         }
218         execute();
219         
220 }
221 void* executeNode(void* arg){
222         ExecutionStuff* es = (ExecutionStuff*)arg;
223
224         es->node->execute();
225
226         sem_wait(es->list);
227         //Add this node to the queue with executed nodes
228         es->execQueue->push(es->node);
229         sem_post(es->list);
230
231         //Signal that the node is available.
232         sem_post(es->nodes);
233
234         pthread_exit((void*)0);
235 }