diff --git a/src/structure/chromosome.py b/src/structure/chromosome.py index 3502542..4b7e682 100644 --- a/src/structure/chromosome.py +++ b/src/structure/chromosome.py @@ -14,7 +14,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, gene) + self.gene_list.insert(index, make_gene(gene)) def remove_gene(self, index): @@ -67,7 +67,10 @@ class Chromosome: chromosome[index] = gene to set the indexed gene. """ - self.gene_list[index] = gene + if isinstance(index, int): + self.gene_list[index] = make_gene(gene) + else: + self.gene_list[index] = (make_gene(item) for item in gene) def __delitem__(self, index): @@ -88,13 +91,13 @@ class Chromosome: return len(self.gene_list) - def __contains__(self, searched_gene): + def __contains__(self, gene): """ 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) + return (make_gene(gene) in self.gene_list) def index_of(self, gene, guess = None): @@ -109,6 +112,9 @@ class Chromosome: the index quicker. """ + # Cast to gene object + gene = make_gene(gene) + # Use built-in method if guess is None: return self.gene_list.index(gene) diff --git a/src/structure/gene.py b/src/structure/gene.py index 6262230..f2d0126 100644 --- a/src/structure/gene.py +++ b/src/structure/gene.py @@ -15,13 +15,14 @@ class Gene: def __eq__(self, other_gene): - """Comparing two genes by their value. - Returns False if either gene is None.""" + """Comparing two genes by their value.""" - if (self is None) or (other_gene is None): - return False - else: - return self.value == other_gene.value + try: + other_value = other_gene.value + except: + other_value = other_gene + + return self.value == other_value def __repr__(self):