2024-11-29 03:11:00 -05:00
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Optimization\n",
"\n",
"Let's consider the following code:"
]
},
{
"cell_type": "code",
2024-12-02 04:47:50 -05:00
"execution_count": 4,
2024-11-29 03:11:00 -05:00
"metadata": {},
2024-12-02 04:47:50 -05:00
"outputs": [
{
"data": {
"text/plain": [
"Text(0, 0.5, 'y')"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAXsAAAEmCAYAAACDLjAiAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjkuMiwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8hTgPZAAAACXBIWXMAAA9hAAAPYQGoP6dpAAAmeElEQVR4nO3de1RU5f4/8PcwwE4EBkFhBhmREstL3k3xeAFNjC4njydXZqmsWl5KTaJz9Gito7lM1E6m51iUdY63TDt5KStTaCljhhf0SBKZQo6JCqFmDCINyjy/P/iyfww3GZz7fr/W2gtm72dmPg/qm8fn2bO3SgghQEREXs3H1QUQEZHjMeyJiBSAYU9EpAAMeyIiBWDYExEpAMOeiEgBGPZERArAsCciUgBfVxfgTBaLBZcuXUJQUBBUKpWryyEiumNCCJSXlyMyMhI+Pk2P3xUV9pcuXYJer3d1GUREdldUVISoqKgmj7tF2KenpyM9PR3nzp0DAPTo0QN///vfkZSUBKDmN9drr72GtWvX4tq1axg0aBDefvtt9OjRw6b3CQoKAlDzQwkODrZrH4iIXMFkMkGv18v51hS3CPuoqCgsW7YMXbp0AQBs2LABjz/+OE6cOIEePXpgxYoVWLlyJdavX4+uXbtiyZIlGD16NE6fPn3bDtZVO3UTHBzMsCcir3K7qWmVu14ILTQ0FG+88QaeffZZREZGIiUlBfPmzQMAmM1mREREYPny5Zg+fXqLX9NkMkGj0aCsrIxhT0ReoaW55nZn41RXV2Pr1q2oqKhAXFwcjEYjSkpKkJiYKLeRJAkjRoxAd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ItbhL3BYMDMmTNx+PBhZGZm4tatW0hMTERFRYVVu4ceegjFxcXytnv3bhdVTETkWdzi2jh79uyxerxu3TqEh4fj+PHjGD58uLxfkqRGL01KRETNc4uRfX1lZWUAgNDQUKv9WVlZCA8PR9euXTF16lSUlpY2+zpmsxkmk8lqIyLlio+PV8wNxutzu7AXQiA1NRVDhw61uhlwUlISNm/ejH379uHNN99ETk4ORo4cCbPZ3ORrpaWlQaPRyJter3dGF4iI3I5bTOPUNWvWLJw8eRIHDx602v/kk0/K3/fs2RMDBgxAdHQ0vvzyS4xr4uJJ8+fPR2pqqvzYZDIx8IncyIULF1BQUIDY2FhERUW5uhyv5lYj+9mzZ2PXrl3Yv3//bf/gdTodoqOj5ZsBN0aSJAQHB1ttRGRfQghUVFTYvL3zzjuIjo7GyJEjER0djXfeecfm12jNdRxv3bqFWbNmISQkBGFhYXj11Vfl16mqqsLcuXPRsWNHtG3bFoMGDWpw85Hs7GwMHz4cbdq0gV6vx4svvtjgZBK3JNyAxWIRM2fOFJGRkeLMmTMtes6VK1eEJEliw4YNLX6fsrIyAUCUlZW1tlQixausrBQ//PCDqKysFEIIcf36dQHAJdv169dtqn3EiBEiMDBQzJkzR/z444/iww8/FAEBAWLt2rVCCCEmTpwohgwZIg4cOCAKCwvFG2+8ISRJknPp5MmTIjAwULz11lvizJkz4ttvvxV9+/YVycnJ9v0h11P/Z15XS3PNLcL++eefFxqNRmRlZYni4mJ5u3HjhhBCiPLycvHyyy+L7OxsYTQaxf79+0VcXJzo2LGjMJlMLX4fhj3RnfP0sO/WrZuwWCzyvnnz5olu3bqJwsJCoVKpxMWLF62eM2rUKDF//nwhhBCTJk0S06ZNszr+zTffCB8fn0aD2F7sEfZuMWefnp4OAFY3AwBqTsFMTk6GWq1GXl4eNm7ciN9++w06nQ4JCQn4+OOP5VuAEZFrBAQE4Pr16zY95+LFi+jWrRssFou8T61W44cffkDH+ne
"text/plain": [
"<Figure size 400x300 with 1 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
2024-11-29 03:11:00 -05:00
"source": [
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"\n",
"# position of flower in 2D\n",
"flower = np.array([29.1, 19.1])\n",
"\n",
"def make_bee_track(t):\n",
" \"\"\"Find bee position at time t\"\"\"\n",
" pos0 = (13.21, 4.56)\n",
" velocity = (3.1, 0.25)\n",
" pos_x = pos0[0] + t*velocity[0]\n",
" pos_y = pos0[1] + t*velocity[1]\n",
" return np.array([pos_x,pos_y])\n",
"\n",
"t = np.linspace(0,15,10)\n",
"bee_track = make_bee_track(t)\n",
"\n",
"fig, ax = plt.subplots(nrows=1, ncols=1, figsize=(4,3))\n",
"ax.plot( [flower[0]], [flower[1]], 'or', label='flower' )\n",
"ax.plot( bee_track[0], bee_track[1], '.-k', label='bee')\n",
"ax.axis('equal')\n",
"ax.legend()\n",
"ax.set_xlabel('x')\n",
"ax.set_ylabel('y')\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In the above code, we *parameterized* the bee trajectory by the variable `t` in the function `make_bee_track()`. This means we could get a new point on the track by choosing a new value of `t`. For example:"
]
},
{
"cell_type": "code",
2024-12-02 04:47:50 -05:00
"execution_count": 5,
2024-11-29 03:11:00 -05:00
"metadata": {},
2024-12-02 04:47:50 -05:00
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[13.21 4.56]\n",
"[13.52 4.585]\n",
"[13.83 4.61]\n",
"[16.31 4.81]\n"
]
}
],
2024-11-29 03:11:00 -05:00
"source": [
"print(make_bee_track(0.0))\n",
"print(make_bee_track(0.1))\n",
"print(make_bee_track(0.2))\n",
"print(make_bee_track(1.0))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now let's measure the distance between the bee and the flower."
]
},
{
"cell_type": "code",
2024-12-02 04:47:50 -05:00
"execution_count": 8,
2024-11-29 03:11:00 -05:00
"metadata": {},
2024-12-02 04:47:50 -05:00
"outputs": [
{
"data": {
"text/plain": [
"array([21.5384238 , 17.73297556, 14.79017429, 13.29572112, 13.73095975,\n",
" 15.93858282, 19.32029244, 23.37188672, 27.80202491, 32.45606569])"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"Text(0, 0.5, 'distance')"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAXoAAAEmCAYAAABs7FscAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjkuMiwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8hTgPZAAAACXBIWXMAAA9hAAAPYQGoP6dpAAA07ElEQVR4nO3deVxU9f4/8NewDYswirIKIi5ACO7mkvuCkruVmmVqat3Siqxr2nKz5bqUS5mpaZb2Tc1rolmumILligoKoqICggoiqMMmA8x8fn+g/AJBcBzmDGdez8djHg+dOTO8OtDLw2fOvI9CCCFARESyZSF1ACIiql0seiIimWPRExHJHIueiEjmWPRERDLHoicikjkWPRGRzLHoiYhkzkrqALVNp9Ph+vXrcHR0hEKhkDoOEdFjE0IgNzcXnp6esLCo/nhd9kV//fp1eHt7Sx2DiMjg0tLS4OXlVe12si96R0dHAKU7xMnJSeI0RESPLycnB97e3mX9Vh3ZF/395RonJycWPRHJSk2Xo/lmLBGRzLHoiYhkjkVPRCRzLHoiIplj0RMRyRyLnohIInmaEqN8HRY9EZEEirU6jFp+CNM2nEJWnqZWv5bsz6MnIjJFa/5ORuKNPGTlFcHKonbHs/CInojIyK7eLsDX+y4CAGaHBqC+vU2tfj0WPRGRkX3yewLuFmvxZFNnPNuh+lk1j4tFT0RkRPsSbiAi4QasLBT4fGSQUabqsuiJiIykoKgEH28/CwCY3MMXfm41G0r2uFj0RERGsvTPS7h25y4a17fDW/1aGu3rsuiJiIwg8UYuvv8rCQAwZ1gr2NsY76RHFj0RUS0TQuDDbfEo0Qn0f8INAwLdjPr1WfRERLVsy6lrOJ58C3bWlpgzLNDoX59FT0RUi+4UFGHu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avDZHwkAgLD+fvBt5CBxIuNg0ROR2Ziz/SzUd4vRytMJU3v4Sh3HaFj0RGQW9p7NwI64dFhaKLDgmdaymDNfU+bzX0pEZiunsBgf/VY6Z/6Vns0Q1FglcSLjYtETkezN23keN3I08G3kgLf6yXPMwcOw6IlI1o4mZWPj8VQAwPxRwbC1lueYg4dh0RORbBUWazFryxkAwLjOTdC5WUOJE0mDRU9EsvXVvotIyS6Am5MSs0Llc2nAR8WiJyJZir+mxuq/kgAAn48IhpOtfC4N+KhY9EQkO8VaHWb+egZancDg1h4YECjfyZQ1waInItlZ/VcSEtJzoLKzxpyhraSOIzkWPRHJStLNPHy17yIA4KMhgXBxVEqcSHoseiKSDZ1OYFZ4HIpKdOjRshGead9Y6kgmgUVPRLKxMToVx5Nvwc7aEnNHBkOhkP9kyppg0RORLGSoCzF/53kAwL8H+sPb2V7iRKaDRU9EdZ4QAh9ui0eupgRtvetjQremUkcyKSx6IqrzdsSlY9+5G7C2VOCLZ1vD0gwuJvIoWPREVKfdzi/CnO1nAQCv924BPzdHiROZHhY9EdVpn+84h6y8IrR0rYfX+zSXOo5JYtETUZ11MPEmtpy6CoUCmP9MayitzG8yZU2w6ImoTsrXlGB2eBwAYELXpujg00DiRKaLRU9EddLCvRdw7c5dNK5vh38P9Jc6jkl7rKIvKirChQsXUFJSotfz582bh06dOsHR0RGurq4YMWIELly4UG6biRMnQqFQlLt16dLlcWITUR13KvU21h5OAQDMHRUMB6WVtIFMnF5FX1BQgMmTJ8Pe3h6tWrVCamrp1VvefPNNzJ8/v8avExUVhWnTpuHo0aOIiIhASUkJQkJCkJ+fX267QYMGIT09vey2c+dOfWITkQwUlegwa8sZCAGMatcYvfxcpI5k8vQq+tmzZ+P06dOIjIyEra1t2f39+/fHpk2bavw6u3f
"text/plain": [
"<Figure size 400x300 with 1 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
2024-11-29 03:11:00 -05:00
"source": [
"def compute_distance(a,b):\n",
" a = np.array(a)\n",
" b = np.array(b)\n",
" return np.sqrt(np.sum((a-b)**2))\n",
"\n",
"n_time_steps = bee_track.shape[1]\n",
"distance = np.zeros(n_time_steps)\n",
"for i in range(n_time_steps):\n",
" bee_pos = bee_track[:,i]\n",
" distance[i] = compute_distance(bee_pos, flower)\n",
"display(distance)\n",
"\n",
"fig, ax = plt.subplots(nrows=1, ncols=1, figsize=(4,3))\n",
"ax.plot( t, distance )\n",
"ax.set_xlabel('t')\n",
"ax.set_ylabel('distance')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Given the plot of distance versus t above, we can see the distance is minimized when t is near 5. What is the bee's position when t is 5?"
]
},
{
"cell_type": "code",
2024-12-02 04:47:50 -05:00
"execution_count": 9,
2024-11-29 03:11:00 -05:00
"metadata": {},
2024-12-02 04:47:50 -05:00
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[28.71 5.81]\n"
]
}
],
2024-11-29 03:11:00 -05:00
"source": [
"print(make_bee_track(5))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can check back to the xy plot to see, indeed, this point is pretty close to the flower.\n",
"\n",
"What if we want to know, however, exactly the closest point? There are several ways to find this. Here we are going to use a \"brute force\" approach which will work on many different problems. Specifically, we will use [scipy.optimize.minimize_scalar](https://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.minimize_scalar.html). The overall idea of this kind of *numerical optimization* is that we find the best fitting parameter to minimize our \"error\".\n",
"\n",
"In this example, we are not so much concerned with the exact algorithm being used, but with the way we call this algorithm.\n",
"\n",
"Let's create a function which uses the `flower` location to compute distance:"
]
},
{
"cell_type": "code",
2024-12-02 04:47:50 -05:00
"execution_count": 10,
2024-11-29 03:11:00 -05:00
"metadata": {},
"outputs": [],
"source": [
"def calc_distance_func(t):\n",
" # assume variable 'flower' in our global scope\n",
" # calculate bee position (also assuming 'make_bee_track' in scope)\n",
2024-12-02 04:47:50 -05:00
" dist = compute_distance(flower, make_bee_track(t))\n",
2024-11-29 03:11:00 -05:00
" print(f't: {t} -> dist: {dist}')\n",
" return dist"
]
},
{
"cell_type": "code",
2024-12-02 04:47:50 -05:00
"execution_count": 11,
2024-11-29 03:11:00 -05:00
"metadata": {},
2024-12-02 04:47:50 -05:00
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"t: 0 -> dist: 21.538423804912004\n"
]
},
{
"data": {
"text/plain": [
"21.538423804912004"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
2024-11-29 03:11:00 -05:00
"source": [
"calc_distance_func(0)"
]
},
{
"cell_type": "code",
2024-12-02 04:47:50 -05:00
"execution_count": 12,
2024-11-29 03:11:00 -05:00
"metadata": {},
2024-12-02 04:47:50 -05:00
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"t: 5 -> dist: 13.295721116208782\n"
]
},
{
"data": {
"text/plain": [
"13.295721116208782"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
2024-11-29 03:11:00 -05:00
"source": [
"calc_distance_func(5)"
]
},
{
"cell_type": "code",
2024-12-02 04:47:50 -05:00
"execution_count": 15,
2024-11-29 03:11:00 -05:00
"metadata": {},
2024-12-02 04:47:50 -05:00
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"t: 0.0 -> dist: 21.538423804912004\n",
"t: 1.0 -> dist: 19.17780487959975\n",
"t: 2.6180339999999998 -> dist: 15.913623755962107\n",
"t: 9.502603885705089 -> dist: 18.222622377854467\n",
"t: 5.24770562096323 -> dist: 13.233470063982764\n",
"t: 6.8729320915536185 -> dist: 13.918754747176704\n",
"t: 5.437736318938615 -> dist: 13.215989304828\n",
"t: 5.47123644934229 -> dist: 13.215645882055249\n",
"t: 5.468503780784455 -> dist: 13.215643128079853\n",
"t: 5.468493134264144 -> dist: 13.2156431280385\n",
"t: 5.468493215207842 -> dist: 13.215643128038504\n",
"t: 5.468493053320446 -> dist: 13.215643128038506\n"
]
}
],
2024-11-29 03:11:00 -05:00
"source": [
"import scipy.optimize\n",
"result = scipy.optimize.minimize_scalar(calc_distance_func)"
]
},
{
"cell_type": "code",
2024-12-02 04:47:50 -05:00
"execution_count": 17,
2024-11-29 03:11:00 -05:00
"metadata": {},
2024-12-02 04:47:50 -05:00
"outputs": [
{
"data": {
"text/plain": [
" message: \n",
" Optimization terminated successfully;\n",
" The returned value satisfies the termination criteria\n",
" (using xtol = 1.48e-08 )\n",
" success: True\n",
" fun: 13.2156431280385\n",
" x: 5.468493134264144\n",
" nit: 8\n",
" nfev: 12"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"result"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"\u001b[0;31mSignature:\u001b[0m\n",
"\u001b[0mscipy\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0moptimize\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mminimize_scalar\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\u001b[0m\n",
"\u001b[0;34m\u001b[0m \u001b[0mfun\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n",
"\u001b[0;34m\u001b[0m \u001b[0mbracket\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mNone\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n",
"\u001b[0;34m\u001b[0m \u001b[0mbounds\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mNone\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n",
"\u001b[0;34m\u001b[0m \u001b[0margs\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n",
"\u001b[0;34m\u001b[0m \u001b[0mmethod\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mNone\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n",
"\u001b[0;34m\u001b[0m \u001b[0mtol\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mNone\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n",
"\u001b[0;34m\u001b[0m \u001b[0moptions\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mNone\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n",
"\u001b[0;34m\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;31mDocstring:\u001b[0m\n",
"Local minimization of scalar function of one variable.\n",
"\n",
"Parameters\n",
"----------\n",
"fun : callable\n",
" Objective function.\n",
" Scalar function, must return a scalar.\n",
"bracket : sequence, optional\n",
" For methods 'brent' and 'golden', `bracket` defines the bracketing\n",
" interval and is required.\n",
" Either a triple ``(xa, xb, xc)`` satisfying ``xa < xb < xc`` and\n",
" ``func(xb) < func(xa) and func(xb) < func(xc)``, or a pair\n",
" ``(xa, xb)`` to be used as initial points for a downhill bracket search\n",
" (see `scipy.optimize.bracket`).\n",
" The minimizer ``res.x`` will not necessarily satisfy\n",
" ``xa <= res.x <= xb``.\n",
"bounds : sequence, optional\n",
" For method 'bounded', `bounds` is mandatory and must have two finite\n",
" items corresponding to the optimization bounds.\n",
"args : tuple, optional\n",
" Extra arguments passed to the objective function.\n",
"method : str or callable, optional\n",
" Type of solver. Should be one of:\n",
"\n",
" - :ref:`Brent <optimize.minimize_scalar-brent>`\n",
" - :ref:`Bounded <optimize.minimize_scalar-bounded>`\n",
" - :ref:`Golden <optimize.minimize_scalar-golden>`\n",
" - custom - a callable object (added in version 0.14.0), see below\n",
"\n",
" Default is \"Bounded\" if bounds are provided and \"Brent\" otherwise.\n",
" See the 'Notes' section for details of each solver.\n",
"\n",
"tol : float, optional\n",
" Tolerance for termination. For detailed control, use solver-specific\n",
" options.\n",
"options : dict, optional\n",
" A dictionary of solver options.\n",
"\n",
" maxiter : int\n",
" Maximum number of iterations to perform.\n",
" disp : bool\n",
" Set to True to print convergence messages.\n",
"\n",
" See :func:`show_options()` for solver-specific options.\n",
"\n",
"Returns\n",
"-------\n",
"res : OptimizeResult\n",
" The optimization result represented as a ``OptimizeResult`` object.\n",
" Important attributes are: ``x`` the solution array, ``success`` a\n",
" Boolean flag indicating if the optimizer exited successfully and\n",
" ``message`` which describes the cause of the termination. See\n",
" `OptimizeResult` for a description of other attributes.\n",
"\n",
"See also\n",
"--------\n",
"minimize : Interface to minimization algorithms for scalar multivariate\n",
" functions\n",
"show_options : Additional options accepted by the solvers\n",
"\n",
"Notes\n",
"-----\n",
"This section describes the available solvers that can be selected by the\n",
"'method' parameter. The default method is the ``\"Bounded\"`` Brent method if\n",
"`bounds` are passed and unbounded ``\"Brent\"`` otherwise.\n",
"\n",
"Method :ref:`Brent <optimize.minimize_scalar-brent>` uses Brent's\n",
"algorithm [1]_ to find a local minimum. The algorithm uses inverse\n",
"parabolic interpolation when possible to speed up convergence of\n",
"the golden section method.\n",
"\n",
"Method :ref:`Golden <optimize.minimize_scalar-golden>` uses the\n",
"golden section search technique [1]_. It uses analog of the bisection\n",
"method to decrease the bracketed interval. It is usually\n",
"preferable to use the *Brent* method.\n",
"\n",
"Method :ref:`Bounded <optimize.minimize_scalar-bounded>` can\n",
"perform bounded minimization [2]_ [3]_. It uses the Brent method to find a\n",
"local minimum in the interval x1 < xopt < x2.\n",
"\n",
"Note that the Brent and Golden methods do not guarantee success unless a\n",
"valid ``bracket`` triple is provided. If a three-point bracket cannot be\n",
"found, consider `scipy.optimize.minimize`. Also, all methods are intended\n",
"only for local minimization. When the function of interest has more than\n",
"one local minimum, consider :ref:`global_optimization`.\n",
"\n",
"**Custom minimizers**\n",
"\n",
"It may be useful to pass a custom minimization method, for example\n",
"when using some library frontend to minimize_scalar. You can simply\n",
"pass a callable as the ``method`` parameter.\n",
"\n",
"The callable is called as ``method(fun, args, **kwargs, **options)``\n",
"where ``kwargs`` corresponds to any other parameters passed to `minimize`\n",
"(such as `bracket`, `tol`, etc.), except the `options` dict, which has\n",
"its contents also passed as `method` parameters pair by pair. The method\n",
"shall return an `OptimizeResult` object.\n",
"\n",
"The provided `method` callable must be able to accept (and possibly ignore)\n",
"arbitrary parameters; the set of parameters accepted by `minimize` may\n",
"expand in future versions and then these parameters will be passed to\n",
"the method. You can find an example in the scipy.optimize tutorial.\n",
"\n",
".. versionadded:: 0.11.0\n",
"\n",
"References\n",
"----------\n",
".. [1] Press, W., S.A. Teukolsky, W.T. Vetterling, and B.P. Flannery.\n",
" Numerical Recipes in C. Cambridge University Press.\n",
".. [2] Forsythe, G.E., M. A. Malcolm, and C. B. Moler. \"Computer Methods\n",
" for Mathematical Computations.\" Prentice-Hall Series in Automatic\n",
" Computation 259 (1977).\n",
".. [3] Brent, Richard P. Algorithms for Minimization Without Derivatives.\n",
" Courier Corporation, 2013.\n",
"\n",
"Examples\n",
"--------\n",
"Consider the problem of minimizing the following function.\n",
"\n",
">>> def f(x):\n",
"... return (x - 2) * x * (x + 2)**2\n",
"\n",
"Using the *Brent* method, we find the local minimum as:\n",
"\n",
">>> from scipy.optimize import minimize_scalar\n",
">>> res = minimize_scalar(f)\n",
">>> res.fun\n",
"-9.9149495908\n",
"\n",
"The minimizer is:\n",
"\n",
">>> res.x\n",
"1.28077640403\n",
"\n",
"Using the *Bounded* method, we find a local minimum with specified\n",
"bounds as:\n",
"\n",
">>> res = minimize_scalar(f, bounds=(-3, -1), method='bounded')\n",
">>> res.fun # minimum\n",
"3.28365179850e-13\n",
">>> res.x # minimizer\n",
"-2.0000002026\n",
"\u001b[0;31mFile:\u001b[0m ~/anaconda3/envs/pm21-dragon/lib/python3.11/site-packages/scipy/optimize/_minimize.py\n",
"\u001b[0;31mType:\u001b[0m function"
]
},
"metadata": {},
"output_type": "display_data"
}
],
2024-11-29 03:11:00 -05:00
"source": [
"scipy.optimize.minimize_scalar?"
]
},
{
"cell_type": "code",
2024-12-02 04:47:50 -05:00
"execution_count": 19,
2024-11-29 03:11:00 -05:00
"metadata": {},
2024-12-02 04:47:50 -05:00
"outputs": [
{
"data": {
"text/plain": [
" message: \n",
" Optimization terminated successfully;\n",
" The returned value satisfies the termination criteria\n",
" (using xtol = 1.48e-08 )\n",
" success: True\n",
" fun: 13.2156431280385\n",
" x: 5.468493134264144\n",
" nit: 8\n",
" nfev: 12"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
2024-11-29 03:11:00 -05:00
"source": [
"result"
]
},
{
"cell_type": "code",
2024-12-02 04:47:50 -05:00
"execution_count": 20,
2024-11-29 03:11:00 -05:00
"metadata": {},
2024-12-02 04:47:50 -05:00
"outputs": [
{
"data": {
"text/plain": [
"scipy.optimize._optimize.OptimizeResult"
]
},
"execution_count": 20,
"metadata": {},
"output_type": "execute_result"
}
],
2024-11-29 03:11:00 -05:00
"source": [
"type(result)"
]
},
{
"cell_type": "code",
2024-12-02 04:47:50 -05:00
"execution_count": 21,
2024-11-29 03:11:00 -05:00
"metadata": {},
2024-12-02 04:47:50 -05:00
"outputs": [
{
"data": {
"text/plain": [
"5.468493134264144"
]
},
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
],
2024-11-29 03:11:00 -05:00
"source": [
"result.x"
]
},
{
"cell_type": "code",
2024-12-02 04:47:50 -05:00
"execution_count": 22,
2024-11-29 03:11:00 -05:00
"metadata": {},
2024-12-02 04:47:50 -05:00
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"t: 5 -> dist: 13.295721116208782\n",
"13.295721116208782\n"
]
}
],
2024-11-29 03:11:00 -05:00
"source": [
"print(calc_distance_func(5))"
]
},
{
"cell_type": "code",
2024-12-02 04:47:50 -05:00
"execution_count": 23,
2024-11-29 03:11:00 -05:00
"metadata": {},
2024-12-02 04:47:50 -05:00
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"t: 5.468493134264144 -> dist: 13.2156431280385\n"
]
},
{
"data": {
"text/plain": [
"13.2156431280385"
]
},
"execution_count": 23,
"metadata": {},
"output_type": "execute_result"
}
],
2024-11-29 03:11:00 -05:00
"source": [
"calc_distance_func(5.468493134264144)"
]
},
{
"cell_type": "code",
2024-12-02 04:47:50 -05:00
"execution_count": 24,
2024-11-29 03:11:00 -05:00
"metadata": {},
2024-12-02 04:47:50 -05:00
"outputs": [
{
"data": {
"text/plain": [
"array([30.16232872, 5.92712328])"
]
},
"execution_count": 24,
"metadata": {},
"output_type": "execute_result"
}
],
2024-11-29 03:11:00 -05:00
"source": [
"# Where is the bee for this value of `t`?\n",
"make_bee_track(5.468493134264144)"
]
},
{
"cell_type": "code",
2024-12-02 04:47:50 -05:00
"execution_count": 25,
2024-11-29 03:11:00 -05:00
"metadata": {},
2024-12-02 04:47:50 -05:00
"outputs": [
{
"data": {
"text/plain": [
"Text(0, 0.5, 'y')"
]
},
"execution_count": 25,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAXsAAAEmCAYAAACDLjAiAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjkuMiwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8hTgPZAAAACXBIWXMAAA9hAAAPYQGoP6dpAAAr6ElEQVR4nO3de1xUdf4/8NcwwMh1FARmEASLwbzjLcUrampUu5qb38pdlUc9vGziJfp+dbUeX9E1SfumVhab7vfrpTTdvC21ppAJWqShK0loCjkaKCxqxgDSoDOf3x8s58fIRcAZ5nJez8fjPGTO+cyc90fr5cfP58w5CiGEABERuTQ3exdARES2x7AnIpIBhj0RkQww7ImIZIBhT0QkAwx7IiIZYNgTEckAw56ISAbc7V1AezKbzbh27Rr8/PygUCjsXQ4R0QMTQqCiogKhoaFwc2t6/C6rsL927RrCw8PtXQYRkdUVFRUhLCysyeMOEfapqalITU3F5cuXAQC9evXCf//3fyM+Ph5A7d9cK1aswKZNm3Dr1i0MGTIE7733Hnr16tWq8/j5+QGo/U3x9/e3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XRHZ2ttDr9eLo0aMiNjZWdOnSRRgMhhafh2FP1DrOHPY9evQQZrNZ2rdkyRLRo0cPUVhYKBQKhbh69arFe8aNGyeWLl0qhBBi+vTpYvbs2RbHjx8/Ltzc3BoNXFuzRtg7xJx9amoqAFg8JACovQQzISEBSqUSeXl52L59O3755RdotVqMGTMGu3fvlh4NRkS25+3tjcrKyla95+rVq+jRowfMZrO0T6lU4ty5c+hy7xO
"text/plain": [
"<Figure size 400x300 with 1 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
2024-11-29 03:11:00 -05:00
"source": [
"fig, ax = plt.subplots(nrows=1, ncols=1, figsize=(4,3))\n",
"ax.plot( [flower[0]], [flower[1]], 'or', label='flower' )\n",
"ax.plot( bee_track[0], bee_track[1], '.-k', label='bee')\n",
"\n",
"x,y = make_bee_track(5.468493134264144)\n",
"ax.plot( [x], [y] ,'x', label='closest')\n",
"ax.axis('equal')\n",
"ax.legend()\n",
"ax.set_xlabel('x')\n",
"ax.set_ylabel('y')\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"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.11.10"
}
},
"nbformat": 4,
"nbformat_minor": 4
}