File:Nelder-Mead Simionescu.gif

原始檔案(1,000 × 1,000 像素,檔案大小:1.31 MB,MIME 類型:image/gif、​循環、​25 畫格、​13秒)


摘要

描述
English: Animated Nelder-Mead minimum search of Simionescu's function.
日期
來源 自己的作品
作者 nicoguaro
GIF開發
InfoField
 
本GIF 點陣圖使用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:共享創意
姓名標示 相同方式分享
您可以自由:
  • 分享 – 複製、發佈和傳播本作品
  • 重新修改 – 創作演繹作品
惟需遵照下列條件:
  • 姓名標示 – 您必須指名出正確的製作者,和提供授權條款的連結,以及表示是否有對內容上做出變更。您可以用任何合理的方式來行動,但不得以任何方式表明授權條款是對您許可或是由您所使用。
  • 相同方式分享 – 如果您利用本素材進行再混合、轉換或創作,您必須基於如同原先的相同或兼容的條款,來分布您的貢獻成品。

說明

添加單行說明來描述出檔案所代表的內容

在此檔案描寫的項目

描繪內容

創作作者 Chinese (Hong Kong) (已轉換拼寫)

沒有維基數據項目的某些值

維基媒體使用者名稱 繁體中文 (已轉換拼寫):​Nicoguaro
作者姓名字串 繁體中文 (已轉換拼寫):​nicoguaro

著作權狀態 繁體中文 (已轉換拼寫)

有著作權 繁體中文 (已轉換拼寫)

共享創意署名-相同方式共享4.0國際 Chinese (Hong Kong) (已轉換拼寫)

檔案來源 Chinese (Taiwan) (已轉換拼寫)

上傳者的原創作品 繁體中文 (已轉換拼寫)

檔案歷史

點選日期/時間以檢視該時間的檔案版本。

日期/時間縮⁠圖尺寸用戶備⁠註
目前2018年6月26日 (二) 19:47於 2018年6月26日 (二) 19:47 版本的縮圖1,000 × 1,000(1.31 MB)NicoguaroImprove line widths
2016年11月22日 (二) 17:36於 2016年11月22日 (二) 17:36 版本的縮圖750 × 698(30 KB)PasimiUser created page with UploadWizard

下列頁面有用到此檔案:

全域檔案使用狀況

以下其他 wiki 使用了這個檔案: