Test Implementation for selection/crossover/mutation

The current test implementation includes random mutation, single point crossover, and tournament selection. The implementation, in short, is a nested approach. The selection method is the only thing actually called by the GA. Both crossover and mutation occur within the selection method. As long as these three systems all follow a standard input/output system, any implementation we build, as well as any user implementations, will work perfectly. The selection function must take GA as a parameter and output a new population. Crossover takes in GA and outputs a population. Mutation takes a chromosome set and outputs a new chromosome set.

Many of the changes in this commit are regarding this test implementation. I have also changed many of the file names from "x_examples" to "x_types" and updated the class names to follow capitalziation standards. I did this because I feel personally like the built-in mutation, crossover, and selection implementations are less "examples" and more just already built implementations to make the code required from the user smaller.
This commit is contained in:
Ryley
2020-10-04 08:00:33 -04:00
parent 6aec9770b6
commit 7e587d48d0
17 changed files with 364 additions and 1 deletions

View File

@ -0,0 +1,5 @@
# FROM (. means local) file_name IMPORT function_name
from .initialization_types import Initialization_Types
from .population_structure.population import Population
from .chromosome_structure.chromosome import Chromosome
from .gene_structure.gene import Gene

View File

@ -1,6 +1,11 @@
<<<<<<< Updated upstream
class chromosome:
# fitness = Empty; genes = [gene, gene, gene, etc.]
=======
class Chromosome:
>>>>>>> Stashed changes
def __init__(self, genes = None):
if genes is None:
self.genes = []

View File

@ -3,7 +3,12 @@ def check_gene(value):
assert value != "" , "Gene can not be empty"
return value
<<<<<<< Updated upstream
class gene:
=======
class Gene:
>>>>>>> Stashed changes
def __init__(self, value):
self.fitness = None
self.value = check_gene(value)
@ -17,7 +22,12 @@ class gene:
def set_fitness(self, fitness):
self.fitness = fitness
<<<<<<< Updated upstream
def set_value(self):
=======
def set_value(self, value):
"""Set value of the gene"""
>>>>>>> Stashed changes
self.value = value
def __repr__(self):

View File

@ -0,0 +1,32 @@
# Import the data structure
from .population_structure.population import Population as create_population
from .chromosome_structure.chromosome import Chromosome as create_chromosome
from .gene_structure.gene import Gene as create_gene
class Initialization_Types:
"""Initialization examples that are used as defaults and examples"""
def random_initialization(self, ga):
"""Takes the initialization inputs and choregraphs them to output the type of population
with the given parameters."""
# Create the population object
population = create_population()
# Fill the population with chromosomes
for i in range(ga.population_size):
chromosome = create_chromosome()
#Fill the Chromosome with genes
for j in range(ga.chromosome_length):
# Using the chromosome_impl to set every index inside of the chromosome
if ga.chromosome_impl != None:
# Each chromosome location is specified with its own function
chromosome.add_gene(create_gene(ga.chromosome_impl(j)))
# Will break if chromosome_length != len(lists) in domain
elif ga.gene_impl != None:
function = ga.gene_impl[0]
chromosome.add_gene(create_gene(function(*ga.gene_impl[1:])))
else:
#Exit because either were not specified
print("You did not specify any initialization constraints.")
population.add_chromosome(chromosome)
return population

View File

@ -1,4 +1,4 @@
class population:
class Population:
# fitness = Empty; population = [chromosome, chromosome, etc.]
def __init__(self, chromosomes = None):
@ -21,7 +21,12 @@ class population:
del self.chromosomes[index]
def get_all_chromosomes(self):
<<<<<<< Updated upstream
return chromosomes
=======
"""returns all chromosomes in the population"""
return self.chromosomes
>>>>>>> Stashed changes
def get_fitness(self):
return self.fitness