3. Nonlinear constrained optimization#
In this chapter, we’ll cover how to apply scipy.optimize.minimize to nonlinear constrained optimization problems. As a reminder, nonlinear constrained optimization considers:
with:
\(f\left(x\right)\), the linear or nonlinear objective function.
\(x\), the \(n\) design variables
\(g_j\left(x\right)\), the \(m\) linear or nonlinear inequality constraints
\(h_k\left(x\right)\), the \(p\) linear or nonlinear inequality constraints
\(x_k^l\) and \(x_k^u\), the \(n\) low er and upper bounds of the design variable
Method#
For linear programs, we can use the function scipy.optimize.minimize again. The documentation of this function is available here:
https://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.minimize.html [The SciPy community, 2024]. In this course we’ll cover only the relevant parts.
For unconstrained optimization we need to run at least scipy.optimize.minimize(fun, x0, bounds, constraints, ...) with:
fun, the function representing the objective function \(f\left(x\right)\) to be minimized.funis a callable. Thescipy.optimize.minimizefunction takes care of defining and inputting our design variable \(x\).x0, the initial guess for our design variable \(x\). It needs to be andarraywith length \(n\)Bounds: A sequence of \(i\)(min, max)pairs for each element in \(x\), defining the minimum \(x_i^l\) and maximum values \(x_i^u\) of that decision variable.Noneis used to specify no bound.constraints, a single or a list of constraint objective either being:scipy.optimize.LinearConstraintscipy.optimize.NonlinearConstraint
As you can see, the constraints are stored in an object scipy.optimize.LinearConstraint and/or scipy.optimize.NonlinearConstraint. These function have the following input, for scipy.optimize.NonlinearConstraint(fun, lb, ub, ...):
fun, the function representing the constraint function \(g\left(x\right)\) or \(h\left(x\right)\) to be minimized. Again,funis a callable. Thescipy.optimize.minimizefunction takes care of defining and inputting our design variable \(x\).lbandub, two arrays containing the lower- and upper bounds for each of the constraint functions \(g\left(x\right)\). This lower bound can be-np.infornp.infto represent one-sides constraints. If the lower- and upper bound are set to the same value, an equality function is modelled.
For linear constraints, the constraint function is stored in a matrix again: scipy.optimize.NonlinearConstraint(A, lb, ub, ...):
A, a twodimensional numpy array with the \(n\) coefficient of the \(m\) linear inequality constraints matrix \({A_{ub}}\).lbandubas forscipy.optimize.NonlinearConstraint
Please note that unlike with linear constraints optimization, the right-hand-side of the constraints are not stored in an upper bound vector, but defined with lb and ub.
The function scipy.optimize.linprog outputs an object scipy.optimize.OptimizeResult similar as scipy.optimize.minimize explained for unconstrained optimization.