Skip to content

A function as mixing_weight_method in MultistartOptions raises UnboundLocalError #753

Backlog

Description

@janosg

Bug

MultistartOptions(mixing_weight_method=...) accepts a function with the signature (iteration, n_iterations, min_weight, max_weight) -> float:

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 value

Cause

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:

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

  1. anshurajbisoyi98-ctrl commented on Nov 26, 2026

    @anshurajbisoyi98-ctrl
    Contributor

    I'd like to pick this up. Both branches should set _weight_method, and the bounds
    should be passed positionally so custom functions don't depend on the names
    min_weight/max_weight. I'll add a test.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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