Several Changes

Crossover/Mutation:
- Split into individual and population subclasses.
- Added sequential population crossover selection.
- Renamed and reimplemented mutation methods.

EasyGA:
- Improved make_obj methods for the chromosomes and populations to take arguments.

Initialization:
- Improved to shorter code.
- Fixed repeated error messages

Chromosome:
- Changed get/set_genes to get/set_gene_list.
This commit is contained in:
SimpleArt
2020-10-12 21:39:17 -04:00
parent 55c92d920f
commit b6ae77c7ea
5 changed files with 114 additions and 103 deletions

View File

@ -27,7 +27,7 @@ class Chromosome:
del self.gene_list[index]
def get_genes(self):
def get_gene_list(self):
return self.gene_list
@ -40,7 +40,7 @@ class Chromosome:
self.gene_list[index] = gene
def set_genes(self, genes):
def set_gene_list(self, genes):
self.gene_list = genes

View File

@ -7,27 +7,26 @@ class Initialization_Methods:
"""Initialization examples that are used as defaults and examples"""
def random_initialization(ga):
"""Takes the initialization inputs and choregraphs them to output the type of population with the given parameters."""
"""Takes the initialization inputs and returns a population with the given parameters."""
# Create the population object
population = create_population()
# Using the chromosome_impl to set every index inside of the chromosome
if ga.chromosome_impl != None:
return create_population([
create_chromosome([
create_gene(ga.chromosome_impl(j))
for j in range(ga.chromosome_length)])
for i in range(ga.population_size)])
# 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.")
break
population.add_chromosome(chromosome)
return population
# Using the gene_impl to set every gene to be the same
elif ga.gene_impl != None:
function = ga.gene_impl[0]
return create_population([
create_chromosome([
create_gene(function(*ga.gene_impl[1:]))
for j in range(ga.chromosome_length)])
for i in range(ga.population_size)])
# Exit because no gene creation method specified
else:
print("You did not specify any initialization constraints.")
return None