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

Merged
merged 4 commits into from
Jan 23, 2025

Conversation

kkofler
Copy link
Contributor

@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.

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

@jschueller
Copy link
Collaborator

is there something more to do for the java rebase ?
or maybe just the java reference doc ?

@kkofler
Copy link
Contributor Author

kkofler commented Jan 23, 2025

Just the doc, give me a few minutes, I am going to take care of the rebase right now.

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.
doc/docs/NLopt_Java_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.setExceptionsEnabled and
nlopt.Opt.getExceptionsEnabled, and adapt the remainder of the section
to mention their impact where appropriate.
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.
@kkofler kkofler force-pushed the exceptions-optional branch from 0ea83d7 to 5e6ff7e Compare January 23, 2025 05:39
@kkofler
Copy link
Contributor Author

kkofler commented Jan 23, 2025

Done. This is now rebased and the Java documentation commit added. Should be ready for merge now.

@jschueller jschueller merged commit 7d87060 into stevengj:master Jan 23, 2025
3 checks passed
@jschueller
Copy link
Collaborator

thanks a lot, I merged everything

@kkofler kkofler deleted the exceptions-optional branch February 15, 2025 16:35
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