Added blank lines and fixed run_testing

This commit is contained in:
SimpleArt
2020-10-12 19:57:57 -04:00
parent 4770473825
commit 3424fd4da7
9 changed files with 86 additions and 18 deletions

View File

@ -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 = ''

View File

@ -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}]'

View File

@ -5,7 +5,7 @@ from .gene_structure.gene import Gene as create_gene
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."""
@ -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

View File

@ -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()}')