Cleaned up list usage
This commit is contained in:
@ -33,7 +33,7 @@ class Mutation_Methods:
|
|||||||
|
|
||||||
def individual_genes(ga, old_chromosome):
|
def individual_genes(ga, old_chromosome):
|
||||||
"""Mutates a random gene in the chromosome and resets the fitness."""
|
"""Mutates a random gene in the chromosome and resets the fitness."""
|
||||||
chromosome = ga.make_chromosome(old_chromosome.get_gene_list())
|
chromosome = ga.make_chromosome(list(old_chromosome))
|
||||||
|
|
||||||
# Loops until enough mutations occur
|
# Loops until enough mutations occur
|
||||||
for n in range(ceil(len(chromosome)*ga.gene_mutation_rate)):
|
for n in range(ceil(len(chromosome)*ga.gene_mutation_rate)):
|
||||||
@ -61,7 +61,7 @@ class Mutation_Methods:
|
|||||||
|
|
||||||
def swap_genes(ga, old_chromosome):
|
def swap_genes(ga, old_chromosome):
|
||||||
"""Mutates a random gene in the chromosome and resets the fitness."""
|
"""Mutates a random gene in the chromosome and resets the fitness."""
|
||||||
chromosome = ga.make_chromosome(old_chromosome.get_gene_list())
|
chromosome = ga.make_chromosome(list(old_chromosome))
|
||||||
|
|
||||||
# Loops until enough mutations occur
|
# Loops until enough mutations occur
|
||||||
for n in range(ceil(len(chromosome)*ga.gene_mutation_rate)):
|
for n in range(ceil(len(chromosome)*ga.gene_mutation_rate)):
|
||||||
|
|||||||
@ -57,7 +57,7 @@ class Chromosome:
|
|||||||
|
|
||||||
def __iter__(self):
|
def __iter__(self):
|
||||||
"""Returns an iterable of the gene list"""
|
"""Returns an iterable of the gene list"""
|
||||||
return iter(self.gene_list)
|
return self.gene_list
|
||||||
|
|
||||||
|
|
||||||
def __getitem__(self, k):
|
def __getitem__(self, k):
|
||||||
@ -75,11 +75,25 @@ class Chromosome:
|
|||||||
return len(self.gene_list)
|
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: ..."""
|
||||||
|
|
||||||
|
return (searched_gene in self.gene_list)
|
||||||
|
|
||||||
|
|
||||||
|
def index_of(self, searched_gene):
|
||||||
|
"""Returns the index of the gene in the current chromosome.
|
||||||
|
Returns -1 if no index found."""
|
||||||
|
|
||||||
|
return self.gene_list.index(searched_gene)
|
||||||
|
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
"""Create a backend string of the chromosome. Ex '1, 2, 3'."""
|
"""Create a backend string of the chromosome. Ex '1, 2, 3'."""
|
||||||
return ', '.join(repr(gene) for gene in self.gene_list)
|
return ', '.join(repr(gene) for gene in self)
|
||||||
|
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
"""Create a printable string of the chromosome. Ex '[1][2][3]'."""
|
"""Create a printable string of the chromosome. Ex '[1][2][3]'."""
|
||||||
return ''.join(str(gene) for gene in self.gene_list)
|
return ''.join(str(gene) for gene in self)
|
||||||
|
|||||||
@ -153,7 +153,7 @@ class Population:
|
|||||||
|
|
||||||
def __iter__(self):
|
def __iter__(self):
|
||||||
"""Returns an iterable of chromosomes"""
|
"""Returns an iterable of chromosomes"""
|
||||||
return iter(self.chromosome_list)
|
return self.chromosome_list
|
||||||
|
|
||||||
|
|
||||||
def __getitem__(self, k):
|
def __getitem__(self, k):
|
||||||
@ -175,22 +175,14 @@ class Population:
|
|||||||
"""Returns True if the current population contains the chromosome and False otherwise.
|
"""Returns True if the current population contains the chromosome and False otherwise.
|
||||||
Ex. if chromosome in ga.population: ..."""
|
Ex. if chromosome in ga.population: ..."""
|
||||||
|
|
||||||
for index in range(len(self)):
|
return (searched_chromosome in self.chromosome_list)
|
||||||
if self[index] == searched_chromosome:
|
|
||||||
return True
|
|
||||||
else:
|
|
||||||
return False
|
|
||||||
|
|
||||||
|
|
||||||
def index_of(self, searched_chromosome):
|
def index_of(self, searched_chromosome):
|
||||||
"""Returns the index of the chromosome in the current population.
|
"""Returns the index of the chromosome in the current population.
|
||||||
Returns -1 if no index found."""
|
Returns -1 if no index found."""
|
||||||
|
|
||||||
for index in range(len(self)):
|
return self.chromosome_list.index(searched_chromosome)
|
||||||
if self[index] == searched_chromosome:
|
|
||||||
return index
|
|
||||||
else:
|
|
||||||
return -1
|
|
||||||
|
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
|
|||||||
Reference in New Issue
Block a user