Fixed bugs involving None types

This commit is contained in:
SimpleArt
2020-12-07 21:07:04 -05:00
parent 23803593fc
commit c19ef0d1be
3 changed files with 17 additions and 16 deletions

View File

@ -243,17 +243,15 @@ class Crossover_Methods:
for _ in range(len(gene_list_2)):
# Remove it if it is already used
for i in range(len(gene_list_1)):
if gene_list_1[i] == gene_list_2[-1]:
gene_list_2.pop(-1)
break
if gene_list_2[-1] in gene_list_1:
gene_list_2.pop(-1)
# Add it if it has not been used
else:
gene_list_1[input_index] = gene_list_2.pop(-1)
input_index += 1
if input_index == index_1:
input_index = index_2
gene_list_1[input_index] = gene_list_2.pop(-1)
input_index += 1
return gene_list_1
@ -296,16 +294,14 @@ class Crossover_Methods:
for _ in range(len(gene_list_2)):
# Remove it if it is already used
for i in range(len(gene_list_1)):
if gene_list_1[i] == gene_list_2[-1]:
gene_list_2.pop(-1)
break
if gene_list_2[-1] in gene_list_1:
gene_list_2.pop(-1)
# Add it if it has not been used
else:
gene_list_1[input_index] = gene_list_2.pop(-1)
input_index += 1
if input_index == index_1:
input_index = index_2
gene_list_1[input_index] = gene_list_2.pop(-1)
input_index += 1
return gene_list_1

View File

@ -159,7 +159,7 @@ class Mutation_Methods:
# Indexes of genes to swap
index_one = index
index_two = random.randrange(index_one)
index_two = random.randrange(len(chromosome))
# Swap genes
chromosome[index_one], chromosome[index_two] = chromosome[index_two], chromosome[index_one]

View File

@ -7,9 +7,14 @@ class Gene:
self.value = deepcopy(value)
def __eq__(self, gene):
"""Comparing two genes by their value."""
return self.value == gene.value
def __eq__(self, other_gene):
"""Comparing two genes by their value.
Returns False if either gene is None."""
if (self is None) or (other_gene is None):
return False
else:
return self.value == other_gene.value
def __repr__(self):