#!/usr/bin/env python #-*- coding: utf-8 -*- """ UH: Code taken from Nicoguaro, Nelder-Mead_Rosenbrock.gif, CC BY 4.0, https://commons.wikimedia.org/w/index.php?title=File:Nelder-Mead_Rosenbrock.gif&oldid=260874010, with different initial simplex and 50 instead of 20 steps Animation of the Nelder-Mead method for the Rosenbrock function. """ from __future__ import division, print_function import numpy as np import matplotlib as mpl mpl.use("Agg") from scipy.optimize import rosen import matplotlib.pyplot as plt import matplotlib.animation as animation from matplotlib import rcParams # In Windows the next line should provide the full path to convert.exe # since convert is a Windows command #rcParams['animation.convert_path'] = "C:\Program Files\ImageMagick-6.9.3-Q16\convert.exe" # UH: for ImageMagick installation with MacPorts on macOS rcParams['animation.convert_path'] = "/usr/bin/convert" rcParams['font.size'] = 12 def nelder_mead_step(fun, verts, alpha=1, gamma=2, rho=0.5, sigma=0.5): """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 = fun(verts.T) # 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) if f[0]<=fr and fr