Running an optimisation¶
- pints.optimise(function, x0, sigma0=None, boundaries=None, transformation=None, method=None)[source]¶
Finds the parameter values that minimise an
ErrorMeasureor maximise aLogPDF.- Parameters:
function – An
pints.ErrorMeasureor apints.LogPDFthat evaluates points in the parameter space.x0 – The starting point for searches in the parameter space. This value may be used directly (for example as the initial position of a particle in
PSO) or indirectly (for example as the center of a distribution inXNES).sigma0 – An optional initial standard deviation around
x0, or a parameter representing the “scale” of the parameters. Can be specified either as a scalar value (same value for all dimensions) or as an array with one entry per dimension. Not all methods will use this information.boundaries – An optional set of boundaries on the parameter space.
transformation – An optional
pints.Transformationto allow the optimiser to search in a transformed parameter space. If used, points shown or returned to the user will first be detransformed back to the original space.method – The class of
pints.Optimiserto use for the optimisation. If no method is specified,CMAESis used.
- Returns:
x_best (numpy array) – The best parameter set obtained
f_best (float) – The corresponding score.
- class pints.OptimisationController(function, x0, sigma0=None, boundaries=None, transformation=None, method=None)[source]¶
Finds the parameter values that minimise an
ErrorMeasureor maximise aLogPDF.- Parameters:
function – An
pints.ErrorMeasureor apints.LogPDFthat evaluates points in the parameter space.x0 – The starting point for searches in the parameter space. For details, see
Optimiser.sigma0 – An optional initial standard deviation around
x0. For details, seeOptimiser.boundaries – An optional set of boundaries on the parameter space. For details, see
Optimiser.transformation – An optional
pints.Transformationto allow the optimiser to search in a transformed parameter space. If used, points shown or returned to the user will first be detransformed back to the original space.method – The class of
pints.Optimiserto use for the optimisation. If no method is specified,CMAESis used.
- evaluations()[source]¶
Returns the number of evaluations performed during the last run, or
Noneif the controller hasn’t ran yet.
- f_guessed_tracking()[source]¶
Returns
Trueif the controller is set to track the optimiser progress usingpints.Optimiser.f_guessed()rather thanpints.Optimiser.f_best().See also
set_f_guessed_tracking().
- function_tolerance()[source]¶
Returns a tuple
(iterations, threshold)specifying the maximum iterations without a significant change in best function evaluation, if this stopping criterion is set, else(None, None).The entries in the tuple correspond directly to the arguments to
set_function_tolerance().
- iterations()[source]¶
Returns the number of iterations performed during the last run, or
Noneif the controller hasn’t ran yet.
- max_evaluations()[source]¶
Returns the maximum number of evaluations if this stopping criterion is set, or
Noneif it is not.
- max_iterations()[source]¶
Returns the maximum iterations if this stopping criterion is set, or
Noneif it is not.See
set_max_iterations().
- max_unchanged_iterations()[source]¶
Deprecated alias of
function_tolerance().
- parallel()[source]¶
Returns the number of parallel worker processes this routine will be run on, or
Falseif parallelisation is disabled.
- parameter_tolerance()[source]¶
Returns a tuple
(iterations, threshold)specifying the maximum iterations without a significant change in best parameters, if this stopping criterion is set, else(None, None).The entries in the tuple correspond directly to the arguments to
set_parameter_tolerance().
- run()[source]¶
Runs the optimisation, returns a tuple
(x, f).The returned
xandfcorrespond to either the bestfseen during the optimisation, or to the best guessedf, depending on the setting forset_f_guessed_tracking(). See :meth:Optimiser.f_guessed()` for details.An optional
callbackfunction can be passed in that will be called at the end of every iteration. The callback should take the arguments(iteration, optimiser), whereiterationis the iteration count (an integer) andoptimiseris the optimiser object.
- set_callback(cb=None)[source]¶
Allows a “callback” function to be passed in that will be called at the end of every iteration.
This can be used for e.g. visualising optimiser progress.
Example:
def cb(opt): plot(opt.xbest()) opt.set_callback(cb)
- set_f_guessed_tracking(use_f_guessed=False)[source]¶
Sets the method used to track the optimiser progress to
pints.Optimiser.f_guessed()orpints.Optimiser.f_best()(default).The tracked
f(and/orx) value is used to evaluate stopping criteria, and is the one returned fromrun().
- set_function_tolerance(iterations=200, threshold=1e-11)[source]¶
Adds a stopping criterion so that the routine halts if the objective function does not change by more than
thresholdfor the given number ofiterations.This criterion is enabled by default. To disable it, use
set_function_tolerance(None).Note that this can be used to implement an absolute “ftol” stopping criteria, by calling
set_function_tolerance(1, ftol).
- set_log_interval(iters=20, warm_up=3)[source]¶
Changes the frequency with which messages are logged.
- Parameters:
interval – A log message will be shown every
itersiterations.warm_up – A log message will be shown every iteration, for the first
warm_upiterations.
- set_log_to_file(filename=None, csv=False)[source]¶
Enables logging to file when a filename is passed in, disables it if
filenameisFalseorNone.The argument
csvcan be set toTrueto write the file in comma separated value (CSV) format. By default, the file contents will be similar to the output on screen.
- set_max_evaluations(evaluations=None)[source]¶
Adds a stopping criterion so that the routine halts after the given number of function
evaluations.This criterion is disabled by default. To enable, pass in any positive integer. To disable again, use
set_max_evaluations(None).
- set_max_iterations(iterations=10000)[source]¶
Adds a stopping criterion so that the routine halts after the given number of
iterations.This criterion is enabled by default. To disable it, use
set_max_iterations(None).
- set_max_unchanged_iterations(iterations=200, threshold=1e-11)[source]¶
Deprecated alias of
function_tolerance().
- set_parallel(parallel=False)[source]¶
Enables/disables parallel evaluation.
If
parallel=True, the method will run using a number of worker processes equal to the detected cpu core count. The number of workers can be set explicitly by settingparallelto an integer greater than 0. Parallelisation can be disabled by settingparallelto0orFalse.
- set_parameter_tolerance(iterations=200, threshold=1e-11)[source]¶
Adds a stopping criterion so that the routine halts if the position in parameter space does not change by more
thresholdfor the given number ofiterations.Thresholds can be defined per parameter, or a single scalar value can be passed in. The position is deemed to have changed if
np.any(np.abs(x_new - x_sig) >= threshold), wherex_sigis either the starting position, or the last position for which the criterion was met.This criterion is disabled by default. Once enabled, it can be disabled again by calling
set_parameter_tolerance(None).Note that this can be used to implement an absolute “xtol” stopping criteria, by calling
set_parameter_tolerance(1, xtol).
- set_threshold(threshold)[source]¶
Adds a stopping criterion causing the routine to stop once the objective function is less than the given
threshold(when minimising, or more when maximising).This criterion is disabled by default, but can be enabled by calling this method with a valid
threshold. To disable it, useset_treshold(None).
- threshold()[source]¶
Returns the threshold stopping criterion, or
Noneif no threshold stopping criterion is set. Seeset_threshold().
- class pints.Optimisation(function, x0, sigma0=None, boundaries=None, transformation=None, method=None)[source]¶
Deprecated alias for
OptimisationController.- evaluations()¶
Returns the number of evaluations performed during the last run, or
Noneif the controller hasn’t ran yet.
- f_guessed_tracking()¶
Returns
Trueif the controller is set to track the optimiser progress usingpints.Optimiser.f_guessed()rather thanpints.Optimiser.f_best().See also
set_f_guessed_tracking().
- function_tolerance()¶
Returns a tuple
(iterations, threshold)specifying the maximum iterations without a significant change in best function evaluation, if this stopping criterion is set, else(None, None).The entries in the tuple correspond directly to the arguments to
set_function_tolerance().
- iterations()¶
Returns the number of iterations performed during the last run, or
Noneif the controller hasn’t ran yet.
- max_evaluations()¶
Returns the maximum number of evaluations if this stopping criterion is set, or
Noneif it is not.
- max_iterations()¶
Returns the maximum iterations if this stopping criterion is set, or
Noneif it is not.See
set_max_iterations().
- max_unchanged_iterations()¶
Deprecated alias of
function_tolerance().
- optimiser()¶
Returns the underlying optimiser object, allowing detailed configuration.
- parallel()¶
Returns the number of parallel worker processes this routine will be run on, or
Falseif parallelisation is disabled.
- parameter_tolerance()¶
Returns a tuple
(iterations, threshold)specifying the maximum iterations without a significant change in best parameters, if this stopping criterion is set, else(None, None).The entries in the tuple correspond directly to the arguments to
set_parameter_tolerance().
- run()¶
Runs the optimisation, returns a tuple
(x, f).The returned
xandfcorrespond to either the bestfseen during the optimisation, or to the best guessedf, depending on the setting forset_f_guessed_tracking(). See :meth:Optimiser.f_guessed()` for details.An optional
callbackfunction can be passed in that will be called at the end of every iteration. The callback should take the arguments(iteration, optimiser), whereiterationis the iteration count (an integer) andoptimiseris the optimiser object.
- set_callback(cb=None)¶
Allows a “callback” function to be passed in that will be called at the end of every iteration.
This can be used for e.g. visualising optimiser progress.
Example:
def cb(opt): plot(opt.xbest()) opt.set_callback(cb)
- set_f_guessed_tracking(use_f_guessed=False)¶
Sets the method used to track the optimiser progress to
pints.Optimiser.f_guessed()orpints.Optimiser.f_best()(default).The tracked
f(and/orx) value is used to evaluate stopping criteria, and is the one returned fromrun().
- set_function_tolerance(iterations=200, threshold=1e-11)¶
Adds a stopping criterion so that the routine halts if the objective function does not change by more than
thresholdfor the given number ofiterations.This criterion is enabled by default. To disable it, use
set_function_tolerance(None).Note that this can be used to implement an absolute “ftol” stopping criteria, by calling
set_function_tolerance(1, ftol).
- set_log_interval(iters=20, warm_up=3)¶
Changes the frequency with which messages are logged.
- Parameters:
interval – A log message will be shown every
itersiterations.warm_up – A log message will be shown every iteration, for the first
warm_upiterations.
- set_log_to_file(filename=None, csv=False)¶
Enables logging to file when a filename is passed in, disables it if
filenameisFalseorNone.The argument
csvcan be set toTrueto write the file in comma separated value (CSV) format. By default, the file contents will be similar to the output on screen.
- set_log_to_screen(enabled)¶
Enables or disables logging to screen.
- set_max_evaluations(evaluations=None)¶
Adds a stopping criterion so that the routine halts after the given number of function
evaluations.This criterion is disabled by default. To enable, pass in any positive integer. To disable again, use
set_max_evaluations(None).
- set_max_iterations(iterations=10000)¶
Adds a stopping criterion so that the routine halts after the given number of
iterations.This criterion is enabled by default. To disable it, use
set_max_iterations(None).
- set_max_unchanged_iterations(iterations=200, threshold=1e-11)¶
Deprecated alias of
function_tolerance().
- set_parallel(parallel=False)¶
Enables/disables parallel evaluation.
If
parallel=True, the method will run using a number of worker processes equal to the detected cpu core count. The number of workers can be set explicitly by settingparallelto an integer greater than 0. Parallelisation can be disabled by settingparallelto0orFalse.
- set_parameter_tolerance(iterations=200, threshold=1e-11)¶
Adds a stopping criterion so that the routine halts if the position in parameter space does not change by more
thresholdfor the given number ofiterations.Thresholds can be defined per parameter, or a single scalar value can be passed in. The position is deemed to have changed if
np.any(np.abs(x_new - x_sig) >= threshold), wherex_sigis either the starting position, or the last position for which the criterion was met.This criterion is disabled by default. Once enabled, it can be disabled again by calling
set_parameter_tolerance(None).Note that this can be used to implement an absolute “xtol” stopping criteria, by calling
set_parameter_tolerance(1, xtol).
- set_threshold(threshold)¶
Adds a stopping criterion causing the routine to stop once the objective function is less than the given
threshold(when minimising, or more when maximising).This criterion is disabled by default, but can be enabled by calling this method with a valid
threshold. To disable it, useset_treshold(None).
- threshold()¶
Returns the threshold stopping criterion, or
Noneif no threshold stopping criterion is set. Seeset_threshold().
- time()¶
Returns the time needed for the last run, in seconds, or
Noneif the controller hasn’t run yet.