Gene Chromosome - danielwilczak101/EasyGA GitHub Wiki
In this sections we will talk about how to create your genes and chromosomes.
All genes the same range
ga.gene_impl is used if you want to set all the genes to the same range with one function.
Each gene to a different range
ga.chromosome_impl is used if you want to set each gene to a different range in the chromosome.
ga.gene_impl
ga.gene_impl is used if you want to set all the genes to the same range with one function.
Here is an example that all genes with random integers between 1,10.
import EasyGA
import random
# Create the Genetic algorithm
ga = EasyGA.GA()
ga.population_size = 3
ga.chromosome_length = 5
ga.generation_goal = 5
ga.gene_impl = lambda: random.randint(1, 10)
ga.evolve()
# Print out the current population
ga.print_population()
Output
Current population:
Chromosome - 0 [5][5][10][7][5] / Fitness = 3
Chromosome - 1 [5][5][10][7][3] / Fitness = 2
Chromosome - 2 [4][5][10][7][3] / Fitness = 1
Chromosome - 3 [5][3][8][2][8] / Fitness = 1
Chromosome - 4 [4][3][8][2][8] / Fitness = 0
Chromosome - 5 [4][3][8][2][8] / Fitness = 0
Using the random.randint function is built into the standard python library. EasyGA can accept any type of function. There is a lot of different types of functions but here are some commonly used functions. A full list can be found here.
ga.gene_impl = lambda: random.randint(1, 10) # For random integers
ga.gene_impl = lambda: random.uniform(1, 10) # For random floats
ga.gene_impl = lambda: random.choice(["up","down","left","right"]) # Picking randomly from list
ga.chromosome_impl:
ga.chromosome_impl is used if you want to set each gene to a different range.
One thing to specify is that the chromosome_length must be the same as chromosome instruction or it will not work.
Using lambdas
If the user is familiar with lambdas, they may also be used.
import EasyGA
import random
# Create the Genetic algorithm
ga = EasyGA.GA()
ga.chromosome_length = 3
ga.generation_goal = 0
# If the user wants to construct whole chromosomes
# simultaneously, or not all genes are created the
# same way.
ga.chromosome_impl = lambda: [
random.randrange(1,100),
random.uniform(10,5),
random.choice(["up","down"])
]
ga.evolve()
# Print out the current population
ga.print_population()
Using functions
Chromosomes can be created using functions quite easily.
import EasyGA
import random
# Create the Genetic algorithm
ga = EasyGA.GA()
ga.chromosome_length = 3
ga.generation_goal = 0
# If the user wants to construct whole chromosomes
# simultaneously, or not all genes are created the
# same way.
def user_chromosome_function():
"""Return a list of data representing a chromosome"""
chromosome_data = [
# Gene instructions set here
random.randrange(1,100),
random.uniform(10,5),
random.choice(["up","down"])
]
return chromosome_data
ga.chromosome_impl = user_chromosome_function
ga.evolve()
# Print out the current population
ga.print_population()
Output
Current population:
Chromosome - 0 [49][9.500000665533763][up] / Fitness = 0
Chromosome - 1 [50][7.367734655276749][down] / Fitness = 0
Chromosome - 2 [39][8.602711219698001][down] / Fitness = 0
Chromosome - 3 [19][8.503058596284955][down] / Fitness = 0
Chromosome - 4 [16][9.62605366279468][up] / Fitness = 0
Chromosome - 5 [93][5.68751532740186][down] / Fitness = 0
See how to setup the fitness function. There are two main ways Maximize Fitness or Minimize Fitness in your genetic algorithm.