X-Git-Url: https://ruin.nu/git/?a=blobdiff_plain;f=planner.cpp;h=9feeec8ed577398bc859e3d2fe9d1fb21266501c;hb=3f82360c5e0c61eccacdbaf3bd077852b9326f34;hp=d29691f95c1d4d620318b2a665f5cebea110d952;hpb=76a6e62c0a20bebba4c828f528f118a1f789b593;p=popboot.git diff --git a/planner.cpp b/planner.cpp index d29691f..9feeec8 100644 --- a/planner.cpp +++ b/planner.cpp @@ -1,10 +1,18 @@ #include "planner.h" #include "node.h" #include -#include using namespace std; using namespace __gnu_cxx; +extern "C" void* executeNode(void* arg); + +struct ExecutionStuff { + sem_t* nodes; + sem_t* list; + Node* node; + queue* execQueue; +}; + Planner::Planner(std::vector actions, Literals init, Literals goal){ _init = init; _goal = goal; @@ -17,55 +25,59 @@ Planner::Planner(std::vector actions, Literals init, Literals goal){ _actions.push_back(act); const Literals& effects = act->effects(0); for (Literals::const_iterator effect = effects.begin(); effect != effects.end(); ++effect){ - cerr << "Adding effect: '" << *effect << "', action: " << action->name() << endl; + //cerr << "Adding effect: '" << *effect << "', action: " << action->name() << endl; _actionEffects[*effect] = act; } } - cerr << "Number of actions: " << _actions.size() << endl; + //cerr << "Number of actions: " << _actions.size() << endl; makePlan(_finish); } Planner::~Planner(){ - cerr << "Deleting " << _addedNodes.size() << " nodes" << endl; + //cerr << "Deleting " << _addedNodes.size() << " nodes" << endl; for (vector::iterator node = _addedNodes.begin(); node != _addedNodes.end(); ++node){ - cerr << "Deleting node " << (*node)->action()->name() << endl; + //cerr << "Deleting node " << (*node)->action()->name() << endl; delete *node; } + for (vector::iterator action = _actions.begin(); action != _actions.end(); ++action){ + //cerr << "Deleting action " << (*action)->name() << endl; + delete *action; + } } void Planner::makePlan(Node* node){ addNode(node); - cerr << "Fetching preconditions for action: " << node->action()->name() << ".. "; + //cerr << "Fetching preconditions for action: " << node->action()->name() << ".. "; const Preconditions& preconds = node->action()->preconditions(); - cerr << "done" << endl; + //cerr << "done" << endl; if (preconds.size() == 0){ - cerr << "Found no preconds" << endl; + //cerr << "Found no preconds" << endl; _start->addChild(node); }else{ for (Preconditions::const_iterator precond = preconds.begin(); precond != preconds.end(); ++precond){ - cerr << "Looking for: '" << precond->first << "'" << endl; + //cerr << "Looking for: '" << precond->first << "'" << endl; hash_map::iterator addedNode = _addedEffects.find(precond->first); if(addedNode != _addedEffects.end()){ - cerr << "Using already added node" << endl; + //cerr << "Using already added node" << endl; addedNode->second->addChild(node); }else { hash_map::iterator action = _actionEffects.find(precond->first); if (action != _actionEffects.end()){ - cerr << "Adding new node" << endl; + //cerr << "Adding new node" << endl; Node* newnode = new Node(action->second); newnode->addChild(node); makePlan(newnode); }else if (precond->second){ - cerr << "Action with effect: " << precond->first << " not found!" << endl; - cerr << "This is a hard precondition, so this action and the children can't be executed." << endl; + //cerr << "Action with effect: " << precond->first << " not found!" << endl; + //cerr << "This is a hard precondition, so this action and the children can't be executed." << endl; return; }else{ - cerr << "Action with effect: " << precond->first << " not found!" << endl; - cerr << "This is a soft precondition, so we will continue" << endl; + //cerr << "Action with effect: " << precond->first << " not found!" << endl; + //cerr << "This is a soft precondition, so we will continue" << endl; node->satisfyCondition(precond->first); _start->addChild(node); } @@ -75,44 +87,134 @@ void Planner::makePlan(Node* node){ } void Planner::addNode(Node* node){ - cerr << "Adding node for action: " << node->action()->name() << endl; + //cerr << "Adding node for action: " << node->action()->name() << endl; const Literals& effects = node->action()->effects(0); - cerr << "Number of effects: " << effects.size() << endl; + //cerr << "Number of effects: " << effects.size() << endl; _addedNodes.push_back(node); for (Literals::const_iterator effect = effects.begin(); effect != effects.end(); ++effect){ - cerr << "Adding node for effect: " << *effect << endl; + //cerr << "Adding node for effect: " << *effect << endl; _addedEffects[*effect] = node; } } void Planner::execute(){ - _start->execute(Literals()); + _executedNodes.push(_start); + sem_init(&_nodes, 0, 1); + sem_init(&_list, 0, 1); + + int executions = 1; + while (executions > 0){ + cerr << "Waiting for a node to finish execution" << endl; + int retval; + sem_getvalue(&_nodes, &retval); + cerr << "Semaphore before wait: " << retval << endl; + sem_wait(&_nodes); + sem_getvalue(&_nodes, &retval); + cerr << "Semaphore after wait: " << retval << endl; + --executions; + + cerr << "Getting node: "; + sem_wait(&_list); + cerr << "Number of nodes in queue: " << _executedNodes.size() << endl; + Node* node = _executedNodes.front(); + _executedNodes.pop(); + sem_post(&_list); + cerr << (int) node << endl; + cerr << node->action()->name() << ", and children.. "; + vector children = node->children(); + cerr << " done" << endl; + if (node == _finish) + return; + cerr << "Iterating over the children, number: " << children.size() << endl; + for(vector::iterator child = children.begin(); child != children.end(); ++child){ + if ((*child)->satisfyConditions(node->effects())){ + ++executions; + cerr << "Creating new thread" << endl; + pthread_attr_t tattr; + pthread_t tid; + pthread_attr_init(&tattr); + pthread_attr_setscope(&tattr, PTHREAD_SCOPE_SYSTEM); + ExecutionStuff* es = new ExecutionStuff; + es->nodes = &_nodes; + es->list = &_list; + es->node = *child; + es->execQueue = &_executedNodes; + pthread_create(&tid, &tattr, executeNode, es); + //executeNode(es); + } + } + + } cerr << "Number of nodes: " << _addedNodes.size() << endl; back_insert_iterator ii(_init); - copy(_init.begin(), _init.end(), ostream_iterator(cout, " ")); - cout << endl; _init.clear(); for (vector::iterator node = _addedNodes.begin(); node != _addedNodes.end(); ++node){ cerr << "Deleting node " << (*node)->action()->name() << endl; if ((*node)->executed()){ + executions++; const Literals& effects = (*node)->effects(); copy(effects.begin(),effects.end(),ii); cerr << "Finding action" << endl; vector::iterator action = find(_actions.begin(), _actions.end(), (*node)->action()); if (action != _actions.end()){ cerr << "Removing executed action: " << (*action)->name() << endl; + delete *action; _actions.erase(action); - //BUG: Sometimes finds the wrong action. - //delete *action; } } delete *node; } _addedNodes.clear(); + _addedEffects.clear(); _actionEffects.clear(); - copy(_init.begin(), _init.end(), ostream_iterator(cout, " ")); - cerr << "Number of nodes left: " << _addedNodes.size() << endl; + copy(_init.begin(), _init.end(), ostream_iterator(cerr, " ")); + cerr << endl; + cerr << "Number of actions left: " << _actions.size() << endl; //TODO: Fill _actionEffects with the remaining effects, create start end end nodes and create a new plan. + if (executions <= 1){ + cerr << "Non of the remaining actions could be executed, quiting." << endl; + return; + } + if (_actions.size() == 0){ + cerr << "No remaining actions, quiting." << endl; + return; + } + for (vector::iterator action = _actions.begin(); action != _actions.end(); ++action){ + const Literals& effects = (*action)->effects(0); + for (Literals::const_iterator effect = effects.begin(); effect != effects.end(); ++effect){ + cerr << "Adding effect: '" << *effect << "', action: " << (*action)->name() << endl; + _actionEffects[*effect] = *action; + } + } + _start = new StartNode(_init); + _finish = new EndNode(_goal); + addNode(_start); + makePlan(_finish); + if (_addedNodes.size() <= 2){ + cerr << "No actions to execute, quiting." << endl; + return; + } + execute(); + +} +void* executeNode(void* arg){ + cerr << "Running new thred." << endl; + ExecutionStuff* es = (ExecutionStuff*)arg; + + if (es == 0) + pthread_exit((void*)1); + + es->node->execute(); + + sem_wait(es->list); + cerr << "Adding pointer with value: " << (int)es->node << endl; + es->execQueue->push(es->node); + sem_post(es->list); + + cerr << "Increasing semaphore" << endl; + sem_post(es->nodes); + + pthread_exit((void*)0); }