Constrained optimization using scipy
Contents
Constrained optimization using scipy¶
Randall Romero Aguilar, PhD
This demo is based on the original Matlab demo accompanying the Computational Economics and Finance 2001 textbook by Mario Miranda and Paul Fackler.
Original (Matlab) CompEcon file: demopt08.m
Running this file requires the Python version of CompEcon. This can be installed with pip by running
!pip install compecon --upgrade
Last updated: 2021-Oct-01
About¶
The problem is
subject to
Using scipy¶
The scipy.optimize.minimize function minimizes functions subject to equality constraints, inequality constraints, and bounds on the choice variables.
import numpy as np
from scipy.optimize import minimize
np.set_printoptions(precision=4,suppress=True)
First, we define the objective function, changing its sign so we can minimize it
def f(x):
return x[0]**2 + (x[1]-1)**2 + 3*x[0] - 2
Second, we specify the inequality constraints using a tuple of two dictionaries (one per constraint), writing each of them in the form \(g_i(x) \geq 0\), that is
cons = ({'type': 'ineq', 'fun': lambda x: 0.5 - 4*x[0] - x[1]},
{'type': 'ineq', 'fun': lambda x: 2.0 - x[0]**2 - x[0]*x[1]})
Third, we specify the bounds on \(x\):
bnds = ((0, None), (0, None))
Finally, we minimize the problem, using the SLSQP method, starting from \(x=[0,1]\)
x0 = [0.0, 1.0]
res = minimize(f, x0, method='SLSQP', bounds=bnds, constraints=cons)
print(res)
fun: -1.7499999999999876
jac: array([ 3., -1.])
message: 'Optimization terminated successfully'
nfev: 10
nit: 3
njev: 3
status: 0
success: True
x: array([0. , 0.5])