Comment updates
This commit is contained in:
@ -1,7 +1,7 @@
|
|||||||
class chromosome:
|
class chromosome:
|
||||||
|
|
||||||
# fitness = Empty; genes = [gene, gene, gene, etc.]
|
|
||||||
def __init__(self, genes = None):
|
def __init__(self, genes = None):
|
||||||
|
"""Initialize the chromosome based on input gene list, defaulted to an empty list"""
|
||||||
if genes is None:
|
if genes is None:
|
||||||
self.genes = []
|
self.genes = []
|
||||||
else:
|
else:
|
||||||
@ -9,29 +9,37 @@ class chromosome:
|
|||||||
self.fitness = None
|
self.fitness = None
|
||||||
|
|
||||||
def add_gene(self, gene, index = -1):
|
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:
|
if index == -1:
|
||||||
index = len(self.genes)
|
index = len(self.genes)
|
||||||
self.genes.insert(index, gene)
|
self.genes.insert(index, gene)
|
||||||
|
|
||||||
def remove_gene(self, index):
|
def remove_gene(self, index):
|
||||||
|
"""Remove a gene from the chromosome at the specified index"""
|
||||||
del self.genes[index]
|
del self.genes[index]
|
||||||
|
|
||||||
def get_genes(self):
|
def get_genes(self):
|
||||||
|
"""Return all genes in the chromosome"""
|
||||||
return self.genes
|
return self.genes
|
||||||
|
|
||||||
def get_fitness(self):
|
def get_fitness(self):
|
||||||
|
"""Return the fitness of the chromosome"""
|
||||||
return self.fitness
|
return self.fitness
|
||||||
|
|
||||||
def set_gene(self, gene, index):
|
def set_gene(self, gene, index):
|
||||||
|
"""Set a gene at a specific index"""
|
||||||
self.genes[index] = gene
|
self.genes[index] = gene
|
||||||
|
|
||||||
def set_genes(self, genes):
|
def set_genes(self, genes):
|
||||||
|
"""Set the entire gene set of the chromosome"""
|
||||||
self.genes = genes
|
self.genes = genes
|
||||||
|
|
||||||
def set_fitness(self, fitness):
|
def set_fitness(self, fitness):
|
||||||
|
"""Set the fitness value of the chromosome"""
|
||||||
self.fitness = fitness
|
self.fitness = fitness
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
|
"""Format the repr() output for the chromosome"""
|
||||||
output_str = ''
|
output_str = ''
|
||||||
for gene in self.genes:
|
for gene in self.genes:
|
||||||
output_str += gene.__repr__()
|
output_str += gene.__repr__()
|
||||||
|
|||||||
@ -4,21 +4,28 @@ def check_gene(value):
|
|||||||
return value
|
return value
|
||||||
|
|
||||||
class gene:
|
class gene:
|
||||||
|
|
||||||
def __init__(self, value):
|
def __init__(self, value):
|
||||||
|
"""Initialize a gene with fitness of value None and the input value"""
|
||||||
self.fitness = None
|
self.fitness = None
|
||||||
self.value = check_gene(value)
|
self.value = check_gene(value)
|
||||||
|
|
||||||
def get_fitness(self):
|
def get_fitness(self):
|
||||||
|
"""Return fitness of the gene"""
|
||||||
return self.fitness
|
return self.fitness
|
||||||
|
|
||||||
def get_value(self):
|
def get_value(self):
|
||||||
|
"""Return value of the gene"""
|
||||||
return self.value
|
return self.value
|
||||||
|
|
||||||
def set_fitness(self, fitness):
|
def set_fitness(self, fitness):
|
||||||
|
"""Set fitness of the gene"""
|
||||||
self.fitness = fitness
|
self.fitness = fitness
|
||||||
|
|
||||||
def set_value(self):
|
def set_value(self):
|
||||||
|
"""Set value of the gene"""
|
||||||
self.value = value
|
self.value = value
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
|
"""Format the repr() output value"""
|
||||||
return f'[{self.value}]'
|
return f'[{self.value}]'
|
||||||
|
|||||||
@ -1,7 +1,7 @@
|
|||||||
class population:
|
class population:
|
||||||
|
|
||||||
# fitness = Empty; population = [chromosome, chromosome, etc.]
|
|
||||||
def __init__(self, chromosomes = None):
|
def __init__(self, chromosomes = None):
|
||||||
|
"""Intiialize the population with fitness of value None, and a set of chromosomes dependant on user-passed parameter"""
|
||||||
if chromosomes is None:
|
if chromosomes is None:
|
||||||
self.chromosomes = []
|
self.chromosomes = []
|
||||||
else:
|
else:
|
||||||
@ -9,47 +9,48 @@ class population:
|
|||||||
self.fitness = None
|
self.fitness = None
|
||||||
|
|
||||||
def get_closet_fitness(self,value):
|
def get_closet_fitness(self,value):
|
||||||
# Get the chomosome that has the closets fitness to the value defined
|
"""Get the chomosome that has the closets fitness to the value defined"""
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def add_chromosome(self, chromosome, index = -1):
|
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:
|
if index == -1:
|
||||||
index = len(self.chromosomes)
|
index = len(self.chromosomes)
|
||||||
self.chromosomes.insert(index, chromosome)
|
self.chromosomes.insert(index, chromosome)
|
||||||
|
|
||||||
def remove_chromosome(self, index):
|
def remove_chromosome(self, index):
|
||||||
|
"""removes a chromosome from the indicated index"""
|
||||||
del self.chromosomes[index]
|
del self.chromosomes[index]
|
||||||
|
|
||||||
def get_all_chromosomes(self):
|
def get_all_chromosomes(self):
|
||||||
|
"""returns all chromosomes in the population"""
|
||||||
return chromosomes
|
return chromosomes
|
||||||
|
|
||||||
def get_fitness(self):
|
def get_fitness(self):
|
||||||
|
"""returns the population's fitness"""
|
||||||
return self.fitness
|
return self.fitness
|
||||||
|
|
||||||
def set_all_chromosomes(self, chromosomes):
|
def set_all_chromosomes(self, chromosomes):
|
||||||
|
"""sets the chromosome set of the population"""
|
||||||
self.chromosomes = chromosomes
|
self.chromosomes = chromosomes
|
||||||
|
|
||||||
def set_chromosome(self, chromosomes, index):
|
def set_chromosome(self, chromosome, index):
|
||||||
self.chromosome[index] = chromosome
|
"""sets a specific chromosome at a specific index"""
|
||||||
|
self.chromosomes[index] = chromosome
|
||||||
|
|
||||||
def set_fitness(self, fitness):
|
def set_fitness(self, fitness):
|
||||||
|
"""Sets the fitness value of the population"""
|
||||||
self.fitness = fitness
|
self.fitness = fitness
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
|
"""Sets the repr() output format"""
|
||||||
return ''.join([chromosome.__repr__() for chromosome in self.chromosomes])
|
return ''.join([chromosome.__repr__() for chromosome in self.chromosomes])
|
||||||
|
|
||||||
def print_all(self):
|
def print_all(self):
|
||||||
# Ex .Current population
|
"""Prints information about the population in the following format:"""
|
||||||
# Chromosome 1 - [gene][gene][gene][.etc] / Chromosome fitness - #
|
"""Ex .Current population"""
|
||||||
|
"""Chromosome 1 - [gene][gene][gene][.etc] / Chromosome fitness - """
|
||||||
print("Current population:")
|
print("Current population:")
|
||||||
for index in range(len(self.chromosomes)):
|
for index in range(len(self.chromosomes)):
|
||||||
print(f'Chromosome - {index} {self.chromosomes[index]}', end = "")
|
print(f'Chromosome - {index} {self.chromosomes[index]}', end = "")
|
||||||
print(f' / Fitness = {self.chromosomes[index].fitness}')
|
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)
|
|
||||||
|
|||||||
Reference in New Issue
Block a user