optimize
|
Minimize a scalar function of one or more variables using Sequential Least Squares Programming (SLSQP). |
Returns the default options for the |
- pyslsqp.optimize(x0, obj=None, grad=None, con=None, jac=None, meq=0, callback=None, xl=None, xu=None, x_scaler=1.0, obj_scaler=1.0, con_scaler=1.0, maxiter=100, acc=1e-06, iprint=1, finite_diff_abs_step=None, finite_diff_rel_step=1.4901161193847656e-08, summary_filename='slsqp_summary.out', warm_start=False, hot_start=False, load_filename=None, save_itr=None, save_filename='slsqp_recorder.hdf5', save_vars=['x', 'objective', 'optimality', 'feasibility', 'step', 'iter', 'majiter', 'ismajor', 'mode'], visualize=False, visualize_vars=['objective', 'optimality', 'feasibility'], keep_plot_open=False, save_figname='slsqp_plot.pdf')[source]
Minimize a scalar function of one or more variables using Sequential Least Squares Programming (SLSQP). This function is a wrapper to the original SLSQP implementation by Dieter Kraft. The wrapper provides a slightly modified interface to the optimization problem and many additional features, compared to the Scipy wrapper.
This function solves the general nonlinear programming problem:
minimize f(x) subject to c_i(x) = 0, i = 1,...,meq c_i(x) >= 0, i = meq+1,...,m xl_i <= x_i <= xu_i , i = 1,...,n
where x is a vector of variables with size n, f(x) is the objective, c(x) is the constraint function, and xl and xu are vectors of lower and upper bounds, respectively. The first meq constraints are equalities while the remaining (m - meq) constraints are inequalities.
- Parameters
- x0np.ndarray
Initial guess for the optimization variables. Array of real elements of size (n,), where ‘n’ is the number of independent variables.
x0is a necessary argument (unlike other arguments which are optional) to inform the optimizer about the number of optimization variables.- objcallable
Objective function to be minimized. The function is called as
obj(x), wherexis the array of independent variables.- concallable
Vector-valued constraint function of size m. The function is called as
con(x), wherexis the array of optimization variables. The first meq constraints are treated as equality constraints, while the remaining (m - meq) constraints are treated as inequality constraints.- xlnp.ndarray or scalar, default=None
Lower bounds on optimization variables. Defaults to None, in which case bounds are assumed to be
-np.inf. xl can be a scalar, in which case all variables have the same lower bound, or an array of real elements of size (n,).- xunp.ndarray or scalar, default=None
Upper bounds on optimization variables. Defaults to None, in which case bounds are assumed to be
np.inf. xu can be a scalar, in which case all variables have the same upper bound, or an array of real elements of size (n,).- x_scalerfloat or np.ndarray, default=1.0
Factor by which the optimization variables are scaled before sending to SLSQP. If float, all variables are scaled by the same factor. If np.ndarray of size (n,), each variable is scaled by the corresponding factor.
- obj_scalerfloat, default=1.0
Factor by which the objective function value is scaled before sending to SLSQP.
- con_scalerfloat or np.ndarray, default=1.0
Factor by which the constraint function values are scaled before sending to SLSQP. If float, all constraints are scaled by the same factor. If np.ndarray of size (m,), each constraint is scaled by the corresponding factor.
- meqint, default=0
The number of equality constraints. Defaults to 0.
- gradcallable, default=None
Gradient of the objective function. If None, the gradient will be approximated using finite differences.
- jaccallable, default=None
Jacobian of the constraint function. If None, the Jacobian will be approximated using finite differences.
- maxiterint, default=100
Maximum number of iterations.
- accfloat, default=1.0E-6
abs(acc) is the stopping criterion and controls the final accuracy. If
acc< 0, a maximization problem is solved. Otherwise, a minimization problem is solved.- iprintint, default=1
Controls the verbosity of the SLSQP algorithm. Set
iprint <= 0to suppress all console outputs. Setiprint = 1to print only the final results upon completion. Setiprint >= 2to print the status of each major iteration and the final results.- finite_diff_abs_stepNone or array_like, default=None
The absolute step size to use for numerical approximation of the derivatives. If None (default), then step is selected using finite_diff_rel_step.
- finite_diff_rel_stepNone or array_like, default=None
The relative step size to use for numerical approximation of the derivatives. The absolute step size is computed as
h = rel_step * max(1, abs(x)), possibly adjusted to fit into the bounds. Not used if finite_diff_abs_step is given. By default, it is selected automatically as_epsilon = np.sqrt(np.finfo(float).eps)approximately 1e-8.- callbackcallable, default=None
Function to be called after each major iteration. The function is called as
callback(x), wherexis the optimization variable vector from the current major iteration.- save_itr{None, ‘all’, ‘major’}, default=None
If ‘all’, all iterations are saved. If ‘major’, only major iterations are saved. By default,
save_itris None, and no iterations are saved.- summary_filenamestr, default=’slsqp.out’
Name of the file to save the summary of the optimization process. By default, the file is saved as
'slsqp_summary.out'.- save_filenamestr, default=’slsqp_recorder.hdf5’
Name of the file to save the iterations. By default, the file is saved as
'slsqp_recorder.hdf5'.- save_varslist, default=[‘x’, ‘objective’, ‘optimality’, ‘feasibility’, ‘step’, ‘mode’, ‘iter’, ‘majiter’, ‘ismajor’]
List of variables to save. The full list of variables available are
['x', 'objective', 'optimality', 'feasibility', 'step', 'mode', 'iter', 'majiter', 'ismajor', 'constraints', 'gradient', 'multipliers', 'jacobian'].- warm_startbool, default=False
If True, the optimization algorithm will use the previous solution from the last optimization as the initial guess.
- hot_startbool, default=None
If True, the optimization algorithm will use the saved objective, constraints, gradient, and jacobian functions from the previous optimization until the iterations reach the last saved iteration. Note that this only works if save_itr for the previous optimization was set to ‘all’. This is useful when the objective, constraints, gradient, and jacobian functions are expensive to compute and the optimization process was interrupted in a prior run.
- load_filenamestr, default=None
Name of the file to load the previous optimization solution or iterates for warm or hot start. If None, the
load_filenameis assumed to be the same as the save_filename. Ifload_filenameis same as the providedsave_filenamewill be updated as: ‘save_filename without extension’ + ‘_warm.hdf5’ or ‘_hot.hdf5’ depending on the warm_start or hot_start.- visualizebool, default=False
Set to True to visualize the optimization process. Only major iterations are visualized.
- visualize_varslist, default=[‘objective’, ‘optimality’, ‘feasibility’]
List of scalar variables to visualize. Available variables are
['x[i]', 'objective', 'optimality', 'feasibility', 'constraints[i]', 'gradient[i]', 'multipliers[i]', 'jacobian[i,j]'].- keep_plot_openbool, default=False
Set to True to keep the plot window open after the optimization process is complete.
- save_fignamestr, default=’slsqp_plot.pdf’
Name of the file to save the plot.
Examples
>>> import numpy as np >>> from pyslsqp import optimize >>> obj = lambda x: np.sum(x**2) >>> grad = lambda x: 2*x >>> xl = 0.0 >>> xu = np.array([1, 1]) >>> x0 = np.array([0.5, 0.5]) >>> results = optimize(x0, obj=obj, grad=grad, xl=xl, xu=xu) No constraints defined. Running an unconstrained optimization problem... Optimization terminated successfully (Exit mode 0) Final objective value : 0.000000e+00 Final optimality : 0.000000e+00 Final feasibility : 0.000000e+00 Number of major iterations : 2 Number of function evaluations : 2 Number of derivative evaluations : 2 Average Derivative evaluation time : ... s per evaluation Average Function evaluation time : ... s per evaluation Total Function evaluation time : ... s [ ...%] Total Derivative evaluation time : ... s [ ...%] Optimizer time : ... s [ ...%] Processing time : ... s [ ...%] Visualization time : ... s [ 0.00%] Total optimization time : ... s [100.00%] Summary saved to : slsqp_summary.out >>> results['x'] array([0., 0.]) >>> con = lambda x: np.array([x[0] - 0.1, x[1] - 0.2]) >>> jac = lambda x: np.array([[1, 0], [0, 1]]) >>> meq = 1 >>> results = optimize(x0, obj=obj, grad=grad, con=con, jac=jac, meq=meq, xl=xl, xu=xu) Optimization terminated successfully (Exit mode 0) Final objective value : 5.000000e-02 Final optimality : 1.538763e-16 Final feasibility : 1.942890e-16 Number of major iterations : 2 Number of function evaluations : 2 Number of derivative evaluations : 2 Average Derivative evaluation time : ... s per evaluation Average Function evaluation time : ... s per evaluation Total Function evaluation time : ... s [ ...%] Total Derivative evaluation time : ... s [ ...%] Optimizer time : ... s [ ...%] Processing time : ... s [ ...%] Visualization time : ... s [ 0.00%] Total optimization time : ... s [100.00%] Summary saved to : slsqp_summary.out >>> results['x'] array([0.1, 0.2])
- pyslsqp.get_default_options()[source]
Returns the default options for the
optimize()function as a dictionary.Examples
>>> options = get_default_options() >>> options {'obj': None, 'grad': None, 'con': None, 'jac': None, 'meq': 0, 'callback': None, 'xl': None, 'xu': None, 'x_scaler': 1.0, 'obj_scaler': 1.0, 'con_scaler': 1.0, 'maxiter': 100, 'acc': 1e-06, 'iprint': 1, 'finite_diff_abs_step': None, 'finite_diff_rel_step': 1.4901161193847656e-08, 'summary_filename': 'slsqp_summary.out', 'warm_start': False, 'hot_start': False, 'load_filename': None, 'save_itr': None, 'save_filename': 'slsqp_recorder.hdf5', 'save_vars': ['x', 'objective', 'optimality', 'feasibility', 'step', 'iter', 'majiter', 'ismajor', 'mode'], 'visualize': False, 'visualize_vars': ['objective', 'optimality', 'feasibility'], 'keep_plot_open': False, 'save_figname': 'slsqp_plot.pdf'}