Skip to content
Merged
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
80 changes: 35 additions & 45 deletions src/optimagic/optimizers/pygmo_optimizers.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,9 @@ class PygmoGaco(Algorithm):
"""Number of cores used for parallel function evaluation."""

seed: int | None = None
"""Seed used to sample the initial population.
"""Seed used to sample the initial population and by the pygmo algorithm.

It is not passed to the pygmo algorithm, whose own random number generator is
seeded randomly, so runs are not reproducible.
If None, runs are not reproducible.

"""

Expand Down Expand Up @@ -284,10 +283,9 @@ class PygmoBeeColony(Algorithm):
"""Number of generations to evolve (the ``gen`` parameter in pygmo)."""

seed: int | None = None
"""Seed used to sample the initial population.
"""Seed used to sample the initial population and by the pygmo algorithm.

It is not passed to the pygmo algorithm, whose own random number generator is
seeded randomly, so runs are not reproducible.
If None, runs are not reproducible.

"""

Expand Down Expand Up @@ -376,10 +374,9 @@ class PygmoDe(Algorithm):
at least 10."""

seed: int | None = None
"""Seed used to sample the initial population.
"""Seed used to sample the initial population and by the pygmo algorithm.

It is not passed to the pygmo algorithm, whose own random number generator is
seeded randomly, so runs are not reproducible.
If None, runs are not reproducible.

"""

Expand Down Expand Up @@ -547,10 +544,9 @@ class PygmoSea(Algorithm):
at least 10."""

seed: int | None = None
"""Seed used to sample the initial population.
"""Seed used to sample the initial population and by the pygmo algorithm.

It is not passed to the pygmo algorithm, whose own random number generator is
seeded randomly, so runs are not reproducible.
If None, runs are not reproducible.

"""

Expand Down Expand Up @@ -637,10 +633,9 @@ class PygmoSga(Algorithm):
at least 64."""

seed: int | None = None
"""Seed used to sample the initial population.
"""Seed used to sample the initial population and by the pygmo algorithm.

It is not passed to the pygmo algorithm, whose own random number generator is
seeded randomly, so runs are not reproducible.
If None, runs are not reproducible.

"""

Expand Down Expand Up @@ -887,10 +882,9 @@ class PygmoSade(Algorithm):
at least 64."""

seed: int | None = None
"""Seed used to sample the initial population.
"""Seed used to sample the initial population and by the pygmo algorithm.

It is not passed to the pygmo algorithm, whose own random number generator is
seeded randomly, so runs are not reproducible.
If None, runs are not reproducible.

"""

Expand Down Expand Up @@ -1086,10 +1080,9 @@ class PygmoCmaes(Algorithm):
at least 64."""

seed: int | None = None
"""Seed used to sample the initial population.
"""Seed used to sample the initial population and by the pygmo algorithm.

It is not passed to the pygmo algorithm, whose own random number generator is
seeded randomly, so runs are not reproducible.
If None, runs are not reproducible.

"""

Expand Down Expand Up @@ -1273,10 +1266,9 @@ class PygmoSimulatedAnnealing(Algorithm):
"""

seed: int | None = None
"""Seed used to sample the initial population.
"""Seed used to sample the initial population and by the pygmo algorithm.

It is not passed to the pygmo algorithm, whose own random number generator is
seeded randomly, so runs are not reproducible.
If None, runs are not reproducible.

"""

Expand Down Expand Up @@ -1413,10 +1405,9 @@ class PygmoPso(Algorithm):
at least 10."""

seed: int | None = None
"""Seed used to sample the initial population.
"""Seed used to sample the initial population and by the pygmo algorithm.

It is not passed to the pygmo algorithm, whose own random number generator is
seeded randomly, so runs are not reproducible.
If None, runs are not reproducible.

"""

Expand Down Expand Up @@ -1646,10 +1637,9 @@ class PygmoPsoGen(Algorithm):
"""Number of cores used for parallel function evaluation."""

seed: int | None = None
"""Seed used to sample the initial population.
"""Seed used to sample the initial population and by the pygmo algorithm.

It is not passed to the pygmo algorithm, whose own random number generator is
seeded randomly, so runs are not reproducible.
If None, runs are not reproducible.

"""

Expand Down Expand Up @@ -1861,10 +1851,9 @@ class PygmoMbh(Algorithm):
at least 250."""

