From 6970872f38ba226339bcd4fec218c857ea18dcc2 Mon Sep 17 00:00:00 2001 From: Michael Clerx Date: Tue, 2 Jan 2024 20:10:17 +0000 Subject: [PATCH] Added xNES tests (requires fix from #1517). --- pints/cptests/_problems.py | 2 + pints/cptests/xnes.py | 82 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+) create mode 100644 pints/cptests/xnes.py diff --git a/pints/cptests/_problems.py b/pints/cptests/_problems.py index 46fa6028e..57ad1619d 100644 --- a/pints/cptests/_problems.py +++ b/pints/cptests/_problems.py @@ -279,6 +279,8 @@ def __init__(self, error, x0, sigma0, boundaries, transformation, method, controller.set_max_iterations(n_iterations) controller.set_max_unchanged_iterations(None) controller.set_log_to_screen(False) + if use_guessed: + controller.set_f_guessed_tracking(True) if method_hyper_parameters is not None: controller.optimiser().set_hyperparameters(method_hyper_parameters) self._x, self._f = controller.run() diff --git a/pints/cptests/xnes.py b/pints/cptests/xnes.py new file mode 100644 index 000000000..25f1029cf --- /dev/null +++ b/pints/cptests/xnes.py @@ -0,0 +1,82 @@ +#!/usr/bin/env python3 +# +# Change point tests for XNES. +# +# This file is part of PINTS (https://github.com/pints-team/pints/) which is +# released under the BSD 3-clause license. See accompanying LICENSE.md for +# copyright notice and full license details. +# +import pints +import pints.cptests as cpt + + +def bounded_fitzhugh_nagumo(n_iterations=100): + """ + Tests :class:`pints.XNES` on a bounded Fitzhugh-Nagumo model, and returns + a dictionary with ``error`` and ``distance``. + + For details of the solved problem, see + :class:`pints.cptests.RunOptimiserOnBoundedUntransformedLogistic`. + """ + problem = cpt.RunOptimiserOnBoundedFitzhughNagumo( + _method, n_iterations, _fguess) + return { + 'error': problem.error(), + 'distance': problem.distance() + } + + +def bounded_untransformed_logistic(n_iterations=300): + """ + Tests :class:`pints.XNES` on a bounded logistic model without + transformations, and returns a dictionary with ``error`` and ``distance``. + + For details of the solved problem, see + :class:`pints.cptests.RunOptimiserOnBoundedUntransformedLogistic`. + """ + problem = cpt.RunOptimiserOnBoundedUntransformedLogistic( + _method, n_iterations, _fguess) + return { + 'error': problem.error(), + 'distance': problem.distance() + } + + +def rosenbrock(n_iterations=100): + """ + Tests :class:`pints.XNES` on a Rosenbrock error and returns a dictionary + with ``error`` and ``distance``. + + For details of the solved problem, see + :class:`pints.cptests.RunOptimiserOnRosenbrockError`. + """ + problem = cpt.RunOptimiserOnRosenbrockError(_method, n_iterations, _fguess) + return { + 'error': problem.error(), + 'distance': problem.distance() + } + + +def two_dim_parabola(n_iterations=50): + """ + Tests :class:`pints.XNES` on a two-dimensional parabolic error and returns + a dictionary with entries ``error`` and ``distance``. + + For details of the solved problem, see + :class:`pints.cptests.RunOptimiserOnTwoDimParabola`. + """ + problem = cpt.RunOptimiserOnTwoDimParabola(_method, n_iterations, _fguess) + return { + 'error': problem.error(), + 'distance': problem.distance() + } + + +_method = pints.XNES +_fguess = True +_change_point_tests = [ + bounded_fitzhugh_nagumo, + bounded_untransformed_logistic, + rosenbrock, + two_dim_parabola, +]