Added fitness function and changed evolve function
This commit is contained in:
@ -4,7 +4,7 @@ import random
|
|||||||
from initialization import population as create_population
|
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
|
||||||
|
from fitness_function import default_fitness_example as default_fitness_example
|
||||||
# Import functionality defaults
|
# Import functionality defaults
|
||||||
from initialization import random_initialization
|
from initialization import random_initialization
|
||||||
|
|
||||||
@ -24,13 +24,16 @@ class GA:
|
|||||||
|
|
||||||
# Defualt EastGA implimentation structure
|
# Defualt EastGA implimentation structure
|
||||||
self.initialization_impl = random_initialization
|
self.initialization_impl = random_initialization
|
||||||
self.update_fitness = True
|
self.fitness_funciton_impl = default_fitness_example
|
||||||
#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 = GenerationTermination(Total_generations)
|
#self.termination_impl = GenerationTermination(Total_generations)
|
||||||
#self.evaluation_impl = TestEvaluation()
|
#self.evaluation_impl = TestEvaluation()
|
||||||
|
|
||||||
|
# If we want the fitnesses to be updated by the computer
|
||||||
|
self.update_fitness = True
|
||||||
|
|
||||||
def initialize_population(self):
|
def initialize_population(self):
|
||||||
"""Initialize the population"""
|
"""Initialize the population"""
|
||||||
self.population = self.initialization_impl(
|
self.population = self.initialization_impl(
|
||||||
@ -39,12 +42,13 @@ class GA:
|
|||||||
self.chromosome_impl,
|
self.chromosome_impl,
|
||||||
self.gene_impl)
|
self.gene_impl)
|
||||||
|
|
||||||
def fitness_impl(self, chromosome):
|
|
||||||
"""Returns the fitness of a chromosome"""
|
|
||||||
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."""
|
||||||
|
while(ga.active())
|
||||||
|
if(self.current_generation == 0):
|
||||||
|
initialize_population()
|
||||||
|
|
||||||
|
get_fitness(population)
|
||||||
|
|
||||||
# run one iteration while the ga is active
|
# run one iteration while the ga is active
|
||||||
while self.active():
|
while self.active():
|
||||||
@ -52,7 +56,8 @@ class GA:
|
|||||||
|
|
||||||
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.termination_impl.active(self)
|
||||||
|
|
||||||
|
|
||||||
def evolve_generation(self, number_of_generations):
|
def evolve_generation(self, number_of_generations):
|
||||||
"""Evolves the ga the specified number of generations.
|
"""Evolves the ga the specified number of generations.
|
||||||
|
|||||||
2
src/fitness_function/default_fitness_example.py
Normal file
2
src/fitness_function/default_fitness_example.py
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
def default_fitness_example():
|
||||||
|
pass
|
||||||
@ -1,5 +1,6 @@
|
|||||||
# __init__.py
|
# __init__.py
|
||||||
from .random_initialization import random_initialization
|
from .random_initialization import random_initialization
|
||||||
from .population_structure.population import population
|
from .population_structure.population import population
|
||||||
from .chromosome_structure.chromosome import chromosome
|
from .chromosome_structure.chromosome import chromosome
|
||||||
from .gene_structure.gene import gene
|
from .gene_structure.gene import gene
|
||||||
|
from .fitness_function import default_fitness_example
|
||||||
|
|||||||
@ -18,6 +18,6 @@ def user_gene_domain(gene_index):
|
|||||||
# If the user wants to use a domain
|
# If the user wants to use a domain
|
||||||
ga.chromosome_impl = user_gene_domain
|
ga.chromosome_impl = user_gene_domain
|
||||||
|
|
||||||
ga.initialize_population()
|
ga.evolve()
|
||||||
|
|
||||||
ga.population.print_all()
|
ga.population.print_all()
|
||||||
|
|||||||
Reference in New Issue
Block a user