Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions WeatherRoutingTool/algorithms/genetic/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,21 @@ def terminate(self, res: Result, problem: RoutingProblem):

super().terminate()

if res is None or res.F is None or res.X is None:
raise RuntimeError(
"Genetic optimization produced no feasible solution "
"(empty result: res.F/res.X is None). "
"This typically means all candidate routes violated constraints. "
"Try relaxing constraints (e.g., disable `water_depth` and/or `via_waypoints` temporarily), "
"reducing the bbox/time window, or verifying weather/depth inputs."
)

if np.size(res.F) == 0 or np.size(res.X) == 0:
raise RuntimeError(
"Genetic optimization produced no feasible solution (empty result array). "
"This typically means all candidate routes violated constraints."
)

best_index = res.F.argmin()
# ensure res.X is of shape (n_sol, n_var)
best_route = np.atleast_2d(res.X)[best_index, 0]
Expand Down
15 changes: 15 additions & 0 deletions tests/test_genetic_termination.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import pytest

from WeatherRoutingTool.algorithms.genetic import Genetic


class _EmptyResult:
F = None
X = None


def test_genetic_terminate_raises_on_empty_result():
genetic = Genetic.__new__(Genetic)

with pytest.raises(RuntimeError, match=r"no feasible solution"):
genetic.terminate(_EmptyResult(), problem=None)