HOG2
GenericSearchUnit.h
Go to the documentation of this file.
1 
27 #include <vector>
28 
29 #include "Unit.h"
30 #include "Map.h"
31 #include "GenericSearchAlgorithm.h"
33 #include "AbsMapUnit.h"
34 //#include "ReservationProvider.h" // occupancy interface - change later
35 //#include "SearchEnvironment.h"
36 
37 #ifndef GENERICSEARCHUNIT_H
38 #define GENERICSEARCHUNIT_H
39 
45 template <class state, class action, class environment>
46 class GenericSearchUnit : public Unit<state,action,environment> {
47 public:
50  virtual ~GenericSearchUnit();
51  virtual const char *GetName() { if (algorithm) return algorithm->GetName(); return "None"; }
53 
54  virtual bool Done() {return (loc == goal);}
55  virtual void GetGoal(state &s) { s=goal;}
56 
59 
60  virtual bool MakeMove(environment *env, OccupancyInterface<state,action> *oi, SimulationInfo<state,action,environment> *si, action& a);
61 
62  virtual void UpdateLocation(environment *env, state &l, bool success, SimulationInfo<state,action,environment> *si);
63 
64  virtual void GetLocation(state& s) {s=loc;}
65  virtual void OpenGLDraw(const environment *, const SimulationInfo<state,action,environment> *) const;
66  //void printRoundStats(FILE *f);
67  void LogStats(StatCollection *stats);
68  void LogFinalStats(StatCollection *stats);
69 
70 // void SetColor(GLfloat _r, GLfloat _g, GLfloat _b) { r=_r; g=_g; b=_b; }
71 // void GetColor(GLfloat& _r, GLfloat& _g, GLfloat& _b) { _r=r; _g=g; _b=b; }
72 protected:
73  virtual void AddPathToCache(environment *env, std::vector<state> &path);
74  bool getCachedMove(action &a);
77  std::vector<action> moves; // states? actions? what?
78  // path *p;
80 
82  //GLfloat r, g, b;
83  state loc, goal, lastloc;
85  bool onTarget;
86 };
87 
88 
89 template <class state, class action, class environment>
91 {
92 
93  loc = start;
94  goal = targ;
95  algorithm = alg;
96  nodesExpanded = 0;
97  nodesTouched = 0;
98 
99  targetTime = 0;
100 
101  target=0;
102 
103  GLfloat _r,_g,_b;
104  _r = (double)rand() / RAND_MAX;
105  _g = (double)rand() / RAND_MAX;
106  _b = (double)rand() / RAND_MAX;
107  this->SetColor(_r,_g,_b);
108 
109 }
110 
111 template <class state, class action, class environment>
113 {
114  target = targ;
115  targ->getLocation(goal);
116 
117  loc = start;
118  algorithm = alg;
119  nodesExpanded = 0;
120  nodesTouched = 0;
121 
122  targetTime = 0;
123 
124 }
125 
126 template <class state, class action, class environment>
128 {
129 }
130 
131 template <class state, class action, class environment>
133 {
134  if (getCachedMove(a))
135  return true;
136 
137  // Check if we have a target defined - i.e. if we're following another unit
138  if (target)
139  {
140  // check if target has moved from goal state - update goal if necessary
141  state targpos;
142  target->GetLocation(targpos);
143  if (~(targpos == goal))
144  goal = targpos;
145  }
146 
147  // find a path to the goal
148 
149  state from = loc;
150  state to = goal;
151 
152  if (from == to)
153  {
154  if (!onTarget)
155  {
156 // if (verbose)
157 // {
158 // printf("STAY ON TARGET!\n");
159 // printf("%p target time %1.4f\n", (void*)this, targetTime);
160 // }
161  if (si)
162  targetTime = si->GetSimulationTime();
163  }
164  onTarget = true;
165  // return kStay ???
166  }
167  else
168  onTarget = false;
169 
170  std::vector<state> path;
171 
172  algorithm->GetPath(theEnv,from, to, path);
173 
174  nodesExpanded+=algorithm->GetNodesExpanded();
175  nodesTouched+=algorithm->GetNodesTouched();
176 
177  // returning an empty path means there is no path between the start and goal
178  if (path.size()==0)
179  {
180 // if (verbose)
181 // printf("SU %s: Path returned NIL\n", this->GetName());
182  return false;
183  //return kStay **************
184  }
185 
186 
187  // a valid path must have at least 2 nodes and start where the unit is located
188  assert((path.size() > 1) && (path[0]==loc));
189 
190  AddPathToCache(theEnv, path);
191 
192  assert(moves.size() > 0);
193 
194  //a = moves.back();
195  //moves.pop_back();
196  a = moves.front();
197  moves.erase(moves.begin());
198  return true;
199 }
200 
201 template <class state, class action, class environment>
203 {
204  // Draw current + goal states as states. May need to find something
205  // different for the goal
206  //printf("Drawing %p at \n", this);
207  {
209  si->GetPublicUnitInfo(si->GetCurrentUnit(), i);
210 
211  GLfloat _r,_g,_b;
212  this->GetColor(_r,_g,_b);
213  theEnv->SetColor(_r, _g, _b);
214 
215  if (fgreater(si->GetSimulationTime(), i.nextTime))
216  theEnv->OpenGLDraw(i.currentState);
217  else
218  theEnv->OpenGLDraw(i.lastState, i.currentState,
219  (si->GetSimulationTime()-i.lastTime)/(i.nextTime-i.lastTime));
220  theEnv->SetColor(1.0, 0.25, 0.25, 0.25);
221 
222  //algorithm->OpenGLDraw();
223 
224  theEnv->SetColor(_r, _g, _b, 0.5);
225  theEnv->OpenGLDraw(goal);
226  }
227 
228  state current = loc;
229  state next;
230 
231  // Draw the cached moves
232  for (unsigned int i=0; i<moves.size(); i++)
233  {
234  theEnv->SetColor(1.0, 0.0, 0.0, 0.5);
235  theEnv->OpenGLDraw(current);
236  //theEnv->OpenGLDraw(current, moves[i]/*,1.0,0,0*/); // draw in red
237  //theEnv->GetNextState(current, moves[i], next);
238  theEnv->ApplyAction(current, moves[i]);
239  }
240 
241 }
242 
243 template <class state, class action, class environment>
245 {
246  if (((nodesExpanded == 0) && (nodesTouched != 0)) ||
247  ((nodesExpanded != 0) && (nodesTouched == 0)))
248  {
249  printf("Error; somehow nodes touched/expanded are inconsistent. t:%d e:%d\n",
250  nodesTouched, nodesExpanded);
251  }
252 
253  if (nodesExpanded != 0)
254  stats->AddStat("nodesExpanded", GetName(), (long)nodesExpanded);
255  if (nodesTouched != 0)
256  stats->AddStat("nodesTouched", GetName(), (long)nodesTouched);
257  nodesExpanded = nodesTouched = 0;
258 }
259 
260 template <class state, class action, class environment>
262 {
263  algorithm->LogFinalStats(stats);
264 }
265 
270 template<class state, class action, class environment>
271 void GenericSearchUnit<state,action,environment>::AddPathToCache(environment *theEnv, std::vector<state> &path)
272 {
273  for (unsigned int i=0; i<path.size()-1; i++)
274  {
275  moves.push_back(theEnv->GetAction(path[i], path[i+1]));
276  }
277 }
278 
279 template<class state, class action, class environment>
281 {
282  if (moves.size() > 0)
283  {
284  a = moves.front();
285  moves.erase(moves.begin());
286  //a = moves.back();
287  //moves.pop_back();
288  return true;
289  }
290  return false;
291 }
292 
293 template<class state, class action, class environment>
295 {
296  // Done in UnitSimulation instead
297  //Update occupancy interface
298  //env->GetOccupancyInterface()->SetStateOccupied(loc,false);
299  //env->GetOccupancyInterface()->SetStateOccupied(l, true);
300 
301  if ((!success) || (l == loc))
302  {
303  moves.resize(0);
304  }
305  //lastLoc = loc;
306  lastTime = si->GetSimulationTime();
307  loc = l;
308 }
309 
310 #endif
Colors::GetColor
rgbColor GetColor(float v, float vmin, float vmax, int type)
Given min/max values, get a color from a color schema.
Definition: Colors.cpp:24
GenericSearchAlgorithm.h
An interface for generic search algorithms.
GenericSearchUnit::goal
state goal
Definition: GenericSearchUnit.h:83
SimulationInfo
Definition: SimulationInfo.h:13
GenericSearchUnit::loc
state loc
Definition: GenericSearchUnit.h:83
GenericSearchUnit::AddPathToCache
virtual void AddPathToCache(environment *env, std::vector< state > &path)
Store path as a vector of actions.
Definition: GenericSearchUnit.h:271
GenericSearchUnit::lastTime
double lastTime
Definition: GenericSearchUnit.h:84
SimulationInfo::GetSimulationTime
virtual double GetSimulationTime() const =0
GenericSearchUnit::MakeMove
virtual bool MakeMove(environment *env, OccupancyInterface< state, action > *oi, SimulationInfo< state, action, environment > *si, action &a)
Definition: GenericSearchUnit.h:132
GenericSearchUnit::nodesTouched
int nodesTouched
Definition: GenericSearchUnit.h:76
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
GenericSearchUnit::getCachedMove
bool getCachedMove(action &a)
Definition: GenericSearchUnit.h:280
GenericSearchAlgorithm
Definition: GenericSearchAlgorithm.h:35
GenericSearchUnit::GetGoal
virtual void GetGoal(state &s)
Definition: GenericSearchUnit.h:55
AbsMapUnit.h
loc
Definition: MapGenerators.cpp:296
GenericSearchUnit::~GenericSearchUnit
virtual ~GenericSearchUnit()
Definition: GenericSearchUnit.h:127
GenericSearchUnit::GetLocation
virtual void GetLocation(state &s)
Definition: GenericSearchUnit.h:64
GenericSearchUnit::LogStats
void LogStats(StatCollection *stats)
log an stats that may have been computed during the last run
Definition: GenericSearchUnit.h:244
GenericSearchUnit::nodesExpanded
int nodesExpanded
Definition: GenericSearchUnit.h:75
GenericSearchUnit::onTarget
bool onTarget
Definition: GenericSearchUnit.h:85
PublicUnitInfo::lastState
state lastState
Definition: SimulationInfo.h:62
GenericSearchUnit::target
Unit< state, action, environment > * target
Definition: GenericSearchUnit.h:81
GenericSearchUnit::GetTarget
virtual Unit< state, action, environment > * GetTarget()
Definition: GenericSearchUnit.h:58
GenericSearchUnit::LogFinalStats
void LogFinalStats(StatCollection *stats)
log any final one-time stats before a simulation is ended
Definition: GenericSearchUnit.h:261
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
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
GenericSearchUnit::OpenGLDraw
virtual void OpenGLDraw(const environment *, const SimulationInfo< state, action, environment > *) const
Definition: GenericSearchUnit.h:202
GenericSearchUnit::moves
std::vector< action > moves
Definition: GenericSearchUnit.h:77
GenericSearchUnit::GetName
virtual const char * GetName()
Definition: GenericSearchUnit.h:51
GenericSearchUnit::GenericSearchUnit
GenericSearchUnit(state &start, state &goal, GenericSearchAlgorithm< state, action, environment > *alg)
Definition: GenericSearchUnit.h:90
GenericSearchUnit::targetTime
double targetTime
Definition: GenericSearchUnit.h:84
GenericSearchUnit::SetTarget
virtual void SetTarget(Unit< state, action, environment > *u)
Definition: GenericSearchUnit.h:57
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
GenericSearchUnit::getAlgorithm
virtual GenericSearchAlgorithm< state, action, environment > * getAlgorithm()
Definition: GenericSearchUnit.h:52
PublicUnitInfo::lastTime
double lastTime
Definition: SimulationInfo.h:65
SimulationInfo::GetCurrentUnit
virtual unsigned int GetCurrentUnit() const =0
Map.h
path
A linked list of nodes which form a continuous path.
Definition: Path.h:20
GenericSearchUnit::Done
virtual bool Done()
Definition: GenericSearchUnit.h:54
PublicUnitInfo
Definition: SimulationInfo.h:9
GenericSearchUnit::UpdateLocation
virtual void UpdateLocation(environment *env, state &l, bool success, SimulationInfo< state, action, environment > *si)
Definition: GenericSearchUnit.h:294
SpreadExecSearchAlgorithm.h
GenericSearchUnit::algorithm
GenericSearchAlgorithm< state, action, environment > * algorithm
Definition: GenericSearchUnit.h:79
GenericSearchUnit::lastloc
state lastloc
Definition: GenericSearchUnit.h:83
Unit.h
Unit
Definition: Unit.h:23
OccupancyInterface
Definition: OccupancyInterface.h:36