|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "## Scipy Optimization [Source](https://docs.scipy.org/doc/scipy/reference/optimize.html#module-scipy.optimize)" |
| 8 | + ] |
| 9 | + }, |
| 10 | + { |
| 11 | + "cell_type": "markdown", |
| 12 | + "metadata": {}, |
| 13 | + "source": [ |
| 14 | + "SciPy optimize provides functions for minimizing (or maximizing) objective functions, possibly subject to constraints. It includes solvers for nonlinear problems (with support for both local and global optimization algorithms), linear programing, constrained and nonlinear least-squares, root finding and curve fitting." |
| 15 | + ] |
| 16 | + }, |
| 17 | + { |
| 18 | + "cell_type": "markdown", |
| 19 | + "metadata": {}, |
| 20 | + "source": [ |
| 21 | + "1. Scalar Functions Optimization\n", |
| 22 | + "2. Local (Multivariate) Optimization\n", |
| 23 | + "3. Global Optimization\n", |
| 24 | + " - **basinhopping** : Find the global minimum of a function using the basin-hopping algorithm\n", |
| 25 | + " - **brute** : Minimize a function over a given range by brute force.\n", |
| 26 | + " - **differential_evolution** : Finds the global minimum of a multivariate function.\n", |
| 27 | + " - **shgo** : Finds the global minimum of a function using SHG optimization.\n", |
| 28 | + " - **dual_annealing**: Find the global minimum of a function using Dual Annealing.\n", |
| 29 | + "\n", |
| 30 | + "\n", |
| 31 | + "4. Least-squares and Curve Fitting\n", |
| 32 | + "5. Root finding" |
| 33 | + ] |
| 34 | + }, |
| 35 | + { |
| 36 | + "cell_type": "markdown", |
| 37 | + "metadata": {}, |
| 38 | + "source": [ |
| 39 | + "### Brute Method (Global Optimization)\n", |
| 40 | + "\n", |
| 41 | + "Minimize a function over a given range by brute force.\n", |
| 42 | + "\n", |
| 43 | + "Uses the “brute force” method, i.e., computes the function’s value at each point of a multidimensional grid of points, to find the global minimum of the function.\n", |
| 44 | + "\n", |
| 45 | + "The function is evaluated everywhere in the range with the datatype of the first call to the function, as enforced by the vectorize NumPy function. The value and type of the function evaluation returned when ```full_output=True``` are affected in addition by the finish argument (see Notes).\n", |
| 46 | + "\n", |
| 47 | + "The brute force approach is inefficient because the number of grid points increases exponentially - the number of grid points to evaluate is ```Ns ** len(x)```. Consequently, even with coarse grid spacing, even moderately sized problems can take a long time to run, and/or run into memory limitations." |
| 48 | + ] |
| 49 | + }, |
| 50 | + { |
| 51 | + "cell_type": "code", |
| 52 | + "execution_count": 6, |
| 53 | + "metadata": {}, |
| 54 | + "outputs": [], |
| 55 | + "source": [ |
| 56 | + "import numpy as np\n", |
| 57 | + "import numpy.linalg as la\n", |
| 58 | + "import scipy.optimize as sopt\n", |
| 59 | + "from scipy.optimize import minimize\n", |
| 60 | + "\n", |
| 61 | + "import matplotlib.pyplot as pt\n", |
| 62 | + "from mpl_toolkits.mplot3d import axes3d\n", |
| 63 | + "%matplotlib inline\n", |
| 64 | + "import seaborn as sns\n", |
| 65 | + "sns.set()" |
| 66 | + ] |
| 67 | + }, |
| 68 | + { |
| 69 | + "cell_type": "code", |
| 70 | + "execution_count": 7, |
| 71 | + "metadata": {}, |
| 72 | + "outputs": [], |
| 73 | + "source": [ |
| 74 | + "params = (2, 3, 7, 8, 9, 10, 44, -1, 2, 26, 1, -2, 0.5)\n", |
| 75 | + "\n", |
| 76 | + "def f1(z, *params):\n", |
| 77 | + " x, y = z\n", |
| 78 | + " a, b, c, d, e, f, g, h, i, j, k, l, scale = params\n", |
| 79 | + " return (a * x**2 + b * x * y + c * y**2 + d*x + e*y + f)\n", |
| 80 | + "\n", |
| 81 | + "def f2(z, *params):\n", |
| 82 | + " x, y = z\n", |
| 83 | + " a, b, c, d, e, f, g, h, i, j, k, l, scale = params\n", |
| 84 | + " return (-g*np.exp(-((x-h)**2 + (y-i)**2) / scale))\n", |
| 85 | + "\n", |
| 86 | + "def f3(z, *params):\n", |
| 87 | + " x, y = z\n", |
| 88 | + " a, b, c, d, e, f, g, h, i, j, k, l, scale = params\n", |
| 89 | + " return (-j*np.exp(-((x-k)**2 + (y-l)**2) / scale))\n", |
| 90 | + "\n", |
| 91 | + "def f(z, *params):\n", |
| 92 | + " return f1(z, *params) + f2(z, *params) + f3(z, *params)" |
| 93 | + ] |
| 94 | + }, |
| 95 | + { |
| 96 | + "cell_type": "code", |
| 97 | + "execution_count": 8, |
| 98 | + "metadata": {}, |
| 99 | + "outputs": [], |
| 100 | + "source": [ |
| 101 | + "rranges = (slice(-4, 4, 0.25), slice(-4, 4, 0.25))" |
| 102 | + ] |
| 103 | + }, |
| 104 | + { |
| 105 | + "cell_type": "code", |
| 106 | + "execution_count": 9, |
| 107 | + "metadata": {}, |
| 108 | + "outputs": [], |
| 109 | + "source": [ |
| 110 | + "from scipy import optimize\n", |
| 111 | + "resbrute = optimize.brute(f, rranges, args=params, full_output=True,\n", |
| 112 | + " finish=optimize.fmin)" |
| 113 | + ] |
| 114 | + }, |
| 115 | + { |
| 116 | + "cell_type": "code", |
| 117 | + "execution_count": 10, |
| 118 | + "metadata": {}, |
| 119 | + "outputs": [ |
| 120 | + { |
| 121 | + "data": { |
| 122 | + "text/plain": [ |
| 123 | + "-3.4085818767996527" |
| 124 | + ] |
| 125 | + }, |
| 126 | + "execution_count": 10, |
| 127 | + "metadata": {}, |
| 128 | + "output_type": "execute_result" |
| 129 | + } |
| 130 | + ], |
| 131 | + "source": [ |
| 132 | + "resbrute[0] # global minimum\n", |
| 133 | + "resbrute[1] # function value at global minimum" |
| 134 | + ] |
| 135 | + }, |
| 136 | + { |
| 137 | + "cell_type": "markdown", |
| 138 | + "metadata": {}, |
| 139 | + "source": [ |
| 140 | + "------" |
| 141 | + ] |
| 142 | + }, |
| 143 | + { |
| 144 | + "cell_type": "markdown", |
| 145 | + "metadata": {}, |
| 146 | + "source": [ |
| 147 | + "### References" |
| 148 | + ] |
| 149 | + }, |
| 150 | + { |
| 151 | + "cell_type": "markdown", |
| 152 | + "metadata": {}, |
| 153 | + "source": [ |
| 154 | + "1. https://andreask.cs.illinois.edu/cs357-s15/public/demos/12-optimization/Steepest%20Descent.html\n", |
| 155 | + "2. https://scipy-lectures.org/advanced/mathematical_optimization/auto_examples/plot_gradient_descent.html\n", |
| 156 | + "3. https://docs.scipy.org/doc/scipy/reference/tutorial/optimize.html\n", |
| 157 | + "4. https://scipy-cookbook.readthedocs.io/index.html\n", |
| 158 | + "5. http://folk.ntnu.no/leifh/teaching/tkt4140/._main000.html" |
| 159 | + ] |
| 160 | + } |
| 161 | + ], |
| 162 | + "metadata": { |
| 163 | + "kernelspec": { |
| 164 | + "display_name": "Python 3", |
| 165 | + "language": "python", |
| 166 | + "name": "python3" |
| 167 | + }, |
| 168 | + "language_info": { |
| 169 | + "codemirror_mode": { |
| 170 | + "name": "ipython", |
| 171 | + "version": 3 |
| 172 | + }, |
| 173 | + "file_extension": ".py", |
| 174 | + "mimetype": "text/x-python", |
| 175 | + "name": "python", |
| 176 | + "nbconvert_exporter": "python", |
| 177 | + "pygments_lexer": "ipython3", |
| 178 | + "version": "3.7.3" |
| 179 | + } |
| 180 | + }, |
| 181 | + "nbformat": 4, |
| 182 | + "nbformat_minor": 2 |
| 183 | +} |
0 commit comments