The problem seems to arise from initial step size which is set by
nlopt_set_default_initial_step. The algorithm BOBYQA seems to have
problems, if the initials steps for the different dimensions don't have the
same sign, even if the bound constraints are set (see example attached).
The initial step set in nlopt_set_default_initial_step is always positive,
except for cases with no boundaries. My suggestion for a possible work
around: When using the BOBYQA, add a loop in 'nlopt_optimize_' which makes
sure that the initial steps are positive before calling the function
'bobyqa'.
2013/4/13 [email protected] <[email protected]>
> I did some debugging and was able to track the error down, at least a bit.
> I attached a small example demonstrating the problem. The code exits with
> "nlopt invalid argument" if lower and upper bound are not set, otherwise
> everything works.
>
> 2013/4/13 [email protected] <[email protected]>
>
> Dear Steven,
>>
>> that was also my first thought, but I removed all constraints and the
>> error still occurs. I tested the following Code:
>>
>> -------------------------------
>> double minf;
>> nlopt::result result;
>>
>> nlopt::opt opt_first(nlopt::LN_BOBYQA, N);
>> opt_first.set_min_objective(opt_func,
>> reinterpret_cast<void*>(¶meter));
>> opt_first.set_xtol_rel(1e-4);
>> result = opt_first.optimize(Xmin, minf);
>>
>> nlopt::opt opt_second(nlopt::LN_BOBYQA, N);
>> opt_second.set_min_objective(opt_func,
>> reinterpret_cast<void*>(¶meter));
>> opt_second.set_xtol_rel(1e-4);
>> result = opt_second.optimize(Xmin, minf);
>> -------------------------------
>>
>> The first run works as expected, the second one fails without even
>> calling opt_func once. If I reset Xmin in between the two runs, the second
>> run works as expected. The code works for example with NEWUOA. I also tried
>> to reproduce the error with the Rosenbrock function as opt_func, but
>> everything works fine there.
>>
>> Thanks,
>> Klaus
>>
>> 2013/4/12 Steven G. Johnson <[email protected]>
>>
>>
>>> On Apr 12, 2013, at 7:54 AM, "[email protected]" <
>>> [email protected]> wrote:
>>>
>>> > Dear all,
>>> >
>>> > I'm using NLopt to solve a non-linear optimization problem without
>>> constraints. I was testing the algorithm BOBYQA, which is causing some
>>> trouble. Depending on the initial guess, I get an the following error:
>>> >
>>> > terminate called after throwing an instance of 'std::invalid_argument'
>>> > what(): nlopt invalid argument
>>>
>>> The initial guess must satisfy your bound constraints. Are you
>>> violating those?
>>>
>>>
>>> _______________________________________________
>>> NLopt-discuss mailing list
>>> [email protected]
>>> http://ab-initio.mit.edu/cgi-bin/mailman/listinfo/nlopt-discuss
>>>
>>
>>
>
#include <iostream>
#include <vector>
#include <assert.h>
#include <nlopt.hpp>
using namespace std;
double myvfunc(const std::vector<double> &x, std::vector<double> &grad, void *my_func_data)
{
assert ( grad.empty() );
double f = 0;
for (unsigned int i=0; i<x.size(); ++i) {
f += x.at(i)*x.at(i);
}
return f;
}
int main()
{
int N = 2;
std::vector<double> x(N);
double minf;
x.at(0) = 0.5;
x.at(1) = -0.5;
nlopt::opt opt(nlopt::LN_BOBYQA, N);
opt.set_initial_step(x);
opt.set_lower_bounds(-2);
opt.set_upper_bounds(2);
opt.set_min_objective(myvfunc, NULL);
opt.set_xtol_rel(1e-4);
nlopt::result result = opt.optimize(x, minf);
for (int i=0; i<N; ++i) {
cout << x.at(i) << " ";
}
cout << endl;
}
_______________________________________________
NLopt-discuss mailing list
[email protected]
http://ab-initio.mit.edu/cgi-bin/mailman/listinfo/nlopt-discuss