HOG2
GenericPatrolUnit.h
Go to the documentation of this file.
1 #ifndef GENERICPATROLUNIT_H
2 #define GENERICPATROLUNIT_H
3 
4 #include <cstdlib> // for random number
5 #include "Unit.h"
6 #include "GenericSearchUnit.h"
7 #include "ReservationProvider.h"
8 //#include "SearchEnvironment.h"
9 
10 template <class state, class action, class environment>
11 class GenericPatrolUnit : public GenericSearchUnit<state,action, environment> {
12 public:
14  GenericPatrolUnit(state &s, GenericSearchAlgorithm<state,action,environment>* alg, GLfloat _r, GLfloat _g, GLfloat _b);
15  virtual const char *GetName() { return name; }
16  void SetName(char* myname) { strncpy(name, myname,128); }
17  virtual void GetLocation(state& s) { s=loc;}
18  virtual bool MakeMove(environment *env, OccupancyInterface<state,action> *, SimulationInfo<state,action,environment> *si, action &dir);
19 
20  virtual void OpenGLDraw(const environment *, const SimulationInfo<state,action,environment> *) const;
21  void AddPatrolLocation(state &s);
22  state& GetGoal(); // get CURRENT goal?
24  virtual bool Done();
25  void UpdateLocation(environment *, state &l, bool success, SimulationInfo<state,action,environment> *si);
26  void LogStats(StatCollection *stats);
27  void LogFinalStats(StatCollection *stats);
28  void SetNumPatrols(int num) {numPatrols = num;}
29 
35  void SetTrimPath(bool trim) {trimPath = trim;}
36 
45  void SetTrimWindow(double d) {trimWindow = d;}
46 
53  void SetDrawUnit(bool draw) {drawUnit = draw;}
54 // void SetColor(GLfloat _r, GLfloat _g, GLfloat _b) {Unit::Set GLFloat _r,_g,_b;
55 // GetColor(_r,_g,_b);
56 // glColor3f(_r,_g,_b);this->r=_r; this->g=_g; this->b=_b;}
57 
58 //private:
59 private:
60  //GLfloat r, g, b;
61  state loc;
63  int counter;
64  action oldDir, oldDirColl;
65  double totalDistance;
67 
68  // this used to return path cost, but that wasn't used...
69  /*double*/ void GoToLoc(environment *env, int which);
70  void AddPathToCache(environment *env, std::vector<state> &path);
71  std::vector<action> moves;
72  std::vector<state> locs;
73 
75 
82 
83 
84  std::vector<int> nodesExpandedPatrols;
85  std::vector<int> nodesTouchedPatrols;
86  std::vector<int> numFailedMovesPatrols;
87  std::vector<int> numDirectionChangesPatrols;
89  std::vector<double> totalDistancePatrols;
90 
91  char name[128];
92 
93  bool trimPath;
94  double trimWindow;
95 
96  bool drawUnit;
97 };
98 
99 template <class state, class action, class environment>
101 :GenericSearchUnit<state,action,environment>(s,s,alg)
102 {
103  lastFailedMove = -10;
104  counter = 0;
105  numPatrols = -1; // infinitely patrol
106 
107  locs.push_back(s);
108  loc = s;
109  //setObjectType(kWorldObject);
110  currTarget = -1;
113  algorithm = alg;
114 
115  GLfloat _r,_g,_b;
116  _r = (double)rand() / RAND_MAX;
117  _g = (double)rand() / RAND_MAX;
118  _b = (double)rand() / RAND_MAX;
119  this->SetColor(_r,_g,_b);
120  strncpy(name, "GenericPatrolUnit",128);
121 
122  totalDistance = 0;
123  oldDir = oldDirColl;// = kStay;
124 
125  trimPath = false;
126  trimWindow = 5;
127  drawUnit = true;
128 }
129 
130 template <class state, class action, class environment>
131 GenericPatrolUnit<state,action,environment>::GenericPatrolUnit(state &s, GenericSearchAlgorithm<state,action,environment>* alg, GLfloat _r, GLfloat _g, GLfloat _b):GenericSearchUnit<state,action,environment>(s,s,alg)
132 {
133  lastFailedMove = -10;
134  counter = 0;
135  numPatrols = -1; // infinitely patrol
136 
137  locs.push_back(s);
138  loc = s;
139 
140  currTarget = -1;
143  algorithm = alg;
144 
145  this->r = _r;
146  this->g = _g;
147  this->b = _b;
148 
149  strncpy(name, "GenericPatrolUnit",128);
150 
151  totalDistance = 0;
152  oldDir = oldDirColl = kStay;
153 
154  trimPath = false;
155  trimWindow = 5;
156  drawUnit = true;
157 }
158 
159 template <class state, class action, class environment>
161 {
162  if (Done())
163  {
164  //printf("Done is true (%d)\n", Done());
165  return false;
166  }
167 
168  // Return a stored move
169  if (moves.size() > 0)
170  {
171  //dir = moves.back();
172  //moves.pop_back();
173  std::cout << "Action: " << dir << std::endl;
174  dir = moves.front();
175  moves.erase(moves.begin());
176  return true;
177  }
178 
179  if (currTarget != -1)
180  {
181  // update current target and patrol counter if we're at our target location
182  if (theEnv->GoalTest(loc, locs[currTarget]))
183  {
184  currTarget = (currTarget+1)%locs.size();
185  if ((numPatrols != -1)&&(currTarget == 1))
186  {
187  //log stats for this patrol & reset
188  nodesExpandedPatrols.push_back(nodesExpanded);
189  nodesExpanded = 0;
190  numFailedMovesPatrols.push_back(numFailedMoves);
191  numFailedMoves = 0;
192  numDirectionChangesPatrols.push_back(numDirectionChanges);
193  numDirectionChanges = 0;
194  numDirectionChangesCollisionsPatrols.push_back(numDirectionChangesCollisions);
195  numDirectionChangesCollisions = 0;
196  totalDistancePatrols.push_back(totalDistance);
197  totalDistance = 0;
198 
199  counter++;
200  }
201  }
202 
203  // If we're right beside our goal and it's blocked, make a random move
204 // else if (env->GetAction(locs[currTarget], loc) != kTeleport)// &&
205 // //(env->GetAction(locs[currTarget],loc) != kStay))
206 // {
207 // if (env->GetOccupancyInfo()->GetStateOccupied(locs[currTarget]))
208 // {
210 // srand(time(0));
211 // std::vector<tDirection> actions;
212 // env->GetActions(loc, actions);
213 // dir = actions[(rand())%(actions.size())];
214 // return true;
215 // }
216 // }
217 
218  if ((numPatrols == -1 ) || (counter < numPatrols))
219  {
220  GoToLoc(theEnv, currTarget);
221  }
222  else
223  {
224  //std::cout<<"else\n";
225  return false;
226  }
227 
228  if (moves.size() > 0)
229  {
230  dir = moves.front();
231  moves.erase(moves.begin());
232  return true;
233  }
234  }
235  //Stay where you are
236  //std::cout<<"at end\n";
237  return false;
238 }
239 
240 template <class state, class action, class environment>
241 void GenericPatrolUnit<state,action,environment>::GoToLoc(environment *theEnv, int which)
242 {
243  std::vector<state> path;
244 
245 // std::cout << "Planning between " << loc << " and " << locs[which] << std::endl;
246  //printf("%s ",GetName());
247  algorithm->GetPath(theEnv, loc, locs[which], path);
248  nodesExpanded += algorithm->GetNodesExpanded();
249  nodesTouched += algorithm->GetNodesTouched();
250 // std::cout << algorithm->GetNodesExpanded() << " nodes expanded\n";
251 // std::cout << "Path length: " << theEnv->GetPathLength(path) << " (" << path.size() << " moves)" << std::endl;
252 // std::cout << "Initial Heuristic: " << theEnv->HCost(loc, locs[which]) << std::endl;
253 // // std::cout<<"Path ";
254  for (int i = 0; i < path.size(); i++)
255  {
256  std::cout << path[i] << ", ";
257  }
258  std::cout << std::endl;
259  if (path.size() > 0)
260  {
261  AddPathToCache(theEnv, path);
262  }
263 }
264 
265 template <class state, class action, class environment>
267 {
268  currTarget = 1;
269  locs.push_back(s);
270 }
271 
272 template <class state, class action, class environment>
273 void GenericPatrolUnit<state,action,environment>::AddPathToCache(environment* theEnv, std::vector<state> &p)
274 {
275  moves.resize(0);
276  for (unsigned int i=0; i<p.size()-1; i++)
277  {
278  if (trimPath && (theEnv->HCost(p[i+1], loc) > trimWindow))
279  break;
280  moves.push_back(theEnv->GetAction(p[i], p[i+1]));
281  }
282 }
283 
284 template <class state, class action, class environment>
286 {
287  if (drawUnit)
288  {
289  //printf("Drawing %p at \n", this);
291  si->GetPublicUnitInfo(si->GetCurrentUnit(), i);
292 // printf("(%f-%f)/(%f-%f)\n",
293 // si->GetSimulationTime(), i.lastTime, i.nextTime, i.lastTime);
294  theEnv->SetColor(1.0, 0.25, 0.25, 1.0);
295 // std::cout << si->GetCurrentUnit() << " is at " << i.currentState << std::endl;
296  if (fgreater(si->GetSimulationTime(), i.nextTime))
297  theEnv->OpenGLDraw(i.currentState);
298  else
299  theEnv->OpenGLDraw(i.lastState, i.currentState,
300  (si->GetSimulationTime()-i.lastTime)/(i.nextTime-i.lastTime));
301 // theEnv->SetColor(1.0, 0.25, 0.25, 0.25);
302 // algorithm->OpenGLDraw();
303 
304  // draw path
305  state curr = i.lastState;
306  state next = i.currentState;
307  glLineWidth(4.0);
308  theEnv->GLDrawLine(curr, next);
309  for (unsigned int x = 0; x+1 < moves.size(); x++)
310  {
311  curr = next;
312  theEnv->ApplyAction(next, moves[x]);
313  theEnv->GLDrawLine(curr, next);
314  }
315  glLineWidth(1.0);
316  }
317 // if ((0)&&(drawUnit))
318 // {
319 //
320 // state current = loc;
321 // state next;
322 // for (unsigned int i=0; i<moves.size(); i++)
323 // {
324 // env->OpenGLDraw(current, moves[i]/*,1.0,0,0*/); // draw in red
325 // env->GetNextState(current, moves[i], next);
326 // current = next;
327 // }
328 // }
329 }
330 
331 template <class state, class action, class environment>
333 {
334  if (!success)
335  {
336  //printf("%s ",GetName());
337  //std::cout<<"Move failed\n";
338  lastFailedMove = si->GetSimulationTime();
339  }
340 
341  if (!(l==loc))
342  {
343  action dir = theEnv->GetAction(loc,l);
344 
345  if (!(dir == oldDir))
346  {
347  numDirectionChanges++;
348  oldDir = dir;
349  }
350  if (!(dir == oldDirColl))
351  {
352  numDirectionChangesCollisions++;
353  oldDirColl = dir;
354  }
355  }
356 
357  if ((!success))//||(l == loc))
358  {
359  numFailedMoves++;
360  moves.resize(0);
361  //oldDirColl = kStay;
362  //if (currTarget != -1)
363  // currTarget = 0;
364  }
365  totalDistance += theEnv->GCost(loc, l);
366  loc = l;
367 }
368 
369 template <class state, class action, class environment>
371 {
372  if (numPatrols == -1)
373  {
374  return false;
375  }
376  else if (counter >= numPatrols)
377  {
378  //printf("Done\n");
379  return true;
380  }
381  return false;
382 }
383 
384 template <class state, class action, class environment>
386 {
387  if (nodesExpanded != 0)
388  {
389  sc->AddStat("nodesExpanded", GetName(), (long)nodesExpanded);
390  nodesExpanded = 0;
391  }
392  if (nodesTouched != 0)
393  {
394  sc->AddStat("nodesTouched", GetName(), (long)nodesTouched);
395  nodesExpanded = 0;
396  }
397  if (totalDistance != 0)
398  {
399  sc->SumStat("gCost", GetName(), totalDistance);
400  totalDistance = 0;
401  }
402 }
403 
404 template <class state, class action, class environment>
406 {
407  //Report per loop
408  for (unsigned int i=1; ((int)i<=numPatrols)&&(i<nodesExpandedPatrols.size()); i++)
409  {
410  char num[8];
411  sprintf(num,"%d",i);
412  char* myname = new char[256];
413  strcpy(myname,GetName());
414  strcat(myname,"_");
415  strcat(myname, num);
416 
417  sc->AddStat("nodesExpanded", myname, (long)(nodesExpandedPatrols[i-1]));
418  sc->AddStat("distanceTravelled", myname, totalDistancePatrols[i-1]);
419  sc->AddStat("directionChanges",myname, (long)(numDirectionChangesPatrols[i-1]));
420  sc->AddStat("directionChangesCollision",myname, (long)(numDirectionChangesCollisionsPatrols[i-1]));
421  sc->AddStat("failedMoves",myname, (long)(numFailedMovesPatrols[i-1]));
422  }
423 
424 
425  //Want:
426  // * nodesExpanded
427  // * distance travelled
428  // * failedMoves
429  // * change in direction
430 
431 /* sc->AddStat("distanceTravelled", GetName(), totalDistance);
432  sc->AddStat("directionChanges",GetName(), (long)(numDirectionChanges));
433  sc->AddStat("nodesExpanded", GetName(), (long)(nodesExpanded));
434  sc->AddStat("directionChangesCollision",GetName(), (long)(numDirectionChangesCollisions));
435  sc->AddStat("failedMoves",GetName(), (long)(numFailedMoves));*/
436 
437 }
438 
439 #endif
GenericPatrolUnit
Definition: GenericPatrolUnit.h:11
GenericPatrolUnit::nodesExpandedPatrols
std::vector< int > nodesExpandedPatrols
Definition: GenericPatrolUnit.h:84
GenericPatrolUnit::totalDistance
double totalDistance
Definition: GenericPatrolUnit.h:65
GenericPatrolUnit::totalDistancePatrols
std::vector< double > totalDistancePatrols
Definition: GenericPatrolUnit.h:89
GenericPatrolUnit::trimWindow
double trimWindow
Definition: GenericPatrolUnit.h:94
GenericPatrolUnit::nodesTouchedPatrols
std::vector< int > nodesTouchedPatrols
Definition: GenericPatrolUnit.h:85
SimulationInfo
Definition: SimulationInfo.h:13
GenericPatrolUnit::counter
int counter
Definition: GenericPatrolUnit.h:63
GenericPatrolUnit::GenericPatrolUnit
GenericPatrolUnit(state &s, GenericSearchAlgorithm< state, action, environment > *alg)
Definition: GenericPatrolUnit.h:100
GenericPatrolUnit::oldDirColl
action oldDirColl
Definition: GenericPatrolUnit.h:64
GenericPatrolUnit::oldDir
action oldDir
Definition: GenericPatrolUnit.h:64
Unit::SetColor
virtual void SetColor(GLfloat _r, GLfloat _g, GLfloat _b)
Definition: Unit.h:44
SimulationInfo::GetSimulationTime
virtual double GetSimulationTime() const =0
GenericPatrolUnit::lastFailedMove
double lastFailedMove
Definition: GenericPatrolUnit.h:66
Unit::r
GLfloat r
Definition: Unit.h:86
d
mcData d[]
Definition: MotionCaptureMovement.cpp:21
GenericPatrolUnit::algorithm
GenericSearchAlgorithm< state, action, environment > * algorithm
Definition: GenericPatrolUnit.h:74
GenericPatrolUnit::numDirectionChangesCollisionsPatrols
std::vector< int > numDirectionChangesCollisionsPatrols
Definition: GenericPatrolUnit.h:88
GenericSearchUnit
A general unit which collects path information from a GenericSearchAlgorithm and incrementally execut...
Definition: GenericSearchUnit.h:46
PublicUnitInfo::nextTime
double nextTime
Definition: SimulationInfo.h:66
PublicUnitInfo::currentState
state currentState
Definition: SimulationInfo.h:61
Unit::g
GLfloat g
Definition: Unit.h:86
GenericPatrolUnit::GetName
virtual const char * GetName()
Definition: GenericPatrolUnit.h:15
GenericPatrolUnit::LogFinalStats
void LogFinalStats(StatCollection *stats)
log any final one-time stats before a simulation is ended
Definition: GenericPatrolUnit.h:405
GenericPatrolUnit::MakeMove
virtual bool MakeMove(environment *env, OccupancyInterface< state, action > *, SimulationInfo< state, action, environment > *si, action &dir)
Definition: GenericPatrolUnit.h:160
GenericPatrolUnit::Done
virtual bool Done()
Definition: GenericPatrolUnit.h:370
GenericPatrolUnit::GetGoal
state & GetGoal()
GenericPatrolUnit::SetName
void SetName(char *myname)
Definition: GenericPatrolUnit.h:16
GenericPatrolUnit::locs
std::vector< state > locs
Definition: GenericPatrolUnit.h:72
GenericSearchAlgorithm
Definition: GenericSearchAlgorithm.h:35
GenericSearchUnit::GetGoal
virtual void GetGoal(state &s)
Definition: GenericSearchUnit.h:55
GenericPatrolUnit::numFailedMoves
int numFailedMoves
Definition: GenericPatrolUnit.h:79
GenericPatrolUnit::trimPath
bool trimPath
Definition: GenericPatrolUnit.h:93
kStay
@ kStay
Definition: Map2DEnvironment.h:79
GenericPatrolUnit::numDirectionChangesPatrols
std::vector< int > numDirectionChangesPatrols
Definition: GenericPatrolUnit.h:87
GenericPatrolUnit::nodesExpanded
int nodesExpanded
Definition: GenericPatrolUnit.h:77
loc
Definition: MapGenerators.cpp:296
StatCollection::SumStat
void SumStat(const char *category, const char *owner, double value)
Given stats for the category and owner, find an existing stat (chronologically backwards search) with...
Definition: StatCollection.cpp:109
GenericSearchUnit.h
GenericPatrolUnit::numDirectionChangesCollisions
int numDirectionChangesCollisions
Definition: GenericPatrolUnit.h:81
GenericPatrolUnit::currTarget
int currTarget
Definition: GenericPatrolUnit.h:76
PublicUnitInfo::lastState
state lastState
Definition: SimulationInfo.h:62
GenericPatrolUnit::moves
std::vector< action > moves
Definition: GenericPatrolUnit.h:71
GenericPatrolUnit::loc
state loc
Definition: GenericPatrolUnit.h:61
GenericPatrolUnit::LogStats
void LogStats(StatCollection *stats)
log an stats that may have been computed during the last run
Definition: GenericPatrolUnit.h:385
fgreater
bool fgreater(double a, double b)
Definition: FPUtil.h:29
SimulationInfo::GetPublicUnitInfo
virtual void GetPublicUnitInfo(unsigned int unitnum, PublicUnitInfo< state, action, environment > &) const =0
GenericPatrolUnit::GoToLoc
void GoToLoc(environment *env, int which)
Definition: GenericPatrolUnit.h:241
GenericPatrolUnit::OpenGLDraw
virtual void OpenGLDraw(const environment *, const SimulationInfo< state, action, environment > *) const
Definition: GenericPatrolUnit.h:285
path
std::vector< xyLoc > path
Definition: Sample.cpp:43
StatCollection
The StatCollection class is for collecting stats across different parts of the simulation.
Definition: StatCollection.h:34
GenericPatrolUnit::numFailedMovesPatrols
std::vector< int > numFailedMovesPatrols
Definition: GenericPatrolUnit.h:86
ReservationProvider.h
GenericPatrolUnit::numPatrols
int numPatrols
Definition: GenericPatrolUnit.h:62
GenericPatrolUnit::name
char name[128]
Definition: GenericPatrolUnit.h:91
GenericPatrolUnit::GetGoal
void GetGoal(state &s)
Definition: GenericPatrolUnit.h:23
GenericPatrolUnit::UpdateLocation
void UpdateLocation(environment *, state &l, bool success, SimulationInfo< state, action, environment > *si)
Definition: GenericPatrolUnit.h:332
StatCollection::AddStat
void AddStat(const char *category, const char *owner, double value)
Add a new stat entry for the given category, owner and value.
Definition: StatCollection.cpp:67
PublicUnitInfo::lastTime
double lastTime
Definition: SimulationInfo.h:65
SimulationInfo::GetCurrentUnit
virtual unsigned int GetCurrentUnit() const =0
GenericPatrolUnit::numDirectionChanges
int numDirectionChanges
Definition: GenericPatrolUnit.h:80
GenericPatrolUnit::SetTrimPath
void SetTrimPath(bool trim)
Set whether we want to trim the planned path.
Definition: GenericPatrolUnit.h:35
GenericPatrolUnit::SetTrimWindow
void SetTrimWindow(double d)
Set the window at which we want to trim the planned path.
Definition: GenericPatrolUnit.h:45
path
A linked list of nodes which form a continuous path.
Definition: Path.h:20
PublicUnitInfo
Definition: SimulationInfo.h:9
GenericPatrolUnit::AddPatrolLocation
void AddPatrolLocation(state &s)
Definition: GenericPatrolUnit.h:266
GenericPatrolUnit::SetDrawUnit
void SetDrawUnit(bool draw)
Set whether we want to draw the unit.
Definition: GenericPatrolUnit.h:53
GenericPatrolUnit::SetNumPatrols
void SetNumPatrols(int num)
Definition: GenericPatrolUnit.h:28
GenericPatrolUnit::drawUnit
bool drawUnit
Definition: GenericPatrolUnit.h:96
GenericPatrolUnit::AddPathToCache
void AddPathToCache(environment *env, std::vector< state > &path)
Store path as a vector of actions.
Definition: GenericPatrolUnit.h:273
Unit::b
GLfloat b
Definition: Unit.h:86
GenericPatrolUnit::nodesTouched
int nodesTouched
Definition: GenericPatrolUnit.h:78
Unit.h
OccupancyInterface
Definition: OccupancyInterface.h:36
GenericPatrolUnit::GetLocation
virtual void GetLocation(state &s)
Definition: GenericPatrolUnit.h:17