Skip to content

Fix: Genetic algorithm crashes when no feasible solution exists #164

@Her0n24

Description

@Her0n24

Genetic algorithm crashes when no feasible solution exists (res.F is None -> AttributeError in terminate)

Edit: This looks related to #29 (closed), but the same error message still reappear on main. I'd like some advice for seeing this behavior.

Summary

When running the genetic algorithm with certain constraint combinations (e.g., including water_depth), the optimiser can produce no feasible solutions. In this case, pymoo returns a Result where res.F/ res.X is None, but Genetic.terminate() assumes they exist and crashes.

Steps and Configuration to Reproduce

Attached config.multipleroutes.json producing the error

config.multipleroutes.json

  1. Use the above configuration and ensure "water_depth" is included in CONSTRAINTS_LIST in config.multipleroutes.json and config.py
  2. Run python cli.py -f /path/to/config.json --debug True
  3. Observe crash at termination

Actual Behavior

Program crashes with
File "/Users/<>/Documents/open_source/WeatherRoutingTool/WeatherRoutingTool/algorithms/genetic/init.py", line 160, in terminate best_index = res.F.argmin() ^^^^^^^^^^^^ AttributeError: 'NoneType' object has no attribute 'argmin'

Traceback points to Genetic.terminate() in init.py

Expected Behavior

If no feasible solution exists, the program should fail with a clearer message like

  • No feasible solution found, all candidates violated constraints. Please rerun.

Context

If "water_depth" if removed from CONSTRAINTS_LIST in the attached config.json, the same run produces a solution and figures.
No feasible solution scenario is valid/ expected, but just the crashing and error type/ message is not.

Proposed Fix

Add a guard in Genetic.terminate() to check res, res.F and res.X for None/emptiness and raise a descriptive exception.

I'm happy to solve the issue and make a small PR.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions