Changed example

This commit is contained in:
danielwilczak101
2020-09-27 23:25:16 -04:00
parent 1797d88c0b
commit 472c9c2379
2 changed files with 13 additions and 10 deletions

View File

@ -11,7 +11,7 @@ from initialization.random_initialization import random_initialization
class GA: class GA:
def __init__(self): def __init__(self):
"""Initialize the GA.""" """Initialize the GA."""
# Default variables # Default variables
self.chromosome_impl = None self.chromosome_impl = None
self.gene_impl = None self.gene_impl = None
@ -21,7 +21,7 @@ class GA:
self.chromosome_length = 3 self.chromosome_length = 3
self.population_size = 5 self.population_size = 5
self.mutation_rate = 0.03 self.mutation_rate = 0.03
# Defualt EastGA implimentation structure # Defualt EastGA implimentation structure
self.initialization_impl = random_initialization self.initialization_impl = random_initialization
self.update_fitness = True self.update_fitness = True
@ -38,18 +38,18 @@ class GA:
self.chromosome_length, self.chromosome_length,
self.chromosome_impl, self.chromosome_impl,
self.gene_impl) self.gene_impl)
def fitness_impl(self, chromosome): def fitness_impl(self, chromosome):
"""Returns the fitness of a chromosome""" """Returns the fitness of a chromosome"""
pass pass
def evolve(self): def evolve(self):
"""Runs the ga until the ga is no longer active.""" """Runs the ga until the ga is no longer active."""
# run one iteration while the ga is active # run one iteration while the ga is active
while self.active(): while self.active():
self.evolve_generation(1) self.evolve_generation(1)
def active(self): def active(self):
"""Returns if the ga should terminate or not""" """Returns if the ga should terminate or not"""
return self.current_generation < self.generations return self.current_generation < self.generations
@ -58,17 +58,17 @@ class GA:
"""Evolves the ga the specified number of generations. """Evolves the ga the specified number of generations.
If update_fitness is set then all fitness values are updated. If update_fitness is set then all fitness values are updated.
Otherwise only fitness values set to None (i.e. uninitialized fitness values) are updated.""" Otherwise only fitness values set to None (i.e. uninitialized fitness values) are updated."""
# run the specified number of times # run the specified number of times
for n in range(number_of_generations): for n in range(number_of_generations):
# for each chromosome in the population # for each chromosome in the population
for chromosome in self.population.get_all_chromosomes(): for chromosome in self.population.get_all_chromosomes():
# if the fitness should be updated, update it # if the fitness should be updated, update it
if self.update_fitness or chromosome.get_fitness() is None: if self.update_fitness or chromosome.get_fitness() is None:
chromosome.set_fitness(self.fitness_impl(chromosome)) chromosome.set_fitness(self.fitness_impl(chromosome))
# apply selection, crossover, and mutation # apply selection, crossover, and mutation
def make_gene(self,value): def make_gene(self,value):

View File

@ -3,9 +3,12 @@ import random
# Create the Genetic algorithm # Create the Genetic algorithm
ga = EasyGA.GA() ga = EasyGA.GA()
ga.chromosome_length = 3
def user_gene_domain(gene_index): def user_gene_domain(gene_index):
"""Each gene index is assosiated to its index in the chromosome""" """Each gene index is assosiated to its index in the chromosome"""
chromosome = [ chromosome = [
# Gene instructions set here
random.randrange(1,100), random.randrange(1,100),
random.uniform(10,5), random.uniform(10,5),
random.choice(["up","down"]) random.choice(["up","down"])