I am running an optimization using NLOpt's Python interface. At a certain point, after a number of iterations, I get an nlopt.RoundoffLimited exception. According to the documentation (http://ab-initio.mit.edu/wiki/index.php/NLopt_Reference#Error_codes_.28negative_return_values.29), after such an exception, "the optimization still typically returns a useful result." How do I actually view the intermediate result? I am running code like:
opt = nlopt.opt(...)
# ... some optimization settings
try:
opt_results = opt.optimize(guess)
except nlopt.RoundoffLimited:
# How do I get the latest parameters from opt,
# after the optimization has failed?
I can get the objective value just fine using opt.last_optimize_result(), but I can't find the API call to get the parameters that result in this objective value.
Thanks!
I still haven't found a particularly elegant solution, but I'll post this for now in case somebody stumbles upon the same problem. Here is one approach to recovering the previous valid optimization parameters prior to an optimization exception:
# globals
previous_args = None
current_args = None
# objective function
def objective_function(args, gradient):
global previous_args
global current_args
previous_args = current_args
current_args = args
# ... the rest of your objective function
return function_value
# optimization code using objective_function
opt = nlopt.opt(...)
try:
args = opt.optimize(guess)
except nlopt.RoundoffLimited:
args = previous_args
# you should do some sanity checks on args.
# for example, one reason you may see RoundoffLimited
# is args on the order of 1e-300 or so.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With