]> ruin.nu Git - popboot.git/blob - planner.cpp
Changed random seed to use usec instead of seconds
[popboot.git] / planner.cpp
1 #include "planner.h"
2 #include "node.h"
3 #include <iostream>
4 #include <vector>
5 using namespace std;
6 using namespace __gnu_cxx;
7
8 Planner::Planner(std::vector<Action> actions, Literals init, Literals goal){
9         _init = init;
10         _goal = goal;
11         _start = new StartNode(_init);
12         _finish = new EndNode(_goal);
13         addNode(_start);
14
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;
22                 }
23         }
24         cerr << "Number of actions: " << _actions.size() << endl;
25         makePlan(_finish);
26 }
27
28 Planner::~Planner(){
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;
32                 delete *node;
33         }
34 }
35
36
37 void Planner::makePlan(Node* node){
38         addNode(node);
39
40         cerr << "Fetching preconditions for action: " << node->action()->name() << ".. ";
41         const Preconditions& preconds = node->action()->preconditions();
42         cerr << "done" << endl;
43
44
45         if (preconds.size() == 0){
46                 cerr << "Found no preconds" << endl;
47                 _start->addChild(node);
48         }else{
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);
55                         }else {
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);
61                                         makePlan(newnode);
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;
65                                         return;
66                                 }else{
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);
71                                 }
72                         }
73                 }
74         }
75 }
76
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);
82
83         for (Literals::const_iterator effect = effects.begin(); effect != effects.end(); ++effect){
84                 cerr << "Adding node for effect: " << *effect << endl;
85                 _addedEffects[*effect] = node;
86         }
87 }
88
89
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, " "));
95         cout << endl;
96         _init.clear();
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.
108                                 //delete *action;
109                         }
110                 }
111                 delete *node;
112         }
113         _addedNodes.clear();
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.
118 }