From 2941be665c45d48a63b5076182673f100dc2edfc Mon Sep 17 00:00:00 2001 From: SimpleArt <71458112+SimpleArt@users.noreply.github.com> Date: Wed, 2 Dec 2020 20:39:51 -0500 Subject: [PATCH] Avoid one-sided edge cases - Single-point crossover allows either parent to have their genes come first now. - Rounding to integers is doing with an additional +/- 0.5 random value so that integer genes do not get stuck at one value when rounding at the midpoint of two integers e.g. 0.5 now randomly rounds to 0 or 1 instead of always 0. --- src/crossover/crossover_methods.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/crossover/crossover_methods.py b/src/crossover/crossover_methods.py index 7822d52..f49c753 100644 --- a/src/crossover/crossover_methods.py +++ b/src/crossover/crossover_methods.py @@ -103,7 +103,11 @@ class Crossover_Methods: ] swap_index = random.choices(range(N), weights)[0] - return parent_1[:swap_index] + parent_2[swap_index:] + # Randomly choose which parent's genes are selected first. + if random.choice([True, False]): + return parent_1[:swap_index] + parent_2[swap_index:] + else: + return parent_2[:-swap_index] + parent_1[-swap_index:] @genes_to_chromosome @@ -136,7 +140,7 @@ class Crossover_Methods: value = weight*values_1 + (1-weight)*random.uniform(value_1, value_2) if type(value_1) == type(value_2) == int: - value = round(value) + value = round(value + random.uniform(-0.5, 0.5)) yield value @@ -154,7 +158,7 @@ class Crossover_Methods: value = weight*value_1 + (1-weight)*value_2 if type(value_1) == type(value_2) == int: - value = round(value) + value = round(value + random.uniform(-0.5, 0.5)) yield value @@ -175,6 +179,6 @@ class Crossover_Methods: value = (2-weight)*value_1 + (weight-1)*value_2 if type(value_1) == type(value_2) == int: - value = round(value) + value = round(value + random.uniform(-0.5, 0.5)) yield value