-
Notifications
You must be signed in to change notification settings - Fork 608
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
Conversation
e94cd1f
to
4859f16
Compare
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
is there something more to do for the java rebase ? |
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.
0ea83d7
to
5e6ff7e
Compare
Done. This is now rebased and the Java documentation commit added. Should be ready for merge now. |
thanks a lot, I merged everything |
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.