Artificial Intelligence Genetic Algorithm tutorial 1

What we will be covering in this series

In this genetic algorithm tutorial we will be taking the game we built in our phaser platformer game series. Then we will be teaching our player to automatically learn our level and complete it without supervision.

Just to recap this is the game and what the level looks like :

What is a genetic algorithm?

Genetic algorithms try to model nature and the ability to evolve over time to obtain certain survival objectives. Traditionally in genetic algorithms this is called the fitness score. Initially the genetic algorithm will create a random population of individuals with chromosomes. We then pick random individuals to breed together and this creates new individuals. We test their chromosome output against what we intend to optimise or learn and assign a fitness to that individual. The second generation we select the top ten fittest individuals and breed them together and so on. Until we get the highest possible fitness.

What else can genetic algorithms be applied to?

The general purpose of genetic algorithms are that they can solve optimisation problems and find optimal outcomes for properties within a system.  Some of the applications include :

  • Optimisation of algorithmic trading on stock exchanges.
  • Finding more optimal models in engineering problems.
  • Game artificial intelligence.
  • Path finding.

How will we be applying this to our platformer game?

In our game we will identify some options which the player can take. Some of which are to move right, move left and jump. So to make things simple we will label each with a character.

‘U’ – Jump
‘R’ – Move right
‘L’ – Move left

The chromosome

We then will generate chromosomes which will look something similar to the following and will be our action string.


Then we will have our game simulate the input of the chromosome to move our player around on the screen and to perform actions. Our fitness will be determined by how far our player manages to get to the right of the screen.

As you may have guessed we are going to optimise for this property as our fitness score.

Whats up next?

In our next genetic algorithm tutorial we will build our javascript classes from scratch to build the “GA” into our game world.


Comments 1

Leave a Reply

Your email address will not be published. Required fields are marked *