Member-only story

DEAP: A basic overview

Gaurav Kumar
2 min readOct 8, 2023
Source:Google Image

DEAP (Distributed Evolutionary Algorithms in Python) is a framework for prototyping and testing evolutionary algorithms, including genetic algorithms. The fitness function is a fundamental part of any evolutionary algorithm, as it evaluates and assigns a fitness value to each individual in the population.

Here’s a basic example to guide you on how to implement fitness functions in DEAP:

Import Required Libraries:

import random
from deap import base, creator, tools

Define the Fitness Class and Individual:

To begin with, you need to create the fitness class and the type of individual that will be used. Here, we’ll aim to maximize a fitness function, so we’ll use FitnessMax:

creator.create("FitnessMax", base.Fitness, weights=(1.0,))
creator.create("Individual", list, fitness=creator.FitnessMax)

Define Your Fitness Function:

In this example, we’ll define a simple fitness function that returns the sum of the elements of an individual:

def evalFitness(individual):
return sum(individual),

Setup the Toolbox:

The toolbox is a central concept in DEAP. It allows you to register various components required for the genetic algorithm, like the mutation…

--

--

Gaurav Kumar
Gaurav Kumar

No responses yet