{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "editable": false, "nbgrader": { "cell_type": "code", "checksum": "29874499fd2a20779e162626f76f6776", "grade": false, "grade_id": "cell-e894ec2378fa46eb", "locked": true, "schema_version": 3, "solution": false, "task": false } }, "outputs": [], "source": [ "# You must run this cell, but you can ignore its contents.\n", "\n", "import hashlib\n", "\n", "def ads_hash(ty):\n", " \"\"\"Return a unique string for input\"\"\"\n", " ty_str = str(ty).encode()\n", " m = hashlib.sha256()\n", " m.update(ty_str)\n", " return m.hexdigest()[:10]" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "editable": false, "nbgrader": { "cell_type": "code", "checksum": "6f1fee68b047ccaf945f9998a29d8651", "grade": false, "grade_id": "cell-ada87b3b188b2d39", "locked": true, "schema_version": 3, "solution": false, "task": false } }, "outputs": [], "source": [ "# You must also run this cell.\n", "\n", "import numpy as np\n", "import matplotlib.pyplot as plt" ] }, { "cell_type": "markdown", "metadata": { "deletable": false, "editable": false, "nbgrader": { "cell_type": "markdown", "checksum": "bd84ae4c048d756b33d0fd85d55f1aec", "grade": false, "grade_id": "cell-d7462f8b0f718bef", "locked": true, "schema_version": 3, "solution": false, "task": false } }, "source": [ "# Exercise - Optimization first steps\n", "\n", "We are going to take our first steps towards optimization by returning to a bumblebee example.\n", "\n", "We are going to define the positions of a flower and the flight path of a bumblebee." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "editable": false, "nbgrader": { "cell_type": "code", "checksum": "13882d6eb738408b7520368a4b5e8ff5", "grade": false, "grade_id": "cell-78cb75cddd4fc5ef", "locked": true, "schema_version": 3, "solution": false, "task": false } }, "outputs": [], "source": [ "flower = np.array([7.5, 10.3])" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "editable": false, "nbgrader": { "cell_type": "code", "checksum": "5f73a413f4a03726e882c755a3430c17", "grade": false, "grade_id": "cell-ab9736432799913b", "locked": true, "schema_version": 3, "solution": false, "task": false } }, "outputs": [], "source": [ "def make_bee_track(t):\n", " pos0 = (-10,3)\n", " velocity = (4.0, 0.2)\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,20)\n", "bee_track = make_bee_track(t)" ] }, { "cell_type": "markdown", "metadata": { "deletable": false, "editable": false, "nbgrader": { "cell_type": "markdown", "checksum": "6aa761a551cb0142afbfe7e7d81947ed", "grade": false, "grade_id": "cell-08928dcd585933a7", "locked": true, "schema_version": 3, "solution": false, "task": false } }, "source": [ "Here we plot these positions." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "editable": false, "nbgrader": { "cell_type": "code", "checksum": "c8e52b43cbe861d8eddacccc87aeb1e2", "grade": false, "grade_id": "cell-4b58d56276437c3a", "locked": true, "schema_version": 3, "solution": false, "task": false } }, "outputs": [], "source": [ "fig, ax = plt.subplots(nrows=1, ncols=1)\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();" ] }, { "cell_type": "markdown", "metadata": { "deletable": false, "editable": false, "nbgrader": { "cell_type": "markdown", "checksum": "0f3a53f8323083b2effbac78689c09d7", "grade": false, "grade_id": "cell-701acd6a5b4a9581", "locked": true, "schema_version": 3, "solution": false, "task": false } }, "source": [ "## Q1 Draw a figure as above with, additionally, a blue line between each point on `bee_track` and `flower`.\n", "\n", "When complete, your figure should look like this:\n", "\n", "\n", "\n", "\n", "Hint, draw the flower first. Then, make a for loop which steps through each position of the bee. Inside the for loop, draw a line segment between the bee and the flower." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "nbgrader": { "cell_type": "code", "checksum": "38fbad4ec96d8ade393535b625140003", "grade": true, "grade_id": "cell-43f8774b1a82b15b", "locked": false, "points": 1, "schema_version": 3, "solution": true, "task": false } }, "outputs": [], "source": [ "# YOUR CODE HERE\n", "raise NotImplementedError()" ] }, { "cell_type": "markdown", "metadata": { "deletable": false, "editable": false, "nbgrader": { "cell_type": "markdown", "checksum": "89805e9c9678ff01669528f20a596bf6", "grade": false, "grade_id": "cell-45a057824a59090e", "locked": true, "schema_version": 3, "solution": false, "task": false } }, "source": [ "## Q2 write a function called `my_distance` which takes two arguments, each of which is a length 2 sequence of `x`, `y` position and returns the Euclidean distance between these points." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "nbgrader": { "cell_type": "code", "checksum": "9bd3bd0fb3a886f4696138c95e0058ab", "grade": false, "grade_id": "cell-9d86e35ff582bf8f", "locked": false, "schema_version": 3, "solution": true, "task": false } }, "outputs": [], "source": [ "# YOUR CODE HERE\n", "raise NotImplementedError()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "editable": false, "nbgrader": { "cell_type": "code", "checksum": "b359ea8e38a47e5320e08ce6158dc39c", "grade": true, "grade_id": "cell-d96cb3c1a49758db", "locked": true, "points": 0, "schema_version": 3, "solution": false, "task": false } }, "outputs": [], "source": [ "assert my_distance((0,0),(3,4)) == 5\n", "assert my_distance((3,4), (0,0)) == 5\n", "assert my_distance((13,14), (10,10)) == 5\n", "assert my_distance((10,10), (13,14)) == 5" ] }, { "cell_type": "markdown", "metadata": { "deletable": false, "editable": false, "nbgrader": { "cell_type": "markdown", "checksum": "5220e92a49ceb61457be556c246b28ce", "grade": false, "grade_id": "cell-9426c238469156ae", "locked": true, "schema_version": 3, "solution": false, "task": false } }, "source": [ "## Q3 compute the distance between each point on `bee_track` and `flower`. Put the results in a 1D numpy array called `distance`.\n", "\n", "Hint: recall the function you wrote in the \"numpy basics\" exercise called `compute_distance`." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "nbgrader": { "cell_type": "code", "checksum": "922ea2de206db155b31c1b6020e50c27", "grade": false, "grade_id": "cell-bf78a3429e48b28d", "locked": false, "schema_version": 3, "solution": true, "task": false } }, "outputs": [], "source": [ "# YOUR CODE HERE\n", "raise NotImplementedError()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "editable": false, "nbgrader": { "cell_type": "code", "checksum": "0304ff76ac1ade2c3221a53c5df5fbb2", "grade": true, "grade_id": "cell-70fd1e669decb646", "locked": true, "points": 1, "schema_version": 3, "solution": false, "task": false } }, "outputs": [], "source": [ "# If this runs without error, it means the answer in your previous cell was correct.\n", "assert ads_hash(np.round(distance*1000).astype(np.int32))=='54f4f2edcb'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Q4 make a plot of the bee track parameter `t` on the X axis and `distance` on the Y axis.\n", "\n", "It should look like this:\n", "\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "nbgrader": { "cell_type": "code", "checksum": "c03c9f9064ad82267ed7ad1ff2274116", "grade": true, "grade_id": "cell-acdc61a7b1539944", "locked": false, "points": 1, "schema_version": 3, "solution": true, "task": false } }, "outputs": [], "source": [ "# YOUR CODE HERE\n", "raise NotImplementedError()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Q5 Using `calc_distance_func` from the lecture, find the value of `t` that minimizes the distance between the bee and the flower. Save the result in `best_t`." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "nbgrader": { "cell_type": "code", "checksum": "ea188c9450758aa26fecfdc16042a9f2", "grade": false, "grade_id": "cell-23d19427610d2072", "locked": false, "schema_version": 3, "solution": true, "task": false } }, "outputs": [], "source": [ "# YOUR CODE HERE\n", "raise NotImplementedError()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "editable": false, "nbgrader": { "cell_type": "code", "checksum": "7a33eaaaaca8781f883635cee037a71d", "grade": true, "grade_id": "cell-0a3594afd2952a33", "locked": true, "points": 1, "schema_version": 3, "solution": false, "task": false } }, "outputs": [], "source": [ "# If this runs without error, it means the answer in your previous cell was correct.\n", "assert ads_hash(np.round(best_t*1000).astype(np.int32))=='dec1ab2f6d'" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "make_bee_track(best_t)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Q6 What is the position of the bee when it is closest to the flower? Save the result as a numpy array in `best_pos`." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "nbgrader": { "cell_type": "code", "checksum": "20073a563548884f84b927f1c1f154eb", "grade": false, "grade_id": "cell-cac598c7f739d146", "locked": false, "schema_version": 3, "solution": true, "task": false } }, "outputs": [], "source": [ "# YOUR CODE HERE\n", "raise NotImplementedError()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "deletable": false, "editable": false, "nbgrader": { "cell_type": "code", "checksum": "4e73e4a1310b052d0bd683f8e5b40fd3", "grade": true, "grade_id": "cell-9d882991838f0996", "locked": true, "points": 1, "schema_version": 3, "solution": false, "task": false } }, "outputs": [], "source": [ "# If this runs without error, it means the answer in your previous cell was correct.\n", "assert type(best_pos)==np.ndarray\n", "assert best_pos.ndim==1\n", "assert best_pos.shape==(2,)\n", "assert ads_hash(np.round(best_pos[0]*1000).astype(np.int32))=='e33b9415bc'\n", "assert ads_hash(np.round(best_pos[1]*1000).astype(np.int32))=='f71cbfce4c'" ] } ], "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.7" } }, "nbformat": 4, "nbformat_minor": 4 }