Optimisation: xNES

This example shows you how to run a global optimisation with xNES.

For a more elaborate example of an optimisation, see: basic optimisation example.


In [2]:
from __future__ import print_function
import pints
import pints.toy as toy
import numpy as np
import matplotlib.pyplot as pl

# Load a forward model
model = toy.LogisticModel()

# Create some toy data
real_parameters = [0.015, 500]
times = np.linspace(0, 1000, 1000)
values = model.simulate(real_parameters, times)

# Add noise
values += np.random.normal(0, 10, values.shape)

# Create an object with links to the model and time series
problem = pints.SingleOutputProblem(model, times, values)

# Select a score function
score = pints.SumOfSquaresError(problem)

# Select some boundaries
boundaries = pints.RectangularBoundaries([0, 400], [0.03, 600])

# Perform an optimization with boundaries and hints
x0 = 0.011, 400
sigma0 = [0.01, 100]
found_parameters, found_value = pints.optimise(
    score,
    x0,
    sigma0,
    boundaries,
    method=pints.XNES,
    )

# Show score of true solution
print('Score at true solution: ')
print(score(real_parameters))

# Compare parameters with original
print('Found solution:          True parameters:' )
for k, x in enumerate(found_parameters):
    print(pints.strfloat(x) + '    ' + pints.strfloat(real_parameters[k]))

# Show quality of fit
pl.figure()
pl.xlabel('Time')
pl.ylabel('Value')
pl.plot(times, values, label='Nosiy data')
pl.plot(times, problem.evaluate(found_parameters), label='Fit')
pl.legend()
pl.show()


Minimising error measure
using Exponential Natural Evolution Strategy (xNES)
Running in sequential mode.
Population size: 6
Iter. Eval. Best      Time m:s
0     6      2780827    0:00.0
1     12     2780827    0:00.0
2     18     1869945    0:00.0
3     24     1455345    0:00.0
20    126    90555.84   0:00.1
40    246    90353.97   0:00.1
60    366    90353.9    0:00.2
80    486    90353.9    0:00.2
100   606    90353.9    0:00.3
120   726    90353.9    0:00.4
140   846    90353.9    0:00.4
160   966    90353.9    0:00.5
180   1086   90353.9    0:00.6
200   1206   90353.9    0:00.6
220   1326   90353.9    0:00.7
240   1446   90353.9    0:00.7
260   1566   90353.9    0:00.8
280   1686   90353.9    0:00.9
300   1806   90353.9    0:01.0
320   1926   90353.9    0:01.1
340   2046   90353.9    0:01.1
360   2166   90353.9    0:01.1
380   2286   90353.9    0:01.2
400   2406   90353.9    0:01.3
420   2526   90353.9    0:01.3
440   2646   90353.9    0:01.4
460   2766   90353.9    0:01.5
480   2886   90353.9    0:01.5
500   3006   90353.9    0:01.6
520   3126   90353.9    0:01.6
540   3246   90353.9    0:01.7
548   3288   90353.9    0:01.7
Halting: No significant change for 200 iterations.
Score at true solution: 
91162.4460941
Found solution:          True parameters:
 1.49542284064299764e-02     1.49999999999999994e-02
 5.00898298576238972e+02     5.00000000000000000e+02