I am using nlopt's c++ interface to solve non-linear optimisation problems.
nlopt::opt opt;
opt.set_maxeval(10);
opt.set_max_objective(foo);
double result;
std::vector<double> params(10,0);
opt.optimize(params, result);
// later on in the code
opt.optimize(params, result); // this uses the updated param values, but the step size starts from scratch
However, I'd like the last line to continue optimising not just with the last params value, but also with the step_size that was last used, but I don't think I have access to that value.
How can I achieve something like this?
Caveat: This is a guess--something for you to try.
The object has a member double *dx that stores the step. If you do not explicitly specify values for initial step, the dx value is allocated at the start of nlopt_optimize with default values and freed at the end.
Look at the API documentation: http://ab-initio.mit.edu/wiki/index.php/NLopt_Reference
There are two functions of interest: nlopt_get_initial_step and nlopt_set_initial_step.
Normal usage is using values of your own choosing, call set_initial_step with them. Then, you call optimize. Then, you repeat this process with your own new values.
But, for what you want, try this:
Initially, use get_initial_step to get the default values. Then, do an explicit set with set_initial_step. This will make the internal dx array in the object persist after optimize is called. I checked this in the nlopt source code.
Now, call optimize.
Now, peek at the dx array inside the object.
If you get updated values--you're home free. If not, well, you may need to write some custom code.
You may just be able to continue calling optimize but not doing the set_initial_step call the second or third time.
Pseudo code for the "normal" case:
// normal usage
obj.set_initial_step(my_values_1);
obj.optimize();
obj.set_initial_step(my_values_2);
obj.optimize();
Pseudo code for the "trick":
// special usage
obj.get_initial_step(x,my_values_1);
obj.set_initial_step(my_values_1);
obj.optimize();
// check to see if the values have been updated relative to my_values_1
// if the trick works, this should use the updated values
obj.optimize();
// if the trick works, this should use the updated values
obj.optimize();
UPDATE:
Peeking at the object's dx value requires a bit of hacking/trickery. The standard nlopt.h treats nlopt_opt as an opaque pointer. The actual struct definition is only available from nlopt-internal.h which means you have to have the full source and extract the struct definition from that.
That's if you want positive confirmation that the values have changed. You may be able to infer [by some method] that you're getting different values (e.g. your limit function(s) detect something) without having to peek at dx.
Or, you can "wing it" by providing updated values using the API in its intended manner.
I tried a test program and didn't see the dx change, but I know little [read: nothing] about NL optimization, so I have no idea if my test case was valid or would show valid changes to dx
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