Location>code7788 >text

Implementing a simple genetic algorithm using the PythonDEAP library

Popularity:439 ℃/2025-01-18 23:33:59


My blog has a better eating experience

DEAP (Distributed Evolutionary Algorithms in Python) is an evolutionary computing framework for rapid prototyping and experimentation. It supports a variety of evolutionary algorithms, including genetic algorithms, genetic programming, evolutionary strategies, particle swarm optimization, etc. DEAP is designed for flexibility and ease of use, allowing researchers and developers to easily implement and test a variety of evolutionary algorithms.

Official documentation

Official GitHub repository

Since my main focus is not Python, it can even be said that I knew nothing about Python before. If there are any mistakes, I hope readers can correct me.

This article also focuses on explaining usage, and does not cover specific class inheritance and principles.

here to seek\(f(x) = (x - 5) ^ 2\)and\(f(x) = -x ^ 2 + 4x + 4\)exist\([0, 31]\)The goal is to make all of the above as large as possible, taking multi-objective optimization as an example.

0.Load DEAP library


import random

from deap import base, creator, tools, algorithms

Since you do not need all the functions of the deap library, only a few modules to be used are loaded here.

1. Create a fitness function


def evaluate(individual):

    ans = 0

    for i in range(len(individual)):

        ans += (individual[i] << i)

    return ((ans - 5) ** 2, -ans ** 2 + ans * 4 + 4)

Because fitness is a tuple by default, we choose to return a tuple here. Each integer is stored in binary because in the subsequent gene exchange, the two genes are required to be lists, and integers cannot be exchanged.

2. Create individuals and populations

def init_individual(icls):

     content = [toolbox.attr_int() for _ in range(5)]

     individual = icls(content)

      = (0.0, 0.0) #Initialize fitness

     return individual

 ("FitnessMulti", , weights=(1.0, 1.0)) #Create fitness

 ("Individual", list, fitness=) #Create an individual class, the individual is a list, and fitness is one of the attributes

 toolbox = ()

 ("attr_int", , 0, 1) # Then generate 0 or 1 as each bit of binary

 ("individual", init_individual, )#Register individual generation function

 ("population", , list, )#Register the population generation function and generate the population as a list

The weights in the sixth line here are fitness, and the library defaults to a tuple. Because this example is a multi-objective optimization of a double function, the tuple has two elements. The absolute value of each element represents the weight of the dimensional function. Positive or negative indicates whether to maximize or minimize; if positive, it indicates the goal. is to maximize the elements of this dimension.

Introduction to each function

Basic usage

In the DEAP library, is a very important function used to dynamically create new types. These types are typically used to define individual representations (such as chromosomes) and fitness functions. The flexibility allows DEAP to adapt to a variety of different evolutionary algorithm needs.


(name, base, **kwargs)

Parameter description:

Parameter name type illustrate
name string The name of the new type.
base Python built-in classes or custom classes The function used to generate each object (such as a function that generates random individuals).
**kwargs It depends on the situation Additional properties, typically used to add fitness functions or other custom properties.

Basic usage

Used to register functions or operations into the toolbox object for easy calling in algorithms. Usually used to register operations such as individual generation, crossover, mutation, and selection. As in the given code, individual and population are functions that can be called directly after registration.


(alias, method, *args, **kargs)

Parameter description:

Parameter name type illustrate
lias type This is the name you define for the operation, which can later be called via .
method callable object This is the function or method that actually performs the operation.
*args Variable positional parameters If the method requires several parameters, they can be passed through *args.
**kargs Variable keyword arguments If a method requires keyword arguments, they can be passed via **kargs.

Take the original code as an example


("individual", init_individual, )

We registered a function named individual, which can be called later through (). The function actually executes the content of the init_individual function, which is the previously created individual class and is passed in to init_individual as a parameter.

Basic usage

In the DEAP library, is a utility function for generating repeating structures. It is often used to initialize individuals or populations, generate lists or other data structures containing repeated elements. The following are the detailed parameters and their usage:


(container, func, n)

Parameter description:

Parameter name type illustrate
container type The container type used to store the generated objects (e.g. list, set, etc.).
func callable object The function used to generate each object (such as a function that generates random individuals).
n integer The number of objects that need to be generated (e.g. population size).

In fact, the func function is executed n times and stored in the container type.

Take the original code as an example


("population", , list, )

We registered a function named population, which actually executes the content of the function. The list indicates that the generated object, that is, the population, is a list, passed in as a parameter, and is the content of the repeated execution of initRepeat. Note that the number of repeated executions n is not written here. In this way, we can freely choose the size of the generated population in subsequent population generation.

Also, because in this example our individual is an integer, which is a list of five elements. But in some cases, our individual may not be an element. In this case, the function that generates the individual can also be implemented in the same way. For example: I can define an individual as consisting of five lists, each list has five elements (0 or 1). You can register like this:


("individual", , list, init_individual, , n = 5)

Notice:The registration function here is wrong, because this way of writing will cause the third parameter to be regarded as the number of repetitions, and will not be regarded as the parameter of init_individual. Therefore init_individual should use anonymous function form to avoid this situation. No modifications were made here.

3. Create other required parameters and functions

("evaluate", evaluate) #Register fitness function

 ("mate", ) #Use two-point intersection method

 ("mutate", , indpb=0.1) #Use bit flip mutation, each gene mutation probability is 0.1

 ("select", tools.selNSGA2) # Because it is a multi-objective optimization problem, use the NSGA-II selection mechanism

 population_size = 50 # Population size

 generations = 40 # Number of breeding generations

 crossover_prob = 0.9 # crossover probability

 mutation_prob = 0.2# Individual mutation probability

 population = (n = population_size) #Generate a population of size 50

In this process, there are many genetic crossover, mutation, and selection mechanisms, and readers can choose freely according to their needs.

4. Execute genetic algorithm


for gen in range(generations):

    offspring = (population, toolbox, cxpb = crossover_prob, mutpb = mutation_prob)

    fits = map(, offspring)

    for fit, ind in zip(fits, offspring):

         = fit

    population = (offspring + population, k = population_size)

Basic usage


(population, toolbox, cxpb, mutpb)

Parameter name type illustrate
population Registered population class Current population.
toolbox Toolbox object Contains registered crossover, mutation, and selection operations.
cxpb floating point number Represents the probability of crossover between two individuals.
mutpb floating point number Represents the probability of individual mutation.

varAnd will call the registered function from toolbox; :crossover operation; :mutation operation.

If necessary, you can output the final population or combine it with matplotlib to draw a chart.