Added gene mutation rate

This commit is contained in:
SimpleArt
2020-10-27 17:32:40 -04:00
parent 0b5f42966c
commit 00af4dbbe7
3 changed files with 39 additions and 24 deletions

View File

@ -45,7 +45,8 @@ class attributes:
self.fitness_goal = None
# Mutation variables
self.mutation_rate = 0.10
self.chromosome_mutation_rate = 0.10
self.gene_mutation_rate = 0.01
# Default EasyGA implimentation structure
self.initialization_impl = Initialization_Methods.random_initialization
@ -86,5 +87,5 @@ class attributes:
@population_size.setter
def population_size(self, value_input):
if(value_input == 0):
raise ValueError("Population size must be greater then 0")
raise ValueError("Population length must be greater then 0")
self._population_size = value_input

View File

@ -7,13 +7,18 @@ class Mutation_Methods:
def random_selection(ga):
"""Selects random chromosomes"""
mutation_count = 0
# Loop through the population
for index in range(ga.population.size()):
# Loop until enough mutations occur
while mutation_count < ga.population.size()*ga.chromosome_mutation_rate:
# Randomly apply mutations
if random.uniform(0, 1) < ga.mutation_rate:
ga.population.set_chromosome(ga.mutation_individual_impl(ga, ga.population.get_chromosome(index)), index)
# Loop through the population
for index in range(ga.population.size()):
# Randomly apply mutations
if random.uniform(0, 1) < ga.chromosome_mutation_rate:
mutation_count += 1
ga.population.set_chromosome(ga.mutation_individual_impl(ga, ga.population.get_chromosome(index)), index)
class Individual:
@ -43,19 +48,25 @@ class Mutation_Methods:
def single_gene(ga, chromosome):
"""Mutates a random gene in the chromosome and resets the fitness."""
chromosome.set_fitness(None)
mutation_count = 0
# Using the chromosome_impl
if ga.chromosome_impl != None:
index = random.randint(0, chromosome.size()-1)
chromosome.set_gene(ga.make_gene(ga.chromosome_impl()[index]), index)
# Loops until enough mutations occur
while mutation_count < chromosome.size()*ga.gene_mutation_rate:
mutation_count += 1
# Using the gene_impl
elif ga.gene_impl != None:
index = random.randint(0, chromosome.size()-1)
chromosome.set_gene(ga.make_gene(ga.gene_impl()), index)
# Using the chromosome_impl
if ga.chromosome_impl != None:
index = random.randint(0, chromosome.size()-1)
chromosome.set_gene(ga.make_gene(ga.chromosome_impl()[index]), index)
# Exit because no gene creation method specified
else:
print("You did not specify any initialization constraints.")
# Using the gene_impl
elif ga.gene_impl != None:
index = random.randint(0, chromosome.size()-1)
chromosome.set_gene(ga.make_gene(ga.gene_impl()), index)
# Exit because no gene creation method specified
else:
print("You did not specify any initialization constraints.")
break
return chromosome

View File

@ -4,13 +4,16 @@ import EasyGA
# Create the Genetic algorithm
ga = EasyGA.GA()
# Mutate and reproduce frequently
ga.parent_ratio = 0.25
ga.mutation_rate = 0.25
# Reproduce 30% of the population.
# Mutate 20% of the population.
# Mutate 3% of the genes in each mutated chromosome.
ga.parent_ratio = 0.30
ga.chromosome_mutation_rate = 0.20
ga.gene_mutation_rate = 0.03
# Create 25 chromosomes each with 10 genes
ga.population_size = 50
ga.chromosome_length = 10
ga.population_size = 100
ga.chromosome_length = 25
# Create random genes from 0 to 10
ga.gene_impl = lambda: random.randint(0, 10)
@ -21,7 +24,7 @@ ga.target_fitness_type = 'min'
# Terminate when a chromosome has all 0's
ga.fitness_goal = 0
ga.generation_goal = None
ga.generation_goal = 150
while ga.active():
ga.evolve_generation(10)