seed: int | None = None
"""Seed used to sample the initial population.
"""Seed used to sample the initial population and by the pygmo algorithm.

It is not passed to the pygmo algorithm, whose own random number generator is
seeded randomly, so runs are not reproducible.
If None, runs are not reproducible.

"""

Expand Down Expand Up @@ -1987,10 +1976,9 @@ class of algorithms by adopting, instead, the natural gradient. xNES is one
at least 64."""

seed: int | None = None
"""Seed used to sample the initial population.
"""Seed used to sample the initial population and by the pygmo algorithm.

It is not passed to the pygmo algorithm, whose own random number generator is
seeded randomly, so runs are not reproducible.
If None, runs are not reproducible.

"""

Expand Down Expand Up @@ -2160,10 +2148,9 @@ class PygmoGwo(Algorithm):
at least 64."""

seed: int | None = None
"""Seed used to sample the initial population.
"""Seed used to sample the initial population and by the pygmo algorithm.

It is not passed to the pygmo algorithm, whose own random number generator is
seeded randomly, so runs are not reproducible.
If None, runs are not reproducible.

"""

Expand Down Expand Up @@ -2371,10 +2358,9 @@ class PygmoIhs(Algorithm):
"""

seed: int | None = None
"""Seed used to sample the initial population.
"""Seed used to sample the initial population and by the pygmo algorithm.

It is not passed to the pygmo algorithm, whose own random number generator is
seeded randomly, so runs are not reproducible.
If None, runs are not reproducible.

"""

Expand Down Expand Up @@ -2513,10 +2499,9 @@ class PygmoDe1220(Algorithm):
at least 64."""

seed: int | None = None
"""Seed used to sample the initial population.
"""Seed used to sample the initial population and by the pygmo algorithm.

It is not passed to the pygmo algorithm, whose own random number generator is
seeded randomly, so runs are not reproducible.
If None, runs are not reproducible.

"""

Expand Down Expand Up @@ -2657,6 +2642,9 @@ def _solve_internal_problem(

# ====================================================================================

# pygmo algorithms that do not use random numbers and do not accept a seed.
_DETERMINISTIC_METHODS = frozenset({"compass_search"})


def _minimize_pygmo(
problem: InternalOptimizationProblem,
Expand All @@ -2683,6 +2671,8 @@ def _minimize_pygmo(
raise ValueError(f"{method} requires finite bounds for all parameters.")

pygmo_problem = _create_pygmo_problem(problem, len(x0), n_cores)
if seed is not None and method not in _DETERMINISTIC_METHODS:
specific_options = {**specific_options, "seed": seed}
algo = _create_algorithm(method, specific_options, n_cores)
pop = _create_population(
problem=pygmo_problem,
Expand Down
28 changes: 28 additions & 0 deletions tests/optimagic/optimizers/test_pygmo_optimizers.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
"""Test optimization helper functions."""

import warnings

import numpy as np
import pytest

from optimagic.algorithms import ALL_ALGORITHMS
from optimagic.config import IS_PYGMO_INSTALLED
from optimagic.optimization.optimize import minimize
from optimagic.optimizers.pygmo_optimizers import (
_convert_str_to_int,
get_population_size,
)
from optimagic.parameters.bounds import Bounds

test_cases = [
# popsize, x, lower_bound, expected
Expand All @@ -30,3 +36,25 @@ def test_convert_str_to_int():
_convert_str_to_int(d, 5)
with pytest.raises(ValueError):
_convert_str_to_int(d, "hello")


PYGMO_ALGORITHMS = sorted(name for name in ALL_ALGORITHMS if name.startswith("pygmo_"))


@pytest.mark.skipif(not IS_PYGMO_INSTALLED, reason="pygmo is not installed.")
@pytest.mark.parametrize("algorithm", PYGMO_ALGORITHMS)
def test_seed_makes_runs_reproducible(algorithm):
algo = ALL_ALGORITHMS[algorithm]().with_option(seed=0)
with warnings.catch_warnings():
warnings.simplefilter("ignore")
algo = algo.with_option_if_applicable(stopping_maxiter=10)

def run():
return minimize(
fun=lambda x: x @ x,
params=np.array([1.0, 2.0]),
algorithm=algo,
bounds=Bounds(lower=np.full(2, -5.0), upper=np.full(2, 5.0)),
).params

np.testing.assert_array_equal(run(), run())
Loading