Here I will give some details on how the current implementation of the genetic algorithm works.
The genetic code for each creature is structured as a Dna object, this object contains an array called chromosomes where each position holds a 2D vector, and it also contains an integer value representing how long the chromosomes array is. As mentioned on a previous post, this vectors are used to determine each of the creature’s jumps.
When a new creature is created their Dna is also created and initialized. For the initialization the amount of vectors is the only information required. After that the vectors on the array will be filled with random values within a predetermined range. This range is determined by a global variable that will be referred to as maximum initial impulse.
Given that the maximum initial impulse is 3, one possible Dna could look like this:
Amount of Dna code:
3
Chromosomes:
Pos. 0 | Pos. 1 | Pos. 2 |
x = 2.3 | x = 1.2 | x = -2.8 |
y = -1.2 | y = -2.6 | y = -0.5 |
As these values are intended to compose the impulse that makes the creature jump when applied to its body, the Y value of every vector has to be always negative (a positive value would make the creature launch itself onto the ground and the resulting “jump” would actually be due to the creature’s bounce on the floor. It would not look very physically realistic and also most of the impulse would be lost).
Mutation
When mutation is applied to a Dna a chance of mutation and an amount of mutation has to be provided. In the case of version 0.3, the chance is 50%, and the amount is also 50%. What this means is that there is a 50% chance of each jump (vector) being mutated and when it is, the resulting value will be the previous plus a random value between -50% and 50% of the maximum initial impulse.
A possible mutated version of the previous Dna example, considering the chance of mutating and the amount of mutation both equals to 50%, could be this:
Pos. 0 | Pos. 1 | Pos. 2 |
x = 2.3 (not changed) | x = 1.8 (mutated: 1.2 + 0.6) | x = -2.8 (not changed) |
y = -1.2 (not changed) | y = -3.5 (mutated: -2.6 + (-0.9)) | y = -0.5 (not changed) |
On the example only the second position (jump) was mutated (there was a 50% chance of this happening).
This is what happened to X:
Mutated X = previous X + (maximum initial impulse * random value between -50% and 50%)
Mutated X = 1.2 + (3 * 20%)
Mutated X = 1.8
And to Y:
Mutated Y = previous Y + (maximum initial impulse * random value between -50% and 50%)
Mutated Y = -2.6 + (3 * (-30%))
Mutated Y = -3.5