In this notebook, we demonstrate an example of parameter estimation for a single-particle model using the AdamW optimiser [1][2]. The AdamW optimiser is an algorithm for gradient-based optimisation, combining the advantages of the Adaptive Gradient Algorithm (AdaGrad) and Root Mean Square Propagation (RMSProp).
[1]: Adam: A Method for Stochastic Optimization
[2]: Decoupled Weight Decay Regularization
Before we begin, we need to ensure that we have all the necessary tools. We will install PyBOP and upgrade dependencies:
%pip install --upgrade pip ipywidgets -q
%pip install pybop -q
/Users/engs2510/Documents/Git/Second_PyBOP/.nox/notebooks-overwrite/bin/python3: No module named pip Note: you may need to restart the kernel to use updated packages. /Users/engs2510/Documents/Git/Second_PyBOP/.nox/notebooks-overwrite/bin/python3: No module named pip 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
pybop.plot.PlotlyManager().pio.renderers.default = "notebook_connected"
Let's fix the random seed in order to generate consistent output during development, although this does not need to be done in practice.
np.random.seed(8)
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.AdamW)
optim.set_max_unchanged_iterations(40)
optim.set_max_iterations(150)
NOTE: Boundaries ignored by AdamW
We proceed to run the AdamW optimisation algorithm to estimate the parameters:
results = optim.run()
Halt: Maximum number of iterations (150) reached. OptimisationResult: Initial parameters: [0.60593931 0.46706684] Optimised parameters: [0.76335438 0.66225687] Final cost: 0.0004830773369301502 Optimisation time: 14.283170223236084 seconds Number of iterations: 150 SciPy result available: No
After the optimisation, we can examine the estimated parameter values:
results.x # This will output the estimated parameters
array([0.76335438, 0.66225687])
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.plot.quick(problem, problem_inputs=results.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 with updated bounds
bounds = np.asarray([[0.6, 0.9], [0.5, 0.8]])
pybop.plot.surface(optim, bounds=bounds);
This notebook illustrates how to perform parameter estimation using AdamW in PyBOP, providing insights into the optimisation process through various visualisations.