root/deus/KeySeeker.cpp

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

added old DEUS project to SVN

Line 
1 // KeySeeker.cpp
2 // implementation for KeySeeker class
3 // Part of UIUC SigArt 2003 project DEUS
4 // Written 03/2003 by Steve Hanneke
5
6 #include "KeySeeker.h"
7
8 void Append(vector<PlanNode*>& ret, vector<PlanNode*>& newp)
9 {
10     for(int n=0; n<newp.size(); ++n)
11         ret.push_back(newp[n]);
12 }
13
14 bool AllPrevsSat(PlanNode* plan)
15 {
16     for(int p=0; p<plan->prevs.size(); ++p)
17         if(plan->prevs[p]->num != 1) return false;
18     return true;
19 }
20 vector<PlanNode*> _SquashPlan(PlanNode* plan)
21 {
22     if(plan == NULL) return vector<PlanNode*>();
23     if(!AllPrevsSat(plan)) return vector<PlanNode*>();
24     plan->num = 1;
25     vector<PlanNode*> ret;
26     for(int b=0; b<plan->branches.size(); ++b)
27     {
28         Append(ret, _SquashPlan(plan->branches[b]));
29     }
30     return ret;
31 }
32
33 void ResetFlags(PlanNode* plan)
34 {
35     if(plan == NULL) return;
36     plan->num = 0;
37     for(int b=0; b<plan->branches.size(); ++b)
38         ResetFlags(plan->branches[b]);
39 }  
40
41 vector<PlanNode*> SquashPlan(PlanNode* plan)
42 {
43     ResetFlags(plan);
44     return _SquashPlan(plan);
45 }
46
47
48
49 // StoryNode is here assumed to be in a tree
50 ///~ should we augment to allow for DAG stories?
51 ///~ this function should be called as part of a breadth-first traversal
52 int KeySeeker::NewSeek(StoryNode* root)
53 {
54     ResetAttempteds(root->frame);
55     attempts = 0;
56     return ContinueSeek(root);
57 }
58
59 int KeySeeker::Resatisfy(void)
60 {
61     PlanNode* plan = NULL;
62     int timer = time(0);
63     try {
64         if(!RP.Replan(plan, timer)) return 0;
65         pending_execution.push_back(SquashPlan(plan));
66     } catch(TimeOutException tout) { throw tout; }
67     // got a good plan
68     // now load it to the executor
69         return 1;
70 }
71
72 // returns 1 for success, 0 for fail (maybe caller should come back to this node later)
73 // and -1 for 'ran out of time,' in which case the caller should call ContinueSeek again later
74 int KeySeeker::ContinueSeek(StoryNode* root)
75 {
76     ///~~ make this iterative too
77     ResetTimer();
78     // try to achieve the subplot details
79     try {
80         while(!root->visited && attempts++ < MAX_ATTEMPTS)
81             root->visited = Achieve(root->frame);
82     }catch(TimeOutException) { --attempts; return -1; }
83     return root->visited;
84 }
85
86 // returns 1 iff frame and one path of preconds satisfied
87 int KeySeeker::Achieve(KeyFrameNode* frame)
88 {
89         if (!frame) return 1;
90
91     if(frame->conditions.satisfied) return 1;
92     if(frame->attempted == attempts) return 0; // already tried and failed
93
94     // try to achieve a precond (any precond will do, but we search left-to-right)
95     try {
96                 if(frame->preconds.size() == 0)
97                 {
98                         if(PlanAndExecute(frame))
99                         {
100                                 frame->conditions.satisfied = 1;
101                                 return 1;
102                         }
103                 }
104                 else
105                 {
106                         for(int p=0; p<frame->preconds.size(); ++p)
107                         {
108                                 if(Achieve(frame->preconds[p]))
109                                 {
110                                         try{
111                                                 if(PlanAndExecute(frame))
112                                                 {
113                                                         frame->conditions.satisfied = 1;
114                                                         return 1;
115                                                 }
116                                         }catch(TimeOutException pae_to) {
117                                                 frame->attempted = - attempts; // placeholder
118                                                 throw pae_to;
119                                         }
120                                 }
121                         }
122                 }
123         frame->attempted = attempts;
124         return 0; // failed
125     }catch(TimeOutException to) { throw to; }
126 }
127
128 void KeySeeker::ResetAttempteds(KeyFrameNode* frame)
129 {
130         if (!frame) return;
131
132     frame->attempted = 0;
133     for(int i=0; i<frame->preconds.size(); ++i)
134         ResetAttempteds(frame->preconds[i]);
135 }
136
137 int KeySeeker::PlanAndExecute(KeyFrameNode* frame)
138 {
139     try {
140         if(frame->attempted < 0) // some placeholder
141         {
142             frame->attempted = attempts - 1; // undo placeholder
143             PlanNode* plan = NULL;
144             if(RP.ResumePlanning(plan, timer))
145             {   
146                                
147                                 pending_execution.push_back(SquashPlan(plan));
148                                
149                                
150                                 return 1;
151             }
152             else
153                 return 0;
154         }
155         else // first time trying to plan for this frame
156         {
157             PlanNode* plan = NULL;
158             if(RP.MakeNewPlan(frame, plan, timer))
159             {
160                 pending_execution.push_back(SquashPlan(plan));
161                                 return 1;
162             }
163             else
164                 return 0;
165         }
166        
167     }catch(TimeOutException to) { throw to; }
168 }
169
Note: See TracBrowser for help on using the browser.