nlopt_opt NLOPT_STDCALL nlopt_create(nlopt_algorithm algorithm, unsigned n)
{
     nlopt_opt opt;

     if (((int) algorithm) < 0 || algorithm >= NLOPT_NUM_ALGORITHMS)
	  return NULL;

     opt = (nlopt_opt) malloc(sizeof(struct nlopt_opt_s));
     if (opt) {
	  opt->algorithm = algorithm;
	  opt->n = n;
	  opt->f = NULL; opt->f_data = NULL; opt->pre = NULL;
	  opt->maximize = 0;
	  opt->munge_on_destroy = opt->munge_on_copy = NULL;

	  opt->lb = opt->ub = NULL;
	  opt->m = opt->m_alloc = 0;
	  opt->fc = NULL;
	  opt->p = opt->p_alloc = 0;
	  opt->h = NULL;

	  opt->stopval = -HUGE_VAL;
	  opt->ftol_rel = opt->ftol_abs = 0;
	  opt->xtol_rel = 0; opt->xtol_abs = NULL;
	  opt->maxeval = 0;
	  opt->maxtime = 0;
	  opt->force_stop = 0;
	  opt->force_stop_child = NULL;

	  opt->local_opt = NULL;
	  opt->stochastic_population = 0;
	  opt->vector_storage = 0;
	  opt->dx = NULL;
	  opt->work = NULL;

	  if (n > 0) {
	       opt->lb = (double *) malloc(sizeof(double) * (n));
	       if (!opt->lb) goto oom;
	       opt->ub = (double *) malloc(sizeof(double) * (n));
	       if (!opt->ub) goto oom;
	       opt->xtol_abs = (double *) malloc(sizeof(double) * (n));
	       if (!opt->xtol_abs) goto oom;
	       nlopt_set_lower_bounds1(opt, -HUGE_VAL);
	       nlopt_set_upper_bounds1(opt, +HUGE_VAL);
	       nlopt_set_xtol_abs1(opt, 0.0);
	  }
     }

     return opt;

oom:
     nlopt_destroy(opt);
     return NULL;
}
Esempio n. 2
0
void minim_nlopt(density_t *ndft){

  nlopt_opt opt;
  double minf;
  int i, status;
  double *x;

  nlopt_function_data function_data;
  nlopt_par par;

  x  = (double *)malloc(ipf.npar*sizeof(double));
  for(i = 0; i < ipf.npar; i++) x[i] = gsl_vector_get(ipf.gamma, i);

  /* Here we choose the minimization method from NLOPT (check the
     avaible algorithms at
     http://ab-initio.mit.edu/wiki/index.php/NLopt_Algorithms)
     and specify the dimension of the problem */
  par.algorithm = 1;  parse_int("nlopt_algorithm", &par.algorithm);
  switch(par.algorithm){
  case 1 :
    opt = nlopt_create(NLOPT_LN_BOBYQA, ipf.npar);
    if(myid == 0)  printf("\n\n%s", nlopt_algorithm_name(NLOPT_LN_BOBYQA));
    break;
  case 2 :
    opt = nlopt_create(NLOPT_GN_DIRECT, ipf.npar);
    if(myid == 0)  printf("\n\n%s", nlopt_algorithm_name(NLOPT_GN_DIRECT));
    break;
  case 3 :
    opt = nlopt_create(NLOPT_GD_STOGO, ipf.npar);
    if(myid == 0)  printf("\n\n%s", nlopt_algorithm_name(NLOPT_GD_STOGO));
    break;
  case 4:
    opt = nlopt_create(NLOPT_GN_ESCH, ipf.npar);
    if(myid == 0)  printf("\n\n%s", nlopt_algorithm_name(NLOPT_GN_ESCH));
    break;
  default :
    if(myid == 0)  printf("\n\nWARNING: wrong choice for the NLOPT algorithm\nTHe optimization will start  with the default algorithm:");
    opt = nlopt_create(NLOPT_GN_DIRECT_L, ipf.npar);
    if(myid == 0)  printf("\n%s", nlopt_algorithm_name(NLOPT_GN_DIRECT_L));
    break;
  }

  nlopt_set_min_objective(opt, nlopt_gfunction, &function_data);

  par.rel_tol = 1e-4;  parse_double("nlopt_rel_tol", &par.rel_tol);
  nlopt_set_xtol_rel(opt, par.rel_tol);

  /* Set the lower and upper bounds of the optimization
     to a single constant lb or ub respectively */
  par.lb = -2.0;  parse_double("nlopt_lb", &par.lb);
  par.ub = 2.0;  parse_double("nlopt_ub", &par.ub);
  nlopt_set_lower_bounds1(opt, par.lb);
  nlopt_set_upper_bounds1(opt, par.ub);

  function_data.counter = 0;
  function_data.ndft = density_get_val(ndft);

  messages_basic("\n\n\nStarting the optimization.\n\n\n");

  if ((status = nlopt_optimize(opt, x, &minf)) <  0){
    if (myid == 0) printf("nlopt failed! status = %d\n", status);
    parallel_end(EXIT_FAILURE);
  }

  if(myid == 0) printf("Success. status = %d, iterations = %d, minf = %.12lg\n", status, function_data.counter, minf);
  if(myid == 0) {for(i = 0; i < ipf.npar; i++) printf("%.5f ", x[i]);}

  nlopt_destroy(opt);

  for(i = 0; i < ipf.npar; i++) gsl_vector_set(ipf.gamma, i, x[i]);
  ipf_write(ipf, ipf_ref, "pot");

  parallel_end(EXIT_SUCCESS);
}