From 0a8eb875347e406681547b57cfb839e7162ead67 Mon Sep 17 00:00:00 2001 From: TerenceLiu Date: Mon, 20 Feb 2023 15:45:55 +0800 Subject: [PATCH] Update 'algorithm/optimization/gradient.py' --- algorithm/optimization/gradient.py | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/algorithm/optimization/gradient.py b/algorithm/optimization/gradient.py index df2dfe4..793a60a 100644 --- a/algorithm/optimization/gradient.py +++ b/algorithm/optimization/gradient.py @@ -150,7 +150,7 @@ class gd2d(object): def initialization_default(self, environ): pio.renderers.default = environ # 'notebook' or 'colab' or 'jupyterlab' - self.wg_expr = widgets.Dropdown(options=[("(1 - 8 * x1 + 7 * x1**2 - (7/3) * x1**3 + (1/4) * x1**4) * x2**2 * E**(-x2)", "(1 - 8 * x1 + 7 * x1**2 - (7/3) * x1**3 + (1/4) * x1**4) * x2**2 * E**(-x2)"), ("x1 - x2 + 2*x1*x2 + 2*x1**2 + x2**2", "x1 - x2 + 2*x1*x2 + 2*x1**2 + x2**2"), ("(4-x1)**2 + x2**2", "(4-x1)**2 + x2**2"), ("x1**2 + 10*x2**2", "x1**2 + 10*x2**2")], value="(1 - 8 * x1 + 7 * x1**2 - (7/3) * x1**3 + (1/4) * x1**4) * x2**2 * E**(-x2)", descrption="Expression") + self.wg_expr = widgets.Dropdown(options=[("(1 - 8 * x1 + 7 * x1**2 - (7/3) * x1**3 + (1/4) * x1**4) * x2**2 * E**(-x2)", "(1 - 8 * x1 + 7 * x1**2 - (7/3) * x1**3 + (1/4) * x1**4) * x2**2 * E**(-x2)"), ("x1 - x2 + 2*x1*x2 + 2*x1**2 + x2**2", "x1 - x2 + 2*x1*x2 + 2*x1**2 + x2**2"), ("(4-x1)**2 + x2**2", "(4-x1)**2 + x2**2"), ("x1**2 + (10*x2)**2", "x1**2 + (10*x2)**2")], value="(1 - 8 * x1 + 7 * x1**2 - (7/3) * x1**3 + (1/4) * x1**4) * x2**2 * E**(-x2)", descrption="Expression") self.wg_x0 = widgets.Text(value="0,2", description="Startpoint:") self.wg_lr = widgets.FloatText(value="1e-1", description="step size:") self.wg_epsilon = widgets.FloatText(value="1e-5", description="criterion:") @@ -214,8 +214,8 @@ class gd2d(object): expression_list = ["(1 - 8 * x1 + 7 * x1**2 - (7/3) * x1**3 + (1/4) * x1**4) * x2**2 * E**(-x2)", "(1 - 8 * x1 + 7 * x1**2 - (7/3) * x1**3 + (1/4) * x1**4) * x2**2 * E**(-x2)"] if self.wg_expr.value in expression_list: xx1, xx2 = np.arange(0, 5, 0.25), np.arange(0, 5, 0.25) - else: - xx1, xx2 = np.arange(-5, 5, 0.25), np.arange(-5, 5, 0.25) + elif self.wg_expr.value == "(4-x1)**2 + x2**2": + xx1, xx2 = np.arange(-4, 12, 0.25), np.arange(-8, 8, 0.25) xx1_tangent = np.arange(np.array(self.xn_list)[:, 0].min(), np.array(self.xn_list)[:, 0].max(), 0.1) xx2_tangent = np.arange(np.array(self.xn_list)[:, 1].min(), np.array(self.xn_list)[:, 1].max(), 0.1) xx1_o, xx2_o = xx1, xx2 @@ -230,14 +230,22 @@ class gd2d(object): partial_x2 = lambdify((x1, x2), diff(expr, x2), "numpy") self.partial_x1, self.partial_x2 = partial_x1, partial_x2 plane = partial_x1(np.array(self.xn_list)[:, 0], np.array(self.xn_list)[:, 1]) * (x1 - np.array(self.xn_list)[:, 0]) + partial_x2(np.array(self.xn_list)[:, 0], np.array(self.xn_list)[:, 1]) * (x2 - np.array(self.xn_list)[:, 1]) + f_xn - + self.plane = plane z = [lambdify((x1, x2), plane[i], "numpy")(xx1_tangent, xx2_tangent) for i in range(0, len(plane))] - self.z = z + try: + if z[-1] == 0: + z[-1] = np.zeros(z[0].shape) + except: + pass ## projection z_offset = (np.min(fx)) * np.ones(fx.shape) proj_z = lambda x, y, z: z colorsurfz = proj_z(xx1, xx2, fx) + self.z = z + self.xx1_tangent, self.xx2_tangent = xx1_tangent, xx2_tangent + self.f_xn = f_xn + 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, marker=dict(size=5)), row=1, col=1) @@ -246,11 +254,11 @@ class gd2d(object): 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, marker=dict(size=5), line={"color":"blue", "width":3, 'dash': 'dash'}), + go.Scatter3d(x=np.array(self.xn_list)[:k+1,0], y=np.array(self.xn_list)[:k+1,1], z=f_xn, marker=dict(size=5), line={"color":"red", "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":"blue", "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":"green", "width":3, 'dash': 'dash'})], + go.Scatter3d(x=np.array(self.xn_list)[:k+1, 0], y=np.array(self.xn_list)[:k+1, 1], z=f_xn, line={"color":"red", "width":3, 'dash': 'dash'}), + go.Scatter3d(x=np.array(self.xn_list)[:k+1, 0].flatten(), y=np.array(self.xn_list)[:k, 1].flatten(), z=z_offset.flatten(), marker=dict(size=5), line={"color":"green", "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