Repository navigation
Description
Bug
MultistartOptions(mixing_weight_method=...) accepts a function with the signature (iteration, n_iterations, min_weight, max_weight) -> float:
optimagic/src/optimagic/optimization/multistart_options.py
Lines 50 to 52 in 1d05a14
| mixing_weight_method: ( | |
| Literal["tiktak", "linear"] | Callable[[int, int, float, float], float] | |
| ) = "tiktak" |
Every multistart optimization with such a function fails before the first local optimization starts.
How to reproduce
import numpy as np
import optimagic as om
def constant_weight(iteration, n_iterations, min_weight, max_weight):
return 0.5
om.minimize(
fun=lambda x: x @ x,
params=np.ones(2),
algorithm="scipy_lbfgsb",
bounds=om.Bounds(lower=-np.ones(2), upper=np.ones(2)),
multistart=om.MultistartOptions(mixing_weight_method=constant_weight),
)
# UnboundLocalError: cannot access local variable '_weight_method' where it is not
# associated with a valueCause
In get_internal_multistart_options_from_public, the branch for a callable sets weight_func, but the next statement overwrites weight_func with partial(_weight_method, ...). _weight_method is only defined in the else branch:
optimagic/src/optimagic/optimization/multistart_options.py
Lines 387 to 396 in 1d05a14
| if callable(options.mixing_weight_method): | |
| weight_func = 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], | |
| ) |
if callable(options.mixing_weight_method):
weight_func = 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],
)The code was added with the MultistartOptions object in #513 (version 0.5.0). No test passes a function as mixing_weight_method; tests/optimagic/optimization/test_multistart_options.py only checks that an invalid string raises an error.
Possible fix
Select the function in both branches and pass mixing_weight_bounds to both, as the documented signature with min_weight and max_weight suggests:
if callable(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],
)Add a test that runs a multistart optimization with a custom weight function and checks that the function receives the bounds.
Found while reviewing #743. The bug is on main (1d05a14), independent of #743.
Activity
anshurajbisoyi98-ctrl commentedon Nov 26, 2026
I'd like to pick this up. Both branches should set
_weight_method, and the boundsshould be passed positionally so custom functions don't depend on the names
min_weight/max_weight. I'll add a test.