pybop.optimisers.base_optimiser#

Classes#

BaseOptimiser

A base class for defining optimisation methods.

OptimisationResult

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.

x0[source]#

Initial parameter values for the optimisation.

Type:

numpy.ndarray

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

_parameter_sensitivities()[source]#
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:

OptimisationResult

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.

set_base_options()[source]#

Update the base optimiser options and remove them from the options dictionary.

_minimising = True[source]#
_needs_sensitivities = False[source]#
_transformation = None[source]#
allow_infeasible_solutions = False[source]#
bounds = None[source]#
default_max_iterations = 1000[source]#
log[source]#
property minimising[source]#
property needs_sensitivities[source]#
parameters[source]#
physical_viability = False[source]#
result = None[source]#
sigma0 = 0.02[source]#
property transformation[source]#
unset_options[source]#
unset_options_store[source]#
verbose = True[source]#
x0[source]#
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.

x[source]#

The solution of the optimisation.

Type:

ndarray

final_cost[source]#

The cost associated with the solution x.

Type:

float

n_iterations[source]#

Number of iterations performed by the optimiser.

Type:

int

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]#
_get_single_or_all(attr)[source]#
add_result(result)[source]#

Add a preprocessed OptimisationResult.

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.

check_physical_viability(x)[source]#

Check if the optimised parameters are physically viable.

Parameters:

x (array-like) – Optimised parameter values.

total_runtime() float | None[source]#

Calculates the total runtime across all runs.

_best_run = None[source]#
_final_cost = [][source]#
_fisher = [][source]#
_n_evaluations = [][source]#
_n_iterations = [][source]#
_pybamm_solution = [][source]#
_scipy_result = [][source]#
_sensitivities = None[source]#
_time = [][source]#
_transformation[source]#
_x = [][source]#
_x0 = [][source]#
cost[source]#
property final_cost[source]#
property final_cost_best[source]#
property fisher[source]#
property fisher_best[source]#
minimising[source]#
property n_evaluations[source]#
property n_evaluations_best[source]#
property n_iterations[source]#
property n_iterations_best[source]#
n_runs = 0[source]#
optim[source]#
property pybamm_solution[source]#
property pybamm_solution_best[source]#
property scipy_result[source]#
property scipy_result_best[source]#
property sensitivities[source]#
property time[source]#
property time_best[source]#
property x[source]#
property x0[source]#
property x0_best[source]#
property x_best[source]#