Setting up

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:

  1. 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
  1. 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 ...

Get Hands-On Genetic Algorithms with Python now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.