From 8750868cabda3d7e1ef3f5e120daa3912cbd9925 Mon Sep 17 00:00:00 2001 From: TerenceLiu Date: Mon, 19 Sep 2022 01:41:05 +0800 Subject: [PATCH] animate 3d surface --- .../interactive-checkpoint.ipynb | 100 ++++++++++++++++-- algorithm/interactive.ipynb | 18 ++-- .../.ipynb_checkpoints/gd-checkpoint.py | 66 +++++++----- algorithm/optimization/gd.py | 66 +++++++----- 4 files changed, 180 insertions(+), 70 deletions(-) diff --git a/algorithm/.ipynb_checkpoints/interactive-checkpoint.ipynb b/algorithm/.ipynb_checkpoints/interactive-checkpoint.ipynb index 90c5375..bd2b12e 100644 --- a/algorithm/.ipynb_checkpoints/interactive-checkpoint.ipynb +++ b/algorithm/.ipynb_checkpoints/interactive-checkpoint.ipynb @@ -2,7 +2,7 @@ "cells": [ { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "id": "af1d5646-4b13-4039-9f76-8042bc9dbda3", "metadata": {}, "outputs": [], @@ -12,20 +12,106 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "id": "0a5c51d2-8b18-4143-b6f1-73a909ccb623", "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "application/vnd.jupyter.widget-view+json": { + "model_id": "9059357c5be5412c9c92e436a2a6499d", + "version_major": 2, + "version_minor": 0 + }, + "text/plain": [ + "VBox(children=(HBox(children=(VBox(children=(Text(value='x**3 - x**(1/2)', description='Expression:', style=Te…" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "application/vnd.jupyter.widget-view+json": { + "model_id": "b60f5896d3394f3195c55eaf4fe3fe23", + "version_major": 2, + "version_minor": 0 + }, + "text/plain": [ + "Output()" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "application/vnd.jupyter.widget-view+json": { + "model_id": "d21a3883fd054ad7849e00eed23780ac", + "version_major": 2, + "version_minor": 0 + }, + "text/plain": [ + "Output()" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], "source": [ "gd1 = gradient_descent_1d(environ=\"jupyterlab\")" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "id": "15c6e757-cde3-422b-be7e-3f55b7752142", "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "application/vnd.jupyter.widget-view+json": { + "model_id": "6fa72562a120492da5df506e493ccb0a", + "version_major": 2, + "version_minor": 0 + }, + "text/plain": [ + "VBox(children=(HBox(children=(VBox(children=(Text(value='(sin(x1) - 2) ** 2 + (sin(x2) - 2) ** 2', description…" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "application/vnd.jupyter.widget-view+json": { + "model_id": "69599d86bf5a49ca82788ca5fc19b4d3", + "version_major": 2, + "version_minor": 0 + }, + "text/plain": [ + "Output()" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "application/vnd.jupyter.widget-view+json": { + "model_id": "36f1d55d6496429888a95493e534b62f", + "version_major": 2, + "version_minor": 0 + }, + "text/plain": [ + "Output()" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], "source": [ "gd2 = gradient_descent_2d(environ=\"jupyterlab\")" ] @@ -33,7 +119,7 @@ { "cell_type": "code", "execution_count": null, - "id": "a05e9830-1643-47c1-a7ea-7c286888cfd4", + "id": "081eff94-4d25-4455-8ff1-86b4518a052c", "metadata": {}, "outputs": [], "source": [] @@ -55,7 +141,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.7.13" } }, "nbformat": 4, diff --git a/algorithm/interactive.ipynb b/algorithm/interactive.ipynb index ae10f88..bd2b12e 100644 --- a/algorithm/interactive.ipynb +++ b/algorithm/interactive.ipynb @@ -19,7 +19,7 @@ { "data": { "application/vnd.jupyter.widget-view+json": { - "model_id": "46088f2d27c748c1b8cf9ac9340abe90", + "model_id": "9059357c5be5412c9c92e436a2a6499d", "version_major": 2, "version_minor": 0 }, @@ -33,7 +33,7 @@ { "data": { "application/vnd.jupyter.widget-view+json": { - "model_id": "68d79d493fdd445a9854baf7080f018d", + "model_id": "b60f5896d3394f3195c55eaf4fe3fe23", "version_major": 2, "version_minor": 0 }, @@ -47,7 +47,7 @@ { "data": { "application/vnd.jupyter.widget-view+json": { - "model_id": "6f81d5b1dfc240be807ad8108049d130", + "model_id": "d21a3883fd054ad7849e00eed23780ac", "version_major": 2, "version_minor": 0 }, @@ -65,14 +65,14 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 3, "id": "15c6e757-cde3-422b-be7e-3f55b7752142", "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { - "model_id": "a71627f5371d4f2f91fb0ae93c22d847", + "model_id": "6fa72562a120492da5df506e493ccb0a", "version_major": 2, "version_minor": 0 }, @@ -86,7 +86,7 @@ { "data": { "application/vnd.jupyter.widget-view+json": { - "model_id": "fa8540f9cbcb4eec943319ce3b6d2d42", + "model_id": "69599d86bf5a49ca82788ca5fc19b4d3", "version_major": 2, "version_minor": 0 }, @@ -100,7 +100,7 @@ { "data": { "application/vnd.jupyter.widget-view+json": { - "model_id": "371417e579484f4084a832349172f065", + "model_id": "36f1d55d6496429888a95493e534b62f", "version_major": 2, "version_minor": 0 }, @@ -119,7 +119,7 @@ { "cell_type": "code", "execution_count": null, - "id": "a05e9830-1643-47c1-a7ea-7c286888cfd4", + "id": "081eff94-4d25-4455-8ff1-86b4518a052c", "metadata": {}, "outputs": [], "source": [] @@ -141,7 +141,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.7.13" } }, "nbformat": 4, diff --git a/algorithm/optimization/.ipynb_checkpoints/gd-checkpoint.py b/algorithm/optimization/.ipynb_checkpoints/gd-checkpoint.py index c6df601..6e8709c 100644 --- a/algorithm/optimization/.ipynb_checkpoints/gd-checkpoint.py +++ b/algorithm/optimization/.ipynb_checkpoints/gd-checkpoint.py @@ -171,35 +171,47 @@ class gradient_descent_2d(object): f = lambdify((x1, x2), expr, "numpy") fx = f(xx1, xx2) f_xn = f(np.array(self.xn_list)[:, 0], np.array(self.xn_list)[:, 1]) + + frames, steps = [], [] + for k in range(len(f_xn)): + #frame = go.Frame(data=[go.Surface(x=xx1, y=xx2, z=fx, showscale=True, opacity=0.8)]) + #fig.add_trace(go.Scatter3d(x=np.array(self.xn_list)[:k, 0], y=np.array(self.xn_list)[:k, 1], z=f_xn)) + frame = go.Frame(dict(data=[go.Scatter3d(x=np.array(self.xn_list)[:k,0], y=np.array(self.xn_list)[:k,1], z=f_xn)], name=f'frame{k+1}'), traces=[1]) + frames.append(frame) + step = dict( + method="update", + args=[{"visible": [True]}, + {"title": "Slider switched to step: " + str(k+1)}], # layout attribute + ) + steps.append(step) + + sliders = [dict(steps= [dict(method= 'animate', + args= [[f'frame{k+1}'], + dict(mode= 'immediate', + frame= dict( duration=0, redraw= True ), + transition=dict( duration=0) + ) + ], + #label='Date : {}'.format(date_range[k]) + ) for k in range(0,len(frames))], + transition= dict(duration=0), + x=0, + y=0, + currentvalue=dict(font=dict(size=12), visible=True, xanchor= 'center'), + len=1.0) + ] - fig = go.Figure() - fig.add_surface(x=xx1, y=xx2, z=fx, showscale=True, opacity=0.9) - fig.update_traces(contours_z=dict(show=True, usecolormap=True, highlightcolor="limegreen", project_z=True)) - - frames = [] - frames.append({'data':copy.deepcopy(fig['data']),'name':f'frame{0}'}) - - line_marker=dict(color="#de1032", width=5) - fig.add_traces(go.Scatter3d(x=None, y=None, z=None, mode='lines+markers', line={"color":"#de1032", "width":5})) - - frames = [go.Frame(data= [go.Scatter3d(x=np.array(self.xn_list)[:k,0], - y=np.array(self.xn_list)[:k,1], - z=f_xn)],traces= [1], - name=f'frame{k+2}')for k in range(len(f_xn))] - - fig.update(frames=frames) - fig.update_layout(updatemenus=[dict(type="buttons", buttons=[dict(label="Play", method="animate", args=[None]), - dict(label="Pause", method="animate", args= [[None], - dict(mode='immediate', - transition= { 'duration': 0 }, - frame= dict( redraw=True, duration=0))]) - ])]) - - fig.update_layout(margin=dict(l=0, r=0, b=0, t=0)) - # define slider + trace1 = go.Surface(x=xx1, y=xx2, z=fx, showscale=True, opacity=0.8) + trace2 = go.Scatter3d(x=None, y=None, z=None) + fig = go.Figure(data=[trace1, trace2], frames=frames) + #fig.add_surface(x=xx1, y=xx2, z=fx, showscale=True, opacity=0.9) + #fig.update_traces(contours_z=dict(show=True, usecolormap=True, highlightcolor="limegreen", project_z=True)) + #fig.update(frames=frames) + fig.update_layout(updatemenus=[dict(type="buttons", buttons=[dict(label="Play", method="animate", args=[None, dict(fromcurrent=True)]), \ + dict(label="Pause", method="animate", args=[[None], dict(fromcurrent=True, mode='immediate', transition= {'duration': 0}, frame=dict(redraw=True, duration=0))])])], + margin=dict(l=0, r=0, b=0, t=0), sliders=sliders) fig.show() - - \ No newline at end of file + \ No newline at end of file diff --git a/algorithm/optimization/gd.py b/algorithm/optimization/gd.py index c6df601..6e8709c 100644 --- a/algorithm/optimization/gd.py +++ b/algorithm/optimization/gd.py @@ -171,35 +171,47 @@ class gradient_descent_2d(object): f = lambdify((x1, x2), expr, "numpy") fx = f(xx1, xx2) f_xn = f(np.array(self.xn_list)[:, 0], np.array(self.xn_list)[:, 1]) + + frames, steps = [], [] + for k in range(len(f_xn)): + #frame = go.Frame(data=[go.Surface(x=xx1, y=xx2, z=fx, showscale=True, opacity=0.8)]) + #fig.add_trace(go.Scatter3d(x=np.array(self.xn_list)[:k, 0], y=np.array(self.xn_list)[:k, 1], z=f_xn)) + frame = go.Frame(dict(data=[go.Scatter3d(x=np.array(self.xn_list)[:k,0], y=np.array(self.xn_list)[:k,1], z=f_xn)], name=f'frame{k+1}'), traces=[1]) + frames.append(frame) + step = dict( + method="update", + args=[{"visible": [True]}, + {"title": "Slider switched to step: " + str(k+1)}], # layout attribute + ) + steps.append(step) + + sliders = [dict(steps= [dict(method= 'animate', + args= [[f'frame{k+1}'], + dict(mode= 'immediate', + frame= dict( duration=0, redraw= True ), + transition=dict( duration=0) + ) + ], + #label='Date : {}'.format(date_range[k]) + ) for k in range(0,len(frames))], + transition= dict(duration=0), + x=0, + y=0, + currentvalue=dict(font=dict(size=12), visible=True, xanchor= 'center'), + len=1.0) + ] - fig = go.Figure() - fig.add_surface(x=xx1, y=xx2, z=fx, showscale=True, opacity=0.9) - fig.update_traces(contours_z=dict(show=True, usecolormap=True, highlightcolor="limegreen", project_z=True)) - - frames = [] - frames.append({'data':copy.deepcopy(fig['data']),'name':f'frame{0}'}) - - line_marker=dict(color="#de1032", width=5) - fig.add_traces(go.Scatter3d(x=None, y=None, z=None, mode='lines+markers', line={"color":"#de1032", "width":5})) - - frames = [go.Frame(data= [go.Scatter3d(x=np.array(self.xn_list)[:k,0], - y=np.array(self.xn_list)[:k,1], - z=f_xn)],traces= [1], - name=f'frame{k+2}')for k in range(len(f_xn))] - - fig.update(frames=frames) - fig.update_layout(updatemenus=[dict(type="buttons", buttons=[dict(label="Play", method="animate", args=[None]), - dict(label="Pause", method="animate", args= [[None], - dict(mode='immediate', - transition= { 'duration': 0 }, - frame= dict( redraw=True, duration=0))]) - ])]) - - fig.update_layout(margin=dict(l=0, r=0, b=0, t=0)) - # define slider + trace1 = go.Surface(x=xx1, y=xx2, z=fx, showscale=True, opacity=0.8) + trace2 = go.Scatter3d(x=None, y=None, z=None) + fig = go.Figure(data=[trace1, trace2], frames=frames) + #fig.add_surface(x=xx1, y=xx2, z=fx, showscale=True, opacity=0.9) + #fig.update_traces(contours_z=dict(show=True, usecolormap=True, highlightcolor="limegreen", project_z=True)) + #fig.update(frames=frames) + fig.update_layout(updatemenus=[dict(type="buttons", buttons=[dict(label="Play", method="animate", args=[None, dict(fromcurrent=True)]), \ + dict(label="Pause", method="animate", args=[[None], dict(fromcurrent=True, mode='immediate', transition= {'duration': 0}, frame=dict(redraw=True, duration=0))])])], + margin=dict(l=0, r=0, b=0, t=0), sliders=sliders) fig.show() - - \ No newline at end of file + \ No newline at end of file