From f4dd4700c7e3ae36a76644c87aca92a19b657013 Mon Sep 17 00:00:00 2001 From: coolneng Date: Mon, 21 Jun 2021 02:25:54 +0200 Subject: [PATCH] Add crossover probability --- src/genetic_algorithm.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/genetic_algorithm.py b/src/genetic_algorithm.py index 91d9136..50d4306 100644 --- a/src/genetic_algorithm.py +++ b/src/genetic_algorithm.py @@ -146,13 +146,18 @@ def group_parents(parents): return parent_pairs -def crossover(mode, parents, m): +def crossover(mode, parents, m, probability=0.7): parent_groups = group_parents(parents) offspring = [] if mode == "uniform": - for element in parent_groups: + expected_crossovers = int(len(parents) * probability) + cutoff = expected_crossovers // 2 + for element in parent_groups[:cutoff]: offspring.append(uniform_crossover(element, m)) offspring.append(uniform_crossover(element, m)) + for element in parent_groups[cutoff:]: + offspring.append(element[0]) + offspring.append(element[1]) else: for element in parent_groups: first_offspring, second_offspring = position_crossover(element)