From 83d087616a4b53fa373008035141b472ded4fde9 Mon Sep 17 00:00:00 2001 From: Tom van Woudenberg Date: Wed, 24 Apr 2024 19:28:43 +0200 Subject: [PATCH 1/8] Moved widget --- ...ear_constrained_optimization_example.ipynb | 24 +++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/book/pages/linear_constrained_optimization_example.ipynb b/book/pages/linear_constrained_optimization_example.ipynb index e8e0b1f..0eb97e5 100644 --- a/book/pages/linear_constrained_optimization_example.ipynb +++ b/book/pages/linear_constrained_optimization_example.ipynb @@ -44,6 +44,18 @@ "- Bounds: book are only transported from distribution centers to stores." ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Find best solution manually\n", + "\n", + ":::{card} Test yourself\n", + "Try and adjust the book transports yourself. Can you find the optimal solution?\n", + "\n", + ":::" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -195,18 +207,6 @@ "```" ] }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Find best solution manually\n", - "\n", - ":::{card} Test yourself\n", - "Try and adjust the book transports yourself. Can you find the optimal solution?\n", - "\n", - ":::" - ] - }, { "cell_type": "markdown", "metadata": {}, From 710420bc62344ba7cad075e023e08074db8986ec Mon Sep 17 00:00:00 2001 From: Tom van Woudenberg Date: Wed, 24 Apr 2024 19:56:00 +0200 Subject: [PATCH 2/8] Added maps iframe --- book/_static/maps.html | 138 ++++++++++++++++++ ...Unconstrained_optimization_example_1.ipynb | 42 +----- 2 files changed, 141 insertions(+), 39 deletions(-) create mode 100644 book/_static/maps.html diff --git a/book/_static/maps.html b/book/_static/maps.html new file mode 100644 index 0000000..520b6b4 --- /dev/null +++ b/book/_static/maps.html @@ -0,0 +1,138 @@ + + + + Profit Calculation + + + + + +
+
+ + + + diff --git a/book/pages/Unconstrained_optimization_example_1.ipynb b/book/pages/Unconstrained_optimization_example_1.ipynb index aa2e06b..f2cc9a0 100644 --- a/book/pages/Unconstrained_optimization_example_1.ipynb +++ b/book/pages/Unconstrained_optimization_example_1.ipynb @@ -98,48 +98,12 @@ "source": [ "An approach to solve this problem might be to try out some values. You can do so in the applet below. The plot below shows the negative profit for some number of bathmetry maps sold.\n", "\n", - ":::{card}\n", - "Click {fa}`rocket` --> {guilabel}`Live Code` to enable this applet. Try and adjust the values for $n$, the number of bathmetry maps sold. How small can you get the negative profit?\n", + ":::{card} Test yourself\n", + "Try and adjust the values for $n$, the number of bathmetry maps sold. How small can you get the negative profit?\n", + "\n", ":::" ] }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "tags": [ - "thebe-remove-input-init" - ] - }, - "outputs": [], - "source": [ - "from ipywidgets import widgets, interact\n", - "import numpy as np\n", - "import matplotlib.pylab as plt\n", - "%config InlineBackend.figure_formats = ['svg']\n", - "\n", - "n_range = np.linspace(0,100,100)\n", - "def func(n,truth):\n", - " fig, ax = plt.subplots(1, 1)\n", - " title = 'Negative profit for amount of bathmetry maps sold is € ' + str(round(75*n - (150 - 0.01 * n**2)*n,2))\n", - " ax.clear()\n", - " ax.set_title(title)\n", - " if truth:\n", - " ax.plot(n_range,75*n_range - (150 - 0.01 * n_range**2)*n_range)\n", - " ax.plot(n,75*n - (150 - 0.01 * n**2)*n,'o');\n", - " ax.set_xlim([0,100])\n", - " ax.set_xlabel('Number of bathmetry maps $n$ sold (-)')\n", - " ax.set_ylabel('Negative profit (€)')\n", - " ax.set_ylim([-3000,3000])\n", - " ax.spines['right'].set_color('none')\n", - " ax.spines['top'].set_color('none')\n", - " ax.spines['bottom'].set_position('zero')\n", - " ax.spines['left'].set_position('zero')\n", - " plt.show();\n", - "\n", - "interact(func, n = widgets.IntSlider(min=0, max=100, value=20, step=1, description=\"#maps n sold\"), truth = widgets.Checkbox(value=False, description='Show objective function', disabled=False));" - ] - }, { "cell_type": "markdown", "metadata": {}, From 645d066e170267dba43049db9df10c2bc2f05a9a Mon Sep 17 00:00:00 2001 From: Tom van Woudenberg Date: Wed, 24 Apr 2024 19:58:59 +0200 Subject: [PATCH 3/8] typo --- book/pages/Unconstrained_optimization_example_1.ipynb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/book/pages/Unconstrained_optimization_example_1.ipynb b/book/pages/Unconstrained_optimization_example_1.ipynb index f2cc9a0..f2c431b 100644 --- a/book/pages/Unconstrained_optimization_example_1.ipynb +++ b/book/pages/Unconstrained_optimization_example_1.ipynb @@ -96,11 +96,13 @@ "cell_type": "markdown", "metadata": {}, "source": [ + "### Find best solution manually\n", + "\n", "An approach to solve this problem might be to try out some values. You can do so in the applet below. The plot below shows the negative profit for some number of bathmetry maps sold.\n", "\n", ":::{card} Test yourself\n", "Try and adjust the values for $n$, the number of bathmetry maps sold. How small can you get the negative profit?\n", - "\n", + "\n", ":::" ] }, From 81961eb516ae1d0ba9b1895f63018d6c86c42231 Mon Sep 17 00:00:00 2001 From: Tom van Woudenberg Date: Wed, 24 Apr 2024 20:02:41 +0200 Subject: [PATCH 4/8] Update maps.html --- book/_static/maps.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/book/_static/maps.html b/book/_static/maps.html index 520b6b4..72ca5ce 100644 --- a/book/_static/maps.html +++ b/book/_static/maps.html @@ -56,7 +56,7 @@ y: [75 * n - (150 - 0.01 * Math.pow(n, 2)) * n], type: 'scatter', mode: 'markers', - name: 'Selected point', + name: 'Manual solution', marker: { size: 10 // Set the size of the marker to a larger value } From 05a150a95d671ee8fd64a035a9141522fb1111f9 Mon Sep 17 00:00:00 2001 From: Tom van Woudenberg Date: Wed, 24 Apr 2024 21:41:45 +0200 Subject: [PATCH 5/8] add another nice html element --- book/_static/curve.html | 129 ++++++++++++++++++ ...Unconstrained_optimization_example_2.ipynb | 69 +--------- 2 files changed, 133 insertions(+), 65 deletions(-) create mode 100644 book/_static/curve.html diff --git a/book/_static/curve.html b/book/_static/curve.html new file mode 100644 index 0000000..f05eaf0 --- /dev/null +++ b/book/_static/curve.html @@ -0,0 +1,129 @@ + + + + Profit Calculation + + + + + +
+
+ + + + \ No newline at end of file diff --git a/book/pages/Unconstrained_optimization_example_2.ipynb b/book/pages/Unconstrained_optimization_example_2.ipynb index d91f10f..06f9f44 100644 --- a/book/pages/Unconstrained_optimization_example_2.ipynb +++ b/book/pages/Unconstrained_optimization_example_2.ipynb @@ -106,75 +106,14 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "The plot below shows the data points and the total error squared.\n", + "### Find best solution manually\n", "\n", - ":::{card}\n", - "Click {fa}`rocket` --> {guilabel}`Live Code` to enable this applet. Try and adjust the values for $a$, $b$ and $c$. How small can you get the total error squared?\n", + ":::{card} Test yourself\n", + "Try and adjust the values for $a$, $b$ and $c$. How small can you get the total error squared?\n", + "\n", ":::" ] }, - { - "cell_type": "code", - "execution_count": 1, - "metadata": { - "tags": [ - "thebe-remove-input-init" - ] - }, - "outputs": [ - { - "data": { - "text/plain": [ - "
" - ] - }, - "metadata": {}, - "output_type": "display_data" - }, - { - "data": { - "application/vnd.jupyter.widget-view+json": { - "model_id": "5f49b3a736124147813aad4a830ab420", - "version_major": 2, - "version_minor": 0 - }, - "text/plain": [ - "interactive(children=(FloatSlider(value=2.0, description='a', max=5.0, step=0.2), FloatSlider(value=2.0, descr…" - ] - }, - "metadata": {}, - "output_type": "display_data" - } - ], - "source": [ - "from ipywidgets import widgets, interact\n", - "import numpy as np\n", - "import matplotlib.pylab as plt\n", - "\n", - "xi = np.array([0.97,0,0.5,0.85,0.7,0.19,0.41,0.78])\n", - "yi = np.array([0.97,0.06,0.7,0.74,0.2,0.34,0.29,0.94])\n", - "x_range = np.linspace(0.0001,1,100)\n", - "plt.figure()\n", - "def func(a,b,c):\n", - " fig, ax = plt.subplots(1, 1)\n", - " ax.plot(xi,yi,'x')\n", - " ax.plot(x_range,a * x_range**b + c);\n", - " ax.set_xlim([-0,1])\n", - " ax.set_ylim([-0,1])\n", - " y_est = a * xi ** b + c\n", - " error = np.sum((yi-y_est)**2)\n", - " title = 'Total error squared $=$'+str(round(error,4))\n", - " ax.set_title(title)\n", - " for i in range(8):\n", - " ax.annotate(\"\",xy=[xi[i],yi[i]],xytext=[xi[i],max(min(a * xi[i] ** b + c,1),0)],arrowprops=dict(arrowstyle= '<->'))\n", - " plt.show();\n", - "\n", - "interact(func,\n", - " a = widgets.FloatSlider(min=0, max=5, value=2, step=0.2, description=\"a\"),\n", - " b = widgets.FloatSlider(min=0, max=5, value=2, step=0.2, description=\"b\"),\n", - " c = widgets.FloatSlider(min=0, max=1, value=0.5, step=0.05, description=\"c\"));" - ] - }, { "cell_type": "markdown", "metadata": {}, From 47b39f8ad9e860b590bae62b3a669c86eaad4372 Mon Sep 17 00:00:00 2001 From: Tom van Woudenberg Date: Wed, 24 Apr 2024 21:51:51 +0200 Subject: [PATCH 6/8] Update curve.html --- book/_static/curve.html | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/book/_static/curve.html b/book/_static/curve.html index f05eaf0..0c94604 100644 --- a/book/_static/curve.html +++ b/book/_static/curve.html @@ -82,6 +82,24 @@ }; Plotly.newPlot('outputContainer', data, layout); + + // Add arrows to the plot + for (var i = 0; i < xi.length; i++) { + var arrow = { + x: [xi[i], xi[i]], + y: [yi[i], Math.max(Math.min(a * xi[i] ** b + c, 1), 0)], + mode: 'lines', + line: { + color: 'red', + width: 1, + dash: 'dash' + }, + showlegend: false + }; + data.push(arrow); + } + + Plotly.update('outputContainer', data, layout); } var sliderContainer = document.getElementById("sliderContainer"); From b99ce2807b85cc79bbd90a7da649ceaf50252d9e Mon Sep 17 00:00:00 2001 From: Tom van Woudenberg Date: Wed, 24 Apr 2024 21:52:13 +0200 Subject: [PATCH 7/8] Update Unconstrained_optimization_example_2.ipynb --- book/pages/Unconstrained_optimization_example_2.ipynb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/book/pages/Unconstrained_optimization_example_2.ipynb b/book/pages/Unconstrained_optimization_example_2.ipynb index 06f9f44..5de22ae 100644 --- a/book/pages/Unconstrained_optimization_example_2.ipynb +++ b/book/pages/Unconstrained_optimization_example_2.ipynb @@ -110,7 +110,7 @@ "\n", ":::{card} Test yourself\n", "Try and adjust the values for $a$, $b$ and $c$. How small can you get the total error squared?\n", - "\n", + "\n", ":::" ] }, From 0cd0e7fbf3a7bc506e5895030f6986fa0c9369cd Mon Sep 17 00:00:00 2001 From: Tom van Woudenberg Date: Thu, 25 Apr 2024 07:52:04 +0200 Subject: [PATCH 8/8] Update books.html --- book/_static/books.html | 3 +++ 1 file changed, 3 insertions(+) diff --git a/book/_static/books.html b/book/_static/books.html index a9648e5..5f8515a 100644 --- a/book/_static/books.html +++ b/book/_static/books.html @@ -115,6 +115,9 @@ button.innerHTML = "Evaluate"; button.onclick = eval; outputContainer.appendChild(button); + + // Call func with initial values + eval(); \ No newline at end of file