Scipy Optimization Interface#

pyoed.optimization.scipy_optimization.ScipyOptimizer([...])

Unified (with PyOED optimziers) interface to the scipy.optimize.minimize optimization routines.

pyoed.optimization.scipy_optimization.ScipyOptimizerConfigs(*)

Configuration dataclass for the ScipyOptimizer.

pyoed.optimization.scipy_optimization.ScipyOptimizerResults(*)

Container for results/data generated by the ScipyOptimizer.

A module implementing a unified interface to Scipy Optimization routine.

class ScipyOptimizerConfigs(*, debug=False, verbose=False, output_dir='./_PYOED_RESULTS_', size=None, name='ScipyOptimizer: Optimization using scipy.optimize.minimize', screen_output_iter=1, file_output_iter=100, fun=None, maximize=False, x0=<factory>, args=None, method='L-BFGS-B', jac=None, hess=None, hessp=None, bounds=None, constraints=None, tol=None, callback=None, options=<factory>)[source]#

Bases: OptimizerConfigs

Configuration dataclass for the ScipyOptimizer. The attributes here mirror the arguments of scipy.optimize.minimize. The documentation of attribute is removed here for clarity, though, we list them for completeness. Additionally, we add one extra flag maximize to add flexibility to the optimizer to solve maximization as well as minimization (simply by flipping the sign of the function and its derivatives)`

Parameters:
  • verbose (bool) – a boolean flag to control verbosity of the object.

  • debug (bool) – a boolean flag that enables adding extra functionlity in a debug mode

  • output_dir (str | Path) – the base directory where the output files will be saved.

  • name (str) – name of the optimizer. Default is None.

  • size (int | None) – dimension of the target/solution variable. Not all optimizers use this, but it is added for unification.

  • screen_output_iter (int) – iteration interval for screen output. Default is 1. Note that this should be a positive integer to enforce proper effect.

  • file_output_iter (int) – iteration interval for file output. Default is 1. Note that this should be a positive integer to enforce proper effect.

  • fun (Callable | None)

  • x0 (ndarray)

  • args (Tuple | None)

  • method (str)

  • jac (Callable | None)

  • hess (Callable | None)

  • hessp (Callable | None)

  • bounds (Sequence | None)

  • constraints (Sequence | None)

  • tol (float | None)

  • callback (Callable | None)

  • options (dict | None)

  • maximize (bool)

  • maxiter

fun: Callable | None#
maximize: bool#
x0: ndarray#
args: Tuple | None#
method: str#
jac: Callable | None#
hess: Callable | None#
hessp: Callable | None#
bounds: Sequence | None#
constraints: Sequence | None#
tol: float | None#
callback: Callable | None#
options: dict | None#
__init__(*, debug=False, verbose=False, output_dir='./_PYOED_RESULTS_', size=None, name='ScipyOptimizer: Optimization using scipy.optimize.minimize', screen_output_iter=1, file_output_iter=100, fun=None, maximize=False, x0=<factory>, args=None, method='L-BFGS-B', jac=None, hess=None, hessp=None, bounds=None, constraints=None, tol=None, callback=None, options=<factory>)#
class ScipyOptimizer(configs=None)[source]#

Bases: Optimizer

Unified (with PyOED optimziers) interface to the scipy.optimize.minimize optimization routines.

Parameters:

configs (ScipyOptimizerConfigs | dict | None) – Configurations of ScipyOptimizer, either as an instance of ScipyOptimizerConfigs, a dictionary, or None. The default is None, in which case, the default configurations provided by ScipyOptimizerConfigs are used.

__init__(configs=None)[source]#
validate_configurations(configs, raise_for_invalid=True)[source]#

Check the passed configuratios and make sure they are conformable with each other, and with current configurations once combined. This guarantees that any key-value pair passed in configs can be properly used

Note

Here only the locally-defined configurations in ScipyOptimizerConfigs are validated. Finally, super classes validators are called.

Parameters:
  • configs (dict | ScipyOptimizerConfigs) – full or partial (subset) configurations to be validated

  • raise_for_invalid (bool) – if True raise TypeError for invalid configrations type/key. Default True

Returns:

flag indicating whether passed configurations dictionary is valid or not

Raises:
Return type:

bool

update_configurations(**kwargs)[source]#

Take any set of keyword arguments, and lookup each in the configurations, and update as nessesary/possible/valid

Raises:

PyOEDConfigsValidationError – if invalid configurations passed

objective_function_value(x)[source]#

Evaluate the objective function self.configurations.fun at the passed x.

Parameters:

x – variable at which to evaluate the objective function value

Returns:

value of the objective/utility function at the passed instance x

objective_function_gradient(x)[source]#

Evaluate the gradient of the objective function self.configurations.jac at the passed x. If jac is not registered (i.e., None), finite difference approximation of the gradient is returned.

Parameters:

x – variable at which to evaluate the objective function value

Returns:

value of the objective/utility function at the passed instance x

solve(x0=None)[source]#

Start solving the optimization problem using scipy optimizer with registered configurations.

Parameters:

x0None or ``iterable` to be used as the initial guess. If None, the registered x0 in the configurations is used.

