Created
March 30, 2014 17:55
-
-
Save njg/9876852 to your computer and use it in GitHub Desktop.
testing minimization methods
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
{ | |
"metadata": { | |
"name": "" | |
}, | |
"nbformat": 3, | |
"nbformat_minor": 0, | |
"worksheets": [ | |
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"%matplotlib inline\n", | |
"import numpy as np\n", | |
"from scipy.optimize import minimize" | |
], | |
"language": "python", | |
"outputs": [], | |
"prompt_number": 1 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"def rosen(x):\n", | |
" \"\"\"The Rosenbrock function\"\"\"\n", | |
" return sum(100.0*(x[1:]-x[:-1]**2.0)**2.0 + (1-x[:-1])**2.0)\n", | |
"\n", | |
"methods=['Nelder-Mead', 'Powell', 'CG', 'BFGS', 'Anneal', 'SLSQP'] " | |
], | |
"language": "python", | |
"outputs": [], | |
"prompt_number": 2 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"x0=np.array([2,3,4,5])\n", | |
"\n", | |
"for m in methods:\n", | |
" print m\n", | |
" opt={'disp':True}\n", | |
"\n", | |
" if m=='Anneal':\n", | |
" opt['maxiter']=500\n", | |
" #opt['schedule']='fast'\n", | |
" #opt['schedule']='cauchy'\n", | |
" opt['schedule']='boltzmann'\n", | |
" res=minimize(rosen,x0,method=m,\n", | |
" \t\t\toptions=opt)\n", | |
" print res, '\\n'\n", | |
"\n" | |
], | |
"language": "python", | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"Nelder-Mead\n", | |
"Optimization terminated successfully.\n", | |
" Current function value: 0.000000\n", | |
" Iterations: 330\n", | |
" Function evaluations: 557\n", | |
" status: 0\n", | |
" nfev: 557\n", | |
" success: True\n", | |
" fun: 1.3587127674836083e-09\n", | |
" x: array([ 0.99999699, 0.99999729, 0.99999459, 0.99999064])\n", | |
" message: 'Optimization terminated successfully.'\n", | |
" nit: 330 \n", | |
"\n", | |
"Powell\n", | |
"Optimization terminated successfully." | |
] | |
}, | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"\n", | |
" Current function value: 0.000000\n", | |
" Iterations: 28\n", | |
" Function evaluations: 1364\n", | |
" status: 0\n", | |
" success: True\n", | |
" direc: array([[ -4.79377475e-04, -7.63426520e-04, -1.54154727e-03,\n", | |
" -3.08976968e-03],\n", | |
" [ -3.85266935e-03, -9.73515947e-03, -1.95291302e-02,\n", | |
" -3.78494124e-02],\n", | |
" [ -1.64624628e-02, -4.25115876e-02, -9.35815385e-02,\n", | |
" -2.22211515e-01],\n", | |
" [ 1.75089522e-08, 3.58033159e-08, 2.85708782e-08,\n", | |
" 7.02602663e-08]])\n", | |
" nfev: 1364\n", | |
" fun: 5.1795359650505254e-25\n", | |
" x: array([ 1., 1., 1., 1.])\n", | |
" message: 'Optimization terminated successfully.'\n", | |
" nit: 28 \n", | |
"\n", | |
"CG\n", | |
"Optimization terminated successfully." | |
] | |
}, | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"\n", | |
" Current function value: 0.000000\n", | |
" Iterations: 124\n", | |
" Function evaluations: 1440\n", | |
" Gradient evaluations: 240\n", | |
" status: 0\n", | |
" success: True\n", | |
" njev: 240\n", | |
" nfev: 1440\n", | |
" fun: 1.232110081155319e-10\n", | |
" x: array([ 0.99999759, 0.99999517, 0.99999031, 0.99998059])\n", | |
" message: 'Optimization terminated successfully.'\n", | |
" jac: array([ 5.40941797e-06, 5.19370724e-06, -1.74661663e-07,\n", | |
" -6.75569820e-06]) \n", | |
"\n", | |
"BFGS\n", | |
"Optimization terminated successfully.\n", | |
" Current function value: 0.000000\n", | |
" Iterations: 60\n", | |
" Function evaluations: 480\n", | |
" Gradient evaluations: 80\n", | |
" status: 0\n", | |
" success: True\n", | |
" njev: 80\n", | |
" nfev: 480\n", | |
" hess_inv: array([[ 0.02412562, 0.04570413, 0.09044236, 0.18041822],\n", | |
" [ 0.04570413, 0.09142452, 0.18071695, 0.36046589],\n", | |
" [ 0.09044236, 0.18071695, 0.36262278, 0.72304154],\n", | |
" [ 0.18041822, 0.36046589, 0.72304154, 1.44665636]])\n", | |
" fun: 4.69120880742064e-11\n", | |
" x: array([ 0.9999985 , 0.99999701, 0.99999403, 0.99998804])\n", | |
" message: 'Optimization terminated successfully.'\n", | |
" jac: array([ 6.94736551e-07, -2.47249630e-06, 2.79980237e-06,\n", | |
" -8.76512818e-07]) \n", | |
"\n", | |
"Anneal\n", | |
"Warning: Maximum number of iterations exceeded." | |
] | |
}, | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"\n", | |
" status: 3\n", | |
" success: False\n", | |
" accept: 9024\n", | |
" nfev: 25101\n", | |
" T: 4676272727.9951382\n", | |
" fun: 1056134.5319457012\n", | |
" x: array([ -4.88510617e+00, -1.06598901e-01, 3.66246724e+01,\n", | |
" 1.24845183e+03])\n", | |
" message: 'Maximum cooling iterations reached'\n", | |
" nit: 501 \n", | |
"\n", | |
"SLSQP\n", | |
"Optimization terminated successfully. (Exit mode 0)\n", | |
" Current function value: 2.88053419653e-08\n", | |
" Iterations: 38\n", | |
" Function evaluations: 249\n", | |
" Gradient evaluations: 38\n", | |
" status: 0\n", | |
" success: True\n", | |
" njev: 38\n", | |
" nfev: 249\n", | |
" fun: 2.8805341965344145e-08\n", | |
" x: array([ 0.99997245, 0.99994604, 0.99989499, 0.99980146])\n", | |
" message: 'Optimization terminated successfully.'\n", | |
" jac: array([ 0.00789731, 0.01259049, -0.03355575, 0.01202431, 0. ])\n", | |
" nit: 38 \n", | |
"\n" | |
] | |
} | |
], | |
"prompt_number": 3 | |
} | |
] | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment