Structure - danielwilczak101/EasyGA GitHub Wiki

On this page you will find how the structure goes from generation->population->chromosome->gene. It is very valuable to know the data structure of EasyGA. This will help with creating your own fitness function and just a better overview of the whole system.

EasyGA Structure Picture

All examples work off of the EasyGA standard example. Just put any of the examples after the ga.evolve() function.

import EasyGA

# Create the genetic algorithm
ga = EasyGA.GA()

# Evolve the genetic algorithm
ga.evolve()

Population:

To get access to the population, just use the ga.population object. As mentioned above, the population may be used as a list of chromosomes. The population also includes additional attributes and methods related to various methods used within the ga, such as the ability to print a population directly:

print(ga.population)

Output:

Chromosome - 0 [5][5][5][4][2][5][5][5][5][5] / Fitness = 8
Chromosome - 1 [5][5][5][4][2][5][5][5][5][5] / Fitness = 8
Chromosome - 2 [5][5][5][4][2][5][5][5][5][5] / Fitness = 8
Chromosome - 3 [5][5][5][4][2][5][5][5][5][5] / Fitness = 8
Chromosome - 4 [5][5][5][4][2][5][5][5][5][2] / Fitness = 7
etc.

A more detailed description can be found on the population page.

Chromosome:

To get access to a chromosome in the population, one may use ga.population[i] or to loop over the population directly (for chromosome in ga.population:). As mentioned above, chromosomes may be used as a list of genes. The chromosome also includes additional attributes (such as its fitness) and methods related to various methods used within the ga, such as the ability to print a chromosome directly:

print(ga.population[0])
print(ga.population[0].fitness)

Output:

[5][5][5][4][2][5][5][5][5][5]
8

A more detailed description can be found on the chromosome page.

Gene:

To get access to a gene in the chromosome, one may use chromosome[i] or to loop over the population directly (for gene in chromosome:). As mentioned above, chromosomes may be used as a list of genes. To extract a value from a gene, use gene.value. The gene also includes additional methods related to various methods used within the ga, such as the ability to print a gene directly:

# Get first gene in the first chromosome of the population
print(ga.population[0][0])

# Get the actual value of the first gene
print(ga.population[0][0].value)

Output:

[5]
5

A more detailed description can be found on the gene page.

Data Structure

The data structure used may be treated largely as a list. The population may be used as a list of chromosomes. The chromosomes may be used as a list of genes. This includes basic operations such as:

size_of_population = len(ga.population)

best_chromosome = ga.population[0]

for gene in best_chromosome:
    print(gene)

copied_list_of_genes = list(best_chromosome)

Full example:

import EasyGA

# Create the Genetic algorithm
ga = EasyGA.GA()

# Initialize the population
ga.evolve_generation()

population       = ga.population      # The population is sorted by best fitness.
best_chromosome  = ga.population[0]   # The best chromosome in the population.
worst_chromosome = ga.population[-1]  # The worst chromosome in the population.

# Show the best chromosome and its fitness
print("The best chromosome : ")
for gene in best_chromosome:
    print(gene, end = "")
print(f"\nFitness : {best_chromosome.fitness}")

# Show the worst chromosome and its fitness
print("The worst chromosome : ")
for gene in worst_chromosome:
    print(gene, end = "")
print(f"\nFitness : {worst_chromosome.fitness}")

Output:

The best chromosome : [5][9][1][10][5][4][5][10][2][2]
Fitness : 3
The worst chromosome : [8][4][8][2][2][10][1][4][8][1]
Fitness : 0