Last active
March 8, 2019 16:42
-
-
Save ahmed2m/25535b18fabf8a36ae66829ee740829b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"from scipy.optimize import linprog\n", | |
"from scipy.optimize import Bounds\n", | |
"from scipy.optimize import minimize\n", | |
"import numpy as np \n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
" con: array([], dtype=float64)\n", | |
" fun: -37.0\n", | |
" message: 'Optimization terminated successfully.'\n", | |
" nit: 3\n", | |
" slack: array([0., 0., 3.])\n", | |
" status: 0\n", | |
" success: True\n", | |
" x: array([3., 5.])\n" | |
] | |
} | |
], | |
"source": [ | |
"c = [-4,-5]\n", | |
"a = [[1,1],[1,3],[2,1]]\n", | |
"b = [8,18,14]\n", | |
"\n", | |
"res = linprog(c,A_ub=a,b_ub=b, method='simplex') \n", | |
"print (res)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 7, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
" ================ Results when initial guess is (-3.0, 3.0) ================\n", | |
" fun: -3.2235359612692725e-05\n", | |
" jac: array([-0.000199 , 0.00013173])\n", | |
" message: 'Optimization terminated successfully.'\n", | |
" nfev: 4\n", | |
" nit: 1\n", | |
" njev: 1\n", | |
" status: 0\n", | |
" success: True\n", | |
" x: array([-3., 3.])\n", | |
" ================ Results when initial guess is (-1, 1) ================\n", | |
" fun: -8.106213317674746\n", | |
" jac: array([-0.00058389, -0.00409687])\n", | |
" message: 'Optimization terminated successfully.'\n", | |
" nfev: 45\n", | |
" nit: 10\n", | |
" njev: 10\n", | |
" status: 0\n", | |
" success: True\n", | |
" x: array([-0.00935726, 1.58124096])\n", | |
" ================ Results when initial guess is (2, -1) ================\n", | |
" fun: -3.592489910983635\n", | |
" jac: array([-0.00011516, -0.00030747])\n", | |
" message: 'Optimization terminated successfully.'\n", | |
" nfev: 30\n", | |
" nit: 7\n", | |
" njev: 7\n", | |
" status: 0\n", | |
" success: True\n", | |
" x: array([ 1.2856778 , -0.00489063])\n", | |
" ================ Results when initial guess is (3, 1) ================\n", | |
" fun: -3.7765807176898147\n", | |
" jac: array([ 0.00097468, -0.00355253])\n", | |
" message: 'Optimization terminated successfully.'\n", | |
" nfev: 39\n", | |
" nit: 8\n", | |
" njev: 8\n", | |
" status: 0\n", | |
" success: True\n", | |
" x: array([-0.4600389 , -0.62935856])\n" | |
] | |
} | |
], | |
"source": [ | |
"def f(x):\n", | |
" return -1*(3*( (1-x[0])**2 ) * np.exp( -(x[0]**2)-(x[1]+1)**2 ) - 10*(x[0]/5 - (x[0]**3) -(x[1]**5)) * np.exp(-(x[0]**2) - (x[1]**2)) - (1/3)*np.exp(-((x[0]+1)**2) -(x[1]**2)))\n", | |
"\n", | |
"x0 = np.array([1,2], dtype=np.float64)\n", | |
"for i in [(-3.0,3.0),(-1,1),(2,-1),(3,1)]:\n", | |
" \n", | |
" x0[0]=i[0]\n", | |
" x0[1]=i[1]\n", | |
" res = minimize(f,x0,method='SLSQP')\n", | |
" print(\" ================ Results when initial guess is \",i,\"================\")\n", | |
" print(str(res))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python [conda env:selected_venv]", | |
"language": "python", | |
"name": "conda-env-selected_venv-py" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.7.2" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment