- Home /
How to speed up simulation time without distorting results
I'm currently working on an AI for a small RTS. For training the AI I need to run a premade set of actions for 600 2000 times which would result in 333.3 hours of simulation. To shorten it I tried to increase Time.timeScale. For timeScale = 10 it worked just fine and the statistics I recorded showed no visible difference to timeScale 1.
"recordingLength": 600.0029907226563,
"fitness": 619.1399536132813,
"militaryScore": 570,
"actionsCount": 60,
...
The fitness is a combination of the military score and the total produced resources. The military score is the combat value of all produced military units. Action count is the total of executed actions within the 600 seconds of simulation.
The first 10 actions where executed at:
1.0040292739868165
1.0040292739868165
16.0040283203125
16.0040283203125
16.0040283203125
16.0040283203125
16.0040283203125
27.0040283203125
27.0040283203125
74.0040283203125
I expected the fitness, military score and action count to be nearly the same for every timeScale but that was not the case. For timeScale 50 the results where quite different:
"recordingLength": 600.0029907226563,
"fitness": 180.74000549316407,
"militaryScore": 150,
"actionsCount": 53,
...
Time of execution of the first 10 actions:
1.0084590911865235
1.0084590911865235
21.00845718383789
21.00845718383789
21.00845718383789
21.00845718383789
21.00845718383789
32.00845718383789
32.00845718383789
85.00845336914063
It looks like the AI is not able to execute as many actions on higher timeScale as it is on timeScale 10 or less.
There are two loops that could be responsible:
This one is located in the class that is responsible for recording executed actions, general statistics and ending the simulation after a set time.
void FixedUpdate()
{
if (this.currentTime < GlobalVariables.simulationLength)
{
this.currentTime += Time.fixedDeltaTime;
this.timer += Time.fixedDeltaTime;
if (this.timer > this.timeSteps)
{
SaveStats(); // saves some small data
Debug.Log(">>> " + this.currentTime);
this.timer -= this.timeSteps; // timeSteps is set to 1
}
}
else
{
// Saving final stats and ending simulation
}
}
This FixedUpdate is located in the AI class and tries to execute as many queued actions as possible. If an action is not ready to be executed the AI waits for the tick (1 second) and tries again.
void FixedUpdate()
{
this.timer += Time.fixedDeltaTime;
if (this.timer > this.tickTime) // tickTime is 1
{
bool actionWasSuccessful;
do
{
actionWasSuccessful = TryExecuteNextAction();
}
while (actionWasSuccessful);
this.timer -= this.tickTime;
}
}
I tried it with void Update() at first but changed to FixedUpdate to be frame-rate independent.
My idea was that the simulation runs an scaled equivalent of 600 seconds and therefore is able to act the same way if it runs 10 times or 50 times as fast as normal. Within this time the AI checks every second if it is able to execute the next actions. One thing I noticed with void Update() is that on timeScale 50 some frames take longer than 1-2 seconds to complete. Consequently the check for actions every second was passed over some times. But this should not be an issue with FixedUpdate.
I also tried setting Maximum Allowed Timestep to 100 so the frame would not be finished prematurely and the visible framerate is not important for the simulation.
It should be mentioned that the simulation is not deterministic due to the navigation and local avoidance of the workers executing the actions. But the recorded differences on the same timeScale are pretty small, the fitness varies by around 10.
Does someone know a way to speed up simulation without altering the process and results?
Thank you in advance, please tell me if some important informations are missing or something ist not comprehensible.
Your answer
![](https://koobas.hobune.stream/wayback/20220613033808im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Is it possible to tick the fixed timestep manually or increase the timescale beyond 100? 2 Answers
Speeding up the game while still making it run deterministically 0 Answers
Pause game on Start doesn't quite work 1 Answer
Slow motion all but one - yet another round, hopefully the last 6 Answers
Physics calculations vs TimeScale vs FixedDeltaTime 1 Answer