pybop.optimisers.base_optimiser#

Classes#

BaseOptimiser

A base class for defining optimisation methods.

MultiOptimisationResult

Multi run optimisation result class. Stores the results

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

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:

MultiOptimisationResult

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]#
unset_options[source]#
unset_options_store[source]#
verbose = True[source]#
x0[source]#
class pybop.optimisers.base_optimiser.MultiOptimisationResult[source]#

Multi run optimisation result class. Stores the results of multiple optimisation runs.

results[source]#

The list of OptimisationResults for each optimisation run

Type:

list

Properties#
----------
x[source]#

The solution of the best optimisation run.

Type:

ndarray

final_cost[source]#

The cost associated with the best solution x.

Type:

float

n_iterations[source]#

Number of iterations performed by the optimiser for the best optimisation run.

Type:

int

scipy_result[source]#

The result obtained from a SciPy optimiser for the best optimisation run.

Type:

scipy.optimize.OptimizeResult, optional

time[source]#

The total time across all optimisation runs.

Type:

float

__str__() str[source]#

A string representation of the MultiOptimisationResult object.

Returns:

A formatted string containing optimisation result information.

Return type:

str

add_run(result: OptimisationResult)[source]#

Adds a new optimisation result.

average_iterations() float | None[source]#

Calculates the average number of iterations across all runs.

best_run() OptimisationResult | None[source]#

Returns the result with the best final cost.

best_x() float | None[source]#

Returns the best parameters, x across the optimisation

check_physical_viability(x)[source]#
get_scipy_result()[source]#
total_runtime() float | None[source]#

Calculates the total runtime across all runs.

property final_cost[source]#
property n_evaluations[source]#
property n_iterations[source]#
results: list[OptimisationResult] = [][source]#
property scipy_result[source]#
property time[source]#
property x[source]#
property x0[source]#
class pybop.optimisers.base_optimiser.OptimisationResult(optim: BaseOptimiser, x: pybop.Inputs | numpy.ndarray = None, final_cost: float | 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

__str__() str[source]#

A string representation of the OptimisationResult object.

Returns:

A formatted string containing optimisation result information.

Return type:

str

_calculate_final_cost() float[source]#

Calculate the final cost using the cost function and optimised parameters.

Returns:

The calculated final cost.

Return type:

float

_validate_parameters() None[source]#

Validate the optimised parameters and ensure they produce a finite cost value.

Raises:

ValueError – If the optimized 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.

get_scipy_result() scipy.optimize.OptimizeResult | None[source]#

Get the SciPy optimisation result object.

Returns:

The SciPy optimisation result object if available, None otherwise.

Return type:

OptimizeResult or None

_transformation[source]#
cost[source]#
final_cost[source]#
fisher = None[source]#
minimising[source]#
n_evaluations = None[source]#
n_iterations = None[source]#
optim[source]#
scipy_result = None[source]#
time = None[source]#
x[source]#