ScipyOptimizer#
- class lumopt2.optimizer.scipy_optimizer.ScipyOptimizer(method: str = 'L-BFGS-B', max_iter: int | None = None, max_feval: int | None = None, ftol: float = 1e-06, gtol: float = 1e-05, bounds: list | None = None, options: Dict[str, Any] | None = None, max_eval: int | None = None)#
Wrapper for scipy.optimize.minimize optimizers, particularly L-BFGS-B.
This class provides a clean interface to scipy’s optimization methods with proper handling of gradients, bounds, and optimization parameters commonly used in photonic inverse design.
- Parameters:
- method
str,optional Optimization method to use. Supported methods include ‘L-BFGS-B’, ‘BFGS’, ‘CG’, ‘Nelder-Mead’, ‘Powell’, ‘SLSQP’, etc. See scipy.optimize.minimize documentation for full list (default: ‘L-BFGS-B’).
- max_iter
int,optional Maximum number of iterations (default: 100).
- max_feval
int,optional Hard upper bound on the number of objective-function evaluations. A single SciPy iteration may request many evaluations (line search, finite-difference gradients, simplex updates, …), so this knob is independent of
max_iter. Translates tomaxfunforL-BFGS-BandmaxfevforNelder-Mead/Powell. If the chosen method does not expose a function-evaluation budget, the value is ignored with a warning (default: None).- ftol
float,optional Tolerance for termination by the change of the objective function. For L-BFGS-B, this is the factr parameter multiplied by machine precision (default: 1e-6).
- gtol
float,optional Tolerance for termination by the gradient norm (default: 1e-5).
- bounds
listoftuple,optional Bounds for each parameter as (min, max) pairs. If None, uses unbounded optimization (default: None). When bounds are provided, LumOpt2 requires every lower/upper bound to be finite.
- options
dict,optional Additional options to pass to scipy.optimize.minimize (default: None).
- max_eval
int,optional Backward-compatible alias for
max_iter(default: None).Deprecated since version Use:
max_iterto cap iterations ormax_fevalto cap function evaluations.
- method
- Attributes:
- method
str The optimization method being used.
- max_iter
int Maximum number of iterations.
- max_feval
intorNone Maximum number of objective-function evaluations (
Nonefor no cap).- ftol
float Function tolerance.
- gtol
float Gradient tolerance.
- bounds
listorNone Parameter bounds.
- options
dict Additional scipy options.
- result
scipy.optimize.OptimizeResultorNone Result object from the last optimization run.
- history
dict Dictionary storing optimization history with keys: - ‘params’: list of parameter arrays at each iteration - ‘fom’: list of FOM values at each iteration - ‘gradient’: list of gradient arrays at each iteration
- method
Notes
The L-BFGS-B method is particularly well-suited for inverse design because: - It handles box constraints (bounds) naturally - It’s memory-efficient for high-dimensional problems - It uses gradient information efficiently - It converges quickly for smooth objective functions
The optimizer expects the objective function to be maximized. If you need to minimize, negate your FOM.
Examples
Basic L-BFGS-B optimization:
>>> optimizer = ScipyOptimizer(method='L-BFGS-B', max_iter=50) >>> result = optimizer.optimize(objective_func, gradient_func, initial_params)
With bounds:
>>> bounds = [(0, 1) for _ in range(n_params)] >>> optimizer = ScipyOptimizer(method='L-BFGS-B', bounds=bounds) >>> result = optimizer.optimize(objective_func, gradient_func, initial_params)
Using different method:
>>> optimizer = ScipyOptimizer(method='SLSQP', max_iter=100, ftol=1e-8) >>> result = optimizer.optimize(objective_func, gradient_func, initial_params)
Methods
Get the optimization history.
Get the full scipy optimization result object.
ScipyOptimizer.optimize(objective_func, ...)Run the optimization.
Attributes
Return True if the optimizer uses a gradient-free method.