pybop.optimisers.base_optimiser#
Classes#
A base class for defining optimisation methods. |
|
Stores the result of the optimisation. |
Module Contents#
- class pybop.optimisers.base_optimiser.BaseOptimiser(cost, **optimiser_kwargs)[source]#
A base class for defining optimisation methods.
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 pints.ErrorMeasure) – An objective function to be optimised, which can be either a pybop.Cost or PINTS error measure
**optimiser_kwargs (optional) – Valid option keys and their values.
- bounds[source]#
Dictionary containing the parameter bounds with keys ‘lower’ and ‘upper’.
- Type:
dict
- sigma0[source]#
Initial step size or standard deviation around
x0. Either a scalar value (one standard deviation for all coordinates) or an array with one entry per dimension. Not all methods will use this information.- Type:
float or sequence
- verbose[source]#
If True, the optimisation progress is printed (default: False).
- Type:
bool, optional
- physical_viability[source]#
If True, the feasibility of the optimised parameters is checked (default: False).
- Type:
bool, optional
- allow_infeasible_solutions[source]#
If True, infeasible parameter values will be allowed in the optimisation (default: True).
- Type:
bool, optional
- log[source]#
A log of the parameter values tried during the optimisation and associated costs.
- Type:
dict
- abstract _run()[source]#
Contains the logic for the optimisation algorithm.
This method should be implemented by child classes to perform the actual optimisation.
- Raises:
NotImplementedError – If the method has not been implemented by the subclass.
- abstract _set_up_optimiser()[source]#
Parse optimiser options and prepare the optimiser.
This method should be implemented by child classes.
- Raises:
NotImplementedError – If the method has not been implemented by the subclass.
- cost_call(x: pybop.Inputs | list, calculate_grad: bool = False) float | tuple[float, numpy.ndarray][source]#
Call the cost function to minimise, applying any given transformation to the input parameters.
- Parameters:
x (Inputs or list-like) – The input parameters for which the cost and optionally the gradient will be computed.
calculate_grad (bool, optional, default=False) – If True, both the 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).
- Return type:
float or tuple
- log_update(x=None, x_best=None, cost=None, cost_best=None, x0=None)[source]#
Update the log with new values.
- Parameters:
x (list or array-like, optional) – Parameter values (default: None).
x_best (list or array-like, optional) – Parameter values corresponding to the best cost yet (default: None).
cost (list, optional) – Cost values corresponding to x (default: None).
cost_best – Cost values corresponding to x_best (default: None).
- abstract name()[source]#
Returns the name of the optimiser, to be overwritten by child classes.
- Returns:
The name of the optimiser
- Return type:
str
- run()[source]#
Run the optimisation and return the optimised parameters and final cost.
- Returns:
results – The pybop optimisation result class.
- Return type:
- set_allow_infeasible_solutions(allow: bool = True)[source]#
Set whether to allow infeasible solutions or not.
- Parameters:
iterations (bool, optional) – Whether to allow infeasible solutions.
- class pybop.optimisers.base_optimiser.OptimisationResult(optim: BaseOptimiser, x: pybop.Inputs | numpy.ndarray = None, final_cost: float | None = None, sensitivities: dict | None = None, n_iterations: int | None = None, n_evaluations: int | None = None, time: float | None = None, scipy_result=None)[source]#
Stores the result of the optimisation.
- scipy_result[source]#
The result obtained from a SciPy optimiser.
- Type:
scipy.optimize.OptimizeResult, optional
- pybamm_solution[source]#
The best solution object(s) obtained from the optimisation.
- Type:
pybamm.Solution or list[pybamm.Solution], optional
- __str__() str[source]#
A string representation of the OptimisationResult object.
- Returns:
A formatted string containing optimisation result information.
- Return type:
str
- _extend(x: list[pybop.Inputs] | list[numpy.ndarray], final_cost: list[float], fisher: list, n_iterations: list[int], n_evaluations: list[int], time: list[float], scipy_result: list, x0: list, pybamm_solution: list[pybamm.Solution])[source]#
- average_iterations() float | None[source]#
Calculates the average number of iterations across all runs.
- check_for_finite_cost() None[source]#
Validate the optimised parameters and ensure they produce a finite cost value.
- Raises:
ValueError – If the optimised parameters do not produce a finite cost value.