File:Nelder-Mead Simionescu.gif

来自testwiki
跳转到导航 跳转到搜索
原始文件 (1,000 × 1,000像素,文件大小:1.31 MB,MIME类型:image/gif、​循环、​25帧、​13秒)
注意:由于技术限制,高分辨率GIF图像的缩略图无法进行动画处理。

本文件来自维基共享资源并可能被其他项目使用。 其文件描述页上的描述显示在下面。

摘要

描述
English: Animated Nelder-Mead minimum search of Simionescu's function.
日期
来源 自己的作品
作者 nicoguaro
GIF开发
InfoField
 本图表使用Matplotlib创作。
源代码
InfoField

Python code

from numpy import cos, arctan2
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation

plt.rcParams["font.size"] = 10
plt.rcParams["mathtext.fontset"] = "cm"

# Minimization
def nelder_mead_step(fun, verts, alpha=1, gamma=2, rho=0.5,
                     sigma=0.5, beta=1.0):
    """Nelder-Mead iteration according to Wikipedia _[1]
    
    
    References
    ----------
     .. [1] Wikipedia contributors. "Nelder–Mead method." Wikipedia,
         The Free Encyclopedia. Wikipedia, The Free Encyclopedia,
         1 Sep. 2016. Web. 20 Sep. 2016. 
    """
    nverts, _ = verts.shape
    f = np.apply_along_axis(fun, 1, verts, beta=beta)
    # 1. Order
    order = np.argsort(f)
    verts = verts[order, :]
    f = f[order]
    # 2. Calculate xo, the centroid"
    xo = verts[:-1, :].mean(axis=0)
    # 3. Reflection
    xr = xo + alpha*(xo - verts[-1, :])
    fr = fun(xr, beta)
    if f[0]<=fr and fr<f[-2]:
        new_verts = np.vstack((verts[:-1, :], xr))
    # 4. Expansion
    elif fr<f[0]:
        xe = xo + gamma*(xr - xo)
        fe = fun(xe, beta)
        if fe < fr:
            new_verts = np.vstack((verts[:-1, :], xe))
        else:
            new_verts = np.vstack((verts[:-1, :], xe))
    # 5. Contraction
    else:
        xc = xo + rho*(verts[-1, :] - xo)
        fc = fun(xc, beta)
        if fc < f[-1]:
            new_verts = np.vstack((verts[:-1, :], xc))
    # 6. Shrink
        else:
            new_verts = np.zeros_like(verts)
            new_verts[0, :] = verts[0, :]
            for k in range(1, nverts):
                new_verts[k, :] = sigma*(verts[k,:] - verts[0,:])
 
    return new_verts

def fun(x, beta=1.0):
    """Simionescu function using log-barrier method"""
    x1, x2 = x
    if x1**2 + x2**2 < (1 + 0.2*cos(8*arctan2(x1, x2)))**2:
        barrier = -beta*np.log((1 + 0.2*cos(8*arctan2(x1, x2)))**2 - x1**2 - x2**2)
    else:
        barrier = np.inf
    return x1*x2 + barrier

# Animation

def data_gen(num):
    plt.gca().cla
    x0 = np.array([0.4, -0.6])
    x1 = np.array([-0.3, -0.6])
    x2 = np.array([0.7, 0.6])
    verts = np.vstack((x0, x1, x2))
    beta = 1.0
    for cont in range(num):
        verts = nelder_mead_step(fun, verts, beta=beta)
        beta /=2
    # Plots
    plt.cla()
    poly = plt.Polygon(verts, facecolor="none", edgecolor="k",
                       linewidth=0.5, zorder=4)
    plt.gca().add_patch(poly)
    x1, x2 = np.mgrid[-1.25:1.25:101j, -1.25:1.25:101j]
    z = x1*x2
    cons = x1**2 + x2**2 - (1 + 0.2*cos(8*arctan2(x1, x2)))**2
    z[cons > 0.02] = np.nan
    levels = np.linspace(-1, 1, 30)
    plt.contour(x1, x2, z, levels, cmap="seismic", linewidths=1)
    plt.contour(x1, x2, cons, [0], colors="black", linewidths=1)
    plt.axis("image")
    plt.xlabel(r"$x$", fontsize=14)
    plt.ylabel(r"$y$", fontsize=14)

fig = plt.figure(figsize=(5, 5))
ani = animation.FuncAnimation(fig, data_gen, range(25))
ani.save("Nelder-Mead_Simionescu.gif", writer='imagemagick', fps=2,
         dpi=200)
plt.show()

许可协议

我,本作品著作权人,特此采用以下许可协议发表本作品:
w:zh:知识共享
署名 相同方式共享
本文件采用知识共享署名-相同方式共享 4.0 国际许可协议授权。
您可以自由地:
  • 共享 – 复制、发行并传播本作品
  • 修改 – 改编作品
惟须遵守下列条件:
  • 署名 – 您必须对作品进行署名,提供授权条款的链接,并说明是否对原始内容进行了更改。您可以用任何合理的方式来署名,但不得以任何方式表明许可人认可您或您的使用。
  • 相同方式共享 – 如果您再混合、转换或者基于本作品进行创作,您必须以与原先许可协议相同或相兼容的许可协议分发您贡献的作品。

说明

添加一行文字以描述该文件所表现的内容

此文件中描述的项目

描繪內容

文件历史

点击某个日期/时间查看对应时刻的文件。

日期/时间缩⁠略⁠图大小用户备注
当前2018年6月26日 (二) 20:472018年6月26日 (二) 20:47版本的缩略图1,000 × 1,000​(1.31 MB)wikimediacommons>NicoguaroImprove line widths

以下页面使用本文件: