5 using namespace __gnu_cxx;
7 extern "C" void* executeNode(void* arg);
9 struct ExecutionStuff {
13 queue<Node*>* execQueue;
16 Planner::Planner(std::vector<Action> actions, Literals init, Literals goal){
19 _start = new StartNode(_init);
20 _finish = new EndNode(_goal);
23 for(vector<Action>::iterator action = actions.begin(); action != actions.end(); ++action){
24 Action* act = new Action(*action);
25 _actions.push_back(act);
26 const Literals& effects = act->effects(0);
27 for (Literals::const_iterator effect = effects.begin(); effect != effects.end(); ++effect){
28 _actionEffects[*effect] = act;
35 //Iterating over the remaining nodes and deleting them
36 for (vector<Node*>::iterator node = _addedNodes.begin(); node != _addedNodes.end(); ++node){
39 //iterating over the the remaining actions and deleting them
40 for (vector<Action*>::iterator action = _actions.begin(); action != _actions.end(); ++action){
46 void Planner::makePlan(Node* node){
49 const Preconditions& preconds = node->action()->preconditions();
51 if (preconds.size() == 0){
52 //Add the node as a child to start if there are no preconditions
53 _start->addChild(node);
55 //iterate over the preconditions
56 for (Preconditions::const_iterator precond = preconds.begin(); precond != preconds.end(); ++precond){
57 //Check if there is a node with this precondition as an effect
58 hash_map<string,Node*>::iterator addedNode = _addedEffects.find(precond->first);
59 if(addedNode != _addedEffects.end()){
60 //Use this node if there is one
61 //cerr << "Using already added node for effect " << precond->first << ", on node: " << node->action()->name() << endl;
62 addedNode->second->addChild(node);
64 //Check if there is an action which satisfies this effect
65 hash_map<string, Action*>::iterator action = _actionEffects.find(precond->first);
66 if (action != _actionEffects.end()){
67 //Create a new node for the found effect and add the current
69 Node* newnode = new Node(action->second);
70 newnode->addChild(node);
72 }else if (precond->second){
73 //No such action found, and since it was a hard preconition
74 //we need to stop here.
75 cerr << "Could not satisfy the effect: " << precond->first << " for: " << node->action()->name() << endl;
78 //No action found, but it was a soft precondition, so we can
79 //satisfy it and continue.
80 //cerr << "Could not satisfy the effect: " << precond->first << " for: " << node->action()->name() << endl;
81 node->satisfyCondition(precond->first);
82 _start->addChild(node);
89 void Planner::addNode(Node* node){
90 const Literals& effects = node->action()->effects(0);
91 _addedNodes.push_back(node);
93 //Iterate over the effects for this node and add them to the map.
94 for (Literals::const_iterator effect = effects.begin(); effect != effects.end(); ++effect){
95 _addedEffects[*effect] = node;
100 void Planner::execute(){
101 sem_init(&_nodes, 0, 1);
102 sem_init(&_list, 0, 1);
104 //We've "executed" the start node.
105 _executedNodes.push(_start);
108 //As long as there executed nodes in the queue.
109 while (executions > 0){
110 //Wait for a node to be added to the queue
114 //Pop the first node from the queue.
116 Node* node = _executedNodes.front();
117 _executedNodes.pop();
120 //We don't need to continue if the end node was executed.
124 vector<Node*> children = node->children();
126 //Iterate over the children for this node
127 for(vector<Node*>::iterator child = children.begin(); child != children.end(); ++child){
128 //Satisfy the preconditions the current node had as effect.
129 if ((*child)->satisfyConditions(node->effects())){
130 //If all preconditions were satisified we can create a new thread
131 //and execute this child.
133 pthread_attr_t tattr;
135 pthread_attr_init(&tattr);
136 pthread_attr_setscope(&tattr, PTHREAD_SCOPE_SYSTEM);
137 //Create a struct object for the thread and add the
138 //needed members to it.
139 ExecutionStuff* es = new ExecutionStuff;
143 es->execQueue = &_executedNodes;
144 pthread_create(&tid, &tattr, executeNode, es);
149 cerr << "Number of nodes: " << _addedNodes.size() << endl;
150 //Clearing the init vector, effects will be added below.
152 Preconditions goal = _finish->preconditions();
153 for (Preconditions::const_iterator precond = goal.begin(); precond != goal.end(); ++precond){
154 _goal.push_back(precond->first);
157 cout << "Unsatisfied preconditions so far: " << _goal.size() << ": ";
158 copy(_goal.begin(), _goal.end(), ostream_iterator<string>(cout, " "));
161 //iterator for inserting effects at the end of _init
162 back_insert_iterator<Literals> ii(_init);
164 //Iterate throu the nodes, to see what's been done
165 for (vector<Node*>::iterator node = _addedNodes.begin(); node != _addedNodes.end(); ++node){
166 if ((*node)->executed()){
167 //Node was executed, adding the effects to _init.
169 const Literals& effects = (*node)->effects();
170 copy(effects.begin(),effects.end(),ii);
172 vector<Action*>::iterator action = find(_actions.begin(), _actions.end(), (*node)->action());
173 if (action != _actions.end()){
174 //The action can be deleted, since it's already been executed.
176 _actions.erase(action);
179 //The node is not needed anymore
182 //Clearing the vectors and maps, since they are obsolete now.
184 _addedEffects.clear();
185 _actionEffects.clear();
187 cout << "Effects achieved so far: " << _init.size() << ": ";
188 copy(_init.begin(), _init.end(), ostream_iterator<string>(cout, " "));
191 if (executions <= 1){
192 cerr << "Non of the remaining actions could be executed, quiting." << endl;
195 if (_actions.size() == 0){
196 //Nothing left to do, quitting.
200 cout << "Replanning..." << endl;
202 //Adding the effecs for the remaining actions.
203 for (vector<Action*>::iterator action = _actions.begin(); action != _actions.end(); ++action){
204 const Literals& effects = (*action)->effects(0);
205 for (Literals::const_iterator effect = effects.begin(); effect != effects.end(); ++effect){
206 _actionEffects[*effect] = *action;
209 _start = new StartNode(_init);
210 _finish = new EndNode(_goal);
213 if (_addedNodes.size() <= 2){
214 cerr << "No actions to execute, quiting." << endl;
220 void* executeNode(void* arg){
221 ExecutionStuff* es = (ExecutionStuff*)arg;
226 //Add this node to the queue with executed nodes
227 es->execQueue->push(es->node);
230 //Signal that the node is available.
233 pthread_exit((void*)0);