From 5fb26545d45c41ccf1301bd21d9aac0b5ada0b55 Mon Sep 17 00:00:00 2001 From: danielwilczak101 <44122838+danielwilczak101@users.noreply.github.com> Date: Thu, 3 Dec 2020 02:16:10 -0500 Subject: [PATCH] pushed fix --- src/crossover/crossover_methods.py | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/src/crossover/crossover_methods.py b/src/crossover/crossover_methods.py index 578f0b0..0cc3825 100644 --- a/src/crossover/crossover_methods.py +++ b/src/crossover/crossover_methods.py @@ -22,7 +22,8 @@ def _check_weight(individual_method): if 0 < weight < 1: return individual_method(ga, parent_1, parent_2, weight) else: - raise ValueError("Weight must be between 0 and 1 when using the given crossover method.") + raise ValueError("""Weight must be between 0 and 1 when using + the given crossover method.""") return new_method @@ -75,7 +76,7 @@ class Crossover_Methods: for index in range(len(mating_pool)): # for each parent in the mating pool yield ga.crossover_individual_impl( # apply crossover to - ga, # + ga, # mating_pool[index], # the parent and mating_pool[index-1], # the previous parent 0.5 # with equal weight @@ -90,7 +91,7 @@ class Crossover_Methods: for parent in mating_pool: # for each parent in the mating pool yield ga.crossover_individual_impl( # apply crossover to - ga, # + ga, # parent, # the parent and random.choice(mating_pool), # a random parent 0.5 # with equal weight @@ -106,16 +107,20 @@ class Crossover_Methods: def single_point(ga, parent_1, parent_2, weight = 0.5): """Cross two parents by swapping genes at one random point.""" + minimum_parent_length = min(len(parent_1), len(parent_2)) + # Equally weighted indexes if weight == 0.5: - swap_index = random.randrange(N) + swap_index = random.randrange(minimum_parent_length) # Use weighted random index. else: - n = min(len(parent_1), len(parent_2)) - t = 2*weight if (weight < 0.5) else 0.5 / (1-weight) - x = random.random() - swap_index = int(n * (1-(1-x)**t)**(1/t)) + weight_conversion = 2*weight if (weight < 0.5) else 0.5 / (1-weight) + rand_num = random.random() + swap_index = int( + minimum_parent_length * + (1-(1-rand_num)**weight_conversion)**(1/weight_conversion) + ) # Randomly choose which parent's genes are selected first. if random.choice([True, False]):