Skip to content

Commit

Permalink
add infeasible cost tests, remove redundant scipyminimise maxiter opt…
Browse files Browse the repository at this point in the history
…ions check
  • Loading branch information
BradyPlanden committed Mar 13, 2024
1 parent e7aef79 commit afd4990
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 10 deletions.
13 changes: 4 additions & 9 deletions pybop/optimisers/scipy_optimisers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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])

Expand All @@ -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,
Expand Down Expand Up @@ -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.")

Expand All @@ -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])

Expand Down
2 changes: 1 addition & 1 deletion tests/unit/test_cost.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit afd4990

Please sign in to comment.