From afd4990f84ff4acfd0d07a944beea270df9c118e Mon Sep 17 00:00:00 2001 From: Brady Planden Date: Wed, 13 Mar 2024 15:25:37 +0000 Subject: [PATCH] add infeasible cost tests, remove redundant scipyminimise maxiter options check --- pybop/optimisers/scipy_optimisers.py | 13 ++++--------- tests/unit/test_cost.py | 2 +- 2 files changed, 5 insertions(+), 10 deletions(-) diff --git a/pybop/optimisers/scipy_optimisers.py b/pybop/optimisers/scipy_optimisers.py index 5fc12ea6..010c653b 100644 --- a/pybop/optimisers/scipy_optimisers.py +++ b/pybop/optimisers/scipy_optimisers.py @@ -49,9 +49,10 @@ def _runoptimise(self, cost_function, x0, bounds): A tuple (x, final_cost) containing the optimized parameters and the value of `cost_function` at the optimum. """ - # Add callback storing history of parameter values self.log = [[x0]] + self.options = {"maxiter": self._max_iterations} + # Add callback storing history of parameter values def callback(x): self.log.append([x]) @@ -74,12 +75,6 @@ def cost_wrapper(x): (lower, upper) for lower, upper in zip(bounds["lower"], bounds["upper"]) ) - # Set max iterations - if self._max_iterations is not None: - self.options = {"maxiter": self._max_iterations} - else: - self.options.pop("maxiter", None) - result = minimize( cost_wrapper, x0, @@ -158,6 +153,8 @@ def _runoptimise(self, cost_function, x0=None, bounds=None): A tuple (x, final_cost) containing the optimized parameters and the value of ``cost_function`` at the optimum. """ + self.log = [] + if bounds is None: raise ValueError("Bounds must be specified for differential_evolution.") @@ -167,8 +164,6 @@ def _runoptimise(self, cost_function, x0=None, bounds=None): ) # Add callback storing history of parameter values - self.log = [] - def callback(x, convergence): self.log.append([x]) diff --git a/tests/unit/test_cost.py b/tests/unit/test_cost.py index 7d3a4ea6..b6f4daf3 100644 --- a/tests/unit/test_cost.py +++ b/tests/unit/test_cost.py @@ -153,10 +153,10 @@ def test_costs(self, cost): for i in range(len(record)): assert "Non-physical point encountered" in str(record[i].message) - if isinstance(cost, pybop.RootMeanSquaredError): # Test infeasible locations cost.problem._model.allow_infeasible_solutions = False assert cost([1.1]) == np.inf + assert cost.evaluateS1([1.1]) == (np.inf, cost._de) # Test exception for non-numeric inputs with pytest.raises(ValueError):