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
The problem is
\begin{equation*} \max\{-x_0^2 - (x_1-1)^2 - 3x_0 + 2\} \end{equation*}subject to
\begin{align*} 4x_0 + x_1 &\leq 0.5\\ x_0^2 + x_0x_1 &\leq 2.0\\ x_0 &\geq 0 \\ x_1 &\geq 0 \end{align*}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)
def f(x):
return x[0]**2 + (x[1]-1)**2 + 3*x[0] - 2
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]})
bnds = ((0, None), (0, None))
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])