Commented EasyGA.py
This commit is contained in:
@ -1,4 +1,5 @@
|
|||||||
import random
|
import random
|
||||||
|
|
||||||
# Import all the data prebuilt modules
|
# Import all the data prebuilt modules
|
||||||
from initialization.population_structure.population import population as create_population
|
from initialization.population_structure.population import population as create_population
|
||||||
from initialization.chromosome_structure.chromosome import chromosome as create_chromosome
|
from initialization.chromosome_structure.chromosome import chromosome as create_chromosome
|
||||||
@ -7,9 +8,10 @@ from initialization.gene_structure.gene import gene as create_gene
|
|||||||
# Import functionality defaults
|
# Import functionality defaults
|
||||||
from initialization.random_initialization import random_initialization
|
from initialization.random_initialization import random_initialization
|
||||||
|
|
||||||
|
|
||||||
class GA:
|
class GA:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
|
"""Initialize the GA."""
|
||||||
|
|
||||||
# Default variables
|
# Default variables
|
||||||
self.chromosome_impl = None
|
self.chromosome_impl = None
|
||||||
self.gene_impl = None
|
self.gene_impl = None
|
||||||
@ -19,6 +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
|
||||||
@ -28,7 +31,8 @@ class GA:
|
|||||||
#self.termination_impl = GenerationTermination(Total_generations)
|
#self.termination_impl = GenerationTermination(Total_generations)
|
||||||
#self.evaluation_impl = TestEvaluation()
|
#self.evaluation_impl = TestEvaluation()
|
||||||
|
|
||||||
def initialize(self):
|
def initialize_population(self):
|
||||||
|
"""Initialize the population"""
|
||||||
self.population = self.initialization_impl(
|
self.population = self.initialization_impl(
|
||||||
self.population_size,
|
self.population_size,
|
||||||
self.chromosome_length,
|
self.chromosome_length,
|
||||||
@ -43,7 +47,11 @@ class GA:
|
|||||||
"""Updates the ga to the next generation.
|
"""Updates the ga to the next generation.
|
||||||
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."""
|
||||||
|
|
||||||
|
# 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 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))
|
||||||
|
|
||||||
@ -52,15 +60,18 @@ class GA:
|
|||||||
return self.current_generation < self.generations
|
return self.current_generation < self.generations
|
||||||
|
|
||||||
def evolve_generation(self, number_of_generations):
|
def evolve_generation(self, number_of_generations):
|
||||||
# If you want to evolve through a number of generations
|
"""Evolves the ga the specified number of generations."""
|
||||||
# and be able to pause and output data based on that generation run.
|
for n in range(number_of_generations):
|
||||||
pass
|
self.evolve()
|
||||||
|
|
||||||
def make_gene(self,value):
|
def make_gene(self,value):
|
||||||
|
"""Let's the user create a gene."""
|
||||||
return create_gene(value)
|
return create_gene(value)
|
||||||
|
|
||||||
def make_chromosome(self):
|
def make_chromosome(self):
|
||||||
|
"""Let's the user create a chromosome."""
|
||||||
return create_chromosome()
|
return create_chromosome()
|
||||||
|
|
||||||
def make_population(self):
|
def make_population(self):
|
||||||
|
"""Let's the user create a population."""
|
||||||
return create_population()
|
return create_population()
|
||||||
|
|||||||
@ -15,6 +15,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()
|
ga.initialize_population()
|
||||||
|
|
||||||
ga.population.print_all()
|
ga.population.print_all()
|
||||||
|
|||||||
Reference in New Issue
Block a user