Genetic algorithms are one of a variety of AI techniques that have been extensively explored by academics but have yet to push their way into game development. However, they offer opportunities for developing interesting game strategies in areas where traditional game AI is weak.
Primer
Genetic algorithms (GAs) are one of a group of random walk techniques. These techniques attempt to solve problems by searching the solution space using some form of guided randomness (Giacopassi et al 123-148).
Sex
Sex is our artificially glamorous description of the actual mechanics of GA evolution (Oh and Hsu 618-637).
The fitness scores f is usually scaled before parents are chosen. The standard method is linear scaling: f ' = af + b, where the coefficients a and b is selected each generation to cause the maximum scaled score to be a specified multiple (usually two) of the population average. This will preserve the genetic diversity in your population. Here's some scaling sample code:
// Population has been sorted according to fitness
int iPopMin = member[POPULATION_SIZE - 1].fitness;
For each member of the next generation, pick a pair of parents from the current generation based on their probability of reproduction. The child then inherits its DNA from its parents according to the following code snippet:
for (int i = 0; i < DNA_LENGTH; i++)
{
child[i] = parent1[i];
if ( event(P_CROSSOVER) )
{
temp = parent1;
parent1 = parent2;
parent2 = temp;
}
}
Then for each child in the new generation, mutate them. Bear in mind that mutation is very rare and serves ...