Update 'algorithm/optimization/gradient.py'
This commit is contained in:
parent
6a49d61a4d
commit
0a8eb87534
|
@ -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"), ("(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_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:")
|
||||||
|
@ -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)"]
|
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:
|
if self.wg_expr.value in expression_list:
|
||||||
xx1, xx2 = np.arange(0, 5, 0.25), np.arange(0, 5, 0.25)
|
xx1, xx2 = np.arange(0, 5, 0.25), np.arange(0, 5, 0.25)
|
||||||
else:
|
elif self.wg_expr.value == "(4-x1)**2 + x2**2":
|
||||||
xx1, xx2 = np.arange(-5, 5, 0.25), np.arange(-5, 5, 0.25)
|
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)
|
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
|
||||||
|
@ -230,14 +230,22 @@ class gd2d(object):
|
||||||
partial_x2 = lambdify((x1, x2), diff(expr, x2), "numpy")
|
partial_x2 = lambdify((x1, x2), diff(expr, x2), "numpy")
|
||||||
self.partial_x1, self.partial_x2 = partial_x1, partial_x2
|
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
|
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))]
|
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
|
## projection
|
||||||
z_offset = (np.min(fx)) * np.ones(fx.shape)
|
z_offset = (np.min(fx)) * np.ones(fx.shape)
|
||||||
proj_z = lambda x, y, z: z
|
proj_z = lambda x, y, z: z
|
||||||
colorsurfz = proj_z(xx1, xx2, fx)
|
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 = 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.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)
|
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)
|
||||||
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),
|
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=False, x=xx1_tangent, y=xx2_tangent, z=z[k]),
|
||||||
go.Surface(visible=True, showscale=False, opacity=0.8),
|
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+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, 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].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))]
|
traces=[0, 1, 2, 3, 4, 5]) for k in range(len(f_xn))]
|
||||||
fig.frames = frames
|
fig.frames = frames
|
||||||
self.fig_frames = frames
|
self.fig_frames = frames
|
||||||
|
|
Loading…
Reference in New Issue