Example 1: Rosenbrock function

A simple example showing how to minimize the two-dimensional Rosenbrock function

$$f(x,y) = (1-x)^2 + 100 (y-x^2)^2$$

The true global minimum is at $(x,y)=(1,1)$ where $f(x,y) = 0$.

Note: This notebook expects that iPython has been started with --pylab switch.


In [87]:
from pyde.de import DiffEvol

ngen, npop, ndim = 50, 20, 2

rbf = lambda pv: (1-pv[0])**2 + 100*(pv[1]-pv[0]**2)**2

Simple optimization


In [91]:
de = DiffEvol(rbf, [[-5,6],[-5,6]], npop)
de.optimize(50)
print de.minimum_location, de.minimum_value


[ 1.00364829  1.00710061] 1.76898130211e-05

Usage as a generator


In [92]:
pop, loc = zeros([ngen,npop,ndim]), zeros([ngen,ndim])

de = DiffEvol(rbf, [[-5,6],[-5,6]], npop)
for i,res in enumerate(de(ngen)):
    pop[i,:,:] = de.population.copy()
    loc[i,:] = de.minimum_location.copy()

In [93]:
fig,ax = subplots(1,3,figsize=(13,4), sharey=True)
ax[0].plot(pop[:,:,0], '.k', alpha=0.2)
ax[1].plot(pop[:,:,1], '.k', alpha=0.2)
ax[2].plot(loc[:,:], 'k')
setp(ax, xlabel='Generation')
fig.tight_layout()