Skip to content

Commit

Permalink
Merge pull request #31 from Arm4g3ddon/main
Browse files Browse the repository at this point in the history
Added fallback mechanism for optimization method
  • Loading branch information
knipknap authored Jun 13, 2024
2 parents d909444 + 12de0e8 commit e50605c
Showing 1 changed file with 21 additions and 8 deletions.
29 changes: 21 additions & 8 deletions btl/feeds/calc.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,14 +295,27 @@ def optimize(self):
np.set_printoptions(formatter={'float': lambda x: "{0:0.10f}".format(x)})
with warnings.catch_warnings(): # ignore "out-of bounds" warning
warnings.simplefilter("ignore", category=RuntimeWarning)
result = minimize(self._evaluate_point,
point,
bounds=bounds,
method='SLSQP', # evaluated fastest
#method='Powell',
#method='Nelder-Mead',
#method='TNC',
tol=0.001)
methods = [ 'SLSQP', 'L-BFGS-B', 'Powell', 'Nelder-Mead', 'TNC'] # Added multiple methods to fallback on if one fails See issue https://github.com/knipknap/better-tool-library/issues/29
result = None

for method in methods:
try:
result = minimize(self._evaluate_point,
point,
bounds=bounds,
method=method,
tol=0.001)
if result.success:
break
except Exception as e:
print(f"Optimization failed with method {method}. Error: {str(e)}")
continue

if result is None or not result.success:
print("Optimization failed with all methods.")
else:
print(f"Optimization succeeded with method {method}.")


# Load & recalculate the best result.
self.speed.v, self.chipload.v, self.woc.v, self.doc.v = result.x
Expand Down

0 comments on commit e50605c

Please sign in to comment.