From 23efae27dc294195d92ba15567d44863c66a14f9 Mon Sep 17 00:00:00 2001 From: SimpleArt <71458112+SimpleArt@users.noreply.github.com> Date: Wed, 2 Dec 2020 18:50:59 -0500 Subject: [PATCH] Added selection_probability to stochastic selection. --- src/parent_selection/parent_selection_methods.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/parent_selection/parent_selection_methods.py b/src/parent_selection/parent_selection_methods.py index 3f1e670..e6ce1f7 100644 --- a/src/parent_selection/parent_selection_methods.py +++ b/src/parent_selection/parent_selection_methods.py @@ -204,5 +204,19 @@ class Parent_Selection: in range(len(ga.population)) ] + sum_weights = sum(weights) + + # Rescale and adjust using selection_probability + # so that + # if selection_probability is high, + # parents are more likely selected based on fitness. + # if selection_probability is low, + # parents are more likely selected based on randomness. + weights = [ + weight / sum_weights - ga.selection_probability + 1 + for weight + in weights + ] + # Set the mating pool. ga.population.mating_pool = random.choices(ga.population, weights, k = parent_amount)