In [1]:
# Code attribution: Yiyin Shen, Tyler Caraza-Harter
# Imports
import sklearn
import sklearn.linear_model
import numpy
import math
import scipy
import matplotlib.pyplot as plt
In [2]:
# Comparison of optimization methods on the Rosenbrock function
x, y = numpy.meshgrid(numpy.arange(-1, 1, 0.05), numpy.arange(-1, 1, 0.05))
z = scipy.optimize.rosen([x, y])
fig = plt.figure()
ax = fig.add_subplot(projection = "3d")
ax.plot_surface(x, y, z)
Out[2]:
<mpl_toolkits.mplot3d.art3d.Poly3DCollection at 0x201503a92d0>
In [3]:
# Nelder Mead
sol = scipy.optimize.minimize(scipy.optimize.rosen, [-1, -1], method = "Nelder-Mead", options = {'return_all': True}).allvecs
nm = numpy.transpose(sol)
len(sol), sol[-1]
Out[3]:
(68, array([0.99999886, 0.99999542]))
In [4]:
# BFGS
sol = scipy.optimize.minimize(scipy.optimize.rosen, [-1, -1], jac = scipy.optimize.rosen_der, method = "BFGS", options = {'return_all': True}).allvecs
bfgs = numpy.transpose(sol)
len(sol), sol[-1]
Out[4]:
(32, array([1.00000001, 1.00000002]))
In [5]:
# Newton
sol = scipy.optimize.minimize(scipy.optimize.rosen, [-1, -1], jac = scipy.optimize.rosen_der, hess = scipy.optimize.rosen_hess, method = "Newton-CG", options = {'return_all': True}).allvecs
ncg = numpy.transpose(sol)
len(sol), sol[-1]
Out[5]:
(39, array([0.99997716, 0.99995422]))
In [6]:
# Plot everything
fig = plt.figure()
ax = fig.add_subplot(projection = "3d")
ax.plot_surface(x, y, z, alpha = 0.75)
ax.plot3D(nm[0], nm[1], scipy.optimize.rosen(nm), c = "red", lw = 5)
ax.plot3D(bfgs[0], bfgs[1], scipy.optimize.rosen(bfgs), c = "blue", lw = 5)
ax.plot3D(ncg[0], ncg[1], scipy.optimize.rosen(ncg), c = "green", lw = 5)
Out[6]:
[<mpl_toolkits.mplot3d.art3d.Line3D at 0x20152484bd0>]
In [7]:
# Change angle
fig = plt.figure()
ax = fig.add_subplot(projection = "3d")
ax.plot_surface(x, y, z, alpha = 0.75)
ax.plot3D(nm[0], nm[1], scipy.optimize.rosen(nm), c = "red", lw = 5)
ax.plot3D(bfgs[0], bfgs[1], scipy.optimize.rosen(bfgs), c = "blue", lw = 5)
ax.plot3D(ncg[0], ncg[1], scipy.optimize.rosen(ncg), c = "green", lw = 5)
ax.view_init(45, 45)