Search code examples
coptimizationnlopt

NLopt nullptr exception in C while using the NLOPT SLSQP(Gradient-Based-Algorithm)


Servus Guys I am doing a project about parameter identification with Nlopt(SLSQP), i wrote a test code, but after 3 iterantions Compiler always throw the exception about Nullptr at 'grad' in 'myfunc'(objectfunction) :

'Exception thrown: write access violation. grad was nullptr.'

, where i used finite difference to computing the gradient, because the the finite difference can compute gradient of complex model in my project.

i have tried to change a different step-size from 1e-8 to 1e-6 for finite difference ,after that the code works fine without exception,i dont know the reason, could someone tell me?

double myfunc(unsigned n, const double *x,double *grad,void *data){ 

    double h =1e-8;

    grad[0] = (log(x[0] + h) - log(x[0])) / h; //hier compiler throws exception
    grad[1] = (log(x[1] + h) - log(x[1])) / h;

    printf("\ngrad[0] is %10f grad[1] is %10f\n", grad[0], grad[1]);
    printf("\nx[0] is %10f x[1] is %10f\n",x[0],x[1]);

    return log(x[0]) + log(x[1]);
}

double myconstraint(unsigned n, const double *x, double *grad, void*data) {

    double *a = (double *)data; 
    grad[0] = a[0];
    grad[1] = a[1];
    return x[0] * a[0] + x[1] * a[1] - 5;
}

double myinconstraint(unsigned n, const double *x, double *grad, void *data) {

    grad[0] = 1;
    grad[1] = -1;
    return x[0] - x[1];
}

void main(){
    //test-code

    double f_max = -10000;
    double tol = 1e-16;
    double p[2] = { 1,2 };
    double x[2] = { 1,1 };
    double lb[2] = { 0,0 };
    double ub[2] = { 10000,10000 }; 

    nlopt_opt opter = nlopt_create(NLOPT_LD_SLSQP, 2);      
    nlopt_set_max_objective(opter, myfunc, NULL);

    nlopt_set_lower_bounds(opter, lb);
    nlopt_set_upper_bounds(opter, ub);
    nlopt_add_equality_constraint(opter, myconstraint, p, tol);
    nlopt_add_inequality_constraint(opter, myinconstraint, NULL, tol); 
    nlopt_set_xtol_rel(opter, tol);
    nlopt_set_ftol_abs(opter, tol);

    nlopt_result result = nlopt_optimize(opter, x, &f_max);//?

    printf("Maximum utility=%f, x=(%f,%f)\n", f_max, x[0], x[1]);

    system("pause");
}

hier is the result in command window with step size 1e-8

grad[0] is 1.000000 grad[1] is 1.000000

x[0] is 1.000000 x[1] is 1.000000

grad[0] is 0.600000 grad[1] is 0.600000

x[0] is 1.666667 x[1] is 1.666667

grad[0] is 0.600000 grad[1] is 0.600000

x[0] is 1.666667 x[1] is 1.666667

after that throws the compiler exception


Solution

  • You have to check if grad is NULL and only return the gradient when it is not NULL. From the documentation:

    Also, if the parameter grad is not NULL, then we set grad[0] and grad[1] to the partial derivatives of our objective with respect to x[0] and x[1]. The gradient is only needed for gradient-based algorithms; if you use a derivative-free optimization algorithm, grad will always be NULL and you need never compute any derivatives.

    Your code should therefore look like:

    double myfunc(unsigned n, const double *x,double *grad,void *data)
    { 
        double h = 1e-8;
    
        if (grad) {
            grad[0] = (log(x[0] + h) - log(x[0])) / h;
            grad[1] = (log(x[1] + h) - log(x[1])) / h;
        }
    
        return log(x[0]) + log(x[1]);
    }