HOG2
EpisodicSimulation.h
Go to the documentation of this file.
1 /*
2  * $Id: EpisodicSimulation.h
3  * hog2
4  *
5  * Created by Nathan Sturtevant on 12/17/04.
6  * Modified by Nathan Sturtevant on 02/29/20.
7  *
8  * This file is part of HOG2. See https://github.com/nathansttt/hog2 for licensing information.
9  *
10  */
11 
12 #ifndef UNITRACESIMULATION_H
13 #define UNITRACESIMULATION_H
14 
15 #include "UnitSimulation.h"
16 
26 };
27 
28 template<class state, class action, class environment>
29 class EpisodicSimulation : public UnitSimulation<state, action, environment> {
30 public:
31  EpisodicSimulation(environment *e)
32  :UnitSimulation<state, action, environment>(e)
33  {
34  stopOnConvergence = true;
35  currRound = 0;
36  //setUseBlocking(false);
37  allRacesDone = false;
38  useTravelLimit = false;
39  useMaxRounds = false;
40  targetTolerance = 0.0;
41  disjunctiveTrialEnd = false;
42  verbose = false;
43  }
44  virtual ~EpisodicSimulation() {}
45  int AddUnit(Unit<state, action, environment> *u, double timeOffset=0.)
46  {
47  //std::cout<<"ADDING RACING\n";allRacesDone = false;
49  if (val >= (int)racingInfo.size())
50  racingInfo.resize(val+1);
51  racingInfo[val] = true;
52  return val;
53  }
54  int AddNonRacingUnit(Unit<state, action, environment> *u, double timeOffset=0.)
55  {
56  //std::cout<<"ADDING NONRACING"<<std::endl;
57  allRacesDone = false;
59  if (val >= (int)racingInfo.size())
60  racingInfo.resize(val+1);
61  racingInfo[val] = false;
62  return val;
63  }
64 
65  virtual bool Done() { return allRacesDone; }
66  void SetStopOnConvergence(bool stop) { stopOnConvergence = stop; }
67  void SetTargetTolerance(double x) { targetTolerance = x; }
68  double GetTargetTolerance() { return targetTolerance; }
69  void SetDisjunctiveTrialEnd(bool value) { disjunctiveTrialEnd = value; }
70  //void SetUseSameStart(bool val) { sameStart = val; }
71  int GetCurrRound() { return currRound; }
72  void SetTravelLimit(double lim) { useTravelLimit = true; travelLimit = lim; }
73  void SetTrialLimit(long maxTrials) { useMaxRounds = true; maxRounds = maxTrials; }
74  void DisableTravelLimit() { useTravelLimit = false; }
75  virtual void ClearAllUnits()
77 
78 protected:
79  virtual void DoPreTimestepCalc()
80  {
81  if (allRacesDone)
82  return;
83 
84  if (verbose)
85  printf("EpisodicSimulation::doPreTimestepCalc checking onTarget\n");
86 
87  bool trialEnd;
88 
89  for (unsigned int t = 0; t < this->units.size(); t++)
90  if (this->units[t]->agent->Done())
91  this->units[t]->converged = true;
92 
93  // Trial end depends on the mode we are in
95  {
96  // In the default conjunctive mode a trial ends when all
97  // racing units reach their targets
98  trialEnd = true;
99  // check to see if all units are on top of their target.
100  // we need to modify this if we are ever to use blocking.
101  // (or unblock units whenver they reach their target?)
102  for (unsigned int t = 0; t < this->units.size(); t++)
103  {
104  if (verbose) printf("Check unit %d (%s)\n", t, this->units[t]->agent->GetName());
105 
106  if (!IsUnitRacing(this->units[t]))
107  continue;
108 
109  if (verbose) printf("Unit %d is racing, check if it is on target\n",t);
110 
111  if (!UnitOnTarget(this->units[t]))
112  {
113  if (verbose) printf("Unit %d is not on target, break\n",t);
114  trialEnd = false;
115  break;
116  }
117  }
118  }
119  else {
120  // In the disjunctive mode a trial ends when at least
121  // one racing unit reaches its target
122  trialEnd = false;
123  // check to see if all units are on top of their target.
124  // we need to modify this if we are ever to use blocking.
125  // (or unblock units whenver they reach their target?)
126  for (unsigned int t = 0; t < this->units.size(); t++)
127  {
128  if (verbose) printf("Check unit %d (%s)\n", t, this->units[t]->agent->GetName());
129 
130  if (!IsUnitRacing(this->units[t]))
131  continue;
132 
133  if (verbose) printf("Unit %d is racing, check if it is on target\n",t);
134 
135  if (UnitOnTarget(this->units[t]))
136  {
137  if (verbose) printf("Unit %d is on target, break\n",t);
138  trialEnd = true;
139  break;
140  }
141  }
142  }
143  // if all units are on their target, then start the next round
144  if (!trialEnd)
145  {
146  return;
147  }
148 
149  // we only get into this code if all units are on target...
150 
151  // Go through all racing units and output stats:
152  // - on the distance traveled
153  // - on whether the target was actually reached
154  for (unsigned int t = 0; t < this->units.size(); t++)
155  {
156  if (IsUnitRacing(this->units[t]))
157  {
158  this->stats.AddStat("trialDistanceMoved", this->units[t]->agent->GetName(),
159  (double) this->units[t]->totalDistance);
160  this->stats.AddStat("reachedTarget", this->units[t]->agent->GetName(),
161  (long) (UnitOnTargetStatus(this->units[t]) == kReachedTarget));
162  }
163  }
164 
165  if (!allRacesDone)
166  {
167  this->stats.AddStat("Trial End", "Race Simulation", (long)currRound);
168  if (verbose)
169  {
170  for (unsigned int t = 0; t < this->units.size(); t++)
171  {
172  if (!IsUnitRacing(this->units[t]))
173  continue;
174  std::cout << "Round ended, moving " << t << " (" << this->units[t]->agent->GetName()
175  << ") back to " << this->units[t]->startState << std::endl;
176  }
177  }
178  }
179 
180  // the unit simulation tells us if all groups are done
181  if ((this->EpisodeDone() && /*sameStart &&*/ stopOnConvergence) ||
183  {
184  for (unsigned int t = 0; t < this->units.size(); t++)
185  {
186  if (IsUnitRacing(this->units[t]))
187  this->units[t]->agent->LogFinalStats(&this->stats);
188  }
189 
190  if (verbose) printf("All trials finished; last trial: %d\n", currRound);
191  allRacesDone = true;
192  return;
193  }
194 
195  allRacesDone = false;
196  if (verbose)
197  printf("Continuing trial: %d\n", currRound);
198 
199  // call void StartNewTrial(); on all groups
200  for (unsigned int t = 0; t < this->unitGroups.size(); t++)
201  {
202  this->unitGroups[t]->StartNewTrial(&this->stats);
203  }
204 
205  // call set location on all units back to their starting location
206  // set unit times to the current time
207  for (unsigned int t = 0; t < this->units.size(); t++)
208  {
209  // check if the unit is racing. If not then don't reset it
210  if (!IsUnitRacing(this->units[t]))
211  continue;
212 
213  this->units[t]->totalThinking = 0;
214  this->units[t]->totalDistance = 0;
215 
216  if (this->units[t]->converged) // converged units don't need to reset, except dist travelled
217  continue;
218 
219  // this lets each trial have its own time
220  this->stats.AddStat("MakeMoveThinkingTime", this->units[t]->agent->GetName(), (double)0);
221  // stats.AddStat("distanceMoved", units[t]->agent->GetName(), (double)0);
222 // if (this->units[t]->blocking)
223 // {
224 // bv->Set(this->units[t]->curry*map_width+units[t]->currx, 0);
225 // bv->Set(this->units[t]->starty*map_width+units[t]->startx, 1);
226 // }
227 #pragma mark ---
228  // TODO: if occupancy interface is used we MUST reset the location
229  this->units[t]->currentState = this->units[t]->startState;
230  this->units[t]->agent->GetUnitGroup()->UpdateLocation(this->units[t]->agent, this->env, this->units[t]->startState, false, this);
231  this->units[t]->nextTime = this->currTime;
232  }
233  currRound++;
234  // randomize order -- only matters if they are blocking each other
235  }
236 
237  virtual void DoTimestepCalc(double amount)
238  {
239  if (!allRacesDone)
241  }
242 
243  virtual bool EpisodeDone() // this is wrong...should be asking units about learning
244  {
245  //return false;
246  for (unsigned int t = 0; (t < this->units.size()); t++)
247  {
248 // state loc, goaloc;
249 // this->units[t]->agent->GetLocation(loc);
250 // this->units[t]->agent->GetGoal(goaloc);
251  if (!this->units[t]->agent->Done())
252  return false;
253 // if (!(loc == goaloc))
254 // return false;
255  }
256  return true;
257  }
258 
260  {
261  state s1, s2;
262  //Unit<state, action, environment> *target = ;
263  //if (verbose) printf("Unit %s, its target %s\n", u->agent->GetName(), target->GetName());
264 
266 
267  u->agent->GetLocation(s1);
268  u->agent->GetGoal(s2);
269  if (s1 == s2)
270  return kReachedTarget;
271 
272 // if (!fgreater(this->env->GCost(s1, s2), targetTolerance))
273 // return kReachedTarget;
274 
275  return kNotOnTarget;
276  }
277 
279  {
280  switch (UnitOnTargetStatus(u)) {
281  case kNoTarget: case kOutOfTravel: case kReachedTarget: return true;
282  case kNotOnTarget: return false;
283  default: assert(false);
284  }
285  return false;
286  }
287 
289  {
290  return racingInfo[u->agent->GetNum()];
291 // return true;
292 // return (!u->ignoreOnTarget &&
293 // (u->agent->GetGoal() != NULL));
294  }
295 
298  double travelLimit;
301  long maxRounds;
305  bool verbose;
306  std::vector<bool> racingInfo;
307 };
308 
309 #endif
UnitSimulation.h
EpisodicSimulation::IsUnitRacing
bool IsUnitRacing(UnitInfo< state, action, environment > *u)
Definition: EpisodicSimulation.h:288
UnitSimulation::stats
StatCollection stats
Definition: UnitSimulation.h:146
EpisodicSimulation::ClearAllUnits
virtual void ClearAllUnits()
Definition: EpisodicSimulation.h:75
EpisodicSimulation::currRound
int currRound
Definition: EpisodicSimulation.h:297
UnitInfo::totalDistance
double totalDistance
Definition: UnitSimulation.h:60
EpisodicSimulation::useMaxRounds
bool useMaxRounds
Definition: EpisodicSimulation.h:302
tUnitOnTargetStatus
tUnitOnTargetStatus
A class where units repeatedly path from a start to a goal until they have stopped exploring/learning...
Definition: EpisodicSimulation.h:24
UnitSimulation::DoTimestepCalc
virtual void DoTimestepCalc(double amount)
Definition: UnitSimulation.h:343
EpisodicSimulation
Definition: EpisodicSimulation.h:29
EpisodicSimulation::useTravelLimit
bool useTravelLimit
Definition: EpisodicSimulation.h:299
EpisodicSimulation::DoTimestepCalc
virtual void DoTimestepCalc(double amount)
Definition: EpisodicSimulation.h:237
EpisodicSimulation::SetDisjunctiveTrialEnd
void SetDisjunctiveTrialEnd(bool value)
Definition: EpisodicSimulation.h:69
EpisodicSimulation::verbose
bool verbose
Definition: EpisodicSimulation.h:305
UnitSimulation::env
environment * env
Definition: UnitSimulation.h:143
EpisodicSimulation::allRacesDone
bool allRacesDone
Definition: EpisodicSimulation.h:296
EpisodicSimulation::stopOnConvergence
bool stopOnConvergence
Definition: EpisodicSimulation.h:300
UnitSimulation::ClearAllUnits
virtual void ClearAllUnits()
Definition: UnitSimulation.h:243
EpisodicSimulation::racingInfo
std::vector< bool > racingInfo
Definition: EpisodicSimulation.h:306
EpisodicSimulation::SetTravelLimit
void SetTravelLimit(double lim)
Definition: EpisodicSimulation.h:72
EpisodicSimulation::SetStopOnConvergence
void SetStopOnConvergence(bool stop)
Definition: EpisodicSimulation.h:66
EpisodicSimulation::DisableTravelLimit
void DisableTravelLimit()
Definition: EpisodicSimulation.h:74
EpisodicSimulation::targetTolerance
double targetTolerance
Definition: EpisodicSimulation.h:303
UnitInfo
Definition: UnitSimulation.h:47
EpisodicSimulation::SetTrialLimit
void SetTrialLimit(long maxTrials)
Definition: EpisodicSimulation.h:73
kNoTarget
@ kNoTarget
Definition: EpisodicSimulation.h:25
UnitSimulation::units
std::vector< UnitInfo< state, action, environment > * > units
Definition: UnitSimulation.h:141
EpisodicSimulation::SetTargetTolerance
void SetTargetTolerance(double x)
Definition: EpisodicSimulation.h:67
EpisodicSimulation::AddNonRacingUnit
int AddNonRacingUnit(Unit< state, action, environment > *u, double timeOffset=0.)
Definition: EpisodicSimulation.h:54
EpisodicSimulation::GetCurrRound
int GetCurrRound()
Definition: EpisodicSimulation.h:71
EpisodicSimulation::disjunctiveTrialEnd
bool disjunctiveTrialEnd
Definition: EpisodicSimulation.h:304
EpisodicSimulation::DoPreTimestepCalc
virtual void DoPreTimestepCalc()
Definition: EpisodicSimulation.h:79
UnitSimulation
The basic simulation class for the world.
Definition: UnitSimulation.h:85
kOutOfTravel
@ kOutOfTravel
Definition: EpisodicSimulation.h:25
UnitSimulation::AddUnit
virtual int AddUnit(Unit< state, action, environment > *u, double timeOffset=0.)
Definition: UnitSimulation.h:172
EpisodicSimulation::Done
virtual bool Done()
Definition: EpisodicSimulation.h:65
EpisodicSimulation::GetTargetTolerance
double GetTargetTolerance()
Definition: EpisodicSimulation.h:68
EpisodicSimulation::UnitOnTargetStatus
tUnitOnTargetStatus UnitOnTargetStatus(UnitInfo< state, action, environment > *u)
Definition: EpisodicSimulation.h:259
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
EpisodicSimulation::~EpisodicSimulation
virtual ~EpisodicSimulation()
Definition: EpisodicSimulation.h:44
EpisodicSimulation::UnitOnTarget
bool UnitOnTarget(UnitInfo< state, action, environment > *u)
Definition: EpisodicSimulation.h:278
EpisodicSimulation::maxRounds
long maxRounds
Definition: EpisodicSimulation.h:301
EpisodicSimulation::EpisodicSimulation
EpisodicSimulation(environment *e)
Definition: EpisodicSimulation.h:31
UnitSimulation::currTime
double currTime
Definition: UnitSimulation.h:144
EpisodicSimulation::AddUnit
int AddUnit(Unit< state, action, environment > *u, double timeOffset=0.)
Definition: EpisodicSimulation.h:45
kNotOnTarget
@ kNotOnTarget
Definition: EpisodicSimulation.h:25
UnitInfo::agent
Unit< state, action, environment > * agent
Definition: UnitSimulation.h:50
kReachedTarget
@ kReachedTarget
Definition: EpisodicSimulation.h:25
UnitSimulation::unitGroups
std::vector< UnitGroup< state, action, environment > * > unitGroups
Definition: UnitSimulation.h:142
Unit
Definition: Unit.h:23
EpisodicSimulation::travelLimit
double travelLimit
Definition: EpisodicSimulation.h:298
EpisodicSimulation::EpisodeDone
virtual bool EpisodeDone()
Definition: EpisodicSimulation.h:243