Skip to content

rng implementation - #200

Merged
MartinPontius merged 8 commits into
mainfrom
rng
Jul 22, 2026
Merged

rng implementation#200
MartinPontius merged 8 commits into
mainfrom
rng

Conversation

@kabarmaz

@kabarmaz kabarmaz commented Jul 8, 2026

Copy link
Copy Markdown
Contributor

…tils.py and called it where necessary to harmonize the handling of random seeds.

Related Issue / Discussion:

Relates to issue [https://github.com/52North/WRT-Issues/issues/52]

Changes:

    modified:   WeatherRoutingTool/algorithms/data_utils.py
    modified:   WeatherRoutingTool/algorithms/genetic/__init__.py
    modified:   WeatherRoutingTool/algorithms/genetic/crossover.py
    modified:   WeatherRoutingTool/algorithms/genetic/mutation.py
    modified:   WeatherRoutingTool/algorithms/genetic/population.py
    modified:   WeatherRoutingTool/algorithms/genetic/utils.py
    modified:   WeatherRoutingTool/execute_routing.py *
    modified:   WeatherRoutingTool/ship/ship.py *

*access counter for weather data

Further Details:

Summary:

Initialized a random number generator in genetic/utils.py to harmonize the random seed and random number generation throughout the genetic algorithm. Implemented into the above listed files.

Dependencies:

Dependencies remain unchanged.

PR Checklist:

In the context of this PR, I:

Please consider that PRs which do not meet the requirements specified in the checklist will not be evaluated. Also, PRs with no activities will be closed after a reasonable amount of time.

…tils.py and called it where necessary to harmonize the handling of random seeds.
logger = logging.getLogger("WRT.genetic")


def get_rng(config: Config, seed=1):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a type hint for the return value (numpy.random.Generator) and a short docstring.

@MartinPontius

Copy link
Copy Markdown
Member

@kabarmaz Please also update the tests accordingly, particularly test_genetic.py.

Comment thread WeatherRoutingTool/algorithms/genetic/utils.py
logger = logging.getLogger("WRT.genetic")


def get_rng(config: Config, seed=1):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use DEFAULT_RANDOM_SEED as default function argument instead of 1.

if self.config.GENETIC_FIX_RANDOM_SEED:
logger.info('Fixing random seed for genetic algorithm.')
np.random.seed(1)
seed = 1

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use DEFAULT_RANDOM_SEED instead of 1 (see comments in utils.py).

super().__init__(config)

self.config = config
self.rng = utils.get_rng(config)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this line as self.rng is never used in class Genetic.

constraints_list: ConstraintsList,
prob=.5,
crossover_type="unnamed"

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove empty line.

# ~99.7 % in interval (0, BOAT_SPEED_MAX)
# self.mu = 0.5 * self.config.BOAT_SPEED_BOUNDARIES[1]
self.sigma = 1.54
self.max_acceleration = 1

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why change these lines? If there is no reason, revert it (see also lines 588 and 589 below).

shuffled_cost = rng.permutation(shuffled_cost, axis=0)
shuffled_cost = rng.permutation(shuffled_cost, axis=1)
shuffled_cost = genetic_utils.RNG.permutation(shuffled_cost, axis=0)
shuffled_cost = genetic_utils.RNG.permutation(shuffled_cost, axis=1)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These changes are not working with the latest updates/current implementation. Please correct.

Comment thread WeatherRoutingTool/ship/ship.py Outdated
self.time_min_max = [None, None]
self.lat_min_max = [None, None]
self.lon_min_max = [None, None]
self.counter = 0

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Revert this change.

Comment thread WeatherRoutingTool/ship/ship.py Outdated
if depth:
ship_var = ship_var.sel(depth=depth, method='nearest', drop=False)
ship_var = ship_var.fillna(0).to_numpy()
self.counter += 1

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Revert this change.

Comment thread WeatherRoutingTool/execute_routing.py Outdated
min_fuel_route_postprocessed.write_to_geojson(
routepath / f"{min_fuel_route_postprocessed.route_type}_postprocessed.geojson")

print(boat.counter)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Revert this change.

@kabarmaz
kabarmaz marked this pull request as ready for review July 21, 2026 13:07
Comment thread tests/test_genetic.py Outdated
default_map = Map(32., 15, 36, 29)
constraint_list = basic_test_func.generate_dummy_constraint_list()
np.random.seed(1)
np.random.default_rng(config.GENETIC_RANDOM_SEED)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove the line np.random.default_rng(config.GENETIC_RANDOM_SEED).
This returns a generator which is never used.

In the new structure, it's enough to pass config to RandomPlateauMutation because it creates its own random number generator using the seed specified in the config.

Comment thread tests/test_genetic.py Outdated
constraint_list = basic_test_func.generate_dummy_constraint_list()

np.random.seed(1)
np.random.default_rng(config.GENETIC_RANDOM_SEED)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove the line np.random.default_rng(config.GENETIC_RANDOM_SEED).
This returns a generator which is never used.

In the new structure, it's enough to pass config to RandomPlateauMutation because it creates its own random number generator using the seed specified in the config.

Comment thread tests/test_genetic.py Outdated
default_map = Map(32., 15, 36, 29)
constraint_list = basic_test_func.generate_dummy_constraint_list()
np.random.seed(2)
np.random.default_rng(config.GENETIC_RANDOM_SEED)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove the line np.random.default_rng(config.GENETIC_RANDOM_SEED).
This returns a generator which is never used.

In the new structure, it's enough to pass config to RouteBlendMutation because it creates its own random number generator using the seed specified in the config.

Comment thread tests/test_genetic.py Outdated
constraint_list = basic_test_func.generate_dummy_constraint_list()

np.random.seed(1)
np.random.default_rng(config.GENETIC_RANDOM_SEED)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove the line np.random.default_rng(config.GENETIC_RANDOM_SEED).
This returns a generator which is never used.

In the new structure, it's enough to pass config to RouteBlendMutation because it creates its own random number generator using the seed specified in the config.

Comment thread tests/test_genetic.py Outdated
default_map = Map(32., 15, 36, 29)
constraint_list = basic_test_func.generate_dummy_constraint_list()
np.random.seed(2)
np.random.default_rng(config.GENETIC_RANDOM_SEED)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove the line np.random.default_rng(config.GENETIC_RANDOM_SEED).
This returns a generator which is never used.

Why was np.random.seed(2) used at all? Double-check if the seed was/is considered in the subsequent code.

Comment thread tests/test_genetic.py Outdated
departure_time = datetime(2025, 4, 1, 11, 11)

np.random.seed(2)
np.random.default_rng(config.GENETIC_RANDOM_SEED)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove the line np.random.default_rng(config.GENETIC_RANDOM_SEED).
This returns a generator which is never used.

In the new structure, it's enough to pass config to SinglePointCrossover because it creates its own random number generator using the seed specified in the config.

# FIXME: these numbers should be carefully evaluated
# ~99.7 % in interval (0, BOAT_SPEED_MAX)
# self.mu = 0.5 * self.config.BOAT_SPEED_BOUNDARIES[1]
self.mu = 0.5 * self.config.BOAT_SPEED_BOUNDARIES[1]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-apply comment as the variable is currently not used.


self.speed_opts = speed_opts
self.waypoint_opts = waypoint_opts
self.config = config

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove self.config = config as it is never used.

shuffled_cost = rng.permutation(shuffled_cost, axis=0)
shuffled_cost = rng.permutation(shuffled_cost, axis=1)
shuffled_cost = self.rng.permutation(shuffled_cost, axis=0)
shuffled_cost = self.rng.permutation(shuffled_cost, axis=1)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

self doesn't have an attribute rng.

@MartinPontius
MartinPontius merged commit 5dd032b into main Jul 22, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants