Skip to content

Genetic Algorithm Library : Pyeasyga

A simple and easy-to-use implementation of a Genetic Algorithm library in Python.

pyeasyga provides a simple interface to the power of Genetic Algorithms (GAs). You don’t have to have expert GA knowledge in order to use it.

Installation

At the command line, simply run:

$ pip install pyeasyga

Import the modules

At the command line, simply run:

import random
from pyeasyga.pyeasyga import GeneticAlgorithm

Setup your data and init the GA (Only data is mandatory)

setup data and initialise the GeneticAlgorithm class with the required data parameter, and all or some of the optional parameters

data = [('orange', 50), ('apple', 35), ('kiwi', 40)]

#ga = GeneticAlgorithm(data, 20, 50, 0.8, 0.2, True, True)
#ga = GeneticAlgorithm(data)
ga = GeneticAlgorithm(
    data,
    population_size=20,
    generations=50,
    crossover_probability=0.8,
    mutation_probability=0.2,
    elitism=True,
    maximise_fitness=True
)

Create the individual (Optional)

Optionally, define a function to create a representation of a candidate solution (an individual in GA speak). This function should take in the data defined in step 1. as a parameter.

Set the Genetic Algorithm’s create_individual attribute to your defined function

def create_individual(data):
    individual = []
    for item in data:
        individual.append(random.randint(0, 1))
    return individual

ga.create_individual = create_individual

Genetic Algorithm’s operations (crossover, mutate, selection) (Optional)

Optionally, define and set functions for the Genetic Algorithm’s genetic operators (i.e. crossover, mutate, selection)

def crossover(parent_1, parent_2):
    crossover_index = random.randrange(1, len(parent_1))
    child_1 = parent_1[:crossover_index] + parent_2[crossover_index:]
    child_2 = parent_2[:crossover_index] + parent_1[crossover_index:]
    return child_1, child_2

ga.crossover_function = crossover


def mutate(individual):
    mutate_index = random.randrange(len(individual))
    if individual[mutate_index] == 0:
        individual[mutate_index] = 1
    else:
        individual[mutate_index] = 0

ga.mutate_function = mutate


def selection(population):
    return random.choice(population)

ga.selection_function = selection

Define the Fitness function (Mandatory)

Define a fitness function for the Genetic Algorithm. The function should take two parameters: a candidate solution representation (an individual in GA speak), and the data that is used to help determine the individual’s fitness

Set the Genetic Algorithm’s fitness_function attribute to your defined fitness function

def fitness (individual, data):
    fitness = 0
    if individual.count(1) == 2:
        for (selected, (fruit, profit)) in zip(individual, data):
            if selected==1:
                fitness += profit
    return fitness

ga.fitness_function = fitness

Run the GA program

Run the Genetic Algorithm, Print the best solution, You can also examine all the individuals in the last generation.

ga.run()

print("the best indivudial : ", ga.best_individual())

for individual in ga.last_generation():
    print(individual)