-
Notifications
You must be signed in to change notification settings - Fork 602
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
C++: Allow disabling exceptions in opt::optimize. #580
Open
kkofler
wants to merge
3
commits into
stevengj:master
Choose a base branch
from
kkofler:exceptions-optional
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Conversation
This file contains 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
jschueller
reviewed
Dec 20, 2024
jschueller
reviewed
Dec 21, 2024
Call opt.set_exceptions_enabled(false) before opt.optimize to avoid throwing an exception. The caller is then responsible for checking the returned result or opt.last_result(). The advantage is that, for the SWIG-friendly overload, the returned best point (which can be quite good in, e.g., the roundoff_limited case) is not lost (but then the caller MUST check opt.last_result() to know whether anything has gone wrong). The default is exceptions_enabled=true, so by default, nothing changes. src/api/nlopt-in.hpp (opt::exceptions_enabled): New private member variable. (opt::opt (all overloads)): Initialize it (default it to true, copy it in the copy constructor). (opt::operator=): Copy it. (opt::optimize): Only call mythrow if exceptions_enabled. (get_exceptions_enabled): New public inline getter. (set_exceptions_enabled): New public inline setter.
doc/docs/NLopt_C-plus-plus_Reference.md, doc/docs/NLopt_Guile_Reference.md, doc/docs/NLopt_Python_Reference.md (Performing the optimization): Add "by default" wording to the claim that `optimize()` throws an exception for negative return codes. (The sentence already links to the "Exceptions" section that has the details.) (Exceptions): Document nlopt::opt::set_exceptions_enabled and nlopt::opt::get_exceptions_enabled, and adapt the remainder of the section to mention their impact where appropriate.
kkofler
force-pushed
the
exceptions-optional
branch
from
January 14, 2025 19:22
e94cd1f
to
4859f16
Compare
test/t_java.java: Tests exception handling. This tests a test case known to fail (t_bounded.cxx on LD_SLSQP) with set_exceptions_enabled set to true if the argument is 1, false if the argument is 0, and verifies that the exception is thrown resp. not thrown, as expected. test/CMakeLists.txt: Add the C++ test t_except with the instances 1 (true = exceptions enabled) and 0 (false = exceptions disabled). Both instances pass.
jschueller
approved these changes
Jan 15, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok for me
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.
Call opt.set_exceptions_enabled(false) before opt.optimize to avoid throwing an exception. The caller is then responsible for checking the returned result or opt.last_result(). The advantage is that, for the SWIG-friendly overload, the returned best point (which can be quite good in, e.g., the roundoff_limited case) is not lost (but then the caller MUST check opt.last_result() to know whether anything has gone wrong). The default is exceptions_enabled=true, so by default, nothing changes.
src/api/nlopt-in.hpp (opt::exceptions_enabled): New private member variable.
(opt::opt (all overloads)): Initialize it (default it to true, copy it in the copy constructor).
(opt::operator=): Copy it.
(opt::optimize): Only call mythrow if exceptions_enabled. (get_exceptions_enabled): New public inline getter. (set_exceptions_enabled): New public inline setter.