Comments updated
This commit is contained in:
@ -56,43 +56,81 @@ class Chromosome:
|
||||
|
||||
|
||||
def __iter__(self):
|
||||
"""Returns an iterable of the gene list"""
|
||||
"""
|
||||
Allows the user to use
|
||||
|
||||
iter(chromosome)
|
||||
list(chromosome) == chromosome.gene_list
|
||||
tuple(chromosome)
|
||||
for gene in chromosome
|
||||
|
||||
to loop through the chromosome.
|
||||
"""
|
||||
return iter(self.gene_list)
|
||||
|
||||
|
||||
def __getitem__(self, index):
|
||||
"""Returns the indexed gene"""
|
||||
"""
|
||||
Allows the user to use
|
||||
gene = chromosome[index]
|
||||
to get the indexed gene.
|
||||
"""
|
||||
return self.gene_list[index]
|
||||
|
||||
|
||||
def __setitem__(self, index, gene):
|
||||
"""Sets the indexed gene value"""
|
||||
"""
|
||||
Allows the user to use
|
||||
chromosome[index] = gene
|
||||
to set the indexed gene.
|
||||
"""
|
||||
self.gene_list[index] = gene
|
||||
|
||||
|
||||
def __len__(self):
|
||||
"""Returns the number of genes in the chromosome"""
|
||||
"""
|
||||
Allows the user to use
|
||||
size = len(chromosome)
|
||||
to get the length of the chromosome.
|
||||
"""
|
||||
return len(self.gene_list)
|
||||
|
||||
|
||||
def __contains__(self, searched_gene):
|
||||
"""Returns True if the chromosome contains the gene and False otherwise.
|
||||
Ex. if chromosome in ga.population: ..."""
|
||||
|
||||
"""
|
||||
Allows the user to use
|
||||
if gene in chromosome
|
||||
to check if a gene is in the chromosome.
|
||||
"""
|
||||
return (searched_gene in self.gene_list)
|
||||
|
||||
|
||||
def index_of(self, searched_gene):
|
||||
"""Returns the index of the gene in the current chromosome."""
|
||||
|
||||
"""
|
||||
Allows the user to use
|
||||
index = chromosome.index_of(gene)
|
||||
to find the index of a gene in the chromosome.
|
||||
Be sure to check if the chromosome contains the gene
|
||||
first, or to catch an exception if the gene is not
|
||||
in the chromosome.
|
||||
"""
|
||||
return self.gene_list.index(searched_gene)
|
||||
|
||||
|
||||
def __repr__(self):
|
||||
"""Create a backend string of the chromosome. Ex '1, 2, 3'."""
|
||||
"""
|
||||
Allows the user to use
|
||||
repr(chromosome)
|
||||
to get a backend representation of the chromosome.
|
||||
"""
|
||||
return ', '.join(repr(gene) for gene in self)
|
||||
|
||||
|
||||
def __str__(self):
|
||||
"""Create a printable string of the chromosome. Ex '[1][2][3]'."""
|
||||
"""
|
||||
Allows the user to use
|
||||
str(chromosome)
|
||||
print(chromosome)
|
||||
to get a frontend representation of the chromosome.
|
||||
"""
|
||||
return ''.join(str(gene) for gene in self)
|
||||
|
||||
@ -29,10 +29,19 @@ class Gene:
|
||||
|
||||
|
||||
def __repr__(self):
|
||||
"""Create a backend string of the chromosome. Ex '1'."""
|
||||
"""
|
||||
Allows the user to use
|
||||
repr(gene)
|
||||
to get a backend representation of the gene.
|
||||
"""
|
||||
return str(self.value)
|
||||
|
||||
|
||||
def __str__(self):
|
||||
"""Create a printable string of the chromosome. Ex '[1]'."""
|
||||
"""
|
||||
Allows the user to use
|
||||
str(gene)
|
||||
print(gene)
|
||||
to get a frontend representation of the gene.
|
||||
"""
|
||||
return f'[{str(self.value)}]'
|
||||
|
||||
@ -152,41 +152,75 @@ class Population:
|
||||
|
||||
|
||||
def __iter__(self):
|
||||
"""Returns an iterable of chromosomes"""
|
||||
"""
|
||||
Allows the user to use
|
||||
|
||||
iter(population)
|
||||
list(population) == population.chromosome_list
|
||||
tuple(population)
|
||||
for chromosome in population
|
||||
|
||||
to loop through the population.
|
||||
"""
|
||||
return iter(self.chromosome_list)
|
||||
|
||||
|
||||
def __getitem__(self, index):
|
||||
"""Returns the indexed chromosome"""
|
||||
"""
|
||||
Allows the user to use
|
||||
chromosome = population[index]
|
||||
to get the indexed chromosome.
|
||||
"""
|
||||
return self.chromosome_list[index]
|
||||
|
||||
|
||||
def __setitem__(self, index, chromosome):
|
||||
"""Sets the indexed chromosome"""
|
||||
"""
|
||||
Allows the user to use
|
||||
population[index] = chromosome
|
||||
to set the indexed chromosome.
|
||||
"""
|
||||
self.chromosome_list[index] = chromosome
|
||||
|
||||
|
||||
def __len__(self):
|
||||
"""Returns the number of chromosomes in the current population"""
|
||||
"""
|
||||
Allows the user to use
|
||||
size = len(population)
|
||||
to get the length of the population.
|
||||
"""
|
||||
return len(self.chromosome_list)
|
||||
|
||||
|
||||
def __contains__(self, searched_chromosome):
|
||||
"""Returns True if the current population contains the chromosome and False otherwise.
|
||||
Ex. if chromosome in ga.population: ..."""
|
||||
|
||||
"""
|
||||
Allows the user to use
|
||||
if chromosome in population
|
||||
to check if a chromosome is in the population.
|
||||
"""
|
||||
return (searched_chromosome in self.chromosome_list)
|
||||
|
||||
|
||||
def index_of(self, searched_chromosome):
|
||||
"""Returns the index of the chromosome in the current population."""
|
||||
|
||||
"""
|
||||
Allows the user to use
|
||||
index = population.index_of(chromosome)
|
||||
to find the index of a chromosome in the population.
|
||||
Be sure to check if the population contains the chromosome
|
||||
first, or to catch an exception if the chromosome is not
|
||||
in the population.
|
||||
"""
|
||||
return self.chromosome_list.index(searched_chromosome)
|
||||
|
||||
|
||||
def __repr__(self):
|
||||
"""Returns a backend string representation of the entire population"""
|
||||
|
||||
"""
|
||||
Allows the user to use
|
||||
repr(population)
|
||||
str(population)
|
||||
print(population)
|
||||
to get a backend representation of the population.
|
||||
"""
|
||||
return ''.join(
|
||||
f'Chromosome - {index} {chromosome} ' +
|
||||
f'/ Fitness = {chromosome.fitness}\n'
|
||||
|
||||
Reference in New Issue
Block a user