Genetic algorithm in unity using C#

Using Genetic algorithm to help a cube get to a target

dhruv shindhe
Analytics Vidhya
4 min readMar 21, 2019

--

Introduction

In this article I will take you through an implementation of a genetic algorithm in unity using C#.

Here I implement a very basic Genetic algorithm that is spiced up with unity.

As you can see in the video below, the red block which has to get to the yellow block — initially the red block goes in a number of wrong directions then finally it reaches the yellow block after a few generations.

What is Genetic Algotihm

Genetic algorithm is inspired from the process of natural selection where the fittest individuals live long enough to pass on their genes and the weakest perish quickly .

The image below shows how the algorithm works.

GA flowchart

If you don’t know what it is then I recommend you to go through this here. This walks you through what a genetic algorithm is. If you get to a point where you have understood how he has implemented a GA to type the phrase “to be or not to be” you are good to go. I have followed a similar approach here.

Game Environment in Unity

So let me give you an idea of the game environment in unity. Here we have a red block that has to get to the yellow block without colliding with the white or green side. The black blocks are edges and when the red block collides with the black block the red block has to change its direction i.e after the collision it has to either go straight or right or left or in the backward direction.

This direction is stored in an array. Every time the red block hits collides with an edge the number at the corresponding index in the direction array is seen an the block goes in that direction. For example if the block collides with the 3 edge then the number at index 2 is seen (as indexing in C# is from 0) if the number is 1 then the block turns right.The image below will give you a brief idea.

Direction array values of the block

So as there are 11 edges, the direction array has to be this {0, 1, 2, 1, 0, 1, 2, 1, 0, 1, 2}, so that the red block can reach the yellow block.This array is the target array and every gene’s fitness will be determined based on how close it is to this target array.

There are 4 scripts — newMove.cs, GAobj.cs, population.cs and DNA .cs

I will walk you through all of them but make sure you have a brief understanding of a genetic algorithm.

Game environment in unity3D

DNA class

The DNA class has 2 main instance variables gene,which for the initial population will be randomly generated and fitness will be determined based on how close it is to this target array.

The constructor takes in int no which is the size of the gene which in our case is 11.

fitnessCal() calculates a score based on how the gene is similar to the target element by comparing each element and normalizes it so that it could be later used to create the mating pool with a probabilistic approach.

DNA crossOver(DNA partner) first calculates a random midpoint in the gene and performs crossover or mating.

Mutate() mutate the child DNA so as that the offspring could have genetic diversity and not have the same genes as their parents.

Population class

The population class consists of two lists of type DNA which holds the matingpool and bestgenesholder which will be given to the GAobj class that uses this to spawn blocks to show the GA working.

The constructor creates the initial population and initializes everything.

Inside the while loop, first the fitness of all individuals is calculated then the fittest gene is appended to the bestgenesholder after this we shall check for convergence(in this case convergence occurs when the target gene has been successfully predicted ).

Then the mating pool is crated with a probabilistic approach. For example if A has normalized score of 0.3 then 30% of the mating pool should have A so that when two parents are picked at random to mate the probability of picking A should be 30%.

After this, the DNA of the child is mutated and is the convergence is not attained then we pass the offspring back into the GA and do it again and again till we reach convergence.

GAobj

For those of you who are wondering yes the class name is irrelevant. This class is basically the one that takes the output after convergence from the population class i.e the bestgenesholder and spawns blocks that take the values of the best genes from the 0th generation to the one that achieves convergence.

In start() we create a new population and get the bestgenesholder into this script and call startInatantiating() which instantiate blocks or cars that have the best genes of that particular generation and go on till the best.

newMove

When the block has been instantiated, it first takes its direction from the bestgenesholder list for that particular generation and moves.

As discussed earlier whenever the red block or the car collides with the edge (black block) it checks which edge it is and takes the next direction.

So this is it guys — you have built a very simple GA and spiced it up with unity to help a block move.

Thanks for reading!

--

--

dhruv shindhe
Analytics Vidhya

I write about anything and everything that interests me.