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;
34 for (vector<Action*>::iterator action = _actions.begin(); action != _actions.end(); ++action){
35 cerr << "Deleting action " << (*action)->name() << endl;
41 void Planner::makePlan(Node* node){
44 cerr << "Fetching preconditions for action: " << node->action()->name() << ".. ";
45 const Preconditions& preconds = node->action()->preconditions();
46 cerr << "done" << endl;
49 if (preconds.size() == 0){
50 cerr << "Found no preconds" << endl;
51 _start->addChild(node);
53 for (Preconditions::const_iterator precond = preconds.begin(); precond != preconds.end(); ++precond){
54 cerr << "Looking for: '" << precond->first << "'" << endl;
55 hash_map<string,Node*>::iterator addedNode = _addedEffects.find(precond->first);
56 if(addedNode != _addedEffects.end()){
57 cerr << "Using already added node" << endl;
58 addedNode->second->addChild(node);
60 hash_map<string, Action*>::iterator action = _actionEffects.find(precond->first);
61 if (action != _actionEffects.end()){
62 cerr << "Adding new node" << endl;
63 Node* newnode = new Node(action->second);
64 newnode->addChild(node);
66 }else if (precond->second){
67 cerr << "Action with effect: " << precond->first << " not found!" << endl;
68 cerr << "This is a hard precondition, so this action and the children can't be executed." << endl;
71 cerr << "Action with effect: " << precond->first << " not found!" << endl;
72 cerr << "This is a soft precondition, so we will continue" << endl;
73 node->satisfyCondition(precond->first);
74 _start->addChild(node);
81 void Planner::addNode(Node* node){
82 cerr << "Adding node for action: " << node->action()->name() << endl;
83 const Literals& effects = node->action()->effects(0);
84 cerr << "Number of effects: " << effects.size() << endl;
85 _addedNodes.push_back(node);
87 for (Literals::const_iterator effect = effects.begin(); effect != effects.end(); ++effect){
88 cerr << "Adding node for effect: " << *effect << endl;
89 _addedEffects[*effect] = node;
94 void Planner::execute(){
95 _start->execute(Literals());
96 cerr << "Number of nodes: " << _addedNodes.size() << endl;
97 back_insert_iterator<Literals> ii(_init);
100 for (vector<Node*>::iterator node = _addedNodes.begin(); node != _addedNodes.end(); ++node){
101 cerr << "Deleting node " << (*node)->action()->name() << endl;
102 if ((*node)->executed()){
104 const Literals& effects = (*node)->effects();
105 copy(effects.begin(),effects.end(),ii);
106 cerr << "Finding action" << endl;
107 vector<Action*>::iterator action = find(_actions.begin(), _actions.end(), (*node)->action());
108 if (action != _actions.end()){
109 cerr << "Removing executed action: " << (*action)->name() << endl;
111 _actions.erase(action);
117 _addedEffects.clear();
118 _actionEffects.clear();
119 copy(_init.begin(), _init.end(), ostream_iterator<string>(cerr, " "));
121 cerr << "Number of actions left: " << _actions.size() << endl;
122 //TODO: Fill _actionEffects with the remaining effects, create start end end nodes and create a new plan.
123 if (executions <= 1){
124 cerr << "Non of the remaining actions could be executed, quiting." << endl;
127 if (_actions.size() == 0){
128 cerr << "No remaining actions, quiting." << endl;
131 for (vector<Action*>::iterator action = _actions.begin(); action != _actions.end(); ++action){
132 const Literals& effects = (*action)->effects(0);
133 for (Literals::const_iterator effect = effects.begin(); effect != effects.end(); ++effect){
134 cerr << "Adding effect: '" << *effect << "', action: " << (*action)->name() << endl;
135 _actionEffects[*effect] = *action;
138 _start = new StartNode(_init);
139 _finish = new EndNode(_goal);
142 if (_addedNodes.size() <= 2){
143 cerr << "No actions to execute, quiting." << endl;