In this notebook, we demonstrate an example of parameter estimation for a single-particle model using the Differential Evolution optimiser. The Differential Evolution (DE) algorithm is a stochastic population-based method that is well-suited for multi-dimensional functions. Unlike gradient-based methods, which require gradient information and can easily become trapped in local minima, DE relies on the concept of evolving a population of candidate solutions, using operations like mutation, crossover, and selection. This approach allows DE to search large areas of the solution space, which makes it highly effective for dealing with complex, nonlinear, and multimodal objective functions.
Before we begin, we need to ensure that we have all the necessary tools. We will install PyBOP from its development branch and upgrade some dependencies:
%pip install --upgrade pip ipywidgets
%pip install pybop -q
Requirement already satisfied: pip in /Users/bradyplanden/.pyenv/versions/pybop-env/lib/python3.11/site-packages (23.3.2) Requirement already satisfied: ipywidgets in /Users/bradyplanden/.pyenv/versions/pybop-env/lib/python3.11/site-packages (8.1.1) Requirement already satisfied: comm>=0.1.3 in /Users/bradyplanden/.pyenv/versions/pybop-env/lib/python3.11/site-packages (from ipywidgets) (0.2.0) Requirement already satisfied: ipython>=6.1.0 in /Users/bradyplanden/.pyenv/versions/pybop-env/lib/python3.11/site-packages (from ipywidgets) (8.18.1) Requirement already satisfied: traitlets>=4.3.1 in /Users/bradyplanden/.pyenv/versions/pybop-env/lib/python3.11/site-packages (from ipywidgets) (5.14.0) Requirement already satisfied: widgetsnbextension~=4.0.9 in /Users/bradyplanden/.pyenv/versions/pybop-env/lib/python3.11/site-packages (from ipywidgets) (4.0.9) Requirement already satisfied: jupyterlab-widgets~=3.0.9 in /Users/bradyplanden/.pyenv/versions/pybop-env/lib/python3.11/site-packages (from ipywidgets) (3.0.9) Requirement already satisfied: decorator in /Users/bradyplanden/.pyenv/versions/pybop-env/lib/python3.11/site-packages (from ipython>=6.1.0->ipywidgets) (5.1.1) Requirement already satisfied: jedi>=0.16 in /Users/bradyplanden/.pyenv/versions/pybop-env/lib/python3.11/site-packages (from ipython>=6.1.0->ipywidgets) (0.19.1) Requirement already satisfied: matplotlib-inline in /Users/bradyplanden/.pyenv/versions/pybop-env/lib/python3.11/site-packages (from ipython>=6.1.0->ipywidgets) (0.1.6) Requirement already satisfied: prompt-toolkit<3.1.0,>=3.0.41 in /Users/bradyplanden/.pyenv/versions/pybop-env/lib/python3.11/site-packages (from ipython>=6.1.0->ipywidgets) (3.0.41) Requirement already satisfied: pygments>=2.4.0 in /Users/bradyplanden/.pyenv/versions/pybop-env/lib/python3.11/site-packages (from ipython>=6.1.0->ipywidgets) (2.17.2) Requirement already satisfied: stack-data in /Users/bradyplanden/.pyenv/versions/pybop-env/lib/python3.11/site-packages (from ipython>=6.1.0->ipywidgets) (0.6.3) Requirement already satisfied: pexpect>4.3 in /Users/bradyplanden/.pyenv/versions/pybop-env/lib/python3.11/site-packages (from ipython>=6.1.0->ipywidgets) (4.9.0) Requirement already satisfied: parso<0.9.0,>=0.8.3 in /Users/bradyplanden/.pyenv/versions/pybop-env/lib/python3.11/site-packages (from jedi>=0.16->ipython>=6.1.0->ipywidgets) (0.8.3) Requirement already satisfied: ptyprocess>=0.5 in /Users/bradyplanden/.pyenv/versions/pybop-env/lib/python3.11/site-packages (from pexpect>4.3->ipython>=6.1.0->ipywidgets) (0.7.0) Requirement already satisfied: wcwidth in /Users/bradyplanden/.pyenv/versions/pybop-env/lib/python3.11/site-packages (from prompt-toolkit<3.1.0,>=3.0.41->ipython>=6.1.0->ipywidgets) (0.2.12) Requirement already satisfied: executing>=1.2.0 in /Users/bradyplanden/.pyenv/versions/pybop-env/lib/python3.11/site-packages (from stack-data->ipython>=6.1.0->ipywidgets) (2.0.1) Requirement already satisfied: asttokens>=2.1.0 in /Users/bradyplanden/.pyenv/versions/pybop-env/lib/python3.11/site-packages (from stack-data->ipython>=6.1.0->ipywidgets) (2.4.1) Requirement already satisfied: pure-eval in /Users/bradyplanden/.pyenv/versions/pybop-env/lib/python3.11/site-packages (from stack-data->ipython>=6.1.0->ipywidgets) (0.2.2) Requirement already satisfied: six>=1.12.0 in /Users/bradyplanden/.pyenv/versions/pybop-env/lib/python3.11/site-packages (from asttokens>=2.1.0->stack-data->ipython>=6.1.0->ipywidgets) (1.16.0) Note: you may need to restart the kernel to use updated packages. Note: you may need to restart the kernel to use updated packages.
With the environment set up, we can now import PyBOP alongside other libraries we will need:
import pybop
import numpy as np
To demonstrate parameter estimation, we first need some data. We will generate synthetic data using the PyBOP forward model, which requires defining a parameter set and the model itself.
We start by creating an example parameter set and then instantiate the single-particle model (SPM):
parameter_set = pybop.ParameterSet.pybamm("Chen2020")
model = pybop.lithium_ion.SPM(parameter_set=parameter_set)
We can then simulate the model using the predict
method, with a default constant current to generate voltage data.
t_eval = np.arange(0, 900, 2)
values = model.predict(t_eval=t_eval)
To make the parameter estimation more realistic, we add Gaussian noise to the data.
sigma = 0.001
corrupt_values = values["Voltage [V]"].data + np.random.normal(0, sigma, len(t_eval))
We will now set up the parameter estimation process by defining the datasets for optimisation and selecting the model parameters we wish to estimate.
The dataset for optimisation is composed of time, current, and the noisy voltage data:
dataset = pybop.Dataset(
{
"Time [s]": t_eval,
"Current function [A]": values["Current [A]"].data,
"Voltage [V]": corrupt_values,
}
)
We select the parameters for estimation and set up their prior distributions and bounds:
parameters = [
pybop.Parameter(
"Negative electrode active material volume fraction",
prior=pybop.Gaussian(0.6, 0.02),
bounds=[0.5, 0.8],
),
pybop.Parameter(
"Positive electrode active material volume fraction",
prior=pybop.Gaussian(0.48, 0.02),
bounds=[0.4, 0.7],
),
]
With the datasets and parameters defined, we can set up the optimisation problem, its cost function, and the optimiser.
problem = pybop.FittingProblem(model, parameters, dataset)
cost = pybop.SumSquaredError(problem)
optim = pybop.Optimisation(cost, optimiser=pybop.SciPyDifferentialEvolution)
optim.set_max_iterations(400)
We proceed to run the Differential Evolution optimisation algorithm to estimate the parameters:
x, final_cost = optim.run()
Ignoring x0. Initial conditions are not used for differential_evolution.
After the optimisation, we can examine the estimated parameter values:
x # This will output the estimated parameters
array([0.74999764, 0.66481528])
PyBOP provides various plotting utilities to visualise the results of the optimisation.
We can quickly plot the system's response using the estimated parameters compared to the target:
pybop.quick_plot(x, cost, title="Optimised Comparison");
To assess the optimisation process, we can plot the convergence of the cost function and the trajectories of the parameters:
pybop.plot_convergence(optim)
pybop.plot_parameters(optim);
Finally, we can visualise the cost landscape and the path taken by the optimiser:
# Plot the cost landscape
pybop.plot_cost2d(cost, steps=15)
# Plot the cost landscape with optimisation path and updated bounds
bounds = np.array([[0.6, 0.9], [0.5, 0.8]])
pybop.plot_cost2d(cost, optim=optim, bounds=bounds, steps=15);
This notebook illustrates how to perform parameter estimation using Differential Evolution method in PyBOP, providing insights into the optimisation process through various visualisations.