Skip to content

Commit

Permalink
Ensure offspring new values are within variable limits
Browse files Browse the repository at this point in the history
  • Loading branch information
diogomatoschaves committed Mar 8, 2022
1 parent a0015b4 commit 86279e9
Showing 1 changed file with 5 additions and 0 deletions.
5 changes: 5 additions & 0 deletions geneal/genetic_algorithms/_continuous.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 :])
)
Expand Down

0 comments on commit 86279e9

Please sign in to comment.