6 using namespace __gnu_cxx;
8 Planner::Planner(std::vector<Action> actions, Literals init, Literals goal){
11 _start = new StartNode(_init);
12 _finish = new EndNode(_goal);
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;
22 cerr << "Number of actions: " << _actions.size() << endl;
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;
35 void Planner::makePlan(Node* node){
38 cerr << "Fetching preconditions for action: " << node->action().name() << ".. ";
39 const Preconditions& preconds = node->action().preconditions();
40 cerr << "done" << endl;
43 if (preconds.size() == 0){
44 cerr << "Found no preconds" << endl;
45 _start->addChild(node);
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);
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);
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;
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);
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);
80 for (Literals::const_iterator effect = effects.begin(); effect != effects.end(); ++effect){
81 cerr << "Adding node for effect: " << *effect << endl;
82 _addedEffects[*effect] = node;
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()){
97 cerr << "Number of nodes left: " << _addedNodes.size() << endl;