Added automatic casting to all methods

This commit is contained in:
SimpleArt
2020-12-20 22:01:15 -05:00
parent 97c614c74c
commit c43eef38c4
2 changed files with 36 additions and 14 deletions

View File

@ -1,5 +1,14 @@
from structure import Gene as make_gene 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: class Chromosome:
def __init__(self, gene_list): 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""" """Add a gene to the chromosome at the specified index, defaulted to end of the chromosome"""
if index is None: if index is None:
index = len(self) index = len(self)
self.gene_list.insert(index, make_gene(gene)) self.gene_list.insert(index, to_gene(gene))
def remove_gene(self, index): def remove_gene(self, index):
@ -68,9 +77,9 @@ class Chromosome:
to set the indexed gene. to set the indexed gene.
""" """
if isinstance(index, int): if isinstance(index, int):
self.gene_list[index] = make_gene(gene) self.gene_list[index] = to_gene(gene)
else: 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): def __delitem__(self, index):
@ -97,7 +106,7 @@ class Chromosome:
if gene in chromosome if gene in chromosome
to check if a gene is in the 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): def index_of(self, gene, guess = None):
@ -113,7 +122,7 @@ class Chromosome:
""" """
# Cast to gene object # Cast to gene object
gene = make_gene(gene) gene = to_gene(gene)
# Use built-in method # Use built-in method
if guess is None: if guess is None:

View File

@ -1,5 +1,14 @@
from structure import Chromosome as make_chromosome 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: class Population:
def __init__(self, chromosome_list): def __init__(self, chromosome_list):
@ -50,9 +59,11 @@ class Population:
values already will stay sorted. values already will stay sorted.
""" """
if not isinstance(chromosome_list, list): self.next_population = [
chromosome_list = list(chromosome_list) to_chromosome(chromosome)
self.next_population = chromosome_list + self.next_population for chromosome
in chromosome_list
] + self.next_population
def sort_by_best_fitness(self, ga): def sort_by_best_fitness(self, ga):
@ -66,17 +77,17 @@ class Population:
if index is None: if index is None:
index = len(self) index = len(self)
self.chromosome_list.insert(index, chromosome) self.chromosome_list.insert(index, to_chromosome(chromosome))
def add_parent(self, chromosome): def add_parent(self, chromosome):
"""Adds a chromosome to the mating pool""" """Adds a chromosome to the mating pool"""
self.mating_pool.append(chromosome) self.mating_pool.append(to_chromosome(chromosome))
def add_child(self, chromosome): def add_child(self, chromosome):
"""Adds a chromosome to the next population""" """Adds a chromosome to the next population"""
self.next_population.append(chromosome) self.next_population.append(to_chromosome(chromosome))
def set_parent(self, index): def set_parent(self, index):
@ -113,7 +124,7 @@ class Population:
population[index] = chromosome population[index] = chromosome
to set the indexed chromosome. to set the indexed chromosome.
""" """
self.chromosome_list[index] = chromosome self.chromosome_list[index] = to_chromosome(chromosome)
def __len__(self): def __len__(self):
@ -125,13 +136,13 @@ class Population:
return len(self.chromosome_list) return len(self.chromosome_list)
def __contains__(self, searched_chromosome): def __contains__(self, chromosome):
""" """
Allows the user to use Allows the user to use
if chromosome in population if chromosome in population
to check if a chromosome is in the 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): def index_of(self, chromosome, guess = None):
@ -146,6 +157,8 @@ class Population:
index quicker. index quicker.
""" """
chromosome = to_chromosome(chromosome)
# Use built-in method # Use built-in method
if guess is None: if guess is None:
return self.chromosome_list.index(gene) return self.chromosome_list.index(gene)