Skip to content

Commit

Permalink
Add callback_after_step. Fix fev counting to avoid 2x earlier termina…
Browse files Browse the repository at this point in the history
…tion at max_iterations/2
  • Loading branch information
pafonine committed Dec 8, 2024
1 parent 082a702 commit 4af7b61
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
8 changes: 6 additions & 2 deletions scitbx/lbfgsb.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ def run(target_evaluator,
- If bounds are specified, the optimization is constrained within the
provided lower and upper bounds.
"""
#
callback_after_step = getattr(target_evaluator, "callback_after_step", None)
lbfgsb_minimizer = minimizer(
n = n,
l = lower_bound,
Expand All @@ -106,15 +106,19 @@ def run(target_evaluator,
try:
icall = 0
while 1:
icall += 1
have_request = lbfgsb_minimizer.process(x, f, g)
if(have_request):
requests_f_and_g = lbfgsb_minimizer.requests_f_and_g()
x, f, g = target_evaluator.compute_functional_and_gradients()
icall += 1
continue
assert not lbfgsb_minimizer.requests_f_and_g()
if(lbfgsb_minimizer.is_terminated()): break
if(max_iterations is not None and icall>max_iterations): break
if(callback_after_step is not None):
if(callback_after_step(minimizer) is True):
print("lbfgs minimizer stop: callback_after_step is True")
break
except RuntimeError as e:
lbfgsb_minimizer.error = str(e)
lbfgsb_minimizer.n_calls = icall
Expand Down
2 changes: 2 additions & 0 deletions scitbx/minimizers.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,8 @@ def __init__(self,
Initialize the minimizer with the selected mode and calculator object.
"""
adopt_init_args(self, locals())
self.callback_after_step = getattr(
self.calculator, "callback_after_step", None)
assert mode in ['lbfgs', 'lbfgsb']
self.x = self.calculator.x
# necessary? also done in run_c_plus_plus
Expand Down

0 comments on commit 4af7b61

Please sign in to comment.