From c43eef38c4eae84f7a78759298c76d6145649b18 Mon Sep 17 00:00:00 2001 From: SimpleArt <71458112+SimpleArt@users.noreply.github.com> Date: Sun, 20 Dec 2020 22:01:15 -0500 Subject: [PATCH] Added automatic casting to all methods --- src/structure/chromosome.py | 19 ++++++++++++++----- src/structure/population.py | 31 ++++++++++++++++++++++--------- 2 files changed, 36 insertions(+), 14 deletions(-) diff --git a/src/structure/chromosome.py b/src/structure/chromosome.py index 4b7e682..b6406ab 100644 --- a/src/structure/chromosome.py +++ b/src/structure/chromosome.py @@ -1,5 +1,14 @@ from structure import Gene as make_gene +def to_gene(gene): + """Converts the input to a gene if it isn't already one.""" + + if isinstance(gene, make_gene): + return gene + else: + return make_gene(gene) + + class Chromosome: def __init__(self, gene_list): @@ -14,7 +23,7 @@ class Chromosome: """Add a gene to the chromosome at the specified index, defaulted to end of the chromosome""" if index is None: index = len(self) - self.gene_list.insert(index, make_gene(gene)) + self.gene_list.insert(index, to_gene(gene)) def remove_gene(self, index): @@ -68,9 +77,9 @@ class Chromosome: to set the indexed gene. """ if isinstance(index, int): - self.gene_list[index] = make_gene(gene) + self.gene_list[index] = to_gene(gene) else: - self.gene_list[index] = (make_gene(item) for item in gene) + self.gene_list[index] = (to_gene(item) for item in gene) def __delitem__(self, index): @@ -97,7 +106,7 @@ class Chromosome: if gene in chromosome to check if a gene is in the chromosome. """ - return (make_gene(gene) in self.gene_list) + return (to_gene(gene) in self.gene_list) def index_of(self, gene, guess = None): @@ -113,7 +122,7 @@ class Chromosome: """ # Cast to gene object - gene = make_gene(gene) + gene = to_gene(gene) # Use built-in method if guess is None: diff --git a/src/structure/population.py b/src/structure/population.py index 13f57b1..7fd3aa3 100644 --- a/src/structure/population.py +++ b/src/structure/population.py @@ -1,5 +1,14 @@ from structure import Chromosome as make_chromosome +def to_chromosome(chromosome): + """Converts the input to a chromosome if it isn't already one.""" + + if isinstance(chromosome, make_chromosome): + return chromosome + else: + return make_chromosome(chromosome) + + class Population: def __init__(self, chromosome_list): @@ -50,9 +59,11 @@ class Population: values already will stay sorted. """ - if not isinstance(chromosome_list, list): - chromosome_list = list(chromosome_list) - self.next_population = chromosome_list + self.next_population + self.next_population = [ + to_chromosome(chromosome) + for chromosome + in chromosome_list + ] + self.next_population def sort_by_best_fitness(self, ga): @@ -66,17 +77,17 @@ class Population: if index is None: index = len(self) - self.chromosome_list.insert(index, chromosome) + self.chromosome_list.insert(index, to_chromosome(chromosome)) def add_parent(self, chromosome): """Adds a chromosome to the mating pool""" - self.mating_pool.append(chromosome) + self.mating_pool.append(to_chromosome(chromosome)) def add_child(self, chromosome): """Adds a chromosome to the next population""" - self.next_population.append(chromosome) + self.next_population.append(to_chromosome(chromosome)) def set_parent(self, index): @@ -113,7 +124,7 @@ class Population: population[index] = chromosome to set the indexed chromosome. """ - self.chromosome_list[index] = chromosome + self.chromosome_list[index] = to_chromosome(chromosome) def __len__(self): @@ -125,13 +136,13 @@ class Population: return len(self.chromosome_list) - def __contains__(self, searched_chromosome): + def __contains__(self, chromosome): """ 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) + return (to_chromosome(chromosome) in self.chromosome_list) def index_of(self, chromosome, guess = None): @@ -146,6 +157,8 @@ class Population: index quicker. """ + chromosome = to_chromosome(chromosome) + # Use built-in method if guess is None: return self.chromosome_list.index(gene)