Implements the AdamW optimisation algorithm in PyBOP.
This class extends the AdamW optimiser, which is a variant of the Adam
optimiser that incorporates weight decay. AdamW is designed to be more
robust and stable for training deep neural networks, particularly when
using larger learning rates.
Note that this optimiser does not support boundary constraints.
Parameters:
cost (callable) – The cost function to be minimised.
max_iterations (int, optional) – Maximum number of iterations for the optimisation.
min_iterations (int, optional (default=2)) – Minimum number of iterations before termination.
max_unchanged_iterations (int, optional (default=15)) – Maximum number of iterations without improvement before termination.
multistart (int, optional (default=1)) – Number of optimiser restarts from randomly sample position. These positions
are sampled from the priors.
parallel (bool, optional (default=False)) – Whether to run the optimisation in parallel.
**optimiser_kwargs (optional) –
Valid PINTS option keys and their values, for example:
x0 : array_like
Initial position from which optimisation will start.
sigma0float
Initial step size or standard deviation depending on the optimiser.
boundsdict
A dictionary with ‘lower’ and ‘upper’ keys containing arrays for lower and
upper bounds on the parameters.
AdamW optimiser (adaptive moment estimation with weight decay), as described in [1]_.
This method is an extension of the Adam optimiser that introduces weight decay,
which helps to regularise the weights and prevent overfitting.
This class reimplements the Pints’ Adam Optimiser, but with the weight decay
functionality mentioned above. Original creation and credit is attributed to Pints.
Pseudo-code is given below. Here the value of the j-th parameter at
iteration i is given as p_j[i] and the corresponding derivative is
denoted g_j[i]:
The initial values of the moments are m_j[0]=v_j[0]=0, after which
they decay with rates beta1 and beta2. The default values for these are,
beta1=0.9 and beta2=0.999.
The terms m_j' and v_j' are “initialisation bias corrected”
versions of m_j and v_j (see section 2 of the paper).
The parameter alpha is a step size, which is set as min(sigma0) in
this implementation.
The parameter lam is the weight decay rate, which is set to 0.01
by default in this implementation.
Finally, eps is a small constant used to avoid division by zero, set to
``eps = np.finfo(float).eps in this implementation.
This is an unbounded method: Any boundaries will be ignored.
Implements the Adaptive Covariance Markov Chain Monte Carlo (MCMC) algorithm.
This class extends the Adaptive Covariance MCMC sampler from the PINTS library.
This MCMC method adapts the proposal distribution covariance matrix
during the sampling process to improve efficiency and convergence.
problem (object) – A problem instance containing the data and functions necessary for
evaluating the cost function.
target (array-like) – An array containing the target data to fit.
n_outputs (int) – The number of outputs in the model.
has_separable_problem (bool) – If True, the problem is separable from the cost function and will be
evaluated in advance of the call to self.compute() (default: False).
_de (float) – The gradient of the cost function to use if an error occurs during
evaluation. Defaults to 1.0.
minimising (bool, optional) – If False, tells the optimiser to switch the sign of the cost and gradient
to maximise by default rather than minimise (default: True).
Compute cost and optional gradient for given input parameters.
Parameters:
inputs (Union[Inputs, list, np.ndarray]) – Input parameters for cost computation. Supports list-like evaluation of
multiple input values, shaped [N,M] where N is the number of input positions
to evaluate and M is the number of inputs for the underlying model (i.e. parameters).
calculate_grad (bool, default=False) – If True, both the cost and gradient will be computed. Otherwise, only the
cost is computed.
Returns:
Single input, no gradient: float
Multiple inputs, no gradient: list[float]
Single input with gradient: tuple[float, np.ndarray]
Multiple inputs with gradient: list[tuple[float, np.ndarray]]
Compute the observed Fisher Information Matrix (FIM) for the given data.
The Fisher information is computed as the outer product of the gradients with respect to
the model parameters, scaled by the inverse of the dataset size. This method should only
be used with exponential-based likelihood functions.
Parameters:
inputs (Union[dict[str, float], list-like]) – Input data for model evaluation.
A base class for constructing and simulating models using PyBaMM.
This class serves as a foundation for constructing models based on PyBaMM models. It
provides methods to set up the model, define parameters, and perform simulations. The
class is designed to be subclassed for creating models with custom behaviour.
This class is based on PyBaMM’s Simulation class. A PyBOP model is set up via a
similar 3-step process. The pybamm_model attributes echoes the model attribute of
a simulation, which tracks the model through the build process. Firstly, note that a
PyBaMM model must first be built via build_model before a simulation or PyBOP
model can be built. The 3-step process is then as follows.
The pybamm_model attribute is first defined as an instance of the imported PyBaMM
model, using any given model options. This initial version of the model is saved as
the _unprocessed_model for future reference. Next, the type of each parameter in
the parameter set as well as the geometry of the model is set. Parameters may be set
as an input, interpolant, functional or just a standard PyBaMM parameter. This
version of the model is referred to as the model_with_set_params. After its
creation, the pybamm_model attribute is updated to point at this version of the
model. Finally, the model required for simulations is built by defining the mesh and
processing the discretisation. The complete model is referred to as the built_model
and this version is used to run simulations.
In order to rebuild a model with a different initial state or geometry, the
built_model and the model_with_set_params must be cleared and the pybamm_model
reset to the unprocessed_model in order to start the build process again.
Construct the PyBaMM model, if not already built or if there are changes to any
rebuild_parameters or the initial state.
This method initializes the model components, applies the given parameters,
sets up the mesh and discretisation if needed, and prepares the model
for simulations.
Parameters:
parameters (pybop.Parameters or Dict, optional) – A pybop Parameters class or dictionary containing parameter values to apply to the model.
inputs (Inputs) – The input parameters to be used when building the model.
initial_state (dict, optional) – A valid initial state, e.g. the initial state of charge or open-circuit voltage.
Defaults to None, indicating that the existing initial state of charge (for an ECM)
or initial concentrations (for an EChem model) will be used.
Accepted keys either “Initial open-circuit voltage [V]” or ``”Initial SoC”`
dataset (pybop.Dataset or dict, optional) – The dataset to be used in the model construction.
check_model (bool, optional) – If True, the model will be checked for correctness after construction.
Convert an initial state of charge into a float and an initial open-circuit
voltage into a string ending in “V”.
Parameters:
initial_state (dict) – A valid initial state, e.g. the initial state of charge or open-circuit voltage.
Returns:
If float, this value is used as the initial state of charge (as a decimal between 0
and 1). If str ending in “V”, this value is used as the initial open-circuit voltage.
Return type:
float or str
Raises:
ValueError – If the input is not a dictionary with a single, valid key.
Initialise the Electrochemical Impedance Spectroscopy (EIS) simulation.
This method sets up the mass matrix and solver, converts inputs to the appropriate format,
extracts necessary attributes from the model, and prepares matrices for the simulation.
Parameters:
inputs (dict (optional)) – The input parameters for the simulation.
Solve the model using PyBaMM’s simulation framework and return the solution.
This method sets up a PyBaMM simulation by configuring the model, parameters, experiment
or time vector, and initial state of charge (if provided). Either ‘t_eval’ or ‘experiment’
must be provided. It then solves the simulation and returns the resulting solution object.
Parameters:
inputs (Inputs, optional) – Input parameters for the simulation. Defaults to None, indicating that the
default parameters should be used.
t_eval (array-like, optional) – An array of time points at which to evaluate the solution. Defaults to None,
which means the time points need to be specified within experiment or elsewhere.
parameter_set (Union[pybop.ParameterSet, pybamm.ParameterValues], optional) – A dict-like object containing the parameter values to use for the simulation.
Defaults to the model’s current ParameterValues if None.
experiment (pybamm.Experiment, optional) – A PyBaMM Experiment object specifying the experimental conditions under which
the simulation should be run. Defaults to None, indicating no experiment.
initial_state (dict, optional) – A valid initial state, e.g. the initial state of charge or open-circuit voltage.
Defaults to None, indicating that the existing initial state of charge (for an ECM)
or initial concentrations (for an EChem model) will be used.
Returns:
The solution object returned by a PyBaMM simulation, or a pybamm error in the case
where the parameter values are infeasible and infeasible solutions are not allowed.
Return type:
pybamm.Solution
Raises:
ValueError – If the model has not been configured properly before calling this method or
if PyBaMM models are not supported by the current simulation method.
Set up the model for electrochemical impedance spectroscopy (EIS) simulations.
This method sets up the model for EIS simulations by adding the necessary
algebraic equations and variables to the model.
Originally developed by pybamm-eis: pybamm-team/pybamm-eis
Parameters:
model (pybamm.Model) – The PyBaMM model to be used for EIS simulations.
Execute the forward model simulation and return the result.
Parameters:
inputs (Inputs) – The input parameters for the simulation.
t_eval (array-like) – An array of time points at which to evaluate the solution.
initial_state (dict, optional) – A valid initial state, e.g. the initial state of charge or open-circuit voltage.
Defaults to None, indicating that the existing initial state of charge (for an ECM)
or initial concentrations (for an EChem model) will be used.
Returns:
The solution object returned by a PyBaMM simulation, or a pybamm error in the case
where the parameter values are infeasible and infeasible solutions are not allowed.
Return type:
pybamm.Solution
Raises:
ValueError – If the model has not been built before simulation.
Perform the forward model simulation with sensitivities.
Parameters:
inputs (Inputs) – The input parameters for the simulation.
t_eval (array-like) – An array of time points at which to evaluate the solution and its
sensitivities.
initial_state (dict, optional) – A valid initial state, e.g. the initial state of charge or open-circuit voltage.
Defaults to None, indicating that the existing initial state of charge (for an ECM)
or initial concentrations (for an EChem model) will be used.
Returns:
The solution object returned by a PyBaMM simulation, or a pybamm error in the case
where the parameter values are infeasible and infeasible solutions are not allowed.
Return type:
pybamm.Solution
Raises:
ValueError – If the model has not been built before simulation.
A base class for defining optimisation methods. Optimisers perform minimisation of the cost
function; maximisation may be performed instead using the option invert_cost=True.
This class serves as a base class for creating optimisers. It provides a basic structure for
an optimisation algorithm, including the initial setup and a method stub for performing the
optimisation process. Child classes should override _set_up_optimiser and the _run method with
a specific algorithm.
Parameters:
cost (pybop.BaseCost or callable) – An objective function to be optimised, which can be either a pybop.Cost or callable function.
**optimiser_kwargs (optional) –
Valid option keys and their values, for example:
x0 : numpy.ndarray
Initial values of the parameters for the optimisation.
boundsdict
Dictionary containing bounds for the parameters with keys ‘lower’ and ‘upper’.
sigma0float or sequence
Initial step size or standard deviation in the (search) parameters. Either a scalar value
(same for all coordinates) or an array with one entry per dimension.
Not all methods will use this information.
verbosebool, optional
If True, the optimisation progress and final result is printed (default: False).
verbose_print_rateint, optional
The frequency in iterations to print the optimisation progress (default: 50).
physical_viabilitybool, optional
If True, the feasibility of the optimised parameters is checked (default: False).
allow_infeasible_solutionsbool, optional
If True, infeasible parameter values will be allowed in the optimisation (default: True).
Set the maximum number of iterations without significant change as a stopping criterion.
Credit: PINTS
Parameters:
iterations (int, optional) – The maximum number of unchanged iterations to run (default: 15).
Set to None to remove this stopping criterion.
absolute_tolerance (float, optional) – The minimum significant change (absolute tolerance) in the objective function value
that resets the unchanged iteration counter (default: 1e-5).
relative_tolerance (float, optional) – The minimum significant proportional change (relative tolerance) in the objective function
value that resets the unchanged iteration counter (default: 1e-2).
Enable or disable parallel evaluation.
Credit: PINTS
Parameters:
parallel (bool or int, optional) – If True, use as many worker processes as there are CPU cores. If an integer, use that many workers.
If False or 0, disable parallelism (default: False).
Adds a stopping criterion, allowing the routine to halt once the
objective function goes below a set threshold.
This criterion is disabled by default, but can be enabled by calling
this method with a valid threshold. To disable it, use
set_threshold(None).
Credit: PINTS
Parameters:
threshold (float, optional) – The threshold below which the objective function value is considered optimal
(default: None).
This class extends the BaseSampler class to provide a common interface for
PINTS samplers. The class provides a sample() method that can be used to
sample from the posterior distribution using a PINTS sampler.
sampler (pybop.Sampler) – The sampling algorithm to use.
chains (int) – Number of chains to run concurrently. Each chain contains separate markov samples from
the log_pdf.
x0 (numpy.ndarray) – Initial values of the parameters for the optimisation.
cov0 (list-like) – Initial covariance for the chains in the parameters. Either a scalar value
(same for all coordinates) or an array with one entry per dimension.
Executes the Monte Carlo sampling process and generates samples
from the posterior distribution.
This method orchestrates the entire sampling process, managing
iterations, evaluations, logging, and stopping criteria. It
initialises the necessary structures, handles both single and
multi-chain scenarios, and manages parallel or sequential
evaluation based on the configuration.
Returns:
A numpy array containing the samples from the
posterior distribution if chains are stored in memory,
otherwise returns None.
Return type:
np.ndarray
Raises:
ValueError – If no stopping criterion is set (i.e.,
_max_iterations is None). –
Details:
Checks and ensures at least one stopping criterion is set.
Initialises iterations, evaluations, and other required
structures.
- Sets up the evaluator (parallel or sequential) based on the
configuration.
- Handles the initial phase, if applicable, and manages
intermediate steps in the sampling process.
- Logs progress and relevant information based on the logging
configuration.
- Iterates through the sampling process, evaluating the log
PDF, updating chains, and managing the stopping criteria.
- Finalises and returns the collected samples, or None if
chains are not stored in memory.
This class provides a foundation for implementing various prior distributions.
It includes methods for calculating the probability density function (PDF),
log probability density function (log PDF), and generating random variates
from the distribution.
Enable or disable parallel evaluation.
Credit: PINTS
Parameters:
parallel (bool or int, optional) – If True, use as many worker processes as there are CPU cores. If an integer, use that many workers.
If False or 0, disable parallelism (default: False).
Adapter for the Covariance Matrix Adaptation Evolution Strategy (CMA-ES) optimiser in PINTS.
CMA-ES is an evolutionary algorithm for difficult non-linear non-convex optimisation problems.
It adapts the covariance matrix of a multivariate normal distribution to capture the shape of
the cost landscape.
Parameters:
cost (callable) – The cost function to be minimised.
max_iterations (int, optional) – Maximum number of iterations for the optimisation.
min_iterations (int, optional (default=2)) – Minimum number of iterations before termination.
max_unchanged_iterations (int, optional (default=15)) – Maximum number of iterations without improvement before termination.
multistart (int, optional (default=1)) – Number of optimiser restarts from randomly sample position. These positions
are sampled from the priors.
parallel (bool, optional (default=False)) – Whether to run the optimisation in parallel.
**optimiser_kwargs (optional) –
Valid PINTS option keys and their values, for example:
x0 : array_like
Initial position from which optimisation will start.
sigma0float
Initial step size or standard deviation depending on the optimiser.
boundsdict
A dictionary with ‘lower’ and ‘upper’ keys containing arrays for lower and
upper bounds on the parameters.
Provides the interface between the cost and the optimiser.
Applies any transformation to the input parameter values, calls the cost function, inverts
the sign of the cost and gradient if the target is maximisation and reverses the effect of
any transformation on the gradient.
Parameters:
x (Inputs or list-like) – The input parameters for which the cost and optionally the gradient will be computed.
cost (pybop.BaseCost or callable) – The objective to be optimised, which can be either a pybop.Cost or callable function.
calculate_grad (bool, optional, default=False) – If True, both cost and gradient will be computed. Otherwise, only the cost is computed.
Returns:
If calculate_grad is False, returns the computed cost (float).
If calculate_grad is True, returns a tuple containing the cost (float)
and the gradient (np.ndarray).
Cuckoo Search is a population-based optimisation algorithm inspired by the brood parasitism of some cuckoo species.
It is designed to be simple, efficient, and robust, and is suitable for global optimisation problems.
Parameters:
cost (callable) – The cost function to be minimised.
max_iterations (int, optional) – Maximum number of iterations for the optimisation.
min_iterations (int, optional (default=2)) – Minimum number of iterations before termination.
max_unchanged_iterations (int, optional (default=15)) – Maximum number of iterations without improvement before termination.
multistart (int, optional (default=1)) – Number of optimiser restarts from randomly sample position. These positions
are sampled from the priors.
parallel (bool, optional (default=False)) – Whether to run the optimisation in parallel.
**optimiser_kwargs (optional) –
Valid PINTS option keys and their values, for example:
x0 : array_like
Initial position from which optimisation will start.
sigma0float
Initial step size or standard deviation depending on the optimiser.
boundsdict
A dictionary with ‘lower’ and ‘upper’ keys containing arrays for lower and
upper bounds on the parameters.
Cuckoo Search (CS) optimisation algorithm, inspired by the brood parasitism
of some cuckoo species. This algorithm was introduced by Yang and Deb in 2009.
The algorithm uses a population of host nests (solutions), where each cuckoo
(new solution) tries to replace a worse nest in the population. The quality
or fitness of the nests is determined by the cost function. A fraction
of the worst nests is abandoned at each generation, and new ones are built
randomly.
The pseudo-code for the Cuckoo Search is as follows:
Initialise population of n host nests
While (t < max_generations):
Get a cuckoo randomly by Lévy flights
Evaluate its quality/fitness F
Choose a nest among n (say, j) randomly
If (F > fitness of j):
Replace j with the new solution
Abandon a fraction (pa) of the worst nests and build new ones
Keep the best solutions/nests
Rank the solutions and find the current best
End While
This implementation also uses a decreasing step size for the Lévy flights, calculated
as sigma = sigma0 / sqrt(iterations), where sigma0 is the initial step size and
iterations is the current iteration number.
Parameters:
- pa: Probability of discovering alien eggs/solutions (abandoning rate)
References:
- X. -S. Yang and Suash Deb, “Cuckoo Search via Lévy flights,”
Implements the DiffeRential Evolution Adaptive Metropolis (DREAM) algorithm.
This class extends the DREAM sampler from the PINTS library.
DREAM is a Markov chain Monte Carlo (MCMC) method for sampling
from a probability distribution. It combines the Differential
Evolution (DE) algorithm with the Adaptive Metropolis (AM) algorithm
to explore the parameter space more efficiently.
experiment (object) – The experimental setup to apply the model to.
check_model (bool, optional) – Flag to indicate if the model parameters should be checked for feasibility each iteration (default: True).
signal (list[str], optional) – A list of variables to analyse (default: [“Voltage [V]”]).
domain (str, optional) – The name of the domain (default: “Time [s]”).
additional_variables (list[str], optional) – Additional variables to observe and store in the solution (default additions are: [“Time [s]”, “Current [A]”]).
initial_state (dict, optional) – A valid initial state (default: {“Initial SoC”: 1.0}).
update_capacity (bool, optional) – If True, the nominal capacity is updated with an approximate value for each design.
Implements the Differential Evolution Markov Chain Monte Carlo (MCMC) algorithm.
This class extends the Differential Evolution MCMC sampler from the PINTS library.
This MCMC method uses the Differential Evolution algorithm to explore the
parameter space more efficiently by evolving a population of chains.
Implements the Delayed Rejection Adaptive Metropolis (DRAM) Adaptive Covariance Markov Chain
Monte Carlo (MCMC) algorithm.
This class extends the DRAM Adaptive Covariance MCMC sampler from the PINTS library.
This MCMC method combines Delayed Rejection with Adaptive Metropolis to enhance
the efficiency and robustness of the sampling process.
Implements the Emcee Hammer Markov Chain Monte Carlo (MCMC) algorithm.
This class extends the Emcee Hammer MCMC sampler from the PINTS library.
The Emcee Hammer is an affine-invariant ensemble sampler for MCMC, which is
particularly effective for high-dimensional parameter spaces.
Light wrapper of the PyBaMM Experiment class for generating experiment conditions for PyBaMM models.
Credit: PyBaMM
Base class for experimental conditions under which to run the model. In general, a
list of operating conditions should be passed in. Each operating condition should
be either a pybamm.step._Step class, created using one of the methods
pybamm.step.current, pybamm.step.c_rate, pybamm.step.voltage
, pybamm.step.power, pybamm.step.resistance, or
pybamm.step.string, or a string, in which case the string is passed to
pybamm.step.string.
Parameters:
operating_conditions (list) – List of operating conditions
period (string, optional) – Period (1/frequency) at which to record outputs. Default is 1 minute. Can be
overwritten by individual operating conditions.
temperature (float, optional) – The ambient air temperature in degrees Celsius at which to run the experiment.
Default is None whereby the ambient temperature is taken from the parameter set.
This value is overwritten if the temperature is specified in a step.
termination (list, optional) – List of conditions under which to terminate the experiment. Default is None.
This is different from the termination for individual steps. Termination for
individual steps is specified in the step itself, and the simulation moves to
the next step when the termination condition is met
(e.g. 2.5V discharge cut-off). Termination for the
experiment as a whole is specified here, and the simulation stops when the
termination condition is met (e.g. 80% capacity).
Overwrites and extends BaseCost class for fitting-type cost functions.
This class is intended to be subclassed to create specific cost functions
for evaluating model predictions against a set of data. The cost function
quantifies the goodness-of-fit between the model predictions and the
observed data, with a lower cost value indicating a better fit.
Computes the cost function for the given predictions.
Parameters:
r (np.ndarray) – The residual difference between the model prediction and the target. The
dimensions of r are (len(signal), len(domain_data)).
dy (np.ndarray, optional) – The corresponding gradient with respect to the parameters for each signal.
The dimensions of dy are (len(parameters), len(signal), len(domain_data)).
Returns:
If dy is not None, returns a tuple containing the cost (float) and the
gradient with dimension (len(parameters)), otherwise returns only the cost.
Return type:
np.float64 or tuple[np.float64, np.ndarray[np.float64]]
dataset (dictionary) – Dataset object containing the data to fit the model to.
check_model (bool, optional) – Flag to indicate if the model should be checked (default: True).
signal (list[str], optional) – A list of variables to fit (default: [“Voltage [V]”]).
domain (str, optional) – The name of the domain (default: “Time [s]”).
additional_variables (list[str], optional) – Additional variables to observe and store in the solution (default additions are: [“Time [s]”]).
initial_state (dict, optional) – A valid initial state, e.g. the initial open-circuit voltage (default: None) which will trigger
a model rebuild on each evaluation. Example: {“Initial open-circuit potential [V]”: 4.1}
NOTE: Sensitivities are not support with this arg due to the model rebuilding.
Attributes (Additional)
---------------------
dataset – The dictionary from a Dataset object containing the signal keys and values to fit the model to.
domain_data (np.ndarray) – The domain points in the dataset.
n_domain_data (int) – The number of domain points.
target (np.ndarray) – The target values of the signals.
Represents a Gaussian (normal) distribution with a given mean and standard deviation.
This class provides methods to calculate the probability density function (pdf),
the logarithm of the pdf, and to generate random variates (rvs) from the distribution.
Parameters:
mean (float) – The mean (mu) of the Gaussian distribution.
sigma (float) – The standard deviation (sigma) of the Gaussian distribution.
This class represents a Gaussian log-likelihood, which computes the log-likelihood under
the assumption that measurement noise on the target data follows a Gaussian distribution.
This class estimates the standard deviation of the Gaussian distribution alongside the
parameters of the model.
Compute the Gaussian log-likelihood for the given parameters.
This method only computes the likelihood, without calling problem.evaluate().
Parameters:
y (dict[str, np.ndarray[np.float64]]) – The dictionary of predictions with keys designating the signals for fitting.
dy (dict[str, dict[str, np.ndarray]], optional) – The corresponding gradient with respect to each parameter for each signal.
Returns:
If dy is not None, returns a tuple containing the log-likelihood (float) and the
gradient with dimension (len(parameters)), otherwise returns only the log-likelihood.
Return type:
np.float64 or tuple[np.float64, np.ndarray[np.float64]]
This class represents a Gaussian log-likelihood with a known sigma, which computes the
log-likelihood under the assumption that measurement noise on the target data follows a
Gaussian distribution.
Parameters:
sigma0 (scalar or array) – Initial standard deviation around x0. Either a scalar value (one standard deviation
for all coordinates) or an array with one entry per dimension.
Gradient descent method with a fixed, per-dimension learning rate.
Gradient descent updates the current position in the direction of the
steepest descent, as determined by the negative of the gradient of the
function.
The update rule for each iteration is given by:
\[x_{t+1} = x_t - \eta * \nabla f(x_t)\]
where:
\(x_t\) are the current parameter values at iteration t,
\(\nabla f(x_t)\) is the gradient of the function at \(x_t\),
\(\eta\) is the learning rate, which controls the step size.
This class reimplements the Pints’ Gradient Descent, but with multidimensional,
fixed learning rates. Original creation and credit is attributed to Pints.
Parameters:
x0 (array-like) – Initial starting point for the optimisation. This should be a 1D array
representing the starting parameter values for the function being
optimised.
sigma0 (float or array-like, optional) – Initial learning rate or rates for each dimension. If a scalar is
provided, the same learning rate is applied across all dimensions.
If an array is provided, each dimension will have its own learning
rate. Defaults to 0.02.
boundaries (pybop.Boundaries, optional) – Boundaries for the parameters. This optimiser ignores boundaries and
operates as an unbounded method. Defaults to None.
Calculates the gravimetric energy density (specific energy) of a battery cell,
when applied to a normalised discharge from upper to lower voltage limits. The
goal of maximising the energy density is achieved with self.minimising=False.
The gravimetric energy density [Wh.kg-1] is calculated as
where m is the cell mass, t is the time, T is the total time, I is the current
and V is the voltage. The factor of 1/3600 is included to convert from seconds
to hours.
Inherits all parameters and attributes from DesignCost.
Calculates the gravimetric power density (specific power) of a battery cell,
when applied to a discharge from upper to lower voltage limits. The goal of
maximising the power density is achieved with self.minimising=False.
The time-averaged gravimetric power density [W.kg-1] is calculated as
\[\frac{1}{3600 m T} \int_{t=0}^{t=T} I(t) V(t) \mathrm{d}t\]
where m is the cell mass, t is the time, T is the total time, I is the current
and V is the voltage. The factor of 1/3600 is included to convert from seconds
to hours.
Inherits all parameters and attributes from DesignCost.
Implements the Haario Adaptive Covariance Markov Chain Monte Carlo (MCMC) algorithm.
This class extends the Haario Adaptive Covariance MCMC sampler from the PINTS library.
This MCMC method adapts the proposal distribution’s covariance matrix based on the
history of the chain, improving sampling efficiency and convergence.
Implements the Haario-Bardenet Adaptive Covariance Markov Chain Monte Carlo (MCMC) algorithm.
This class extends the Haario-Bardenet Adaptive Covariance MCMC sampler from the PINTS library.
This MCMC method combines the adaptive covariance approach with an additional
mechanism to improve performance in high-dimensional parameter spaces.
Implements the Hamiltonian Markov Chain Monte Carlo (MCMC) algorithm.
This class extends the Hamiltonian MCMC sampler from the PINTS library.
This MCMC method uses Hamiltonian dynamics to propose new states,
allowing for efficient exploration of high-dimensional parameter spaces.
This class inherits from the PINTS IRPropMin class, which is an optimiser that
uses resilient backpropagation without weight-backtracking. It is designed to handle
problems with large plateaus, noisy gradients, and local minima.
Parameters:
cost (callable) – The cost function to be minimised.
max_iterations (int, optional) – Maximum number of iterations for the optimisation.
min_iterations (int, optional (default=2)) – Minimum number of iterations before termination.
max_unchanged_iterations (int, optional (default=15)) – Maximum number of iterations without improvement before termination.
multistart (int, optional (default=1)) – Number of optimiser restarts from randomly sample position. These positions
are sampled from the priors.
parallel (bool, optional (default=False)) – Whether to run the optimisation in parallel.
**optimiser_kwargs (optional) –
Valid PINTS option keys and their values, for example:
x0 : array_like
Initial position from which optimisation will start.
sigma0float
Initial step size or standard deviation depending on the optimiser.
boundsdict
A dictionary with ‘lower’ and ‘upper’ keys containing arrays for lower and
upper bounds on the parameters.
This class implements the improved resilient backpropagation with weight-backtracking.
It is designed to handle problems with large plateaus, noisy gradients, and local minima.
Parameters:
cost (callable) – The cost function to be minimized.
max_iterations (int, optional) – Maximum number of iterations for the optimisation.
min_iterations (int, optional (default=2)) – Minimum number of iterations before termination.
max_unchanged_iterations (int, optional (default=15)) – Maximum number of iterations without improvement before termination.
multistart (int, optional (default=1)) – Number of optimiser restarts from randomly sample position. These positions
are sampled from the priors.
parallel (bool, optional (default=False)) – Whether to run the optimisation in parallel.
**optimiser_kwargs (optional) –
Valid PINTS option keys and their values, for example:
x0 : array_like
Initial position from which optimisation will start.
sigma0float
Initial step size or standard deviation depending on the optimiser.
boundsdict
A dictionary with ‘lower’ and ‘upper’ keys containing arrays for lower and
upper bounds on the parameters.
The iRprop+ algorithm adjusts step sizes based on the sign of the gradient
in each dimension, increasing step sizes when the sign remains consistent
and decreasing when the sign changes. This implementation includes
weight (parameter) backtracking that reverts the previous step in the
event of a gradient sign changing.
References:
- [1] Igel and Hüsken (2003): Empirical Evaluation of Improved Rprop Algorithms.
- [2] Riedmiller and Braun (1993): A Direct Adaptive Method for Faster Backpropagation.
- [3] Igel and Husk (2003): Improving the Rprop Learning Algorithm.
Parameters:
x0 (array-like) – Initial starting point for the optimisation.
sigma0 (float or array-like, optional) – Initial step size(s). If a scalar is provided, it is applied to all dimensions.
Default is 0.05.
boundaries (pints.Boundaries, optional) – Boundary constraints for the optimisation. If None, no boundaries are applied.
Evaluates the first derivative of the joint log-prior distribution at a given point.
Parameters:
x (Union[float, np.ndarray]) – The point(s) at which to evaluate the first derivative. The length of x
should match the total number of parameters in the joint distribution.
Returns:
A tuple containing the log-probability density and its first derivative at x.
Evaluates the joint log-prior distribution at a given point.
Parameters:
x (Union[float, np.ndarray]) – The point(s) at which to evaluate the distribution. The length of x
should match the total number of parameters in the joint distribution.
Returns:
The joint log-probability density of the distribution at x.
Computes the log-posterior which is proportional to the sum of the log-likelihood and the
log-prior.
Parameters:
log_likelihood (BaseLikelihood) – The likelihood class of type BaseLikelihood.
log_prior (Optional, Union[pybop.BasePrior, stats.rv_continuous]) – The prior class of type BasePrior or stats.rv_continuous.
If not provided, the prior class will be taken from the parameter priors
constructed in the pybop.Parameters class.
gradient_step (float, default: 1e-3) – The step size for the finite-difference gradient calculation
if the log_prior is not of type BasePrior.
Calculate the posterior cost for a given forward model prediction.
Parameters:
y (dict[str, np.ndarray[np.float64]]) – The dictionary of predictions with keys designating the signals for fitting.
dy (dict[str, dict[str, np.ndarray]], optional) – The corresponding sensitivities to each parameter for each signal.
Returns:
If dy is not None, returns a tuple containing the log-likelihood (float) and the
gradient with dimension (len(parameters)), otherwise returns only the log-likelihood.
Return type:
np.float64 or tuple[np.float64, np.ndarray[np.float64]]
Implements the Metropolis Adjusted Langevin Algorithm (MALA) Markov Chain Monte Carlo (MCMC) algorithm.
This class extends the MALA MCMC sampler from the PINTS library.
This MCMC method combines the Metropolis-Hastings algorithm with
Langevin dynamics to improve sampling efficiency and convergence.
This class provides an alternative API to the PyBOP.Sampler() API,
specifically allowing for single user-friendly interface for the
optimisation process.
Computes the mean absolute error (MAE) between model predictions
and target data. The MAE is a measure of the average magnitude
of errors in a set of predictions, without considering their direction.
Computes the cost function for the given predictions.
Parameters:
r (np.ndarray) – The residual difference between the model prediction and the target. The
dimensions of r are (len(signal), len(domain_data)).
dy (np.ndarray, optional) – The corresponding gradient with respect to the parameters for each signal.
The dimensions of dy are (len(parameters), len(signal), len(domain_data)).
Returns:
If dy is not None, returns a tuple containing the cost (float) and the
gradient with dimension (len(parameters)), otherwise returns only the cost.
Return type:
np.float64 or tuple[np.float64, np.ndarray[np.float64]]
Computes the mean square error between model predictions and the target
data, providing a measure of the differences between predicted values and
observed values.
Computes the cost function for the given predictions.
Parameters:
r (np.ndarray) – The residual difference between the model prediction and the target. The
dimensions of r are (len(signal), len(domain_data)).
dy (np.ndarray, optional) – The corresponding gradient with respect to the parameters for each signal.
The dimensions of dy are (len(parameters), len(signal), len(domain_data)).
Returns:
If dy is not None, returns a tuple containing the cost (float) and the
gradient with dimension (len(parameters)), otherwise returns only the cost.
Return type:
np.float64 or tuple[np.float64, np.ndarray[np.float64]]
Implements the Metropolis Random Walk Markov Chain Monte Carlo (MCMC) algorithm.
This class extends the Metropolis Random Walk MCMC sampler from the PINTS library.
This classic MCMC method uses a simple random walk proposal distribution
and the Metropolis-Hastings acceptance criterion.
The Minkowski distance is a generalisation of several distance metrics,
including the Euclidean and Manhattan distances. It is defined as:
\[L_p(x, y) = ( \sum_i |x_i - y_i|^p )^(1/p)\]
where p > 0 is the order of the Minkowski distance. For p ≥ 1, the
Minkowski distance is a metric. For 0 < p < 1, it is not a metric, as it
does not satisfy the triangle inequality, although a metric can be
obtained by removing the (1/p) exponent.
Special cases:
p = 1: Manhattan distance
p = 2: Euclidean distance
p → ∞: Chebyshev distance (not implemented as yet)
This class implements the Minkowski distance as a cost function for
optimisation problems, allowing for flexible distance-based optimisation
across various problem domains.
Computes the cost function for the given predictions.
Parameters:
r (np.ndarray) – The residual difference between the model prediction and the target. The
dimensions of r are (len(signal), len(domain_data)).
dy (np.ndarray, optional) – The corresponding gradient with respect to the parameters for each signal.
The dimensions of dy are (len(parameters), len(signal), len(domain_data)).
Returns:
If dy is not None, returns a tuple containing the cost (float) and the
gradient with dimension (len(parameters)), otherwise returns only the cost.
Return type:
np.float64 or tuple[np.float64, np.ndarray[np.float64]]
Implements the Monomial Gamma Hamiltonian Markov Chain Monte Carlo (MCMC) algorithm.
This class extends the Monomial Gamma Hamiltonian MCMC sampler from the PINTS library.
This MCMC method uses Hamiltonian dynamics with a monomial gamma distribution
for efficient exploration of the parameter space.
Implements the No-U-Turn Sampler (NUTS) algorithm.
This class extends the NUTS sampler from the PINTS library.
NUTS is a Markov chain Monte Carlo (MCMC) method for sampling
from a probability distribution. It is an extension of the
Hamiltonian Monte Carlo (HMC) method, which uses a dynamic
integration time to explore the parameter space more efficiently.
Implements the Nelder-Mead downhill simplex method from PINTS.
This is a deterministic local optimiser. In most update steps it performs
either one evaluation, or two sequential evaluations, so that it will not
typically benefit from parallelisation.
Note that this optimiser does not support boundary constraints.
Parameters:
cost (callable) – The cost function to be minimised.
max_iterations (int, optional) – Maximum number of iterations for the optimisation.
min_iterations (int, optional (default=2)) – Minimum number of iterations before termination.
max_unchanged_iterations (int, optional (default=15)) – Maximum number of iterations without improvement before termination.
multistart (int, optional (default=1)) – Number of optimiser restarts from randomly sample position. These positions
are sampled from the priors.
parallel (bool, optional (default=False)) – Whether to run the optimisation in parallel.
**optimiser_kwargs (optional) –
Valid PINTS option keys and their values, for example:
x0 : array_like
Initial position from which optimisation will start.
sigma0float
Initial step size or standard deviation depending on the optimiser.
boundsdict
A dictionary with ‘lower’ and ‘upper’ keys containing arrays for lower and
upper bounds on the parameters.
A high-level class for optimisation using PyBOP or PINTS optimisers.
This class provides an alternative API to the PyBOP.Optimiser() API,
specifically allowing for single user-friendly interface for the
optimisation process.The class can be used with either PyBOP or PINTS
optimisers.
Parameters:
cost (pybop.BaseCost or pints.ErrorMeasure) – An objective function to be optimized, which can be either a pybop.Cost
optimiser (pybop.Optimiser or subclass of pybop.BaseOptimiser, optional) – An optimiser from either the PINTS or PyBOP framework to perform the optimisation (default: None).
sigma0 (float or sequence, optional) – Initial step size or standard deviation for the optimiser (default: None).
verbose (bool, optional) – If True, the optimisation progress is printed (default: False).
physical_viability (bool, optional) – If True, the feasibility of the optimised parameters is checked (default: True).
allow_infeasible_solutions (bool, optional) – If True, infeasible parameter values will be allowed in the optimisation (default: True).
Implements a particle swarm optimisation (PSO) algorithm.
This class extends the PSO optimiser from the PINTS library. PSO is a
metaheuristic optimisation method inspired by the social behavior of birds
flocking or fish schooling, suitable for global optimisation problems.
Parameters:
cost (callable) – The cost function to be minimised.
max_iterations (int, optional) – Maximum number of iterations for the optimisation.
min_iterations (int, optional (default=2)) – Minimum number of iterations before termination.
max_unchanged_iterations (int, optional (default=15)) – Maximum number of iterations without improvement before termination.
multistart (int, optional (default=1)) – Number of optimiser restarts from randomly sample position. These positions
are sampled from the priors.
parallel (bool, optional (default=False)) – Whether to run the optimisation in parallel.
**optimiser_kwargs (optional) –
Valid PINTS option keys and their values, for example:
x0 : array_like
Initial position from which optimisation will start.
sigma0float
Initial step size or standard deviation depending on the optimiser.
boundsdict
A dictionary with ‘lower’ and ‘upper’ keys containing arrays for lower and
upper bounds on the parameters.
Represents a parameter within the PyBOP framework.
This class encapsulates the definition of a parameter, including its name, prior
distribution, initial value, bounds, and a margin to ensure the parameter stays
within feasible limits during optimization or sampling.
Parameters:
name (str) – The name of the parameter.
initial_value (float, optional) – The initial value to be assigned to the parameter. Defaults to None.
prior (scipy.stats distribution, optional) – The prior distribution from which parameter values are drawn. Defaults to None.
bounds (tuple, optional) – A tuple defining the lower and upper bounds for the parameter.
Defaults to None.
Raises:
ValueError – If the lower bound is not strictly less than the upper bound, or if
the margin is set outside the interval (0, 1).
Set the upper and lower bounds and applies default values
from the prior if no bounds are provided. The default values
are calculated using the boundary_multiplier and the parameters
prior sigma value.
Parameters:
bounds (tuple, optional) – A tuple defining the lower and upper bounds for the parameter.
Defaults to None.
boundary_multiplier (float, optional) – Used to define the bounds when no bounds are passed but the parameter has
a prior distribution (default: 15).
Raises:
ValueError – If the lower bound is not strictly less than the upper bound, or if
the margin is set outside the interval (0, 1).
Set the margin to a specified positive value less than 1.
The margin is used to ensure parameter samples are not drawn exactly at the bounds,
which may be problematic in some optimization or sampling algorithms.
Parameters:
margin (float) – The new margin value to be used, which must be in the interval (0, 1).
Raises:
ValueError – If the margin is not between 0 and 1.
Handles the import and export of parameter sets for battery models.
This class provides methods to load parameters from a JSON file and to export them
back to a JSON file. It also includes custom logic to handle special cases, such
as parameter values that require specific initialization.
Parameters:
parameter_set (Union[str, dict, ParameterValues], optional) – A dictionary of parameters to initialise the ParameterSet with. If not provided, an empty dictionary is used.
json_path (str, optional) – Path to a JSON file containing parameter data. If provided, parameters will be imported from this file during
initialisation.
formation_concentrations (bool, optional) – If True, re-calculates the initial concentrations of lithium in the active material (default: False).
Exports parameters to a JSON file specified by output_json_path.
The current state of the params attribute is written to the file. If fit_params
is provided, these parameters are updated before export. Non-serializable values
are handled and noted in the output JSON.
Parameters:
output_json_path (str) – The file path where the JSON output will be saved.
fit_params (list of fitted parameter objects, optional) – Parameters that have been fitted and need to be included in the export.
Raises:
ValueError – If there are no parameters to export.
Imports parameters from a JSON file specified by the json_path attribute.
If a json_path is provided at initialization or as an argument, that JSON file
is loaded and the parameters are stored in the params attribute. Special cases
are handled appropriately.
Parameters:
json_path (str, optional) – Path to the JSON file from which to import parameters. If provided, it overrides the instance’s json_path.
Returns:
The dictionary containing the imported parameters.
Return type:
dict
Raises:
FileNotFoundError – If the specified JSON file cannot be found.
params_dict (dict) – A dictionary of parameters and values used to update the parameter values
check_already_exists (bool, optional) – Whether to check that a parameter in params_dict already exists when trying
to update it. This is to avoid cases where an intended change in the parameters
is ignored due a typo in the parameter name (default: True).
Represents a set of uncertain parameters within the PyBOP framework.
This class encapsulates the definition of a parameter, including its name, prior
distribution, initial value, bounds, and a margin to ensure the parameter stays
within feasible limits during optimisation or sampling.
values (list or str, optional) – A list of parameter values or one of the strings “initial” or “true” which can be used
to obtain a dictionary of parameters.
Implements the Population Markov Chain Monte Carlo (MCMC) algorithm.
This class extends the Population MCMC sampler from the PINTS library.
This MCMC method uses a population of chains at different temperatures
to explore the parameter space more efficiently and avoid local minima.
Random Search is a simple optimisation algorithm that samples parameter sets randomly
within the given boundaries and identifies the best solution based on fitness.
This optimiser has been implemented for benchmarking and comparisons, convergence will be
better with one of other optimisers in the majority of cases.
Parameters:
cost (callable) – The cost function to be minimised.
max_iterations (int, optional) – Maximum number of iterations for the optimisation.
min_iterations (int, optional (default=2)) – Minimum number of iterations before termination.
max_unchanged_iterations (int, optional (default=15)) – Maximum number of iterations without improvement before termination.
multistart (int, optional (default=1)) – Number of optimiser restarts from randomly sample position. These positions
are sampled from the priors.
parallel (bool, optional (default=False)) – Whether to run the optimisation in parallel.
**optimiser_kwargs (optional) –
Valid PINTS option keys and their values, for example:
x0 : array_like
Initial position from which optimisation will start.
population_sizeint
Number of solutions to evaluate per iteration.
boundsdict
A dictionary with ‘lower’ and ‘upper’ keys containing arrays for lower and
upper bounds on the parameters.
Random Search (RS) optimisation algorithm.
This algorithm explores the parameter space by randomly sampling points.
The algorithm does the following:
1. Initialise a population of solutions.
2. At each iteration, generate n number of random positions within boundaries.
3. Evaluate the quality/fitness of the positions.
4. Replace the best position with improved position if found.
Parameters:
population_size (optional) – Number of solutions to evaluate per iteration.
References:
The Random Search algorithm implemented in this work is based on principles outlined
in “Introduction to Stochastic Search and Optimization: Estimation, Simulation, and
Control” by Spall, J. C. (2003).
The implementation inherits from the PINTS PopulationOptimiser.
Implements the Rao-Blackwell Adaptive Covariance Markov Chain Monte Carlo (MCMC) algorithm.
This class extends the Rao-Blackwell Adaptive Covariance MCMC sampler from the PINTS library.
This MCMC method improves sampling efficiency by combining Rao-Blackwellisation
with adaptive covariance strategies.
Implements the Relativistic Markov Chain Monte Carlo (MCMC) algorithm.
This class extends the Relativistic MCMC sampler from the PINTS library.
This MCMC method uses concepts from relativistic mechanics to propose new states,
allowing for efficient exploration of the parameter space.
Computes the root mean square error between model predictions and the target
data, providing a measure of the differences between predicted values and
observed values.
Computes the cost function for the given predictions.
Parameters:
r (np.ndarray) – The residual difference between the model prediction and the target. The
dimensions of r are (len(signal), len(domain_data)).
dy (np.ndarray, optional) – The corresponding gradient with respect to the parameters for each signal.
The dimensions of dy are (len(parameters), len(signal), len(domain_data)).
Returns:
If dy is not None, returns a tuple containing the cost (float) and the
gradient with dimension (len(parameters)), otherwise returns only the cost.
Return type:
np.float64 or tuple[np.float64, np.ndarray[np.float64]]
Implements the stochastic natural evolution strategy (SNES) optimisation algorithm.
Inheriting from the PINTS SNES class, this optimiser is an evolutionary algorithm
that evolves a probability distribution on the parameter space, guiding the search
for the optimum based on the natural gradient of expected fitness.
Parameters:
cost (callable) – The cost function to be minimised.
max_iterations (int, optional) – Maximum number of iterations for the optimisation.
min_iterations (int, optional (default=2)) – Minimum number of iterations before termination.
max_unchanged_iterations (int, optional (default=15)) – Maximum number of iterations without improvement before termination.
multistart (int, optional (default=1)) – Number of optimiser restarts from randomly sample position. These positions
are sampled from the priors.
parallel (bool, optional (default=False)) – Whether to run the optimisation in parallel.
**optimiser_kwargs (optional) –
Valid PINTS option keys and their values, for example:
x0 : array_like
Initial position from which optimisation will start.
sigma0float
Initial step size or standard deviation depending on the optimiser.
boundsdict
A dictionary with ‘lower’ and ‘upper’ keys containing arrays for lower and
upper bounds on the parameters.
This class aims to provide numerical values with lower magnitude than the
canonical likelihoods, which can improve optimiser convergence in certain
cases.
Adapts SciPy’s differential_evolution function for global optimisation.
This class provides a global optimisation strategy based on differential evolution, useful for
problems involving continuous parameters and potentially multiple local minima.
Parameters:
bounds (dict, sequence or scipy.optimize.Bounds) – Bounds for variables. Must be provided as it is essential for differential evolution.
Each element is a tuple (min, max) for the corresponding variable.
Differential Evolution is a stochastic population based method that is useful for
global optimisation problems. At each pass through the population the algorithm mutates
each candidate solution by mixing with other candidate solutions to create a trial
candidate. The fitness of all candidates is then evaluated and for each candidate if
the trial candidate is an improvement, it takes its place in the population for the next
iteration.
Evaluates a function (or callable object) for the SciPy optimisers
for either a single or multiple positions.
Parameters:
function (callable) – The function to evaluate. This function should accept an input and
optionally additional arguments, returning either a single value or a tuple.
args (sequence, optional) – A sequence containing extra arguments to be passed to the function.
If specified, the function will be called as function(x, *args).
Adapts SciPy’s minimize function for use as an optimisation strategy.
This class provides an interface to various scalar minimisation algorithms implemented in SciPy,
allowing fine-tuning of the optimisation process through method selection and option configuration.
Parameters:
**optimiser_kwargs (optional) –
Valid SciPy Minimize option keys and their values:
x0 : array_like
Initial position from which optimisation will start.
Sequential evaluates a function (or callable object)
for either a single or multiple positions. This class is based
off the PintsSequentialEvaluator class, with additions for
PyBOP’s JAX cost classes.
Parameters:
function (callable) – The function to evaluate. This function should accept an input and
optionally additional arguments, returning either a single value or a tuple.
args (sequence, optional) – A sequence containing extra arguments to be passed to the function.
If specified, the function will be called as function(x, *args).
Adapter for Simulated Annealing optimiser in PyBOP.
Simulated Annealing is a probabilistic optimisation algorithm inspired by the annealing
process in metallurgy. It works by iteratively proposing new solutions and accepting
them based on both their fitness and a temperature parameter that decreases over time.
This allows the algorithm to initially explore broadly and gradually focus on local
optimisation as the temperature decreases.
The algorithm is particularly effective at avoiding local minima and returning a
global solution.
Parameters:
cost (callable) – The cost function to be minimised.
max_iterations (int, optional) – Maximum number of iterations for the optimisation.
min_iterations (int, optional (default=2)) – Minimum number of iterations before termination.
max_unchanged_iterations (int, optional (default=15)) – Maximum number of iterations without improvement before termination.
multistart (int, optional (default=1)) – Number of optimiser restarts from randomly sample position. These positions
are sampled from the priors.
parallel (bool, optional (default=False)) – Whether to run the optimisation in parallel.
**optimiser_kwargs (optional) –
Valid PINTS option keys and their values, for example:
x0 : array_like
Initial position from which optimisation will start.
sigma0float
Initial step size or standard deviation for parameter perturbation.
boundsdict
A dictionary with ‘lower’ and ‘upper’ keys containing arrays for lower and
upper bounds on the parameters.
cooling_schedulecallable, optional
Function that determines how temperature decreases over time.
Simulated Annealing optimiser, implementing the classic temperature-based
probabilistic optimisation method.
This method uses a temperature schedule to control the probability of accepting
worse solutions as it explores the parameter space. As the temperature decreases,
the algorithm becomes more selective, eventually converging to a local or global
optimum.
The probability of accepting a worse solution is given by:
..math::
P(accept) = exp(-(f_{ ext{new}} - fold)/T)
The temperature decreases according to the cooling schedule:
..math::
T = T0 * lpha^k
where:
- :math: T0 is the initial temperature
- :math: lpha is the cooling rate (between 0 and 1)
- :math: k is the iteration number
Parameters:
x0 (numpy array) – Initial position
sigma0 (float) – Initial step size
boundaries (dict, optional) – Optional boundaries for parameters
Implements the Slice Doubling Markov Chain Monte Carlo (MCMC) algorithm.
This class extends the Slice Doubling MCMC sampler from the PINTS library.
This MCMC method uses slice sampling with a doubling procedure to propose new states,
allowing for efficient exploration of the parameter space.
Implements the Slice Rank Shrinking Markov Chain Monte Carlo (MCMC) algorithm.
This class extends the Slice Rank Shrinking MCMC sampler from the PINTS library.
This MCMC method uses slice sampling with a rank shrinking procedure to propose new states,
allowing for efficient exploration of the parameter space.
Implements the Slice Stepout Markov Chain Monte Carlo (MCMC) algorithm.
This class extends the Slice Stepout MCMC sampler from the PINTS library.
This MCMC method uses slice sampling with a stepout procedure to propose new states,
allowing for efficient exploration of the parameter space.
The Sum of Power [1] is a generalised cost function based on the p-th power
of absolute differences between two vectors. It is defined as:
\[C_p(x, y) = \sum_i |x_i - y_i|^p\]
where p ≥ 0 is the power order.
This class implements the Sum of Power as a cost function for
optimisation problems, allowing for flexible power-based optimisation
across various problem domains.
Special cases:
p = 1: Sum of Absolute Differences
p = 2: Sum of Squared Differences
p → ∞: Maximum Absolute Difference
Note that this is not normalised, unlike distance metrics. To get a
distance metric, you would need to take the p-th root of the result.
Computes the cost function for the given predictions.
Parameters:
r (np.ndarray) – The residual difference between the model prediction and the target. The
dimensions of r are (len(signal), len(domain_data)).
dy (np.ndarray, optional) – The corresponding gradient with respect to the parameters for each signal.
The dimensions of dy are (len(parameters), len(signal), len(domain_data)).
Returns:
If dy is not None, returns a tuple containing the cost (float) and the
gradient with dimension (len(parameters)), otherwise returns only the cost.
Return type:
np.float64 or tuple[np.float64, np.ndarray[np.float64]]
Computes the sum of the squares of the differences between model predictions
and target data, which serves as a measure of the total error between the
predicted and observed values.
Computes the cost function for the given predictions.
Parameters:
r (np.ndarray) – The residual difference between the model prediction and the target. The
dimensions of r are (len(signal), len(domain_data)).
dy (np.ndarray, optional) – The corresponding gradient with respect to the parameters for each signal.
The dimensions of dy are (len(parameters), len(signal), len(domain_data)).
Returns:
If dy is not None, returns a tuple containing the cost (float) and the
gradient with dimension (len(parameters)), otherwise returns only the cost.
Return type:
np.float64 or tuple[np.float64, np.ndarray[np.float64]]
Helper class to replace all instances of one or more symbols in an expression tree
with another symbol, as defined by the dictionary symbol_replacement_map
Originally developed by pybamm: pybamm-team/pybamm
Parameters:
symbol_replacement_map (dict {pybamm.Symbol -> pybamm.Symbol}) – Map of which symbols should be replaced by which.
Process boundary conditions for a PybaMM model class
Boundary conditions are dictionaries {“left”: left bc, “right”: right bc}
in general, but may be imposed on the tabs (or not on the tab) for a
small number of variables, e.g. {“negative tab”: neg. tab bc,
“positive tab”: pos. tab bc “no tab”: no tab bc}.
Replace all instances of a symbol in a PyBaMM model class.
Parameters:
unprocessed_model (pybamm.BaseModel) – Model class to assign parameter values to
inplace (bool, optional) – If True, replace the parameters in the model in place. Otherwise, return a
new model with parameter values set. Default is True.
sigma0 (np.ndarray | float) – The covariance matrix of the initial state. If a float is provided, the covariance matrix is set to sigma0 * np.eye(n), where n is the number of states.
To remove a state from the filter, set the corresponding row and col to zero in both sigma0 and process.
process (np.ndarray | float) – The covariance matrix of the process noise. If a float is provided, the covariance matrix is set to process * np.eye(n), where n is the number of states.
To remove a state from the filter, set the corresponding row and col to zero in both sigma0 and process.
measure (np.ndarray | float) – The covariance matrix of the measurement noise. If a float is provided, the covariance matrix is set to measure * np.eye(m), where m is the number of measurements.
dataset (Dataset) – Dataset object containing the data to fit the model to.
check_model (bool, optional) – Flag to indicate if the model should be checked (default: True).
signal (list[str], optional) – A list of variables to observe (default: [“Voltage [V]”]).
domain (str, optional) – The name of the domain (default: “Time [s]”).
initial_state (dict, optional) – A valid initial state, e.g. the initial open-circuit voltage (default: None).
Calculates the (volumetric) energy density of a battery cell, when applied to a
normalised discharge from upper to lower voltage limits. The goal of maximising
the energy density is achieved with self.minimising = False.
The volumetric energy density [Wh.m-3] is calculated as
where v is the cell volume, t is the time, T is the total time, I is the current
and V is the voltage. The factor of 1/3600 is included to convert from seconds
to hours.
Inherits all parameters and attributes from DesignCost.
Calculates the (volumetric) power density of a battery cell, when applied to a
discharge from upper to lower voltage limits. The goal of maximising the power
density is achieved with self.minimising=False.
The time-averaged volumetric power density [W.m-3] is calculated as
\[\frac{1}{3600 v T} \int_{t=0}^{t=T} I(t) V(t) \mathrm{d}t\]
where v is the cell volume, t is the time, T is the total time, I is the current
and V is the voltage. The factor of 1/3600 is included to convert from seconds
to hours.
Inherits all parameters and attributes from DesignCost.
A subclass for constructing a linear combination of cost functions as
a single weighted cost function.
Parameters:
costs (pybop.BaseCost) – The individual PyBOP cost objects.
weights (list[float]) – A list of values with which to weight the cost values.
has_identical_problems (bool) – If True, the shared problem will be evaluated once and saved before the
self.compute() method of each cost is called (default: False).
has_separable_problem (bool) – This attribute must be set to False for WeightedCost objects. If the
corresponding attribute of an individual cost is True, the problem is
separable from the cost function and will be evaluated before the
individual cost evaluation is called.
Implements the Exponential Natural Evolution Strategy (XNES) optimiser from PINTS.
XNES is an evolutionary algorithm that samples from a multivariate normal
distribution, which is updated iteratively to fit the distribution of successful
solutions.
Parameters:
cost (callable) – The cost function to be minimised.
max_iterations (int, optional) – Maximum number of iterations for the optimisation.
min_iterations (int, optional (default=2)) – Minimum number of iterations before termination.
max_unchanged_iterations (int, optional (default=15)) – Maximum number of iterations without improvement before termination.
multistart (int, optional (default=1)) – Number of optimiser restarts from randomly sample position. These positions
are sampled from the priors.
parallel (bool, optional (default=False)) – Whether to run the optimisation in parallel.
**optimiser_kwargs (optional) –
Valid PINTS option keys and their values, for example:
x0 : array_like
Initial position from which optimisation will start.
sigma0float
Initial step size or standard deviation depending on the optimiser.
boundsdict
A dictionary with ‘lower’ and ‘upper’ keys containing arrays for lower and
upper bounds on the parameters.
A simple check for parameter correlations based on numerical approximation
of the Hessian matrix at the optimal point using central finite differences.
dx (array-like, optional) – An array of small positive values used to check proximity to the parameter
bounds and as the perturbation distance in the finite difference calculations.
cost_tolerance (float, optional) – A small positive tolerance used for cost value comparisons (default: 1e-5).