From 86279e9ad3891ff2fa6d633dbcb437184f8c9761 Mon Sep 17 00:00:00 2001 From: Diogo Matos Chaves Date: Mon, 7 Mar 2022 20:16:30 -0400 Subject: [PATCH] Ensure offspring new values are within variable limits --- geneal/genetic_algorithms/_continuous.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/geneal/genetic_algorithms/_continuous.py b/geneal/genetic_algorithms/_continuous.py index 97cbb55..eb04a37 100644 --- a/geneal/genetic_algorithms/_continuous.py +++ b/geneal/genetic_algorithms/_continuous.py @@ -143,10 +143,15 @@ def create_offspring( first_parent[crossover_pt] - sec_parent[crossover_pt] ) else: + variable_limits = self.variables_limits[crossover_pt] + p_new = first_parent[crossover_pt] - np.round( beta * (first_parent[crossover_pt] - sec_parent[crossover_pt]) ) + if not variable_limits[0] <= p_new <= variable_limits[1]: + p_new = np.random.randint(variable_limits[0], variable_limits[1] + 1) + return np.hstack( (first_parent[:crossover_pt], p_new, sec_parent[crossover_pt + 1 :]) )