From 467e8871afc4fd783f2fe8c32afda7f0f347d981 Mon Sep 17 00:00:00 2001 From: TerenceLiu Date: Sun, 19 Feb 2023 12:44:35 +0800 Subject: [PATCH] Update 'algorithm/optimization/gradient.py' --- algorithm/optimization/gradient.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/algorithm/optimization/gradient.py b/algorithm/optimization/gradient.py index b2743e3..5f85bc5 100644 --- a/algorithm/optimization/gradient.py +++ b/algorithm/optimization/gradient.py @@ -121,7 +121,7 @@ class gd_1d(object): fig.add_trace(go.Scatter(x=xx1, y=fx)) fig.add_traces(go.Scatter(x=None, y=None, mode="lines + markers", marker=dict(size=10), 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'})) - fig.add_traces(go.Scatter(x=None, y=None, mode="lines", line={"color":"#de3210", "width":3, 'dash': 'dash'})) + #fig.add_traces(go.Scatter(x=None, y=None, mode="lines", line={"color":"#de3210", "width":3, 'dash': 'dash'})) frames = [go.Frame(data=[go.Scatter(x=xx1, y=fx), go.Scatter(x=np.array(self.xn_list)[:k+1], y=f_xn), go.Scatter(x=tangent_x[k], y=tangent_y[k]), @@ -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)"), ("(sin(x1) - 2) ** 2 + (sin(x2) - 2) ** 2", "(sin(x1) - 2) ** 2 + (sin(x2) - 2) ** 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")], 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:") @@ -314,7 +314,7 @@ class gd2d_compete(object): def initialization(self, environ): pio.renderers.default = environ # 'notebook' or 'colab' or 'jupyterlab' self.timer = 0 - 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)"), ("(sin(x1) - 2) ** 2 + (sin(x2) - 2) ** 2", "(sin(x1) - 2) ** 2 + (sin(x2) - 2) ** 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")], 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="Init point:") self.wg_lr = widgets.FloatText(value="0.1", description="step size:") self.wg_direction_p0 = widgets.Text(value="0.5,1", description="Direction (a1)") @@ -364,7 +364,10 @@ class gd2d_compete(object): with self.plot_output: clear_output(wait=True) x1, x2 =symbols("x1 x2") - xx1, xx2 = np.arange(0, 5, 0.25), np.arange(0, 5, 0.25) + if self.wg_expr.value == "(1 - 8 * x1 + 7 * x1**2 - (7/3) * x1**3 + (1/4) * x1**4) * x2**2 * E**(-x2)": + 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) xx1, xx2 = np.meshgrid(xx1, xx2) func = lambdify((x1, x2), sympify(self.wg_expr.value), "numpy") fx = func(xx1, xx2)