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 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;
24 cerr << "Number of actions: " << _actions.size() << endl;
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;
37 void Planner::makePlan(Node* node){
40 cerr << "Fetching preconditions for action: " << node->action()->name() << ".. ";
41 const Preconditions& preconds = node->action()->preconditions();
42 cerr << "done" << endl;
45 if (preconds.size() == 0){
46 cerr << "Found no preconds" << endl;
47 _start->addChild(node);
49 for (Preconditions::const_iterator precond = preconds.begin(); precond != preconds.end(); ++precond){
50 cerr << "Looking for: '" << precond->first << "'" << endl;
51 hash_map<string,Node*>::iterator addedNode = _addedEffects.find(precond->first);
52 if(addedNode != _addedEffects.end()){
53 cerr << "Using already added node" << endl;
54 addedNode->second->addChild(node);
56 hash_map<string, Action*>::iterator action = _actionEffects.find(precond->first);
57 if (action != _actionEffects.end()){
58 cerr << "Adding new node" << endl;
59 Node* newnode = new Node(action->second);
60 newnode->addChild(node);
62 }else if (precond->second){
63 cerr << "Action with effect: " << precond->first << " not found!" << endl;
64 cerr << "This is a hard precondition, so this action and the children can't be executed." << endl;
67 cerr << "Action with effect: " << precond->first << " not found!" << endl;
68 cerr << "This is a soft precondition, so we will continue" << endl;
69 node->satisfyCondition(precond->first);
70 _start->addChild(node);
77 void Planner::addNode(Node* node){
78 cerr << "Adding node for action: " << node->action()->name() << endl;
79 const Literals& effects = node->action()->effects(0);
80 cerr << "Number of effects: " << effects.size() << endl;
81 _addedNodes.push_back(node);
83 for (Literals::const_iterator effect = effects.begin(); effect != effects.end(); ++effect){
84 cerr << "Adding node for effect: " << *effect << endl;
85 _addedEffects[*effect] = node;
90 void Planner::execute(){
91 _start->execute(Literals());
92 cerr << "Number of nodes: " << _addedNodes.size() << endl;
93 back_insert_iterator<Literals> ii(_init);
94 copy(_init.begin(), _init.end(), ostream_iterator<string>(cout, " "));
97 for (vector<Node*>::iterator node = _addedNodes.begin(); node != _addedNodes.end(); ++node){
98 cerr << "Deleting node " << (*node)->action()->name() << endl;
99 if ((*node)->executed()){
100 const Literals& effects = (*node)->effects();
101 copy(effects.begin(),effects.end(),ii);
102 cerr << "Finding action" << endl;
103 vector<Action*>::iterator action = find(_actions.begin(), _actions.end(), (*node)->action());
104 if (action != _actions.end()){
105 cerr << "Removing executed action: " << (*action)->name() << endl;
106 _actions.erase(action);
107 //BUG: Sometimes finds the wrong action.
114 _actionEffects.clear();
115 copy(_init.begin(), _init.end(), ostream_iterator<string>(cout, " "));
116 cerr << "Number of nodes left: " << _addedNodes.size() << endl;
117 //TODO: Fill _actionEffects with the remaining effects, create start end end nodes and create a new plan.