diff --git a/pymoo/core/crossover.py b/pymoo/core/crossover.py index e7fbd562..c722d514 100644 --- a/pymoo/core/crossover.py +++ b/pymoo/core/crossover.py @@ -76,4 +76,13 @@ def do(self, problem, pop, parents=None, *args, random_state=None, **kwargs): def _do(self, problem, X, *args, random_state=None, **kwargs): pass + @staticmethod + def safe_bounds_do(func): + @functools.wraps(func) + def wrapped_do(self, problem, X, **kwargs): + result = func(self, problem, X, **kwargs) + if np.any(result < (problem.xl or -np.inf)) or np.any(result > (problem.xu or np.inf)): + raise ValueError("The generated solution is out of bounds.") + return result + return wrapped_do diff --git a/pymoo/operators/crossover/sbx.py b/pymoo/operators/crossover/sbx.py index 0d9ed68b..7366156b 100644 --- a/pymoo/operators/crossover/sbx.py +++ b/pymoo/operators/crossover/sbx.py @@ -112,6 +112,8 @@ def __init__(self, self.prob_exch = Real(prob_exch, bounds=(0.0, 1.0), strict=(0.0, 1.0)) self.prob_bin = Real(prob_bin, bounds=(0.0, 1.0), strict=(0.0, 1.0)) + @Crossover.safe_bounds_do + def _do(self, problem, X, **kwargs): def _do(self, problem, X, *args, random_state=None, **kwargs): _, n_matings, _ = X.shape