From 3412c05da2004a0933dd93d46adb924bf8cd385b Mon Sep 17 00:00:00 2001 From: SimpleArt <71458112+SimpleArt@users.noreply.github.com> Date: Thu, 19 Nov 2020 18:02:07 -0500 Subject: [PATCH] Cleaned up list usage --- src/mutation/mutation_methods.py | 4 ++-- src/structure/chromosome.py | 20 +++++++++++++++++--- src/structure/population.py | 14 +++----------- 3 files changed, 22 insertions(+), 16 deletions(-) diff --git a/src/mutation/mutation_methods.py b/src/mutation/mutation_methods.py index c8b2192..e9e23c8 100644 --- a/src/mutation/mutation_methods.py +++ b/src/mutation/mutation_methods.py @@ -33,7 +33,7 @@ class Mutation_Methods: def individual_genes(ga, old_chromosome): """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 for n in range(ceil(len(chromosome)*ga.gene_mutation_rate)): @@ -61,7 +61,7 @@ class Mutation_Methods: def swap_genes(ga, old_chromosome): """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 for n in range(ceil(len(chromosome)*ga.gene_mutation_rate)): diff --git a/src/structure/chromosome.py b/src/structure/chromosome.py index 1b3eea5..d729e99 100644 --- a/src/structure/chromosome.py +++ b/src/structure/chromosome.py @@ -57,7 +57,7 @@ class Chromosome: def __iter__(self): """Returns an iterable of the gene list""" - return iter(self.gene_list) + return self.gene_list def __getitem__(self, k): @@ -75,11 +75,25 @@ class 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: ...""" + + 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): """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): """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) diff --git a/src/structure/population.py b/src/structure/population.py index b602603..92befa3 100644 --- a/src/structure/population.py +++ b/src/structure/population.py @@ -153,7 +153,7 @@ class Population: def __iter__(self): """Returns an iterable of chromosomes""" - return iter(self.chromosome_list) + return self.chromosome_list def __getitem__(self, k): @@ -175,22 +175,14 @@ class Population: """Returns True if the current population contains the chromosome and False otherwise. Ex. if chromosome in ga.population: ...""" - for index in range(len(self)): - if self[index] == searched_chromosome: - return True - else: - return False + return (searched_chromosome in self.chromosome_list) def index_of(self, searched_chromosome): """Returns the index of the chromosome in the current population. Returns -1 if no index found.""" - for index in range(len(self)): - if self[index] == searched_chromosome: - return index - else: - return -1 + return self.chromosome_list.index(searched_chromosome) def __repr__(self):