diff --git a/algorithm/interactive.ipynb b/algorithm/interactive.ipynb index 219a184..df167f7 100644 --- a/algorithm/interactive.ipynb +++ b/algorithm/interactive.ipynb @@ -110,7 +110,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.13" + "version": "3.9.13" } }, "nbformat": 4, diff --git a/algorithm/lecture.ipynb b/algorithm/lecture.ipynb index ca2c0ce..bf8de0c 100644 --- a/algorithm/lecture.ipynb +++ b/algorithm/lecture.ipynb @@ -2,37 +2,154 @@ "cells": [ { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "id": "011c5f34-be40-4f0b-8146-73f66ba18672", "metadata": {}, "outputs": [], "source": [ "from optimization.common import *\n", - "from optimization.gd_new import*" + "from optimization.gradient import *" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "id": "302d9c60", "metadata": { "slideshow": { "slide_type": "fragment" } }, - "outputs": [], + "outputs": [ + { + "data": { + "application/vnd.jupyter.widget-view+json": { + "model_id": "047719f4c6674039838c1b45b32bb5b4", + "version_major": 2, + "version_minor": 0 + }, + "text/plain": [ + "VBox(children=(HBox(children=(Text(value='(x - 2)**2 + 3', description='Expression:', style=TextStyle(descript…" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "application/vnd.jupyter.widget-view+json": { + "model_id": "f6602e1e61e04f58bbcfa2f4325a814b", + "version_major": 2, + "version_minor": 0 + }, + "text/plain": [ + "Output()" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "application/vnd.jupyter.widget-view+json": { + "model_id": "564ae8a816f64bbd90c3b6b961b18706", + "version_major": 2, + "version_minor": 0 + }, + "text/plain": [ + "Output()" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/plain": [ + "" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "funcPlot1d(environ=\"jupyterlab\")" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "id": "a4827f0c", "metadata": {}, + "outputs": [ + { + "data": { + "application/vnd.jupyter.widget-view+json": { + "model_id": "f3047bfdf60a41e69222e33b1fe216ef", + "version_major": 2, + "version_minor": 0 + }, + "text/plain": [ + "VBox(children=(HBox(children=(VBox(children=(Text(value='sin(x) + sin((10.0 / 3.0) * x)', description='Express…" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "application/vnd.jupyter.widget-view+json": { + "model_id": "014093558f9141699a4ea4dfe285afeb", + "version_major": 2, + "version_minor": 0 + }, + "text/plain": [ + "Output()" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "application/vnd.jupyter.widget-view+json": { + "model_id": "0c7fc5eac02345af8210d61c64ec1d0b", + "version_major": 2, + "version_minor": 0 + }, + "text/plain": [ + "Output()" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "a = gd_1d(environ=\"jupyterlab\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "79f3577e-1e5a-46d8-a4d7-c1a45dcaaa07", + "metadata": {}, "outputs": [], "source": [ - "a = gd_1d(environ=\"jupyterlab\")" + "xrange = np.linspace(np.array(a.xn_list)-2, np.array(a.xn_list)+2, 10)\n", + "xrange[0]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "47c012d9-2544-4f2e-8d1d-aec2ae03551a", + "metadata": {}, + "outputs": [], + "source": [ + "np.array(a.xn_list)" ] }, { @@ -60,21 +177,7 @@ }, "outputs": [], "source": [ - "gd_2d(environ=\"jupyterlab\")" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "3c737f58-0d7a-4e55-b50e-ec1e538c3822", - "metadata": {}, - "outputs": [], - "source": [ - "expr = \"(1 - 8 * x1 + 7 * x1**2 - (7/3) * x1**3 + (1/4) * x1**4) * x2**2 * E**(-x2)\"\n", - "xn = np.array([0, 2])\n", - "x1 = symbols('x1')\n", - "x2 = symbols('x2')\n", - "expr = sympify(expr)" + "gd2d(environ=\"jupyterlab\")" ] }, { @@ -90,53 +193,10 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": null, "id": "d5651fc9-3fcd-4e91-8d3c-04897da1ea02", "metadata": {}, - "outputs": [ - { - "data": { - "application/vnd.jupyter.widget-view+json": { - "model_id": "21388df7b7514d58a0e413beff1985ce", - "version_major": 2, - "version_minor": 0 - }, - "text/plain": [ - "VBox(children=(HBox(children=(Dropdown(options=(('(1 - 8 * x1 + 7 * x1**2 - (7/3) * x1**3 + (1/4) * x1**4) * x…" - ] - }, - "metadata": {}, - "output_type": "display_data" - }, - { - "data": { - "application/vnd.jupyter.widget-view+json": { - "model_id": "83d82890dadb4e2eafc36993ff5acbe8", - "version_major": 2, - "version_minor": 0 - }, - "text/plain": [ - "Output()" - ] - }, - "metadata": {}, - "output_type": "display_data" - }, - { - "data": { - "application/vnd.jupyter.widget-view+json": { - "model_id": "e7df9fb42125432a884d262cf6a8e5b3", - "version_major": 2, - "version_minor": 0 - }, - "text/plain": [ - "Output()" - ] - }, - "metadata": {}, - "output_type": "display_data" - } - ], + "outputs": [], "source": [ "from optimization.gradient import *\n", "a = gd2d_compete(environ=\"jupyterlab\")" @@ -148,6 +208,53 @@ "id": "3ce3b0fa-5813-49dd-90de-b28c5d3faf46", "metadata": {}, "outputs": [], + "source": [ + "expr = a.wg_expr.value" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "93a54c5c-02f4-47ed-90cb-ef348d1333be", + "metadata": {}, + "outputs": [], + "source": [ + "x = symbols(\"x\")\n", + "expr = sympify(a.wg_expr.value)\n", + "f = lambdify(x, sympify(expr), \"numpy\")\n", + "f_xn = f(np.array(a.xn_list))\n", + "\n", + "xrange = np.linspace(np.array(a.xn_list)[0]-1, np.array(a.xn_list)[0]+1, 10)\n", + "tangent_line = a.df_list[0] * (x - np.array(a.xn_list[0])) + f_xn[0]\n", + "lambdify(x, tangent_line)(xrange)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "26e9ef27-5dcb-4bb3-8c30-301bc7303dcd", + "metadata": {}, + "outputs": [], + "source": [ + "a.df_list[0] * (x - np.array(a.xn_list[0])) + f_xn[0]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "40ad9639-89fd-4296-8d5c-08f7476546d7", + "metadata": {}, + "outputs": [], + "source": [ + "a.df_list[0]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "00387a2e-f4e1-431d-8b7f-e2b4b75679ab", + "metadata": {}, + "outputs": [], "source": [] } ], @@ -168,7 +275,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.13" + "version": "3.9.13" } }, "nbformat": 4, diff --git a/algorithm/optimization/gradient.py b/algorithm/optimization/gradient.py index edc2e55..4c442a3 100644 --- a/algorithm/optimization/gradient.py +++ b/algorithm/optimization/gradient.py @@ -78,7 +78,6 @@ class gd_1d(object): f = lambdify(x, expr) df = lambdify(x, diff(expr, x)) self.xn_list, self.df_list = [], [] - for n in tqdm(range(0, self.wg_max_iter.value)): gradient = df(xn) self.xn_list.append(xn) @@ -105,13 +104,25 @@ class gd_1d(object): fx = f(xx1) f_xn = f(np.array(self.xn_list)) + tangent_x, tangent_y = [], [] + for i in range(0, len(f_xn)): + xrange = np.linspace(np.array(self.xn_list)[i]-0.5, np.array(self.xn_list)[i]+0.5, 10) + tangent_line = self.df_list[i] * (x - np.array(self.xn_list)[i]) + f_xn[i] + tangent_x.append(xrange) + tangent_y.append(lambdify(x, tangent_line)(xrange)) + fig = go.Figure() - fig.add_scatter(x=xx1, y=fx) - frames = [] - frames.append({'data':copy.deepcopy(fig['data']),'name':f'frame{0}'}) - fig.add_traces(go.Scatter(x=None, y=None, mode="lines + markers", line={"color":"#de1032", "width":1, 'dash': 'dash'})) - frames = [go.Frame(data= [go.Scatter(x=np.array(self.xn_list)[:k], y=f_xn)],traces= [1],name=f'frame{k+2}')for k in range(len(f_xn))] - fig.update(frames=frames) + #fig.add_scatter(x=xx1, y=fx) + + fig.add_trace(go.Scatter(x=xx1, y=fx)) + fig.add_traces(go.Scatter(x=None, y=None, mode="lines + markers", line={"color":"#de1032", "width":3, 'dash': 'dash'})) + fig.add_traces(go.Scatter(x=None, y=None, mode="lines", line={"color":"#debc10", "width":3, 'dash': 'dash'})) + + frames = [go.Frame(data=[go.Scatter(x=xx1, y=fx), + go.Scatter(x=np.array(self.xn_list)[:k], y=f_xn), + go.Scatter(x=tangent_x[k], y=tangent_y[k])], + traces= [0, 1, 2]) for k in range(len(f_xn))] + fig.frames = frames fig.update_layout(height=800, updatemenus=[dict(type="buttons",buttons=[dict(label="Play",method="animate",args=[None, dict(fromcurrent=True, transition=dict(duration=0), frame=dict(redraw=True, duration=1000))])])]) fig.show() @@ -220,17 +231,17 @@ class gd2d(object): fig = make_subplots(rows=1, cols=2, specs=[[{'type': 'surface'}, {'type': 'surface'}]]) fig.add_trace(go.Surface(contours = {"x": {"show": True}, "y":{"show": True}, "z":{"show": True}}, x=xx1, y=xx2, z=fx), row=1, col=1) - fig.add_trace(go.Scatter3d(x=None, y=None, z=None), row=1, col=1) + fig.add_trace(go.Scatter3d(x=None, y=None, z=None, marker=dict(size=5)), row=1, col=1) fig.add_trace(go.Surface(x=None, y=None, z=None, showlegend=False, showscale=False, colorscale='Blues'), row=1, col=1) fig.add_trace(go.Surface(z=list(z_offset), x=xx1, y=xx2, showlegend=False, showscale=False, surfacecolor=colorsurfz), row=1, col=2) - fig.add_trace(go.Scatter3d(x=None, y=None, z=None), row=1, col=2) - fig.add_trace(go.Scatter3d(x=None, y=None, z=None), row=1, col=2) + fig.add_trace(go.Scatter3d(x=None, y=None, z=None, marker=dict(size=5)), row=1, col=2) + fig.add_trace(go.Scatter3d(x=None, y=None, z=None, marker=dict(size=5)), row=1, col=2) frames = [go.Frame(data=[go.Surface(visible=True, showscale=False, opacity=0.8), - go.Scatter3d(x=np.array(self.xn_list)[:k,0], y=np.array(self.xn_list)[:k,1], z=f_xn, line={"color":"#10dedb", "width":3, 'dash': 'dash'}), + go.Scatter3d(x=np.array(self.xn_list)[:k,0], y=np.array(self.xn_list)[:k,1], z=f_xn, marker=dict(size=5), line={"color":"#10dedb", "width":3, 'dash': 'dash'}), go.Surface(visible=False, x=xx1_tangent, y=xx2_tangent, z=z[k]), go.Surface(visible=True, showscale=False, opacity=0.8), go.Scatter3d(x=np.array(self.xn_list)[:k, 0], y=np.array(self.xn_list)[:k, 1], z=f_xn, line={"color":"#10dedb", "width":3, 'dash': 'dash'}), - go.Scatter3d(x=np.array(self.xn_list)[:k, 0].flatten(), y=np.array(self.xn_list)[:k, 1].flatten(), z=z_offset.flatten(), line={"color":"#58de10", "width":3, 'dash': 'dash'})], + go.Scatter3d(x=np.array(self.xn_list)[:k, 0].flatten(), y=np.array(self.xn_list)[:k, 1].flatten(), z=z_offset.flatten(), marker=dict(size=5), line={"color":"#58de10", "width":3, 'dash': 'dash'})], traces=[0, 1, 2, 3, 4, 5]) for k in range(len(f_xn))] fig.frames = frames self.fig_frames = frames @@ -353,12 +364,12 @@ class gd2d_compete(object): fig = make_subplots(rows=1, cols=1, specs=[[{'type': 'surface'}]]) fig.add_trace(go.Surface(x=xx1, y=xx2, z=fx), row=1, col=1) fig.add_trace(go.Scatter3d(x=np.array(self.xn_p0_list)[:, 0], y=np.array(self.xn_p0_list)[:, 1], z=fx_p0, - name="candidate 1", mode="lines+markers", marker=dict(color="green")), row=1, col=1) + name="candidate 1", mode="lines+markers", marker=dict(size=5, color="green")), row=1, col=1) fig.add_trace(go.Scatter3d(x=np.array(self.xn_p1_list)[:, 0], y=np.array(self.xn_p1_list)[:, 1], z=fx_p1, - name="candidate 2", mode="lines+markers", marker=dict(color="blue")), row=1, col=1) - frames = [go.Frame(data = [go.Surface(visible=True, showscale=False, opacity=0.8), - go.Scatter3d(x=np.array(self.xn_p0_list)[:self.timer, 0], y=np.array(self.xn_p0_list)[:self.timer, 1], z=fx_p0), - go.Scatter3d(x=np.array(self.xn_p1_list)[:self.timer, 0], y=np.array(self.xn_p1_list)[:self.timer, 1], z=fx_p1)], + name="candidate 2", mode="lines+markers", marker=dict(size=5, color="blue")), row=1, col=1) + frames = [go.Frame(data = [go.Surface(visible=True, showscale=False, opacity=0.6), + go.Scatter3d(x=np.array(self.xn_p0_list)[:self.timer, 0], y=np.array(self.xn_p0_list)[:self.timer, 1], z=fx_p0), + go.Scatter3d(x=np.array(self.xn_p1_list)[:self.timer, 0], y=np.array(self.xn_p1_list)[:self.timer, 1], z=fx_p1)], traces=[0,1,2])] fig.frames = frames fig.update_layout(scene_aspectmode='manual', scene_aspectratio=dict(x=0, y=0, z=0), height=800)