Updated Implementation Framework

Updated to cover changes made by Dan to Master regarding general design changes

Also added remove_two_worst survivor selection method
This commit is contained in:
RyleyGG
2020-10-05 20:46:25 -04:00
parent e05aa7f62b
commit 665062fdf1
24 changed files with 133 additions and 126 deletions

View File

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

View File

@ -2,37 +2,37 @@ class Chromosome:
def __init__(self, genes = None):
if genes is None:
self.genes = []
self.gene_list = []
else:
self.genes = genes
self.gene_list = genes
self.fitness = None
self.selected = False
def add_gene(self, gene, index = -1):
if index == -1:
index = len(self.genes)
self.genes.insert(index, gene)
index = len(self.gene_list)
self.gene_list.insert(index, gene)
def remove_gene(self, index):
del self.genes[index]
del self.gene_list[index]
def get_genes(self):
return self.genes
return self.gene_list
def get_fitness(self):
return self.fitness
def set_gene(self, gene, index):
self.genes[index] = gene
self.gene_list[index] = gene
def set_genes(self, genes):
self.genes = genes
self.gene_list = genes
def set_fitness(self, fitness):
self.fitness = fitness
def __repr__(self):
output_str = ''
for gene in self.genes:
for gene in self.gene_list:
output_str += gene.__repr__()
return output_str

View File

@ -1,22 +0,0 @@
# Imported library
import random
def check_values(low,high):
#Check to make sure its not less then zero
assert low > 0 , "The random gene low can not be less then zero"
# Check to make sure the high value is not
# lower than or equal to low and not 0.
assert high > low, "High value can not be smaller then low value"
assert high != 0, "High value can not be zero"
def random_gene(gene_input, gene_input_type, gene_index):
created_gene = None
if gene_input_type[gene_index] == "range":
created_gene = random.randint(gene_input[gene_index][0], gene_input[gene_index][1])
elif gene_input_type[gene_index] == "domain":
created_gene = random.choice(gene_input[gene_index])
elif gene_input_type[gene_index] == "float-range":
created_gene = random.uniform(gene_input[gene_index][0], gene_input[gene_index][1])
return created_gene

View File

@ -3,12 +3,12 @@ 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:
class Initialization_Methods:
"""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."""
"""Takes the initialization inputs and choregraphs them to output the type of population with the given parameters."""
# Create the population object
population = create_population()

View File

@ -3,9 +3,9 @@ class Population:
# fitness = Empty; population = [chromosome, chromosome, etc.]
def __init__(self, chromosomes = None):
if chromosomes is None:
self.chromosomes = []
self.chromosome_list = []
else:
self.chromosomes = chromosomes
self.chromosome_list = chromosomes
self.fitness = None
def get_closet_fitness(self,value):
@ -14,46 +14,38 @@ class Population:
def add_chromosome(self, chromosome, index = -1):
if index == -1:
index = len(self.chromosomes)
self.chromosomes.insert(index, chromosome)
index = len(self.chromosome_list)
self.chromosome_list.insert(index, chromosome)
def remove_chromosome(self, index):
del self.chromosomes[index]
del self.chromosome_list[index]
def get_all_chromosomes(self):
"""returns all chromosomes in the population"""
return self.chromosomes
return self.chromosome_list
def get_fitness(self):
return self.fitness
def set_all_chromosomes(self, chromosomes):
self.chromosomes = chromosomes
self.chromosome_list = chromosomes
def set_chromosome(self, chromosome, index = -1):
if index == -1:
index = len(self.chromosomes)-1
self.chromosomes[index] = chromosome
self.chromosome_list[index] = chromosome
def set_fitness(self, fitness):
self.fitness = fitness
def __repr__(self):
for index in range(len(self.chromosomes)):
return f'{self.chromosomes[index]}'
return f'{self.chromosome_list[index]}'
def print_all(self):
# Ex .Current population
# Chromosome 1 - [gene][gene][gene][.etc] / Chromosome fitness - #
print("Current population:")
for index in range(len(self.chromosomes)):
print(f'Chromosome - {index} {self.chromosomes[index]}', end = "")
print(f' / Fitness = {self.chromosomes[index].fitness}')
def generate_first_chromosomes(self, chromosome_count, chromosome_length, gene_lower_bound, gene_upper_bound):
#Creating the chromosomes with Genes of random size
for x in range(chromosome_count):
chromosome = Chromosome(chromosome_length)
for y in range(chromosome_length):
chromosome.gene_set[y] = Gene(random.randint(gene_lower_bound[y], gene_upper_bound[y]))
self.chromosome_set.append(chromosome)
for index in range(len(self.chromosome_list)):
print(f'Chromosome - {index} {self.chromosome_list[index]}', end = "")
print(f' / Fitness = {self.chromosome_list[index].fitness}')

View File

@ -1,31 +0,0 @@
# 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
from .gene_function.gene_random import random_gene as random_gene
def random_initialization(chromosome_length,population_size,gene_function,gene_input,gene_input_type):
if gene_function == random_gene:
# Create the population object
population = create_population()
# Fill the population with chromosomes
for i in range(population_size):
chromosome = create_chromosome()
#Fill the Chromosome with genes
for j in range(chromosome_length):
chromosome.add_gene(create_gene(gene_function(gene_input, gene_input_type, j)))
population.add_chromosome(chromosome)
return population
else: #For user input gene-function, don't do anything with gene_input parameter
# Create the population object
population = create_population()
# Fill the population with chromosomes
for i in range(population_size):
chromosome = create_chromosome()
#Fill the Chromosome with genes
for j in range(chromosome_length):
chromosome.add_gene(create_gene(gene_function()))
population.add_chromosome(chromosome)
return population

View File