In this notebook, we demonstrate an example of parameter estimation for a single-particle model using the Covariance Matrix Adaptation Evolution Strategy (CMA-ES). CMA-ES is an evolutionary algorithm for difficult non-linear, non-convex optimisation problems.
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 /home/engs2510/.pyenv/versions/pybop/lib/python3.11/site-packages (24.0) Requirement already satisfied: ipywidgets in /home/engs2510/.pyenv/versions/pybop/lib/python3.11/site-packages (8.1.2) Requirement already satisfied: comm>=0.1.3 in /home/engs2510/.pyenv/versions/pybop/lib/python3.11/site-packages (from ipywidgets) (0.2.2) Requirement already satisfied: ipython>=6.1.0 in /home/engs2510/.pyenv/versions/pybop/lib/python3.11/site-packages (from ipywidgets) (8.23.0) Requirement already satisfied: traitlets>=4.3.1 in /home/engs2510/.pyenv/versions/pybop/lib/python3.11/site-packages (from ipywidgets) (5.14.2) Requirement already satisfied: widgetsnbextension~=4.0.10 in /home/engs2510/.pyenv/versions/pybop/lib/python3.11/site-packages (from ipywidgets) (4.0.10) Requirement already satisfied: jupyterlab-widgets~=3.0.10 in /home/engs2510/.pyenv/versions/pybop/lib/python3.11/site-packages (from ipywidgets) (3.0.10) Requirement already satisfied: decorator in /home/engs2510/.pyenv/versions/pybop/lib/python3.11/site-packages (from ipython>=6.1.0->ipywidgets) (5.1.1) Requirement already satisfied: jedi>=0.16 in /home/engs2510/.pyenv/versions/pybop/lib/python3.11/site-packages (from ipython>=6.1.0->ipywidgets) (0.19.1) Requirement already satisfied: matplotlib-inline in /home/engs2510/.pyenv/versions/pybop/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 /home/engs2510/.pyenv/versions/pybop/lib/python3.11/site-packages (from ipython>=6.1.0->ipywidgets) (3.0.43) Requirement already satisfied: pygments>=2.4.0 in /home/engs2510/.pyenv/versions/pybop/lib/python3.11/site-packages (from ipython>=6.1.0->ipywidgets) (2.17.2) Requirement already satisfied: stack-data in /home/engs2510/.pyenv/versions/pybop/lib/python3.11/site-packages (from ipython>=6.1.0->ipywidgets) (0.6.3) Requirement already satisfied: typing-extensions in /home/engs2510/.pyenv/versions/pybop/lib/python3.11/site-packages (from ipython>=6.1.0->ipywidgets) (4.10.0) Requirement already satisfied: pexpect>4.3 in /home/engs2510/.pyenv/versions/pybop/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 /home/engs2510/.pyenv/versions/pybop/lib/python3.11/site-packages (from jedi>=0.16->ipython>=6.1.0->ipywidgets) (0.8.3) Requirement already satisfied: ptyprocess>=0.5 in /home/engs2510/.pyenv/versions/pybop/lib/python3.11/site-packages (from pexpect>4.3->ipython>=6.1.0->ipywidgets) (0.7.0) Requirement already satisfied: wcwidth in /home/engs2510/.pyenv/versions/pybop/lib/python3.11/site-packages (from prompt-toolkit<3.1.0,>=3.0.41->ipython>=6.1.0->ipywidgets) (0.2.13) Requirement already satisfied: executing>=1.2.0 in /home/engs2510/.pyenv/versions/pybop/lib/python3.11/site-packages (from stack-data->ipython>=6.1.0->ipywidgets) (2.0.1) Requirement already satisfied: asttokens>=2.1.0 in /home/engs2510/.pyenv/versions/pybop/lib/python3.11/site-packages (from stack-data->ipython>=6.1.0->ipywidgets) (2.4.1) Requirement already satisfied: pure-eval in /home/engs2510/.pyenv/versions/pybop/lib/python3.11/site-packages (from stack-data->ipython>=6.1.0->ipywidgets) (0.2.2) Requirement already satisfied: six>=1.12.0 in /home/engs2510/.pyenv/versions/pybop/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 numpy as np
import pybop
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.7, 0.05),
bounds=[0.6, 0.9],
),
pybop.Parameter(
"Positive electrode active material volume fraction",
prior=pybop.Gaussian(0.58, 0.05),
bounds=[0.5, 0.8],
),
]
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.CMAES)
optim.set_max_iterations(100)
We proceed to run the CMA-ES optimisation algorithm to estimate the parameters:
x, final_cost = optim.run()
After the optimisation, we can examine the estimated parameter values:
x # This will output the estimated parameters
array([0.7516964 , 0.66486237])
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(problem, parameter_values=x, 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.plot2d(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.plot2d(optim, bounds=bounds, steps=15);
This notebook illustrates how to perform parameter estimation using CMA-ES in PyBOP, providing insights into the optimisation process through various visualisations.