Returns:

an instance of ScipyOptimizerResults.

Raises:

TypeError if the initial policy has wrong size or any of the values fall outside the interval [0, 1]

plot_results(results, overwrite=False, bruteforce=False, fun_symbol='U', num_active=None, output_dir=None, keep_plots=False, fontsize=22, line_width=3, usetex=True, show_axis_grids=True, axis_grids_alpha=(0.25, 0.4), plots_format='pdf', **kwargs)[source]#

Given the results returned by solve(), visualize optimization results. This function, creates the following plots:

Note

Adding additional kwargs to enable high flexibility in calling this method.

Returns:

a dictionary with entries/keys:

  • figures a dictionary indexed by plot identifier with value set to the corresponding figure handle (or None if keep_plots is set to False)

  • stats: a dictionary holding statistics

  • extras:

Raises:

TypeError if the results dictionary is invalid

property size#

Dimension/size of the optimziation variable.

class ScipyOptimizerResults(*, optimization_results=None, configurations=None, optimization_trajectory=None, optimization_trajectory_objval=None)[source]#

Bases: OptimizerResults

Container for results/data generated by the ScipyOptimizer.

Parameters:
  • optimization_results (OptimizerResults | None) – results object OptimizerResults returned from the scipy optimizer.

  • configurations (None | dict) – a dictionary holding problem configurations. This can be obtained by calling asdict() of the configurations object used by the optimizer, e.g., ScipyOptimizerConfigs.

  • optimization_trajectory (None | Sequence[ndarray]) – the optimization trajectory (variable over iterations)

  • optimization_trajectory_objval (None | Sequence[float]) – the objective values corresopnding to points on the optimization trajectory

optimization_results: OptimizerResults | None#
configurations: None | dict#
optimization_trajectory: None | Sequence[ndarray]#
optimization_trajectory_objval: None | Sequence[float]#
property x: ndarray#

The optimal solution of the optimization problem.

property success: bool#

Whether or not the optimizer exited successfully.

property message: str#

Description of the cause of the termination.

property fun: float#

Value of objective function at the optimal solution x.

property jac: ndarray#

Value of the Jacobian of the objective function at the optimal solution x (if available).

property hess: object#

Value of the Hessian of the objective function at the optimal solution x (if available).

property hess_inv: object#

Value of the inverse of Hessian of the objective function at the optimal solution x (if available).

property nfev: int#

Number of evaluations of the objective functions.

property njev: int#

Number of evaluations of the the Jacobian of the objective function.

property nhev: int#

Number of evaluations of the the Hessian of the objective function.

property nit: int#

Number of iterations performed by the optimizer.

__init__(*, optimization_results=None, configurations=None, optimization_trajectory=None, optimization_trajectory_objval=None)#