Update 'algorithm/optimization/gradient.py'

This commit is contained in:
TerenceLiu 2023-02-20 14:46:19 +08:00
parent 6be11e6536
commit 66e7b7e32f
1 changed files with 7 additions and 6 deletions

View File

@ -28,7 +28,7 @@ def array_mesh(data, n):
array_mesh.append(data[i:i+n]) array_mesh.append(data[i:i+n])
return np.vstack(array_mesh) return np.vstack(array_mesh)
class gd_1d(object): class gd1d(object):
def __init__(self, environ:str="jupyterlab"): def __init__(self, environ:str="jupyterlab"):
pio.renderers.default = environ # 'notebook' or 'colab' or 'jupyterlab' pio.renderers.default = environ # 'notebook' or 'colab' or 'jupyterlab'
self.wg_expr = widgets.Text(value="sin(x) + sin((10.0 / 3.0) * x)", self.wg_expr = widgets.Text(value="sin(x) + sin((10.0 / 3.0) * x)",
@ -150,7 +150,7 @@ class gd2d(object):
def initialization_default(self, environ): def initialization_default(self, environ):
pio.renderers.default = environ # 'notebook' or 'colab' or 'jupyterlab' 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")], 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 + 10*x2**2"), ("x1**2 + 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_x0 = widgets.Text(value="0,2", description="Startpoint:")
self.wg_lr = widgets.FloatText(value="1e-1", description="step size:") self.wg_lr = widgets.FloatText(value="1e-1", description="step size:")
self.wg_epsilon = widgets.FloatText(value="1e-5", description="criterion:") self.wg_epsilon = widgets.FloatText(value="1e-5", description="criterion:")
@ -211,10 +211,11 @@ class gd2d(object):
x1 = symbols("x1") x1 = symbols("x1")
x2 = symbols("x2") x2 = symbols("x2")
expr = sympify(self.wg_expr.value) expr = sympify(self.wg_expr.value)
xx1 = np.arange(0, 5, 0.25) 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)"]
xx2 = np.arange(0, 5, 0.25) if self.wg_expr.value in expression_list:
#xx1 = np.arange(np.array(self.xn_list)[:, 0].min() * 0.5, np.array(self.xn_list)[:, 0].max() * 1.5, 0.1) xx1, xx2 = np.arange(0, 5, 0.25), np.arange(0, 5, 0.25)
#xx2 = np.arange(np.array(self.xn_list)[:, 1].min() * 0.5, np.array(self.xn_list)[:, 1].max() * 1.5, 0.1) else:
xx1, xx2 = np.arange(-5, 5, 0.25), np.arange(-5, 5, 0.25)
xx1_tangent = np.arange(np.array(self.xn_list)[:, 0].min(), np.array(self.xn_list)[:, 0].max(), 0.1) 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) 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 xx1_o, xx2_o = xx1, xx2