1 #ifndef GENERICPATROLUNIT_H
2 #define GENERICPATROLUNIT_H
10 template <
class state,
class action,
class environment>
69 void GoToLoc(environment *env,
int which);
99 template <
class state,
class action,
class environment>
116 _r = (double)rand() / RAND_MAX;
117 _g = (double)rand() / RAND_MAX;
118 _b = (double)rand() / RAND_MAX;
120 strncpy(
name,
"GenericPatrolUnit",128);
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)
149 strncpy(
name,
"GenericPatrolUnit",128);
159 template <
class state,
class action,
class environment>
169 if (moves.size() > 0)
173 std::cout <<
"Action: " << dir << std::endl;
175 moves.erase(moves.begin());
179 if (currTarget != -1)
182 if (theEnv->GoalTest(
loc, locs[currTarget]))
184 currTarget = (currTarget+1)%locs.size();
185 if ((numPatrols != -1)&&(currTarget == 1))
188 nodesExpandedPatrols.push_back(nodesExpanded);
190 numFailedMovesPatrols.push_back(numFailedMoves);
192 numDirectionChangesPatrols.push_back(numDirectionChanges);
193 numDirectionChanges = 0;
194 numDirectionChangesCollisionsPatrols.push_back(numDirectionChangesCollisions);
195 numDirectionChangesCollisions = 0;
196 totalDistancePatrols.push_back(totalDistance);
218 if ((numPatrols == -1 ) || (counter < numPatrols))
220 GoToLoc(theEnv, currTarget);
228 if (moves.size() > 0)
231 moves.erase(moves.begin());
240 template <
class state,
class action,
class environment>
243 std::vector<state>
path;
247 algorithm->GetPath(theEnv,
loc, locs[which],
path);
248 nodesExpanded += algorithm->GetNodesExpanded();
249 nodesTouched += algorithm->GetNodesTouched();
254 for (
int i = 0; i <
path.size(); i++)
256 std::cout <<
path[i] <<
", ";
258 std::cout << std::endl;
261 AddPathToCache(theEnv,
path);
265 template <
class state,
class action,
class environment>
272 template <
class state,
class action,
class environment>
276 for (
unsigned int i=0; i<p.size()-1; i++)
278 if (trimPath && (theEnv->HCost(p[i+1],
loc) > trimWindow))
280 moves.push_back(theEnv->GetAction(p[i], p[i+1]));
284 template <
class state,
class action,
class environment>
294 theEnv->SetColor(1.0, 0.25, 0.25, 1.0);
308 theEnv->GLDrawLine(curr, next);
309 for (
unsigned int x = 0; x+1 < moves.size(); x++)
312 theEnv->ApplyAction(next, moves[x]);
313 theEnv->GLDrawLine(curr, next);
331 template <
class state,
class action,
class environment>
343 action dir = theEnv->GetAction(
loc,l);
345 if (!(dir == oldDir))
347 numDirectionChanges++;
350 if (!(dir == oldDirColl))
352 numDirectionChangesCollisions++;
365 totalDistance += theEnv->GCost(
loc, l);
369 template <
class state,
class action,
class environment>
372 if (numPatrols == -1)
376 else if (counter >= numPatrols)
384 template <
class state,
class action,
class environment>
387 if (nodesExpanded != 0)
389 sc->
AddStat(
"nodesExpanded", GetName(), (
long)nodesExpanded);
392 if (nodesTouched != 0)
394 sc->
AddStat(
"nodesTouched", GetName(), (
long)nodesTouched);
397 if (totalDistance != 0)
399 sc->
SumStat(
"gCost", GetName(), totalDistance);
404 template <
class state,
class action,
class environment>
408 for (
unsigned int i=1; ((int)i<=numPatrols)&&(i<nodesExpandedPatrols.size()); i++)
412 char* myname =
new char[256];
413 strcpy(myname,GetName());
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]));