An Example by Hand
An Example by Hand
So now that the population has been initialized using a random process, the fitness of each individual can then been calculated using the fitness equation f(x) = 2x.
Next each individual is given a weighting based on its fitness. This weighting affects the individuals chance of being in the mating pool. In this case, the weighting is equal to the fitness of the individual divided by the sum of all the fitnesses.
Lastly, the roulette wheel is spun a number of times equal to the population size, to randomly pick which individuals are selected for the mating pool. It should be noted that each individual may be selected more than once and some may not be selected at all.
Following this, it is now observed that the individual with the lowest fitness (3) has been eliminated from the gene pool and the individual with the highest fitness has been selected for the mating pool twice. This is not guaranteed to happen however as this is a random process but in general poor genetic information is lost and good genetic information is duplicated. Therefore, the mating pool now consists of:
Pairs from this mating pool are then selected at random and the pairs cross-bred. In this example A and B are cross-bred and C and D are cross-bred. To cross-breed the two individuals, a number is randomly selected between 1 and (k1), where k is the number of chromosomes (in this example, 5), as the crossing point. Once the crossing point has been selected, the two individuals breed to create two offspring, each offspring taking some of the genetic information from parent A and part from parent B. For example, individuals A and B mate with a crossing point of 2, as shown in Figure 2.7 below.
Offspring AB takes on the first two chromosomes of Parent A and the last three chromosomes of Parent B. Offspring BA takes on the opposite genetic information, taking the first two chromosomes from Parent B and the last three chromosomes from Parent A.
Lastly, there is a small chance that each chromosome will be mutated. In this example, a mutation rate of 0.05, or 5%, is used meaning that out of the 20 chromosomes in the population it is expected that one chromosome from each generation will be mutated on average. When a chromosome is mutated, it will be flipped to the opposite value, with a 1 becoming a 0 and a 0 becoming a 1. In this example, the algorithm has processed all the chromosomes and none have been mutated. Now the process is repeated, with the algorithm evaluating the fitness of the new generation. Note that this example does not use elitism. For elitism to work, it needs a large population so did does not to overwhelm the algorithm with the genetic information of the best individuals as this will often be a hindrance in overcoming local maxima.
This process is then repeated ad nauseam until a predetermined end criterion has been met. For example, the end criteria may that after 10 generations the algorithm exits or after 10 generations without a change in the highest fitness the algorithm exits. However, even after one generation, we can see that the algorithm has improved from the initial random guesses with the maximum fitness increasing from 50 to 54 and the average fitness increasing from 27.5 to 36.5.