Skip to content
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
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

kkofler
Copy link

@kkofler kkofler commented Dec 20, 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.

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 kkofler force-pushed the exceptions-optional branch from e94cd1f to 4859f16 Compare January 14, 2025 19:22
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.
Copy link
Collaborator

@jschueller jschueller left a 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
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants