Removed walruses
This commit is contained in:
@ -272,28 +272,14 @@ class Attributes:
|
|||||||
self.crossover_individual_impl = Crossover.Individual.Permutation.ox1
|
self.crossover_individual_impl = Crossover.Individual.Permutation.ox1
|
||||||
self.mutation_individual_impl = Mutation.Individual.Permutation.swap_genes
|
self.mutation_individual_impl = Mutation.Individual.Permutation.swap_genes
|
||||||
|
|
||||||
# Count the number of gene pairs they don't have in common
|
|
||||||
def dist(self, chromosome_1, chromosome_2):
|
def dist(self, chromosome_1, chromosome_2):
|
||||||
|
"""Count the number of gene pairs they don't have in common."""
|
||||||
# Used to set values during comprehension
|
|
||||||
set_value = lambda arg: True
|
|
||||||
|
|
||||||
# Index of gene from chromosome 1 in chromosome 2
|
|
||||||
j = 0
|
|
||||||
|
|
||||||
return sum(
|
return sum(
|
||||||
# Add 1 if they are different
|
1
|
||||||
int(chromosome_1[i-1] != chromosome_2[j-1])
|
for x, y
|
||||||
|
in zip(chromosome_1, chromosome_2)
|
||||||
# Loop over chromosome 1
|
if x != y
|
||||||
for i
|
|
||||||
in range(1-cycle, len(chromosome_1))
|
|
||||||
|
|
||||||
# Find index of gene from chromosome 1 in chromosome 2
|
|
||||||
if set_value(j := chromosome_2.index_of(chromosome_1[i]))
|
|
||||||
|
|
||||||
# Additional case to check
|
|
||||||
if j + cycle > 0
|
|
||||||
)
|
)
|
||||||
|
|
||||||
self.dist = dist
|
self.dist = dist
|
||||||
|
|||||||
@ -161,62 +161,3 @@ class Individual:
|
|||||||
|
|
||||||
ga.population.add_child(gene_list_1)
|
ga.population.add_child(gene_list_1)
|
||||||
|
|
||||||
|
|
||||||
@_check_weight
|
|
||||||
def partially_mapped(ga, parent_1, parent_2, *, weight = 0.5):
|
|
||||||
"""Cross two parents by slicing out a random part of one parent
|
|
||||||
and then filling in the rest of the genes from the second parent,
|
|
||||||
preserving the ordering of genes wherever possible.
|
|
||||||
|
|
||||||
NOTE: Needs to be fixed, since genes are not hashable..."""
|
|
||||||
|
|
||||||
# Too small to cross
|
|
||||||
if len(parent_1) < 2:
|
|
||||||
return parent_1.gene_list
|
|
||||||
|
|
||||||
# Unequal parent lengths
|
|
||||||
if len(parent_1) != len(parent_2):
|
|
||||||
raise ValueError("Parents do not have the same lengths.")
|
|
||||||
|
|
||||||
# Swap with weighted probability so that most of the genes
|
|
||||||
# are taken directly from parent 1.
|
|
||||||
if random.choices([0, 1], cum_weights = [weight, 1]) == 1:
|
|
||||||
parent_1, parent_2 = parent_2, parent_1
|
|
||||||
|
|
||||||
# Extract genes from parent 1 between two random indexes
|
|
||||||
index_2 = random.randrange(1, len(parent_1))
|
|
||||||
index_1 = random.randrange(index_2)
|
|
||||||
|
|
||||||
# Create copies of the gene lists
|
|
||||||
gene_list_1 = [None]*index_1 + parent_1[index_1:index_2] + [None]*(len(parent_1)-index_2)
|
|
||||||
gene_list_2 = list(parent_2)
|
|
||||||
|
|
||||||
# Create hash for gene list 2
|
|
||||||
hash = {gene:index for index, gene in enumerate(gene_list_2)}
|
|
||||||
|
|
||||||
# For each gene in the copied segment from parent 2
|
|
||||||
for i in range(index_1, index_2):
|
|
||||||
|
|
||||||
# If it is not already copied,
|
|
||||||
# find where it got displaced to
|
|
||||||
j = i
|
|
||||||
while gene_list_1[(j := hash[gene_list_1[j]])] is not None:
|
|
||||||
pass
|
|
||||||
gene_list_1[j] = gene_list_2[i]
|
|
||||||
|
|
||||||
# Fill in whatever is leftover (copied from ox1).
|
|
||||||
# For each gene from the second parent
|
|
||||||
for _ in range(len(gene_list_2)):
|
|
||||||
|
|
||||||
# Remove it if it is already used
|
|
||||||
if gene_list_2[-1] in gene_list_1:
|
|
||||||
gene_list_2.pop(-1)
|
|
||||||
|
|
||||||
# Add it if it has not been used
|
|
||||||
else:
|
|
||||||
if input_index == index_1:
|
|
||||||
input_index = index_2
|
|
||||||
gene_list_1[input_index] = gene_list_2.pop(-1)
|
|
||||||
input_index += 1
|
|
||||||
|
|
||||||
ga.population.add_child(gene_list_1)
|
|
||||||
|
|||||||
Reference in New Issue
Block a user