Hi,

When using the GN_MLSL algorithm and passing a pointer to a class as f_data in
void nlopt::opt::set_min_objective(nlopt::vfunc f, void* f_data)
I receive the following error at program execution:

Unhandled exception at 0xffffffff in test_mlsl.exe: 0xC0000005: Access 
violation reading location 0xffffffff.
Call Stack:
        ffffffff()      
>       test_mlsl.exe!nlopt::opt::free_myfunc_data(void * p=0x7c840c5b)  Line 
> 157 + 0x23 bytes  C++
        ffffffff()      


optimize() seems to run perfectly fine, I think the error occurs when the
nlopt::opt object goes out of scope.

I have attached a short sample program that produces the above error on
my windows system (using the precompiled DLL version 2.1.2).

If you don't see an immediate problem with my code, I would also appreciate
a quick test on a different system (*nix oder win) to see if the error
also occurs.

Regards,
Juergen


----- source -----

#include <iostream>
#include <nlopt.hpp>

using namespace std;

class Foo
{
public:
    double a;
    Foo(double a): a(a) {}
    double operator()(const vector<double>& vX, vector<double>& vGrad)
    {
        double res = 0;
        for (int i=0; i<(int)vX.size(); i++)
            res += vX[i] * a;
        return res;
    }
    static double wrap(const std::vector<double> &x, std::vector<double> &grad, 
void *data)
    {
        return (*reinterpret_cast<Foo*>(data))(x, grad);
    }
};


int main()
{
    int major, minor, bugfix;
    nlopt::version(major, minor, bugfix);
    cout << major << "." << minor << "." << bugfix << endl;

    const int N = 5;
    vector<double> lb(N, -10);
    vector<double> ub(N, 10);
    vector<double> x(N, 0);

    nlopt::opt opt(nlopt::GN_MLSL, N);
    opt.set_lower_bounds(lb);
    opt.set_upper_bounds(ub);

    nlopt::opt optLocal(nlopt::LN_NELDERMEAD, N);
    opt.set_local_optimizer(optLocal);

    Foo foo(1.0);
    opt.set_min_objective(Foo::wrap, &foo);
    opt.set_maxtime(1);

    double minval = 0;
    nlopt::result result = opt.optimize(x, minval);
    cout << minval << " at ";
    for (int i=0; i<N; i++)
        cout << " " << x[i];
    cout << endl;

    return 0;
}



_______________________________________________
NLopt-discuss mailing list
[email protected]
http://ab-initio.mit.edu/cgi-bin/mailman/listinfo/nlopt-discuss

Reply via email to