Commented EasyGA.py

This commit is contained in:
SimpleArt
2020-09-27 17:26:56 -04:00
parent d1334090a8
commit e66b4d7fd0
2 changed files with 17 additions and 6 deletions

View File

@ -1,4 +1,5 @@
import random
# Import all the data prebuilt modules
from initialization.population_structure.population import population as create_population
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
from initialization.random_initialization import random_initialization
class GA:
def __init__(self):
"""Initialize the GA."""
# Default variables
self.chromosome_impl = None
self.gene_impl = None
@ -19,6 +21,7 @@ class GA:
self.chromosome_length = 3
self.population_size = 5
self.mutation_rate = 0.03
# Defualt EastGA implimentation structure
self.initialization_impl = random_initialization
self.update_fitness = True
@ -28,7 +31,8 @@ class GA:
#self.termination_impl = GenerationTermination(Total_generations)
#self.evaluation_impl = TestEvaluation()
def initialize(self):
def initialize_population(self):
"""Initialize the population"""
self.population = self.initialization_impl(
self.population_size,
self.chromosome_length,
@ -43,7 +47,11 @@ class GA:
"""Updates the ga to the next generation.
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."""
# for each chromosome in the population
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:
chromosome.set_fitness(self.fitness_impl(chromosome))
@ -52,15 +60,18 @@ class GA:
return self.current_generation < self.generations
def evolve_generation(self, number_of_generations):
# If you want to evolve through a number of generations
# and be able to pause and output data based on that generation run.
pass
"""Evolves the ga the specified number of generations."""
for n in range(number_of_generations):
self.evolve()
def make_gene(self,value):
"""Let's the user create a gene."""
return create_gene(value)
def make_chromosome(self):
"""Let's the user create a chromosome."""
return create_chromosome()
def make_population(self):
"""Let's the user create a population."""
return create_population()

View File

@ -15,6 +15,6 @@ def user_gene_domain(gene_index):
# If the user wants to use a domain
ga.chromosome_impl = user_gene_domain
ga.initialize()
ga.initialize_population()
ga.population.print_all()