Hi,
I wanted to use R's lbfgsb method for minimization from C. Unfortunately,
my toy examples always crashes (segmentation fault). What's wrong with it?

double eval(int n, double* par, void *ex) {
  double result = 0;
  for (int i=0; i<n; ++i) {
    result += par[i]*par[i];
  }
  printf("result=%.2f\n", result);
  return result;
}

void grad(int n, double *par, double *gr, void *ex) {
  gr[0] = 2*par[0];
  gr[1] = 2*par[1];
}

int main(void) {
int n = 2;
int m = 5;
double init[] = {2,3};
double lower[] = {-100, -100};
double upper[] = {100, 100};
int nbd[] = {0, 0};
double Fmin;
int fail;
void *ex = 0;
double factr = 1e7;
double pgtol = 0;
int fncount;
int grcount;
int maxit = 10;
char msg[1000];
int trace = 0;
int nREPORT = 10;

/*  from http://cran.r-project.org/doc/manuals/R-exts.html#Optimization
             void lbfgsb(int n, int lmm, double *x, double *lower,
                      double *upper, int *nbd, double *Fmin, optimfn fn,
                      optimgr gr, int *fail, void *ex, double factr,
                      double pgtol, int *fncount, int *grcount,
                      int maxit, char *msg, int trace, int nREPORT); */

lbfgsb(n, m, init, lower, upper, nbd, &Fmin, &eval, &grad, &fail, ex,
factr, pgtol, &fncount, &grcount, maxit, msg, trace, nREPORT);

printf("optimum at (%.2f,%.2f)\n", init[0], init[1]);

return 0;
}

Thanks a lot.

        [[alternative HTML version deleted]]

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to