Added structure directory and improved sort by fitness
This commit is contained in:
@ -1,5 +1,2 @@
|
||||
# FROM (. means local) file_name IMPORT function_name
|
||||
from .initialization_methods import Initialization_Methods
|
||||
from .gene_structure.gene import Gene
|
||||
from .chromosome_structure.chromosome import Chromosome
|
||||
from .population_structure.population import Population
|
||||
from .initialization_methods import Initialization_Methods
|
||||
@ -1,57 +0,0 @@
|
||||
class Chromosome:
|
||||
|
||||
def __init__(self, gene_list = None):
|
||||
if gene_list is None:
|
||||
self.gene_list = []
|
||||
else:
|
||||
self.gene_list = gene_list
|
||||
|
||||
self.fitness = None
|
||||
# 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_gene_list(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_gene_list(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 = ''
|
||||
for gene in self.gene_list:
|
||||
output_str += gene.__repr__()
|
||||
return output_str
|
||||
@ -1,37 +0,0 @@
|
||||
def check_gene(value):
|
||||
#Check to make sure the gene is not empty
|
||||
assert value != "" , "Gene can not be empty"
|
||||
return value
|
||||
|
||||
|
||||
class Gene:
|
||||
|
||||
def __init__(self, value):
|
||||
"""Initialize a gene with fitness of value None and the input value"""
|
||||
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}]'
|
||||
@ -1,79 +0,0 @@
|
||||
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):
|
||||
"""Returns a string representation of the entire population"""
|
||||
pass
|
||||
|
||||
|
||||
def print_all(self):
|
||||
"""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(self.size()):
|
||||
print(f'Chromosome - {index} {self.chromosome_list[index]}', end = "")
|
||||
print(f' / Fitness = {self.chromosome_list[index].get_fitness()}')
|
||||
Reference in New Issue
Block a user