Changes from meeting
This commit is contained in:
@ -5,7 +5,7 @@ from initialization import population as create_population
|
|||||||
from initialization import chromosome as create_chromosome
|
from initialization import chromosome as create_chromosome
|
||||||
from initialization import gene as create_gene
|
from initialization import gene as create_gene
|
||||||
# Import the default fitness function
|
# Import the default fitness function
|
||||||
from fitness_function import is_the_value_5
|
from fitness_function import example_is_it_5
|
||||||
# Import default termination points
|
# Import default termination points
|
||||||
from termination_point import generation_based
|
from termination_point import generation_based
|
||||||
from termination_point import fitness_based
|
from termination_point import fitness_based
|
||||||
@ -29,14 +29,14 @@ class GA:
|
|||||||
# Mutation variables
|
# Mutation variables
|
||||||
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.fitness_funciton_impl = is_the_value_5
|
self.fitness_funciton_impl = example_is_it_5
|
||||||
#self.mutation_impl = PerGeneMutation(Mutation_rate)
|
#self.mutation_impl = PerGeneMutation(Mutation_rate)
|
||||||
#self.selection_impl = TournamentSelection()
|
#self.selection_impl = TournamentSelection()
|
||||||
#self.crossover_impl = FastSinglePointCrossover()
|
#self.crossover_impl = FastSinglePointCrossover()
|
||||||
self.termination_impl = generation_based
|
self.termination_impl = generation_based
|
||||||
#self.evaluation_impl = TestEvaluation()
|
|
||||||
|
|
||||||
# If we want the fitnesses to be updated by the computer
|
# If we want the fitnesses to be updated by the computer
|
||||||
self.update_fitness = True
|
self.update_fitness = True
|
||||||
@ -61,17 +61,7 @@ class GA:
|
|||||||
"""Runs the ga until the termination point has been satisfied."""
|
"""Runs the ga until the termination point has been satisfied."""
|
||||||
# While the termination point hasnt been reached keep running
|
# While the termination point hasnt been reached keep running
|
||||||
while(self.active()):
|
while(self.active()):
|
||||||
# If its the first generation then initialize the population
|
self.evolve_generation()
|
||||||
if(self.current_generation == 0):
|
|
||||||
# Initialize the population
|
|
||||||
self.initialize_population()
|
|
||||||
# First get the fitness of the population
|
|
||||||
self.get_population_fitness(self.population.chromosomes)
|
|
||||||
|
|
||||||
print(f"Ive completed generation - {self.current_generation}")
|
|
||||||
|
|
||||||
self.current_generation += 1
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def active(self):
|
def active(self):
|
||||||
@ -80,17 +70,30 @@ class GA:
|
|||||||
return self.termination_impl(self)
|
return self.termination_impl(self)
|
||||||
|
|
||||||
|
|
||||||
def evolve_generation(self, number_of_generations):
|
def evolve_generation(self, number_of_generations = 1):
|
||||||
"""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
|
Otherwise only fitness values set to None (i.e. uninitialized
|
||||||
fitness values) are updated."""
|
fitness values) are updated."""
|
||||||
|
while(number_of_generations > 0):
|
||||||
|
# If its the first generation then initialize the population
|
||||||
|
if(self.current_generation == 0):
|
||||||
|
# Initialize the population
|
||||||
|
self.initialize_population()
|
||||||
|
# First get the fitness of the population
|
||||||
|
self.get_population_fitness(self.population.chromosomes)
|
||||||
|
|
||||||
# run the specified number of times
|
#selecion -> crossover -> mutation
|
||||||
#for n in range(number_of_generations):
|
#self.selection_impl(self)
|
||||||
|
#self.crossover_impl(self)
|
||||||
|
#self.mutation_impl(self)
|
||||||
|
|
||||||
|
#next_population.append(mutated_offspring)
|
||||||
|
|
||||||
# apply selection, crossover, and mutation
|
# Counter for the local number of generations in evolve_generation
|
||||||
|
number_of_generations -= 1
|
||||||
|
# Add one to the current overall generation
|
||||||
|
self.current_generation += 1
|
||||||
|
|
||||||
def make_gene(self,value):
|
def make_gene(self,value):
|
||||||
"""Let's the user create a gene."""
|
"""Let's the user create a gene."""
|
||||||
|
|||||||
@ -1,2 +1,2 @@
|
|||||||
# FROM (. means local) file_name IMPORT function_name
|
# FROM (. means local) file_name IMPORT function_name
|
||||||
from .is_the_value_5 import is_the_value_5
|
from .example_is_it_5 import example_is_it_5
|
||||||
|
|||||||
@ -1,7 +1,6 @@
|
|||||||
def is_the_value_5(chromosome):
|
def example_is_it_5(chromosome):
|
||||||
"""A very simple case test function - If the chromosomes gene value is a 5 add one
|
"""A very simple case test function - If the chromosomes gene value is a 5 add one
|
||||||
to the chromosomes overall fitness value."""
|
to the chromosomes overall fitness value."""
|
||||||
|
|
||||||
# Overall fitness value
|
# Overall fitness value
|
||||||
fitness = 0
|
fitness = 0
|
||||||
# For each gene in the chromosome
|
# For each gene in the chromosome
|
||||||
@ -1,12 +1,15 @@
|
|||||||
class chromosome:
|
class chromosome:
|
||||||
|
|
||||||
def __init__(self, genes = None):
|
def __init__(self, genes = None):
|
||||||
"""Initialize the chromosome based on input gene list, defaulted to an empty list"""
|
"""Initialize the chromosome based on input gene list, defaulted to an empty list"""
|
||||||
if genes is None:
|
if genes is None:
|
||||||
self.genes = []
|
self.genes = []
|
||||||
else:
|
else:
|
||||||
self.genes = genes
|
self.genes = genes
|
||||||
|
# The fitness of the overal chromosome
|
||||||
self.fitness = None
|
self.fitness = None
|
||||||
|
# If the chromosome has been selected then the flag would switch to true
|
||||||
|
self.selected = False
|
||||||
|
|
||||||
def add_gene(self, gene, index = -1):
|
def add_gene(self, gene, index = -1):
|
||||||
"""Add a gene to the chromosome at the specified index, defaulted to end of the chromosome"""
|
"""Add a gene to the chromosome at the specified index, defaulted to end of the chromosome"""
|
||||||
|
|||||||
@ -8,6 +8,8 @@ ga.max_generations = 5
|
|||||||
# If the user wants to use a domain
|
# If the user wants to use a domain
|
||||||
ga.gene_impl = [random.randrange,1,10]
|
ga.gene_impl = [random.randrange,1,10]
|
||||||
|
|
||||||
|
ga.generation = 36
|
||||||
|
|
||||||
# Run Everyhting
|
# Run Everyhting
|
||||||
ga.evolve()
|
ga.evolve()
|
||||||
|
|
||||||
|
|||||||
@ -1,4 +1,6 @@
|
|||||||
def generation_based(ga):
|
def generation_based(ga):
|
||||||
|
"""Generation based approach to termination - If the current generation is
|
||||||
|
less then the """
|
||||||
status = True
|
status = True
|
||||||
if(ga.current_generation > ga.max_generations):
|
if(ga.current_generation > ga.max_generations):
|
||||||
status = False
|
status = False
|
||||||
|
|||||||
Reference in New Issue
Block a user