root/deus/RegressionPlanner.h

Revision 8, 6.1 kB (checked in by pantley2, 4 years ago)

added old DEUS project to SVN

Line 
1 // RegressionPlanner.h
2 // interface for the RegressionPlanner class
3 // written 03/2003 by Steve Hanneke
4 // part of the 2003 UIUC SigArt project DEUS
5
6 #ifndef __REGRESSION_PLANNER_H__
7 #define __REGRESSION_PLANNER_H__
8
9 struct TimeOutException {};
10 class PlanNode;
11 class RegressionPlanner;
12
13 //#include "structures.h"
14 ///~#include "MiscStructures.h"
15 #include "structures.h"
16 #include "forward_search.h"
17 #include <time.h>
18 #include <iostream>
19 #include <stack>
20 #include <vector>
21 using namespace std;
22
23 #define WORLDSTATE std::pair<World*,PlanNode*>
24
25 #define MAXTIME 10 // time before switching back to forward search
26
27 /*
28 class PlanNode // DAG node
29 {
30 public:
31         PlanNode(const Operator& op) : Op(op) {}
32         bool isLeaf(void) {
33                 return branches.empty();
34         }
35         vector<PlanNode*> branches;
36         vector<PlanNode*> prevs;
37         Operator Op;
38         int num; // always useful for graph algorithms
39 };
40 */
41
42 template <class Type>
43 class MyStack ///~~ write me
44 {
45     struct NullDereferenceException {};
46     class StackNode {
47     public:
48         StackNode(Type& elt) :elem(elt) {}
49         Type elem;
50         StackNode* next;
51     };
52     StackNode* head;
53
54 public:
55     MyStack(void) { head = NULL; }
56     MyStack(const MyStack<Type>& origVal) {
57         head = CopyMe(origVal.head);
58     }
59     const MyStack<Type>& operator=(const MyStack<Type>& origVal) {
60         DeleteNodes(head);
61         head = CopyMe(origVal);
62     }
63     ~MyStack() {
64         DeleteNodes(head);
65     }
66     Type& top(void) {
67         if(empty()) throw NullDereferenceException();
68         else return head->elem;
69     }
70     Type tail(void) {
71         return GetTail(head)->elem;
72     }
73     Type pop(void) {
74         if(empty()) throw NullDereferenceException();
75         Type ret = head->elem;
76         StackNode* node = head;
77         head = head->next;
78         delete node;
79         return ret;
80     }
81     Type& push(Type elt) {
82         StackNode* node = new StackNode(elt);
83         node->next = head;
84         head = node;
85         return head->elem;
86     }
87     void clear(void) { DeleteNodes(head); head = NULL; }
88     bool empty(void) { return (head == NULL); }
89
90 private:
91     StackNode* GetTail(StackNode* node) {
92         if(node == NULL) return NULL;
93         if(node->next == NULL) return node;
94         return GetTail(node->next);
95     }
96     void DeleteNodes(StackNode* node) {
97         if(node == NULL) return;
98         DeleteNodes(node->next);
99         delete node;
100     }
101     StackNode* CopyMe(StackNode* origNode) {
102         if(origNode == NULL) return NULL;
103         StackNode* node = new StackNode(origNode->elem);
104         node->next =  CopyMe(origNode->next);
105         return node;
106     }
107 };
108
109 void DeletePlan(PlanNode* plan);
110
111 class RegressionPlanner
112 {
113 public:
114
115         RegressionPlanner() {}
116         RegressionPlanner(const RegressionPlanner& origVal) { ///~write me
117         }
118         ~RegressionPlanner(void) {///~write me
119
120         }
121         const RegressionPlanner& operator=(const RegressionPlanner& origVal) {///~write me
122         }
123
124         int MakeNewPlan(KeyFrameNode* key_frame, PlanNode*& retPlan, int timer);
125
126         int ResumePlanning(PlanNode*& retPlan, int timer, bool use_stack = true);
127        
128     int Replan(PlanNode*& retPlan, int timer);
129
130 private:
131         // private functions
132
133     int PlanOnRest(PlanNode*&retPlan,pair<WORLDSTATE,Condition>& bindings,
134                                         int which_binding, bool use_stack);
135
136     list<pair<WORLDSTATE,Condition> >::iterator
137         FindComplete(list<pair<WORLDSTATE,Condition> >& possible_bindings,
138         Condition& Conds);
139
140         int NoNulls(vector<vector<GameObject*> >& vars);
141
142         int MakePlan(PlanNode*& retPlan, bool use_stack);
143
144         int MakeSubPlan(PlanNode*& plan, Condition& Conds, WORLDSTATE& state, bool use_stack);
145
146         ///~~int SubPlanOnRest(PlanNode*& plan, Condition& Conds, int which_binding, WORLDSTATE& state);
147     
148     void ConcatPlans(PlanNode*& plan, PlanNode* newplan);
149
150         void _ConcatPlans(PlanNode* plan, PlanNode* newplan);
151
152         // returns 1 iff all threats resolved successfully
153         int ResolveThreats(PlanNode*& plan1, PlanNode*& plan2);
154
155         // returns 1 iff the plans are successfully merged and
156         // the result pointed to by plan1
157         int MergePlans(PlanNode*& plan1, PlanNode*& plan2);
158
159         void MergeRoots(PlanNode*& plan1, PlanNode*& plan2);
160        
161         void ClearStacks(void) {
162             possible_bindings.clear();
163         satisfied_counts.clear();
164
165         // used for PlanOnRest
166         Ops.clear();
167         done = 0;
168
169         // used for MakeSubPlan
170         cond = NULL;
171         OpStack.clear();
172         PlanStack.clear();     
173         }
174
175         void MakeLink(PlanNode* p1, PlanNode* p2) {
176                 p1->branches.push_back(p2);
177                 p2->prevs.push_back(p1);
178         }
179
180         pair<int,int> CountSatisfied(Condition& cond);
181        
182     int _CountSatisfied(Condition& cond, int which_binding);
183    
184     void ResetFlags(PlanNode* plan);
185
186         int PrevsSatisfied(PlanNode* plan);
187
188     void AddForwardPart(PlanNode*& plan, WORLDSTATE& state);
189    
190     void SaveAll(void);
191
192         void ResetTimer(void) {
193                 recorded_time = time(0);
194         }
195         int TimesUp(void) {
196                 return (time(0) - recorded_time > MAXTIME);
197         }
198         template <class T>
199         int Member(T& elt, vector<T>& vec) {
200                 for(vector<T>::iterator e(vec.begin());e!=vec.end();++e)
201                         if(*e == elt) return 1;
202                 return 0;
203         }
204
205     list<pair<list<pair<WORLDSTATE,Condition> >::iterator, pair<int,int> > >::iterator
206         FindBestCount(void);
207    
208     list<pair<list<pair<WORLDSTATE,Condition> >::iterator,pair<int,int> > > CountAllSatisfied(void);
209
210         // private data
211         
212     // used for MakePlan
213     list<pair<WORLDSTATE, Condition> > possible_bindings;
214     list<pair<list<pair<WORLDSTATE,Condition> >::iterator,pair<int,int> > > satisfied_counts;
215    
216     // used for PlanOnRest
217     vector<list<Operator> > Ops;
218     vector<list<Operator> >::iterator ops;
219     list<Operator>::iterator op;
220     int done;
221
222     // used for MakeSubPlan
223     Condition* cond;
224     pair<int,int> satisfied; // first is value, second is index
225         MyStack<pair<vector<list<Operator> >, pair<vector<list<Operator> >::iterator, list<Operator>::iterator> > > OpStack;
226     MyStack<PlanNode*> PlanStack;
227
228     KeyFrameNode* currently_seeking;
229         int recorded_time;
230    
231 };
232
233 #endif
234
Note: See TracBrowser for help on using the browser.