Repository navigation
cobyla: stop when the trust-region step is nan - #653
Merged
Merged
Conversation
When rho becomes so small that rho*rho underflows, trstlp can compute stpful = 0/0. The existing check only caught an infinite step. With a nan step, "step == stpful" is false, so trstlp switched to stage two again and looped forever without evaluating the objective, ignoring maxeval and maxtime (stevengj#370). In other cases the nan step reached cobylb, which then evaluated the objective at nan points until maxeval. Return NLOPT_ROUNDOFF_LIMITED for any non-finite step, as for an infinite one. Add a test with a quadratic (default tolerances) and the example from stevengj#370; both looped forever before.
stevengj
approved these changes
Oct 17, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #370.
The example in #370 (COBYLA on
(2 - cos(x0) + x1^2)^2from (0, 0)) never returns withmaxeval668 or more, even withmaxtimeset; withmaxeval667 it returns. The cause is a nan step intrstlp.When
rhobecomes very small,trstlpcan computestpful = dd / (temp + sd)as 0/0 = nan. There is a check after it from 2010:It does not catch nan. Then, at the end of the iteration,
step == stpfulis false for nan, so the code falls through toL480, "switches to stage two" again and repeats this forever. No function evaluation happens in this loop, somaxevalandmaxtimeare never checked. I found this by counting the passes through the labels in the quadratic example below: onetrstlpcall went throughL480more than 10^7 times withstepandstpfulequal to nan andrho= 7.5e-155 (sorho*rhois subnormal).In other cases the nan step leaves
trstlp, andcobylbevaluates the objective at nan points untilmaxeval. With the six-hump camel function on [-3,3] x [-2,2] from (0.5, 0.5) andmaxeval1000, master calls the objective 32 times with x = (nan, nan) after evaluation 690.This PR makes the check
!nlopt_isfinite(step), so a nan step also returnsNLOPT_ROUNDOFF_LIMITED, as an infinite step already did. This is also the code COBYLA returns whenrhoreachesrhoend= 0.Results with this change:
maxeval668 and 2000 both stop after 668 evaluations with roundoff-limited and f = 1 (the minimum).maxeval667 is unchanged.(x0-1)^2 + 10 (x1-2)^2from (0, 0) with default tolerances andmaxeval2000: master never returns; with this change it stops after 844 evaluations at (1, 2).testoptwith COBYLA on all 22 objectives, 3 seeds and 4 stopping rules (264 runs): 216 runs are identical. In the other 48 runs (objectives 14, 16, 17, 18, all 2-D), master runs to the evaluation limit of 1000 and returnsNLOPT_MAXEVAL_REACHED; with this change the run stops after 692 to 725 evaluations withNLOPT_ROUNDOFF_LIMITEDand the same minimum. These are the runs where master evaluated at nan points.So users who get
NLOPT_MAXEVAL_REACHEDin such a case will getNLOPT_ROUNDOFF_LIMITEDinstead, which the C++ and Python interfaces raise as an exception; the best point and value are the same.New test
test/t_cobyla_nan_step.cxxruns the two examples above. On master it does not return.ctestwith MSVC 2022 on Windows: 78 of 78 tests pass.The reproducer in the 2020 comment on #370 calls a modified internal
trstlpwithrhoabout 1e6, so it may be a different cycle; I did not test it.