Go to the documentation of this file.
12 #ifndef UNITSIMULATION_H
13 #define UNITSIMULATION_H
29 template <
class state,
class action>
39 template <
class state,
class action,
class environment>
42 template <
class state,
class action,
class environment>
46 template <
class state,
class action,
class environment>
84 template<
class state,
class action,
class environment>
121 virtual void OpenGLDraw(
unsigned int whichUnit)
const;
141 std::vector<UnitInfo<state, action, environment> *>
units;
142 std::vector<UnitGroup<state, action, environment> *>
unitGroups;
151 template<
class state,
class action,
class environment>
165 template<
class state,
class action,
class environment>
171 template<
class state,
class action,
class environment>
179 if (ui->
agent->GetUnitGroup() == 0)
181 ui->
agent->SetUnitGroup(unitGroups[0]);
184 ui->
nextTime = currTime + timeOffset;
195 ui->
agent->SetNum( units.size() - 1 );
196 return units.size()-1;
199 template<
class state,
class action,
class environment>
205 template<
class state,
class action,
class environment>
208 if (which < units.size())
209 return units[which]->agent;
213 template<
class state,
class action,
class environment>
216 unitGroups.push_back(ug);
217 return unitGroups.size()-1;
220 template<
class state,
class action,
class environment>
223 return unitGroups.size();
226 template<
class state,
class action,
class environment>
229 if (which < unitGroups.size())
234 template<
class state,
class action,
class environment>
237 info.
init(units[which]->startState, units[which]->currentState,
238 units[which]->lastState, units[which]->lastMove,
239 units[which]->lastTime, units[which]->nextTime);
242 template<
class state,
class action,
class environment>
245 while (units.size() > 0)
249 ui->
agent->LogFinalStats(&stats);
269 template<
class state,
class action,
class environment>
273 for (
unsigned int i=0; i<unitGroups.size(); i++)
275 if (!unitGroups[i]->Done())
304 template<
class state,
class action,
class environment>
306 double minimum = DBL_MAX;
308 for (
unsigned int i = 0; i < units.size(); i++ ) {
309 if ( minimum > units[i]->nextTime - currTime )
310 minimum = units[i]->nextTime - currTime;
312 if ( minimum < 1./30. )
313 fprintf( stderr,
"Warning: GetTimeToNextStep sank under 1./30. (%g).\n", minimum );
318 template<
class state,
class action,
class environment>
328 stats.AddStat(
"simulationTime",
"UnitSimulation", currTime);
329 currTime += timeStep;
330 DoTimestepCalc(timeStep);
331 DoPostTimestepCalc();
337 template<
class state,
class action,
class environment>
342 template<
class state,
class action,
class environment>
345 for (
unsigned int x = 0; x < units.size(); x++)
348 StepUnitTime(units[x], timeStep);
352 while (currTime > units[x]->nextTime)
353 StepUnitTime(units[x], 0);
358 template<
class state,
class action,
class environment>
370 template<
class state,
class action,
class environment>
373 if (currTime < theUnit->nextTime)
return;
375 double moveThinking=0, locThinking=0, moveTime=0;
390 stats.SumStat(
"MakeMoveThinkingTime", u->
GetName(), moveThinking);
392 bool success = MakeUnitMove(theUnit, where, moveTime);
400 stats.SumStat(
"UpdateLocationThinkingTime", u->
GetName(), locThinking);
405 theUnit->
nextTime = currTime + timeStep;
412 theUnit->
nextTime += (locThinking+moveThinking)*penalty + moveTime;
415 theUnit->
nextTime +=
max((locThinking+moveThinking)*penalty + moveTime, timeStep);
435 template<
class state,
class action,
class environment>
438 bool success =
false;
448 std::vector<action> succ;
449 env->GetActions(oldState, succ);
450 for (
unsigned int x = 0; x < succ.size(); x++)
452 if (succ[x] == where)
463 moveCost = env->GCost(oldState, theUnit->
currentState)*theUnit->
agent->GetSpeed();
486 template<
class state,
class action,
class environment>
490 for (
unsigned int x = 0; x < units.size(); x++)
493 units[x]->agent->OpenGLDraw(env,
this);
496 for (
unsigned int x = 0; x <unitGroups.size(); x++)
497 unitGroups[x]->OpenGLDraw(env,
this);
500 template<
class state,
class action,
class environment>
503 if (whichUnit >= units.size())
506 currentActor = whichUnit;
507 units[whichUnit]->agent->OpenGLDraw(env,
this);
void SetThinkingPenalty(double pen)
setPenalty for thinking.
unsigned int currentActor
unsigned int historyIndex
environment * GetEnvironment()
virtual void OpenGLDraw() const
virtual void DoTimestepCalc(double amount)
StatCollection * GetStats()
double GetTimeToNextStep() const
virtual void SetStateOccupied(const state &, bool)=0
void SetStepType(tTimestep step)
virtual const char * GetName()=0
void init(state &_startState, state &_currentState, state &_lastState, action _lastMove, double _lastTime, double _nextTime)
virtual void DoPostTimestepCalc()
int AddUnitGroup(UnitGroup< state, action, environment > *ug)
void SetLogStats(bool val)
virtual void MoveUnitOccupancy(const state &, const state &)=0
bool MakeUnitMove(UnitInfo< state, action, environment > *theUnit, action where, double &moveCost)
virtual void GetLocation(state &)=0
virtual void ClearAllUnits()
virtual bool CanMove(const state &, const state &)=0
std::vector< TimeStep< state, action > > stateHistory
tTimestep GetStepType() const
Private per-unit unitSimulation data.
std::vector< UnitInfo< state, action, environment > * > units
UnitSimulation(environment *se)
TimeStep(state s, double time)
virtual void GetPublicUnitInfo(unsigned int which, PublicUnitInfo< state, action, environment > &info) const
The StatCollection class is for collecting stats across different parts of the simulation.
virtual ~UnitSimulation()
unsigned int GetNumUnitGroups() const
double GetSimulationTime() const
The basic simulation class for the world.
unsigned int GetNumUnits() const
UnitGroup< state, action, environment > * GetUnitGroup()
virtual void LogStats(StatCollection *)
log an stats that may have been computed during the last run
virtual void DoPreTimestepCalc()
virtual SimulationInfo< state, action, environment > * GetSimulationInfo()
double GetThinkingPenalty()
getPenalty for thinking.
virtual int AddUnit(Unit< state, action, environment > *u, double timeOffset=0.)
Unit< state, action, environment > * GetUnit(unsigned int which)
void StepUnitTime(UnitInfo< state, action, environment > *ui, double timeStep)
step time for a single unit.
virtual unsigned int GetCurrentUnit() const
UnitGroup< state, action, environment > * GetUnitGroup(unsigned int which)
Unit< state, action, environment > * agent
std::vector< UnitGroup< state, action, environment > * > unitGroups