Added gene mutation rate
This commit is contained in:
@ -45,7 +45,8 @@ class attributes:
|
|||||||
self.fitness_goal = None
|
self.fitness_goal = None
|
||||||
|
|
||||||
# Mutation variables
|
# Mutation variables
|
||||||
self.mutation_rate = 0.10
|
self.chromosome_mutation_rate = 0.10
|
||||||
|
self.gene_mutation_rate = 0.01
|
||||||
|
|
||||||
# Default EasyGA implimentation structure
|
# Default EasyGA implimentation structure
|
||||||
self.initialization_impl = Initialization_Methods.random_initialization
|
self.initialization_impl = Initialization_Methods.random_initialization
|
||||||
@ -86,5 +87,5 @@ class attributes:
|
|||||||
@population_size.setter
|
@population_size.setter
|
||||||
def population_size(self, value_input):
|
def population_size(self, value_input):
|
||||||
if(value_input == 0):
|
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
|
self._population_size = value_input
|
||||||
|
|||||||
@ -7,12 +7,17 @@ class Mutation_Methods:
|
|||||||
|
|
||||||
def random_selection(ga):
|
def random_selection(ga):
|
||||||
"""Selects random chromosomes"""
|
"""Selects random chromosomes"""
|
||||||
|
mutation_count = 0
|
||||||
|
|
||||||
|
# Loop until enough mutations occur
|
||||||
|
while mutation_count < ga.population.size()*ga.chromosome_mutation_rate:
|
||||||
|
|
||||||
# Loop through the population
|
# Loop through the population
|
||||||
for index in range(ga.population.size()):
|
for index in range(ga.population.size()):
|
||||||
|
|
||||||
# Randomly apply mutations
|
# Randomly apply mutations
|
||||||
if random.uniform(0, 1) < ga.mutation_rate:
|
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)
|
ga.population.set_chromosome(ga.mutation_individual_impl(ga, ga.population.get_chromosome(index)), index)
|
||||||
|
|
||||||
|
|
||||||
@ -43,6 +48,11 @@ class Mutation_Methods:
|
|||||||
def single_gene(ga, chromosome):
|
def single_gene(ga, chromosome):
|
||||||
"""Mutates a random gene in the chromosome and resets the fitness."""
|
"""Mutates a random gene in the chromosome and resets the fitness."""
|
||||||
chromosome.set_fitness(None)
|
chromosome.set_fitness(None)
|
||||||
|
mutation_count = 0
|
||||||
|
|
||||||
|
# Loops until enough mutations occur
|
||||||
|
while mutation_count < chromosome.size()*ga.gene_mutation_rate:
|
||||||
|
mutation_count += 1
|
||||||
|
|
||||||
# Using the chromosome_impl
|
# Using the chromosome_impl
|
||||||
if ga.chromosome_impl != None:
|
if ga.chromosome_impl != None:
|
||||||
@ -57,5 +67,6 @@ class Mutation_Methods:
|
|||||||
# Exit because no gene creation method specified
|
# Exit because no gene creation method specified
|
||||||
else:
|
else:
|
||||||
print("You did not specify any initialization constraints.")
|
print("You did not specify any initialization constraints.")
|
||||||
|
break
|
||||||
|
|
||||||
return chromosome
|
return chromosome
|
||||||
|
|||||||
@ -4,13 +4,16 @@ import EasyGA
|
|||||||
# Create the Genetic algorithm
|
# Create the Genetic algorithm
|
||||||
ga = EasyGA.GA()
|
ga = EasyGA.GA()
|
||||||
|
|
||||||
# Mutate and reproduce frequently
|
# Reproduce 30% of the population.
|
||||||
ga.parent_ratio = 0.25
|
# Mutate 20% of the population.
|
||||||
ga.mutation_rate = 0.25
|
# 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
|
# Create 25 chromosomes each with 10 genes
|
||||||
ga.population_size = 50
|
ga.population_size = 100
|
||||||
ga.chromosome_length = 10
|
ga.chromosome_length = 25
|
||||||
|
|
||||||
# Create random genes from 0 to 10
|
# Create random genes from 0 to 10
|
||||||
ga.gene_impl = lambda: random.randint(0, 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
|
# Terminate when a chromosome has all 0's
|
||||||
ga.fitness_goal = 0
|
ga.fitness_goal = 0
|
||||||
ga.generation_goal = None
|
ga.generation_goal = 150
|
||||||
|
|
||||||
while ga.active():
|
while ga.active():
|
||||||
ga.evolve_generation(10)
|
ga.evolve_generation(10)
|
||||||
|
|||||||
Reference in New Issue
Block a user