Genetic algorithm: running phy simulation for each generation to compute fitness function

Hi Guys,

I am new to Unity and do not have a computer science background. I am currently working on a project that requires using a genetic algorithm for optimization.

  1. It requires the calculation of a fitness function to test how well the current settings perform. In this step, the unity simulation is run for a given time and the fitness is evaluated based on the state of the gameobjects at the end time. This process is repeated for n generations and finally, the best one is picked. Also, each generation must have the same initial conditions, that is the game starts with the same initial position of the game objects each time. I am thinking of using start/stop coroutine at the start and end of each generation. Not sure if this will work but is there anything else that can be done?

  2. This process will be faster if I am able to separate the rendering from physics computation (like simulating n steps, each one with different settings but only rendering one of them actually on the screen or work entirely in the background till the best values have been found). I have no clue about how to separate rendering from physics computation.

It will be really great if anyone can point me in the right direction.

If you want to change how fast time inside your game runs have a look at the time setting in Project Settings.


Also I am pretty sure that physics and rendering calculations are separate in unity.

Case in point: The physics dont change when you have a low framerate.

As the time settings outline they only change the physics side. The rendering side just tries to keep up and if it cant you will get exactly the situation you are asking for.


For reloading your scene:

You will also probably want to serialise your metadata about fitness against your models somewhere so that you know which ones to keep and which ones to bin.

If you want to change how fast time inside your game runs have a look at the time setting in Project Settings.