Skip to content

Commit

Permalink
Updt SciPy & BaseOptimiser for maximum iterations limit - fixes #237
Browse files Browse the repository at this point in the history
  • Loading branch information
BradyPlanden committed Mar 13, 2024
1 parent 9b03734 commit e7aef79
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 15 deletions.
5 changes: 3 additions & 2 deletions pybop/_optimisation.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,15 +175,16 @@ def _run_pybop(self):
final_cost : float
The final cost associated with the best parameters.
"""
x, final_cost = self.optimiser.optimise(
result = self.optimiser.optimise(
cost_function=self.cost,
x0=self.x0,
bounds=self.bounds,
maxiter=self._max_iterations,
)
self.log = self.optimiser.log
self._iterations = result.nit

return x, final_cost
return result.x, self.cost(result.x)

def _run_pints(self):
"""
Expand Down
2 changes: 1 addition & 1 deletion pybop/optimisers/base_optimiser.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def optimise(self, cost_function, x0=None, bounds=None, maxiter=None):
self.cost_function = cost_function
self.x0 = x0
self.bounds = bounds
self.maxiter = maxiter
self._max_iterations = maxiter

# Run optimisation
result = self._runoptimise(self.cost_function, x0=self.x0, bounds=self.bounds)
Expand Down
16 changes: 4 additions & 12 deletions pybop/optimisers/scipy_optimisers.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def cost_wrapper(x):
else:
self.options.pop("maxiter", None)

output = minimize(
result = minimize(
cost_wrapper,
x0,
method=self.method,
Expand All @@ -89,11 +89,7 @@ def cost_wrapper(x):
callback=callback,
)

# Get performance statistics
x = output.x
final_cost = cost_function(x)

return x, final_cost
return result

def needs_sensitivities(self):
"""
Expand Down Expand Up @@ -182,7 +178,7 @@ def callback(x, convergence):
(lower, upper) for lower, upper in zip(bounds["lower"], bounds["upper"])
]

output = differential_evolution(
result = differential_evolution(
cost_function,
bounds,
strategy=self.strategy,
Expand All @@ -191,11 +187,7 @@ def callback(x, convergence):
callback=callback,
)

# Get performance statistics
x = output.x
final_cost = output.fun

return x, final_cost
return result

def set_population_size(self, population_size=None):
"""
Expand Down

0 comments on commit e7aef79

Please sign in to comment.