diff --git a/src/optimagic/optimization/multistart_options.py b/src/optimagic/optimization/multistart_options.py index f17086bed..ec1084100 100644 --- a/src/optimagic/optimization/multistart_options.py +++ b/src/optimagic/optimization/multistart_options.py @@ -1,5 +1,4 @@ from dataclasses import dataclass -from functools import partial from typing import Callable, Literal, Sequence, TypedDict, cast import numpy as np @@ -385,15 +384,14 @@ def get_internal_multistart_options_from_public( batch_evaluator = process_batch_evaluator(options.batch_evaluator) if callable(options.mixing_weight_method): - weight_func = options.mixing_weight_method + _weight_method = options.mixing_weight_method else: _weight_method = WEIGHT_FUNCTIONS[options.mixing_weight_method] - weight_func = partial( - _weight_method, - min_weight=options.mixing_weight_bounds[0], - max_weight=options.mixing_weight_bounds[1], - ) + min_weight, max_weight = options.mixing_weight_bounds + + def weight_func(iteration: int, n_iterations: int) -> float: + return _weight_method(iteration, n_iterations, min_weight, max_weight) if n_samples is None: if options.stopping_maxopt is None: diff --git a/tests/optimagic/optimization/test_multistart_options.py b/tests/optimagic/optimization/test_multistart_options.py index cf3b3d439..5935a2a8c 100644 --- a/tests/optimagic/optimization/test_multistart_options.py +++ b/tests/optimagic/optimization/test_multistart_options.py @@ -161,3 +161,20 @@ def test_get_internal_multistart_options_from_public_defaults(): assert got.n_samples == 500 assert got.stopping_maxopt == 50 assert got.batch_size == 1 + + +def test_get_internal_multistart_options_from_public_callable_mixing_weight(): + def weight(i, n, lower, upper): + return lower + upper * i / n + + options = MultistartOptions( + mixing_weight_method=weight, mixing_weight_bounds=(0.2, 0.5) + ) + + got = get_internal_multistart_options_from_public( + options, + params=np.arange(5), + params_to_internal=lambda x: x, + ) + + assert np.allclose(got.weight_func(2, 10), 0.3)