Jump to content

File:Nelder-Mead Simionescu.gif

Page contents not supported in other languages.
This is a file from the Wikimedia Commons
From Wikipedia, the free encyclopedia

Original file (1,000 × 1,000 pixels, file size: 1.31 MB, MIME type: image/gif, looped, 25 frames, 13 s)

Summary

Description
English: Animated Nelder-Mead minimum search of Simionescu's function.
Date
Source Own work
Author nicoguaro
GIF development
InfoField
 
This plot was created with Matplotlib.
Source code
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()

Licensing

I, the copyright holder of this work, hereby publish it under the following license:
w:en:Creative Commons
attribution share alike
This file is licensed under the Creative Commons Attribution-Share Alike 4.0 International license.
You are free:
  • to share – to copy, distribute and transmit the work
  • to remix – to adapt the work
Under the following conditions:
  • attribution – You must give appropriate credit, provide a link to the license, and indicate if changes were made. You may do so in any reasonable manner, but not in any way that suggests the licensor endorses you or your use.
  • share alike – If you remix, transform, or build upon the material, you must distribute your contributions under the same or compatible license as the original.

Captions

Add a one-line explanation of what this file represents

Items portrayed in this file

depicts

26 June 2018

File history

Click on a date/time to view the file as it appeared at that time.

Date/TimeThumbnailDimensionsUserComment
current19:47, 26 June 2018Thumbnail for version as of 19:47, 26 June 20181,000 × 1,000 (1.31 MB)NicoguaroImprove line widths
17:36, 22 November 2016Thumbnail for version as of 17:36, 22 November 2016750 × 698 (30 KB)PasimiUser created page with UploadWizard

Global file usage

The following other wikis use this file: