Skip to content

Commit

Permalink
Fix test/optimize/test_optimize.py: OptimizeWarning: Unknown solver o… (
Browse files Browse the repository at this point in the history
#1218)

…ptions: maxfun

* Use the correct option for setting the maximum number of function evaluations for each scipy optimizer.

  Closes #970

  Note that this changes the default settings for some optimizers for which `maxfun` has so far been ignored.

* Reenable tests for scipy `trust-constr` and `dogleg` optimizers, closes #613
  • Loading branch information
dweindl authored Nov 29, 2023
1 parent 00514bc commit 56fbf78
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
9 changes: 6 additions & 3 deletions pypesto/optimize/optimizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -471,10 +471,13 @@ def is_least_squares(self):

def get_default_options(self):
"""Create default options specific for the optimizer."""
options = {'disp': False}
if self.is_least_squares():
options = {'max_nfev': 1000, 'disp': False}
else:
options = {'maxfun': 1000, 'disp': False}
options['max_nfev'] = 1000
elif self.method.lower() in ('l-bfgs-b', 'tnc'):
options['maxfun'] = 1000
elif self.method.lower() in ('nelder-mead', 'powell'):
options['maxfev'] = 1000
return options


Expand Down
12 changes: 9 additions & 3 deletions test/optimize/test_optimize.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,19 +54,21 @@ def problem(request) -> pypesto.Problem:
'Powell',
'CG',
'BFGS',
'dogleg',
'Newton-CG',
'L-BFGS-B',
'TNC',
'COBYLA',
'SLSQP',
'trust-constr',
'trust-ncg',
'trust-exact',
'trust-krylov',
'ls_trf',
'ls_dogbox',
]
],
# disabled: ,'trust-constr', 'ls_lm', 'dogleg'
# disabled: 'ls_lm' (ValueError when passing bounds)
('ipopt', ''),
('dlib', ''),
('pyswarm', ''),
Expand Down Expand Up @@ -142,7 +144,10 @@ def problem(request) -> pypesto.Problem:

@pytest.fixture(
params=optimizers,
ids=[f"{i}-{o[0]}" for i, o in enumerate(optimizers)],
ids=[
f"{i}-{o[0]}{'-' + str(o[1]) if isinstance(o[1], str) and o[1] else ''}"
for i, o in enumerate(optimizers)
],
)
def optimizer(request):
return request.param
Expand Down Expand Up @@ -249,7 +254,8 @@ def get_optimizer(library, solver):
options = {'maxiter': 100}

if library == 'scipy':
options['maxfun'] = options.pop('maxiter')
if solver == "TNC" or solver.startswith("ls_"):
options['maxfun'] = options.pop('maxiter')
optimizer = optimize.ScipyOptimizer(method=solver, options=options)
elif library == 'ipopt':
optimizer = optimize.IpoptOptimizer()
Expand Down

0 comments on commit 56fbf78

Please sign in to comment.