Added blank lines and fixed run_testing
This commit is contained in:
@ -1,20 +1,25 @@
|
||||
import random
|
||||
|
||||
# Import all the data structure prebuilt modules
|
||||
from initialization import Population as create_population
|
||||
from initialization import Chromosome as create_chromosome
|
||||
from initialization import Gene as create_gene
|
||||
|
||||
# Structure Methods
|
||||
from fitness_function import Fitness_Examples
|
||||
from initialization import Initialization_Methods
|
||||
from termination_point import Termination_Methods
|
||||
|
||||
# Population Methods
|
||||
from survivor_selection import Survivor_Selection
|
||||
from parent_selection import Parent_Selection
|
||||
|
||||
# Manipulation Methods
|
||||
from mutation import Mutation_Methods
|
||||
from crossover import Crossover_Methods
|
||||
|
||||
class GA:
|
||||
|
||||
def __init__(self):
|
||||
"""Initialize the GA."""
|
||||
# Initilization variables
|
||||
@ -44,12 +49,15 @@ class GA:
|
||||
# Default EasyGA implimentation structure
|
||||
self.initialization_impl = Initialization_Methods.random_initialization
|
||||
self.fitness_function_impl = Fitness_Examples.index_dependent_values
|
||||
|
||||
# Selects which chromosomes should be automaticly moved to the next population
|
||||
self.survivor_selection_impl = Survivor_Selection.fill_in_best
|
||||
|
||||
# Methods for accomplishing parent-selection -> Crossover -> Mutation
|
||||
self.parent_selection_impl = Parent_Selection.Tournament.with_replacement
|
||||
self.crossover_impl = Crossover_Methods.single_point_crossover
|
||||
self.mutation_impl = Mutation_Methods.per_gene_mutation
|
||||
|
||||
# The type of termination to impliment
|
||||
self.termination_impl = Termination_Methods.generation_based
|
||||
|
||||
@ -76,22 +84,26 @@ class GA:
|
||||
|
||||
self.current_generation += 1
|
||||
|
||||
|
||||
def evolve(self):
|
||||
"""Runs the ga until the termination point has been satisfied."""
|
||||
# While the termination point hasnt been reached keep running
|
||||
while(self.active()):
|
||||
self.evolve_generation()
|
||||
|
||||
|
||||
def active(self):
|
||||
"""Returns if the ga should terminate base on the termination implimented"""
|
||||
# Send termination_impl the whole ga class
|
||||
return self.termination_impl(self)
|
||||
|
||||
|
||||
def initialize_population(self):
|
||||
"""Initialize the population using the initialization
|
||||
implimentation that is currently set"""
|
||||
self.population = self.initialization_impl(self)
|
||||
|
||||
|
||||
def set_all_fitness(self,chromosome_set):
|
||||
"""Will get and set the fitness of each chromosome in the population.
|
||||
If update_fitness is set then all fitness values are updated.
|
||||
@ -104,6 +116,7 @@ class GA:
|
||||
# Set the chromosomes fitness using the fitness function
|
||||
chromosome.set_fitness(self.fitness_function_impl(chromosome))
|
||||
|
||||
|
||||
def sort_by_best_fitness(self, chromosome_set):
|
||||
|
||||
chromosome_set_temp = chromosome_set
|
||||
@ -120,14 +133,17 @@ class GA:
|
||||
|
||||
return chromosome_set
|
||||
|
||||
|
||||
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()
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
class Fitness_Examples:
|
||||
"""Fitness function examples used"""
|
||||
|
||||
def is_it_5(chromosome):
|
||||
"""A very simple case test function - If the chromosomes gene value is a 5 add one
|
||||
to the chromosomes overall fitness value."""
|
||||
@ -15,6 +16,7 @@ class Fitness_Examples:
|
||||
|
||||
return fitness
|
||||
|
||||
|
||||
def index_dependent_values(chromosome):
|
||||
"""Test of the GA's ability to improve fitness when the value is index-dependent"""
|
||||
"""If a gene is equal to its index in the chromosome + 1, fitness is incremented"""
|
||||
|
||||
@ -1,4 +1,5 @@
|
||||
class Chromosome:
|
||||
|
||||
def __init__(self, gene_list = None):
|
||||
if gene_list is None:
|
||||
self.gene_list = []
|
||||
@ -9,36 +10,45 @@ class Chromosome:
|
||||
# If the chromosome has been selected then the flag would switch to true
|
||||
self.selected = False
|
||||
|
||||
|
||||
def size(self):
|
||||
"""Returns the number of genes in the chromosome"""
|
||||
return len(self.gene_list)
|
||||
|
||||
|
||||
def add_gene(self, gene, index = -1):
|
||||
"""Add a gene to the chromosome at the specified index, defaulted to end of the chromosome"""
|
||||
if index == -1:
|
||||
index = len(self.gene_list)
|
||||
self.gene_list.insert(index, gene)
|
||||
|
||||
|
||||
def remove_gene(self, index):
|
||||
del self.gene_list[index]
|
||||
|
||||
|
||||
def get_genes(self):
|
||||
return self.gene_list
|
||||
|
||||
|
||||
def get_fitness(self):
|
||||
"""Return the fitness of the chromosome"""
|
||||
return self.fitness
|
||||
|
||||
|
||||
def set_gene(self, gene, index):
|
||||
self.gene_list[index] = gene
|
||||
|
||||
|
||||
def set_genes(self, genes):
|
||||
self.gene_list = genes
|
||||
|
||||
|
||||
def set_fitness(self, fitness):
|
||||
"""Set the fitness value of the chromosome"""
|
||||
self.fitness = fitness
|
||||
|
||||
|
||||
def __repr__(self):
|
||||
"""Format the repr() output for the chromosome"""
|
||||
output_str = ''
|
||||
|
||||
@ -3,6 +3,7 @@ def check_gene(value):
|
||||
assert value != "" , "Gene can not be empty"
|
||||
return value
|
||||
|
||||
|
||||
class Gene:
|
||||
|
||||
def __init__(self, value):
|
||||
@ -10,22 +11,27 @@ class Gene:
|
||||
self.fitness = None
|
||||
self.value = check_gene(value)
|
||||
|
||||
|
||||
def get_fitness(self):
|
||||
"""Return fitness of the gene"""
|
||||
return self.fitness
|
||||
|
||||
|
||||
def get_value(self):
|
||||
"""Return value of the gene"""
|
||||
return self.value
|
||||
|
||||
|
||||
def set_fitness(self, fitness):
|
||||
"""Set fitness of the gene"""
|
||||
self.fitness = fitness
|
||||
|
||||
|
||||
def set_value(self, value):
|
||||
"""Set value of the gene"""
|
||||
self.value = value
|
||||
|
||||
|
||||
def __repr__(self):
|
||||
"""Format the repr() output value"""
|
||||
return f'[{self.value}]'
|
||||
|
||||
@ -28,5 +28,6 @@ class Initialization_Methods:
|
||||
else:
|
||||
#Exit because either were not specified
|
||||
print("You did not specify any initialization constraints.")
|
||||
break
|
||||
population.add_chromosome(chromosome)
|
||||
return population
|
||||
|
||||
@ -1,60 +1,79 @@
|
||||
class Population:
|
||||
|
||||
def __init__(self, chromosome_list = None):
|
||||
"""Intiialize the population with fitness of value None, and a set of chromosomes dependant on user-passed parameter"""
|
||||
|
||||
if chromosome_list is None:
|
||||
self.chromosome_list = []
|
||||
else:
|
||||
self.chromosome_list = chromosome_list
|
||||
|
||||
self.fitness = None
|
||||
self.mating_pool = []
|
||||
|
||||
|
||||
def size(self):
|
||||
"""Returns the size of the population"""
|
||||
return len(self.chromosome_list)
|
||||
|
||||
|
||||
def get_closet_fitness(self,value):
|
||||
"""Get the chomosome that has the closets fitness to the value defined"""
|
||||
pass
|
||||
|
||||
|
||||
def add_chromosome(self, chromosome, index = -1):
|
||||
"""Adds a chromosome to the population at the input index, defaulted to the end of the chromosome set"""
|
||||
if index == -1:
|
||||
index = len(self.chromosome_list)
|
||||
self.chromosome_list.insert(index, chromosome)
|
||||
|
||||
|
||||
def remove_chromosome(self, index):
|
||||
"""removes a chromosome from the indicated index"""
|
||||
del self.chromosome_list[index]
|
||||
|
||||
|
||||
def get_all_chromosomes(self):
|
||||
"""returns all chromosomes in the population"""
|
||||
return self.chromosome_list
|
||||
|
||||
|
||||
def get_fitness(self):
|
||||
"""returns the population's fitness"""
|
||||
return self.fitness
|
||||
|
||||
|
||||
def set_all_chromosomes(self, chromosomes):
|
||||
self.chromosome_list = chromosomes
|
||||
|
||||
|
||||
def set_chromosome(self, chromosome, index = -1):
|
||||
if index == -1:
|
||||
index = len(self.chromosomes)-1
|
||||
self.chromosome_list[index] = chromosome
|
||||
|
||||
|
||||
def set_fitness(self, fitness):
|
||||
"""Sets the fitness value of the population"""
|
||||
self.fitness = fitness
|
||||
|
||||
|
||||
def __repr__(self):
|
||||
for index in range(len(self.chromosomes)):
|
||||
return f'{self.chromosome_list[index]}'
|
||||
"""Returns a string representation of the entire population"""
|
||||
pass
|
||||
|
||||
|
||||
def print_all(self):
|
||||
"""Prints information about the population in the following format:"""
|
||||
"""Ex .Current population"""
|
||||
"""Chromosome 1 - [gene][gene][gene][.etc] / Chromosome fitness - """
|
||||
"""Prints information about the population in the following format:
|
||||
Current population
|
||||
Chromosome 1 - [gene][gene][gene][.etc] / Chromosome fitness -
|
||||
Chromosome 2 - [gene][gene][gene][.etc] / Chromosome fitness -
|
||||
etc.
|
||||
"""
|
||||
|
||||
print("Current population:")
|
||||
for index in range(len(self.chromosome_list)):
|
||||
|
||||
for index in range(self.size()):
|
||||
print(f'Chromosome - {index} {self.chromosome_list[index]}', end = "")
|
||||
print(f' / Fitness = {self.chromosome_list[index].fitness}')
|
||||
print(f' / Fitness = {self.chromosome_list[index].get_fitness()}')
|
||||
|
||||
@ -5,7 +5,9 @@ from initialization.population_structure.population import Population
|
||||
from initialization.chromosome_structure.chromosome import Chromosome
|
||||
|
||||
class Parent_Selection:
|
||||
|
||||
class Tournament:
|
||||
|
||||
def with_replacement(ga):
|
||||
"""
|
||||
Will make tournaments of size tournament_size and choose the winner (best fitness) from the tournament and use it as a parent for the next generation
|
||||
@ -33,7 +35,9 @@ class Parent_Selection:
|
||||
if random.uniform(0, 1) < selection_probability * pow(1-selection_probability, index):
|
||||
ga.population.mating_pool.append(tournament_group[index])
|
||||
|
||||
|
||||
class Roulette:
|
||||
|
||||
def roulette_selection(ga):
|
||||
"""Roulette selection works based off of how strong the fitness is of the
|
||||
chromosomes in the population. The stronger the fitness the higher the probability
|
||||
@ -41,6 +45,7 @@ class Parent_Selection:
|
||||
Where the chromosomes are the numbers to be selected and the board size for
|
||||
those numbers are directly proportional to the chromosome's current fitness. Where
|
||||
the ball falls is a randomly generated number between 0 and 1"""
|
||||
|
||||
total_fitness = sum(ga.population.chromosome_list[i].get_fitness() for i in range(ga.population.size()))
|
||||
rel_fitnesses = []
|
||||
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
import EasyGA
|
||||
import random
|
||||
import EasyGA
|
||||
|
||||
# Create the Genetic algorithm
|
||||
ga = EasyGA.GA()
|
||||
@ -8,6 +8,7 @@ ga.population_size = 100
|
||||
ga.chromosome_length = 10
|
||||
ga.generation_goal = 100
|
||||
ga.gene_impl = [random.randint,1,10]
|
||||
ga.parent_selection_impl = EasyGA.Parent_Selection.Roulette.roulette_selection
|
||||
|
||||
ga.evolve()
|
||||
|
||||
|
||||
@ -4,13 +4,21 @@ class Termination_Methods:
|
||||
def fitness_based(ga):
|
||||
"""Fitness based approach to terminate when the goal fitness has been reached"""
|
||||
|
||||
# Need to start the algorithm if the population is None
|
||||
if ga.population == None:
|
||||
return True
|
||||
for i in range(ga.population.size()):
|
||||
if(ga.population.get_all_chromosomes()[i].fitness >= ga.fitness_goal):
|
||||
|
||||
# Check all chromosomes
|
||||
for chromosome in ga.population.get_all_chromosomes():
|
||||
|
||||
# Stop if a chromosome has reached the fitness_goal
|
||||
if(chromosome.fitness >= ga.fitness_goal):
|
||||
return False
|
||||
|
||||
# Continue if no chromosomes have reached the fitness goal
|
||||
return True
|
||||
|
||||
|
||||
def generation_based(ga):
|
||||
"""Generation based approach to terminate when the goal generation has been reached"""
|
||||
|
||||
|
||||
Reference in New Issue
Block a user