Differential EvolutionΒΆ
This example demonstrates the calibration of the Rosenbrock and Ackley functions using the differential_evolution
function from scipy (http://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.differential_evolution.html#scipy.optimize.differential_evolution).
from matk import matk
from scipy.optimize import rosen
import numpy as np
def myrosen(pars):
return rosen(pars.values())
p = matk(model=myrosen)
p.add_par('p1',min=0,max=2)
p.add_par('p2',min=0,max=2)
p.add_par('p3',min=0,max=2)
p.add_par('p4',min=0,max=2)
p.add_obs('o1',value=0)
result = p.differential_evolution()
print "Rosenbrock problem:"
print "Parameters should be all ones: ", result.x
print "Objective function: ", result.fun
Rosenbrock problem:
Parameters should be all ones: [ 0.99999934 1.0000001 0.99999966 0.99999853]
Objective function: 1.00375896419e-21
def ackley(pars):
x = pars.values()
arg1 = -0.2 * np.sqrt(0.5 * (x[0] ** 2 + x[1] ** 2))
arg2 = 0.5 * (np.cos(2. * np.pi * x[0]) + np.cos(2. * np.pi * x[1]))
return -20. * np.exp(arg1) - np.exp(arg2) + 20. + np.e
p2 = matk(model=ackley)
p2.add_par('p1',min=-5,max=5)
p2.add_par('p2',min=-5,max=5)
p2.add_obs('o1',value=0)
result = p2.differential_evolution()
print "Ackley problem:"
print "Parameters should be zero: ", result.x
print "Objective function: ", result.fun
Ackley problem:
Parameters should be zero: [ -1.11798348e-10 -4.46476189e-12]
Objective function: 1.00150425637e-19