{ "nbformat_minor": 0, "nbformat": 4, "cells": [ { "execution_count": null, "cell_type": "code", "source": [ "%matplotlib inline" ], "outputs": [], "metadata": { "collapsed": false } }, { "source": [ "\nConstraint optimization: visualizing the geometry\n==================================================\n\nA small figure explaining optimization with constraints\n\n" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "import numpy as np\nimport pylab as pl\nfrom scipy import optimize\n\nx, y = np.mgrid[-2.9:5.8:.05, -2.5:5:.05]\nx = x.T\ny = y.T\n\nfor i in (1, 2):\n # Create 2 figure: only the second one will have the optimization\n # path\n pl.figure(i, figsize=(3, 2.5))\n pl.clf()\n pl.axes([0, 0, 1, 1])\n\n contours = pl.contour(np.sqrt((x - 3)**2 + (y - 2)**2),\n extent=[-3, 6, -2.5, 5],\n cmap=pl.cm.gnuplot)\n pl.clabel(contours,\n inline=1,\n fmt='%1.1f',\n fontsize=14)\n pl.plot([-1.5, -1.5, 1.5, 1.5, -1.5],\n [-1.5, 1.5, 1.5, -1.5, -1.5], 'k', linewidth=2)\n pl.fill_between([ -1.5, 1.5],\n [ -1.5, -1.5],\n [ 1.5, 1.5],\n color='.8')\n pl.axvline(0, color='k')\n pl.axhline(0, color='k')\n\n pl.text(-.9, 4.4, '$x_2$', size=20)\n pl.text(5.6, -.6, '$x_1$', size=20)\n pl.axis('equal')\n pl.axis('off')\n\n# And now plot the optimization path\naccumulator = list()\n\ndef f(x):\n # Store the list of function calls\n accumulator.append(x)\n return np.sqrt((x[0] - 3)**2 + (x[1] - 2)**2)\n\n\n# We don't use the gradient, as with the gradient, L-BFGS is too fast,\n# and finds the optimum without showing us a pretty path\ndef f_prime(x):\n r = np.sqrt((x[0] - 3)**2 + (x[0] - 2)**2)\n return np.array(((x[0] - 3)/r, (x[0] - 2)/r))\n\noptimize.fmin_l_bfgs_b(f, np.array([0, 0]), approx_grad=1,\n bounds=((-1.5, 1.5), (-1.5, 1.5)))\n\naccumulated = np.array(accumulator)\npl.plot(accumulated[:, 0], accumulated[:, 1])\n\npl.show()" ], "outputs": [], "metadata": { "collapsed": false } } ], "metadata": { "kernelspec": { "display_name": "Python 2", "name": "python2", "language": "python" }, "language_info": { "mimetype": "text/x-python", "nbconvert_exporter": "python", "name": "python", "file_extension": ".py", "version": "2.7.12", "pygments_lexer": "ipython2", "codemirror_mode": { "version": 2, "name": "ipython" } } } }