The Evaluation, Selection and Crossover Steps

Learn how to implement the evaluation, selection, and crossover steps in a genetic algorithm.

Evaluating the population

After initializing our population, the next step is the evaluation phase. In the previous lesson, we created a stub of the evaluate function. This represents “step 2” in the genetic algorithm and what we would be filling in now.

The main understanding behind this function is that it takes a population, evaluates each chromosome based on a fitness function, and sorts the population based on each chromosome’s fitness. “Fitness” is simply a heuristic that tells us how good or bad a solution is, and the fitness function calculates this fitness for us. In this One-Max problem, the fitness of a chromosome is represented by the sum of the bitstring.

Let us go about implementing the evaluate function now. In our algorithm defined so far, replace the stub of the evaluate function with the following:

Get hands-on with 1200+ tech skills courses.