Hello

I am new starting the library and I am having a probably very silly problem
using it. I modified the example program to do optimize the function
x[0]^2+x[1]^1 with the bound contraint -10<x[i]<10. when I use
the NLOPT_LD_MMA everything works fine, then when I try to use the DIRECT_L
algorithm in the following code:

#include <math.h>
#include <nlopt.h>

double myfunc(unsigned n, const double *x, double *grad, void *my_func_data)
{
  /*if (grad) {
    grad[0] = 2*x[0];
    grad[1] = 2*x[1];
    }*/
  return x[1]*x[1]+x[0]*x[0];
}


int main(){
  double lb[2] = { -10, -10 }; /* lower bounds */
  double ub[2] = { 10, 10 }; /* lower bounds */
  nlopt_opt opt;
  //opt = nlopt_create(NLOPT_LD_MMA, 2); /* algorithm and dimensionality */
  opt = nlopt_create(NLOPT_GN_DIRECT_L, 2); /* algorithm and dimensionality
*/
  nlopt_set_lower_bounds(opt, lb);
  nlopt_set_min_objective(opt, myfunc, NULL);

  nlopt_set_xtol_rel(opt, 1e-4);
  double x[2] = { 1.234, 5.678 };  /* some initial guess */
  double minf; /* the minimum objective value, upon return */
  int num=nlopt_optimize(opt, x, &minf);
  if (num < 0) {
    printf("nlopt failed %d!\n",num);
  }
  else {
    printf("found minimum at f(%g,%g) = %0.10g\n", x[0], x[1], minf);
  }

  nlopt_destroy(opt);

  return 0;

}

and I get:

 nlopt failed -2!

Do you have any idea what I am doing wrong?

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

Reply via email to