Source code for jetgp.hyperparameter_optimizers.powell
import numpy as np
from scipy.optimize import minimize
[docs]
def powell(func, lb, ub, **kwargs):
"""
Powell's method with bounds and multi-start.
Parameters
----------
func : callable
Function to minimize.
lb, ub : array-like
Lower and upper bounds.
kwargs : dict
Optional arguments:
- x0 : initial guess for first restart
- n_restart_optimizer : number of random restarts (default=10)
- debug : bool, print intermediate results (default=False)
- Any Powell-specific options: maxiter, xtol, ftol, disp
"""
x0 = kwargs.pop("x0", None)
n_restart_optimizer = kwargs.pop("n_restart_optimizer", 10)
debug = kwargs.pop("debug", False)
# Extract Powell-specific options
options = {}
for key in ["maxiter", "xtol", "ftol", "disp"]:
if key in kwargs:
options[key] = kwargs.pop(key)
lb = np.array(lb)
ub = np.array(ub)
best_x = None
best_val = np.inf
for i in range(n_restart_optimizer):
if x0 is not None and i == 0:
x_init = np.array(x0)
else:
x_init = np.random.uniform(lb, ub)
res = minimize(
func,
x_init,
method="Powell",
bounds=list(zip(lb, ub)),
options=options,
**kwargs # extra kwargs forwarded
)
if res.fun < best_val:
best_val = res.fun
best_x = res.x
if debug:
print(f"[Powell] Restart {i+1}/{n_restart_optimizer} -> best_val={best_val}")
return best_x, best_val