Before we start the actual genetic algorithm flow, we need to set things up, and the DEAP framework has a quite distinct way of doing it, as shown in the rest of this section:
- We start by importing the essential modules of the DEAP framework, followed by a couple of useful utilities:
from deap import basefrom deap import creatorfrom deap import toolsimport randomimport matplotlib.pyplot as plt
- Next, we declare a few constants that set the parameters for the problem and control the behavior of the genetic algorithm:
# problem constants:ONE_MAX_LENGTH = 100 # length of bit string to be optimized# Genetic Algorithm constants:POPULATION_SIZE = 200 # number of individuals in populationP_CROSSOVER = 0.9 # probability for crossover ...