pyeasyga package

Submodules

pyeasyga.pyeasyga module

pyeasyga module

class pyeasyga.pyeasyga.Chromosome(genes)[source]

Bases: object

Chromosome class that encapsulates an individual’s fitness and solution representation.

class pyeasyga.pyeasyga.GeneticAlgorithm(seed_data, population_size=50, generations=100, crossover_probability=0.8, mutation_probability=0.2, elitism=True, maximise_fitness=True)[source]

Bases: object

Genetic Algorithm class.

This is the main class that controls the functionality of the Genetic Algorithm.

A simple example of usage:

>>> # Select only two items from the list and maximise profit
>>> from pyeasyga.pyeasyga import GeneticAlgorithm
>>> input_data = [('pear', 50), ('apple', 35), ('banana', 40)]
>>> easyga = GeneticAlgorithm(input_data)
>>> def fitness (member, data):
>>>     return sum([profit for (selected, (fruit, profit)) in
>>>                 zip(member, data) if selected and
>>>                 member.count(1) == 2])
>>> easyga.fitness_function = fitness
>>> easyga.run()
>>> print easyga.best_individual()
best_individual()[source]

Return the individual with the best fitness in the current generation.

calculate_population_fitness()[source]

Calculate the fitness of every member of the given population using the supplied fitness_function.

create_first_generation()[source]

Create the first population, calculate the population’s fitness and rank the population by fitness according to the order specified.

create_initial_population()[source]

Create members of the first population randomly.

create_new_population()[source]

Create a new population using the genetic operators (selection, crossover, and mutation) supplied.

create_next_generation()[source]

Create subsequent populations, calculate the population fitness and rank the population by fitness in the order specified.

last_generation()[source]

Return members of the last generation as a generator function.

rank_population()[source]

Sort the population by fitness according to the order defined by maximise_fitness.

run()[source]

Run (solve) the Genetic Algorithm.

Module contents

pyeasyga

A simple and easy-to-use genetic algorithm implementation library in Python.

For a bit array solution representation, simply instantiate the GeneticAlgorithm class with input data, define and supply a fitness function, run the Genetic Algorithm, and retrieve the solution!

Other solution representations will require setting some more attributes.