Skip to content

optimize

run_minimize_with_restarts(objective, gradient, x0, bounds, n_restarts_optimizer=5, method='L-BFGS-B', maxit=100, verb=0, random_state=None)

Runs multiple restarts of the minimize() function and returns the best found result.

Parameters:

Name Type Description Default
objective callable

The objective function to minimize.

required
gradient callable

The gradient of the objective.

required
x0 ndarray

Initial guess for the optimizer.

required
bounds list

List of (min, max) pairs for each element in x0.

required
n_restarts_optimizer int

Number of random-restart attempts.

5
method str

Optimization method. Default “L-BFGS-B”.

'L-BFGS-B'
maxit int

Max iterations.

100
verb int

Verbosity level.

0
random_state int

Seed for the random-number generator to ensure reproducibility.

None

Returns:

Name Type Description
OptimizeResult

The best optimization result among all restarts.

Source code in spotpython/utils/optimize.py
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
def run_minimize_with_restarts(objective, gradient, x0, bounds, n_restarts_optimizer=5, method="L-BFGS-B", maxit=100, verb=0, random_state=None):
    """
    Runs multiple restarts of the minimize() function and returns the best found result.

    Args:
        objective (callable): The objective function to minimize.
        gradient (callable): The gradient of the objective.
        x0 (np.ndarray): Initial guess for the optimizer.
        bounds (list): List of (min, max) pairs for each element in x0.
        n_restarts_optimizer (int): Number of random-restart attempts.
        method (str): Optimization method. Default "L-BFGS-B".
        maxit (int): Max iterations.
        verb (int): Verbosity level.
        random_state (int, optional): Seed for the random-number generator to ensure reproducibility.

    Returns:
        OptimizeResult: The best optimization result among all restarts.
    """
    if random_state is not None:
        np.random.seed(random_state)

    best_result = None
    best_fun = float("inf")

    for _ in range(n_restarts_optimizer):
        # Create a random starting point within bounds
        x0_rand = []
        for (lb, ub), init_val in zip(bounds, x0):
            if lb == -np.inf or ub == np.inf:
                # If unbounded, keep the same initial guess
                x0_rand.append(init_val)
            else:
                x0_rand.append(np.random.uniform(lb, ub))
        x0_rand = np.array(x0_rand)

        result = minimize(
            fun=objective,
            x0=x0_rand,
            method=method,
            jac=gradient,
            bounds=bounds,
            options={"maxiter": maxit, "disp": verb > 0},
        )
        if result.fun < best_fun:
            best_fun = result.fun
            best_result = result

    return best_result