Example #1
0
int main() {
	double lb[2] = { -HUGE_VAL, 0 }; /* lower bounds */
	nlopt_opt opt;
	opt = nlopt_create(NLOPT_LD_MMA, 2); /* algorithm and dimensionality */
	nlopt_set_lower_bounds(opt, lb);
	nlopt_set_min_objective(opt, myfunc, NULL);
	my_constraint_data data[2] = { { 2, 0 }, { -1, 1 } };

	nlopt_add_inequality_constraint(opt, myconstraint, &data[0], 1e-8);
	nlopt_add_inequality_constraint(opt, myconstraint, &data[1], 1e-8);

	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 */

	if (nlopt_optimize(opt, x, &minf) < 0) {
		printf("nlopt failed!\n");
	}
	else {
		printf("found minimum at f(%g,%g) = %0.10g\n", x[0], x[1], minf);
	}

	nlopt_destroy(opt);
}
Example #2
0
static void phyanimal_fit_lambda(PhyAnimal model) {
  nlopt_opt opt;
  double lb, ub, lambda, loglh;

  
  opt = nlopt_create(NLOPT_LN_NELDERMEAD, 1); /* one dimension */
  lb = 0.0;
  ub = 1.0;
  nlopt_set_lower_bounds(opt,&lb);
  nlopt_set_upper_bounds(opt,&ub);
  /* No inequality constraints */
  nlopt_set_max_objective(opt, loglh_worker, model);
  nlopt_set_xtol_rel(opt, 1e-4);
  lambda = 0.1;
  if (nlopt_optimize(opt, &lambda, &loglh)<0) {
    printf("NLOPT failed\n");
  } else {
    printf("Maximum lh %f at lambda %f\n",loglh,lambda);
    printf("Sigma = %f\n",model->sigma);
    printf("Sigma_b = %f\nSigma_e = %f\n",model->sigma*lambda,model->sigma*(1-lambda));
  }
  model->loglh = loglh;
  model->lambda = lambda;
  nlopt_destroy(opt);
  return;
}
nlopt_opt make_opt(const mxArray *opts, unsigned n)
{
     nlopt_opt opt = NULL, local_opt = NULL;
     nlopt_algorithm algorithm;
     double *tmp = NULL;
     unsigned i;

     algorithm = (nlopt_algorithm)
	  struct_val_default(opts, "algorithm", NLOPT_NUM_ALGORITHMS);
     CHECK1(((int)algorithm) >= 0 && algorithm < NLOPT_NUM_ALGORITHMS,
	    "invalid opt.algorithm");

     tmp = (double *) mxCalloc(n, sizeof(double));
     opt = nlopt_create(algorithm, n);
     CHECK1(opt, "nlopt: out of memory");

     nlopt_set_lower_bounds(opt, struct_arrval(opts, "lower_bounds", n,
					       fill(tmp, n, -HUGE_VAL)));
     nlopt_set_upper_bounds(opt, struct_arrval(opts, "upper_bounds", n,
					       fill(tmp, n, +HUGE_VAL)));

     nlopt_set_stopval(opt, struct_val_default(opts, "stopval", -HUGE_VAL));
     nlopt_set_ftol_rel(opt, struct_val_default(opts, "ftol_rel", 0.0));
     nlopt_set_ftol_abs(opt, struct_val_default(opts, "ftol_abs", 0.0));
     nlopt_set_xtol_rel(opt, struct_val_default(opts, "xtol_rel", 0.0));
     nlopt_set_xtol_abs(opt, struct_arrval(opts, "xtol_abs", n,
					   fill(tmp, n, 0.0)));
     nlopt_set_maxeval(opt, struct_val_default(opts, "maxeval", 0.0) < 0 ?
		       0 : struct_val_default(opts, "maxeval", 0.0));
     nlopt_set_maxtime(opt, struct_val_default(opts, "maxtime", 0.0));

     nlopt_set_population(opt, struct_val_default(opts, "population", 0));
     nlopt_set_vector_storage(opt, struct_val_default(opts, "vector_storage", 0));

     if (struct_arrval(opts, "initial_step", n, NULL))
	  nlopt_set_initial_step(opt,
				 struct_arrval(opts, "initial_step", n, NULL));
     
     if (mxGetField(opts, 0, "local_optimizer")) {
	  const mxArray *local_opts = mxGetField(opts, 0, "local_optimizer");
	  CHECK1(mxIsStruct(local_opts),
		 "opt.local_optimizer must be a structure");
	  CHECK1(local_opt = make_opt(local_opts, n),
		 "error initializing local optimizer");
	  nlopt_set_local_optimizer(opt, local_opt);
	  nlopt_destroy(local_opt); local_opt = NULL;
     }

     mxFree(tmp);
     return opt;
}
Eigen::VectorXd TargetTrackingController::getControl(const EKF *ekf, const MultiAgentMotionModel *motionModel, const std::vector<const SensorModel*> &sensorModel, double *f) const {
  Evaluator evaluator(ekf, motionModel, sensorModel, params);

  Eigen::VectorXd p = Eigen::VectorXd::Zero(motionModel->getControlDim());
  Eigen::VectorXd lowerBound = Eigen::VectorXd::Constant(motionModel->getControlDim(), params("multi_rotor_control/controlMin").toDouble());
  Eigen::VectorXd upperBound = Eigen::VectorXd::Constant(motionModel->getControlDim(), params("multi_rotor_control/controlMax").toDouble());

  nlopt_opt opt = nlopt_create(NLOPT_LN_COBYLA, p.size());
//  nlopt_opt opt = nlopt_create(NLOPT_LN_BOBYQA, p.size());
//  nlopt_opt opt = nlopt_create(NLOPT_LN_NEWUOA_BOUND, p.size());
//  nlopt_opt opt = nlopt_create(NLOPT_LN_PRAXIS, p.size());
//  nlopt_opt opt = nlopt_create(NLOPT_LN_NELDERMEAD, p.size());
//  nlopt_opt opt = nlopt_create(NLOPT_LN_SBPLX, p.size());
//  nlopt_opt opt = nlopt_create(NLOPT_GN_ORIG_DIRECT, p.size()); // failed
//  nlopt_opt opt = nlopt_create(NLOPT_GN_ORIG_DIRECT_L, p.size()); // very good: p    0.0118546 -6.27225e-05  6.27225e-05 -2.09075e-05  2.09075e-05 -8.51788e-06 -2.09075e-05           10
//  nlopt_opt opt = nlopt_create(NLOPT_GN_ISRES, p.size()); // rather bad
//  nlopt_opt opt = nlopt_create(NLOPT_GN_CRS2_LM, p.size());
//  nlopt_opt opt = nlopt_create(NLOPT_LD_MMA, p.size());
//  nlopt_opt opt = nlopt_create(NLOPT_LD_CCSAQ, p.size());
//  nlopt_opt opt = nlopt_create(NLOPT_LD_SLSQP, p.size());
//  nlopt_opt opt = nlopt_create(NLOPT_LD_LBFGS, p.size());
//  nlopt_opt opt = nlopt_create(NLOPT_LD_TNEWTON_PRECOND, p.size()); // bad
//  nlopt_opt opt = nlopt_create(NLOPT_LD_TNEWTON_PRECOND_RESTART, p.size()); // bad
//  nlopt_opt opt = nlopt_create(NLOPT_LD_VAR2, p.size());

  nlopt_set_min_objective(opt, f_evaluate, &evaluator);
  nlopt_set_lower_bounds(opt, lowerBound.data());
  nlopt_set_upper_bounds(opt, upperBound.data());
  nlopt_set_ftol_abs(opt, 1E-6);
  nlopt_set_xtol_rel(opt, 1E-3);
  nlopt_set_maxeval(opt, 1E8);
  nlopt_set_maxtime(opt, 7200);
  double pa[p.size()];
  memcpy(pa, p.data(), p.size()*sizeof(double));
  double cost = 0;
//  std::string tmp; std::cerr << "Press enter to start optimization\n"; std::getline(std::cin, tmp);
  nlopt_result ret = nlopt_optimize(opt, pa, &cost);
  Eigen::VectorXd p_res = Eigen::Map<Eigen::VectorXd>(pa, p.size());
  if (f)
    *f = cost;

  std::cerr << "\nInitial guess:\n";
  std::cerr << "  p " << p.transpose() << "\n";
  std::cerr << "  value " << evaluator.evaluate(p) << "\n";

  std::cerr << "Optimization result (return code " << ret << "):\n";
  std::cerr << "  p " << p_res.transpose() << "\n";
  std::cerr << "  value " << evaluator.evaluate(p_res) << "\n";
  nlopt_destroy(opt);
  return p_res;
}
Example #5
0
double Optimize_Torsion_Parameters(int Idx_Soft_Tor)
{
	double lb[N_TOR_PARA] = { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, -100.0 }; /* lower bounds */
	double ub[N_TOR_PARA] = { 12.0, 12.0, 12.0, 12.0, 12.0, 12.0, 12.0, 12.0, 12.0, 12.0,  100.0 }; /* lower bounds */
	double Phase[32][5]={{0, 0, 0, 0, 0}, {0, 0, 0, 0, PI}, {0, 0, 0, PI, 0}, {0, 0, 0, PI, PI}, {0, 0, PI, 0, 0}, {0, 0, PI, 0, PI}, {0, 0, PI, PI, 0}, {0, 0, PI, PI, PI}, {0, PI, 0, 0, 0}, {0, PI, 0, 0, PI}, 
						{0, PI, 0, PI, 0}, {0, PI, 0, PI, PI}, {0, PI, PI, 0, 0}, {0, PI, PI, 0, PI}, {0, PI, PI, PI, 0}, {0, PI, PI, PI, PI}, {PI, 0, 0, 0, 0}, {PI, 0, 0, 0, PI}, {PI, 0, 0, PI, 0}, {PI, 0, 0, PI, PI},
						{PI, 0, PI, 0, 0}, {PI, 0, PI, 0, PI}, {PI, 0, PI, PI, 0}, {PI, 0, PI, PI, PI}, {PI, PI, 0, 0, 0}, {PI, PI, 0, 0, PI}, {PI, PI, 0, PI, 0}, {PI, PI, 0, PI, PI}, {PI, PI, PI, 0, 0}, {PI, PI, PI, 0, PI}, 
						{PI, PI, PI, PI, 0}, {PI, PI, PI, PI, PI}};
	nlopt_opt opt;
	double Chi_SQ;
	int i, j, Idx_Phi;

	Chi_SQ_Min = 1.0E100;
	opt = nlopt_create(NLOPT_LD_LBFGS, N_TOR_PARA);

	nlopt_set_lower_bounds(opt, lb);
	nlopt_set_upper_bounds(opt, ub);
	nlopt_set_min_objective(opt, Callback_Eval_Gradient, NULL);
	nlopt_set_xtol_rel(opt, 1e-8);

	iseed = time(NULL);
	
	ActiveRun = 0;
	Chi_SQ_Min_Local[0] = 1.0E200;
	FailCount = Iteration = 0;

	Idx_Phi = IdxDihSelect[Idx_Soft_Tor];
	
	Para_Tor[10] = 0.0;
	
	for(i=0; i<N_TOR_PARA; i++)	{
		IsPara_Fixed[i] = 1;
	}
	IsPara_Fixed[N_TOR_PARA-1] = 0;	// only optimize the last parameter

	if (nlopt_optimize(opt, Para_Tor, &Chi_SQ) < 0) {
		printf("nlopt failed!\n");
	}
	else {
		printf("Chi_SQ_min = %lf  Chi_SQ = %lf\n", Chi_SQ_Min, Chi_SQ);
	}

	memcpy(Para_Tor, Para_Best, sizeof(double)*N_TOR_PARA);
	CalObjectiveFunction_Tor(Para_Tor);


	nlopt_destroy(opt);

	return Chi_SQ_Min;
}
Example #6
0
/*
 * NLOPT optimization routine for table tennis traj gen
 *
 */
void nlopt_optim_poly_run(double *x, double *params) {

	static double tol[EQ_CONSTR_DIM];
	static double lb[OPTIM_DIM]; /* lower bounds */
	static double ub[OPTIM_DIM]; /* upper bounds */

	set_bounds(lb,ub);
	const_vec(EQ_CONSTR_DIM,1e-2,tol); /* set tolerances equal to second argument */

	nlopt_opt opt;
	opt = nlopt_create(NLOPT_LN_COBYLA, OPTIM_DIM); /* LN = does not require gradients */
	//nlopt_set_xtol_rel(opt, 1e-2);
	//opt = nlopt_create(NLOPT_AUGLAG, OPTIM_DIM); /* algorithm and dimensionality */
	//nlopt_set_local_optimizer(opt, opt);
	nlopt_set_lower_bounds(opt, lb);
	nlopt_set_upper_bounds(opt, ub);
	nlopt_set_min_objective(opt, costfunc, params);
	nlopt_add_inequality_mconstraint(opt, INEQ_CONSTR_DIM, joint_limits_ineq_constr, params, tol);
	nlopt_add_equality_mconstraint(opt, EQ_CONSTR_DIM, kinematics_eq_constr, params, tol);
	nlopt_set_xtol_rel(opt, 1e-2);

	//int maxeval = 20000;
	//nlopt_set_maxeval(opt, maxeval);

	//double maxtime = 0.001;
	//nlopt_set_maxtime(opt, maxtime);

	//init_soln_to_rest_posture(x,params); //parameters are the initial joint positions q0
	double initTime = get_time();
	double minf; /* the minimum objective value, upon return */

	if (nlopt_optimize(opt, x, &minf) < 0) {
	    printf("NLOPT failed!\n");
	}
	else {
		//nlopt_example_run();
		printf("NLOPT took %f ms\n", (get_time() - initTime)/1e3);
	    printf("Found minimum at f = %0.10g\n", minf);
	    test_optim(x,params);
	}
	nlopt_destroy(opt);
}
Example #7
0
double NLfit::run_method(vector<realt>* best_a)
{
    if (opt_ != NULL && na_ != (int) nlopt_get_dimension(opt_)) {
        nlopt_destroy(opt_);
        opt_ = NULL;
    }

    if (opt_ == NULL) {
        opt_ = nlopt_create(algorithm_, na_);
        nlopt_set_min_objective(opt_, calculate_for_nlopt, this);
    }

    // this is also handled in Fit::common_termination_criteria()
    nlopt_set_maxtime(opt_, F_->get_settings()->max_fitting_time);
    nlopt_set_maxeval(opt_, max_eval() - 1); // save 1 eval for final calc.
    nlopt_set_ftol_rel(opt_, F_->get_settings()->ftol_rel);
    nlopt_set_xtol_rel(opt_, F_->get_settings()->xtol_rel);

    double *lb = new double[na_];
    double *ub = new double[na_];
    for (int i = 0; i < na_; ++i) {
        const RealRange& d = F_->mgr.get_variable(i)->domain;
        lb[i] = d.lo;
        ub[i] = d.hi;
    }
    nlopt_set_lower_bounds(opt_, lb);
    nlopt_set_upper_bounds(opt_, ub);
    delete [] lb;
    delete [] ub;

    double opt_f;
    double *a = new double[na_];
    for (int i = 0; i < na_; ++i)
        a[i] = a_orig_[i];
    nlopt_result r = nlopt_optimize(opt_, a, &opt_f);
    F_->msg("NLopt says: " + S(nlresult_to_string(r)));
    best_a->assign(a, a+na_);
    delete [] a;
    return opt_f;
}
Example #8
0
File: LMM.cpp Project: gpcr/rvtests
  void calculateSigma2() {
    // use non linear optimization to calculate sigma2
    nlopt_opt opt;
    const int nParam = sigma2.size();
    std::vector<double> lb(nParam, 1e-10);
    
    opt = nlopt_create(NLOPT_LN_COBYLA, nParam);
    nlopt_set_lower_bounds(opt, lb.data());
    nlopt_set_min_objective(opt, goalFunction, this);
    nlopt_set_xtol_rel(opt, 1e-4);

    double minf; /* the minimum objective value, upon return */
    int retCode = nlopt_optimize(opt, sigma2.data(), &minf);
    if (retCode < 0) {
#ifdef DEBUG              
      printf("nlopt failed [ %d ]!\n", retCode);
#endif
    } else {
#ifdef DEBUG              
      printf("found minimum at %g\n", minf);
#endif
    }
    nlopt_destroy(opt);
  }
Example #9
0
nlopt_result
NLOPT_STDCALL nlopt_minimize_econstrained(
     nlopt_algorithm algorithm,
     int n, nlopt_func_old f, void *f_data,
     int m, nlopt_func_old fc, void *fc_data_, ptrdiff_t fc_datum_size,
     int p, nlopt_func_old h, void *h_data_, ptrdiff_t h_datum_size,
     const double *lb, const double *ub, /* bounds */
     double *x, /* in: initial guess, out: minimizer */
     double *minf, /* out: minimum */
     double minf_max, double ftol_rel, double ftol_abs,
     double xtol_rel, const double *xtol_abs,
     double htol_rel, double htol_abs,
     int maxeval, double maxtime)
{
     char *fc_data = (char *) fc_data_;
     char *h_data = (char *) h_data_;
     nlopt_opt opt;
     nlopt_result ret;
     int i;

     if (n < 0 || m < 0 || p < 0) return NLOPT_INVALID_ARGS;

     opt = nlopt_create(algorithm, (unsigned) n);
     if (!opt) return NLOPT_INVALID_ARGS;

     ret = nlopt_set_min_objective(opt, (nlopt_func) f, f_data);
     if (ret != NLOPT_SUCCESS) { nlopt_destroy(opt); return ret; }

     for (i = 0; i < m; ++i) {
	  ret = nlopt_add_inequality_constraint(opt, (nlopt_func) fc, 
						fc_data + i*fc_datum_size,
						0.0);
	  if (ret != NLOPT_SUCCESS) { nlopt_destroy(opt); return ret; }
     }

     (void) htol_rel; /* unused */
     for (i = 0; i < p; ++i) {
	  ret = nlopt_add_equality_constraint(opt, (nlopt_func) h, 
					      h_data + i*h_datum_size,
					      htol_abs);
	  if (ret != NLOPT_SUCCESS) { nlopt_destroy(opt); return ret; }
     }

     ret = nlopt_set_lower_bounds(opt, lb);
     if (ret != NLOPT_SUCCESS) { nlopt_destroy(opt); return ret; }
     ret = nlopt_set_upper_bounds(opt, ub);
     if (ret != NLOPT_SUCCESS) { nlopt_destroy(opt); return ret; }

     ret = nlopt_set_stopval(opt, minf_max);
     if (ret != NLOPT_SUCCESS) { nlopt_destroy(opt); return ret; }

     ret = nlopt_set_ftol_rel(opt, ftol_rel);
     if (ret != NLOPT_SUCCESS) { nlopt_destroy(opt); return ret; }
     ret = nlopt_set_ftol_abs(opt, ftol_abs);
     if (ret != NLOPT_SUCCESS) { nlopt_destroy(opt); return ret; }

     ret = nlopt_set_xtol_rel(opt, xtol_rel);
     if (ret != NLOPT_SUCCESS) { nlopt_destroy(opt); return ret; }
     ret = nlopt_set_xtol_abs(opt, xtol_abs);
     if (ret != NLOPT_SUCCESS) { nlopt_destroy(opt); return ret; }
     
     ret = nlopt_set_maxeval(opt, maxeval);
     if (ret != NLOPT_SUCCESS) { nlopt_destroy(opt); return ret; }

     ret = nlopt_set_maxtime(opt, maxtime);
     if (ret != NLOPT_SUCCESS) { nlopt_destroy(opt); return ret; }

     ret = nlopt_optimize(opt, x, minf);

     nlopt_destroy(opt);
     return ret;
}
double ColorLines::findMagnitude(const Mat *img, Mat *alpha_mat, double a[3])
{
    int w=img->cols;
    int h=img->rows;
    int t=img->type();
    int c;


    if(img->type()==CV_8UC3){
        c=3;
    } else if(img->type()==CV_8UC1){
        c=1;
    }
    uchar *p_data=img->data;
    uchar *alpha_data=alpha_mat->data;

    //cout << *alpha_mat << endl;
    double img_div[h*w*c];
    double *alpha=(double*)malloc(h*w*sizeof(double));

    for(int i=0; i<h; i++){
        for(int j=0; j<w; j++){
            alpha[i*w+j]=(double)alpha_data[w*i+j]/255.0l;
            for(int k=0; k<c; k++){
                img_div[c*(w*i+j)+k]=(double(p_data[c*(w*i+j)+k])/255.0l)/a[k];
            }
        }
    }



    double *withoutA = (double*)malloc(h*w*c*sizeof(double));

    double temp;
    for(int i=0; i<h; i++){
        for(int j=0; j<w; j++){
            for(int k=0; k<c; k++){


                temp=(double(p_data[c*(w*i+j)+k])/255.0l)-alpha[w*i+j]*a[k];

                if(temp>0){
                    withoutA[c*(w*i+j)+k]=temp;
                } else{
                    withoutA[c*(w*i+j)+k]=0;
                }
            }
        }
    }


    double *gray=(double*)malloc(w*h*sizeof(double));
    double initMag=0.5;
    bool isNegative=true;

    while(isNegative){
            initMag+=0.1;
        isNegative=false;
        for(int i=0; i<h; i++){
            for(int j=0; j<w; j++){
                gray[w*i+j]=0;
                for(int k=0; k<c; k++){
                    temp=withoutA[c*(w*i+j)+k]/(1-alpha[w*i+j]/initMag);
                    isNegative=temp<0;
                    gray[w*i+j]+=temp*temp;
                }
                gray[w*i+j]=sqrt(gray[w*i+j]);

            }
        }
    }


    double *transmission=(double*)malloc(w*h*sizeof(double));
    for(int i=0; i<w*h; i++){
        transmission[i]=1-alpha[i]/initMag;
    }

    double alpha_min=transmission[0];
    double alpha_max=transmission[0];
    for(int i=1; i<w*h; i++){
        if(transmission[i]>alpha_max){
            alpha_max=transmission[i];
        } else if(transmission[i]<alpha_min){
            alpha_min=transmission[i];
        }

    }

    int numBins=50;
    double binW=(alpha_max-alpha_min)/(double)numBins;
    double mtrans[numBins+1];
    double mshading[numBins+1];
    mtrans[0]=alpha_min;
    mshading[0]=0;
    mtrans[numBins]=alpha_max;
    for(int i=1; i<numBins; i++){
        mtrans[i]=mtrans[i-1]+binW;
        mshading[i]=0;
    }

    double val;
    double *inBin=(double*)malloc(w*h*sizeof(double));
    int ind;

    for(int i=0; i<=numBins; i++){
        ind=0;
        val=mtrans[i]+binW/2.0;
        for(int j=0; j<w*h; j++){
            if(transmission[j]<val+binW/2.0 && transmission[j]>=val-binW/2.0){
                inBin[ind]=gray[j];
                ind++;
            }
        }
        if(ind>100){
            qsort(inBin, ind, sizeof(double), comp);
            mshading[i]=inBin[(int)(0.995*ind+0.5)-1];
        }
    }
    double ak[2];
    ak[0]=1;
    ak[1]=1;
    struct_fitError entrada;
    entrada.withoutA=withoutA;
    entrada.alpha=alpha;
    entrada.mshading=mshading;
    entrada.h=w;
    entrada.w=h;
    entrada.c=c;
    entrada.numBins=numBins;
    entrada.initMag=initMag;

    cout << " OPTIMIZATION " << endl;
    nlopt_opt opt;

    //algoritmos de minimização, nao sei qual e melhor..
    opt = nlopt_create(NLOPT_GN_DIRECT_L, 2);
    //opt = nlopt_create(NLOPT_GN_DIRECT, 2);
    //opt = nlopt_create(NLOPT_GN_CRS2_LM, 2);
    //opt = nlopt_create(NLOPT_GD_STOGO, 2);
    //opt = nlopt_create( NLOPT_GD_STOGO_RAND, 2);
    //opt = nlopt_create(NLOPT_GN_ISRES, 2);
    //opt = nlopt_create(NLOPT_GN_ESCH, 2);

    //valores maximos e minimos do espaço de busca, nao sei se 0 e 1 sao valores bons
    double lb[2]={0,0};
    double ub[2]={1,1};
    nlopt_set_lower_bounds(opt, lb);
    nlopt_set_upper_bounds(opt, ub);
    nlopt_set_min_objective(opt, fitError, &entrada);
    //acho que isso esta relacionado com a precisao, se aumentar o resultado fica ruim, se diminuir muito nao converge nunca
    nlopt_set_xtol_rel(opt, 1e-1);

    //numero maximo de iterações, pode ser que precise aumentar
    nlopt_set_maxeval(opt, 500);

    double minf;

    if (nlopt_optimize(opt, ak, &minf) < 0) {
            printf("nlopt failed!\n");
    }
    double mag=initMag/ak[0];
    free(alpha);
    free(gray);
    free(inBin);
    free(transmission);
    //printf("resultado final: %f %f %f\n", ak[0], ak[1], mag);
    return mag;

}
Example #11
0
double Optimize_Torsion_Parameters(void)
{
	double lb[N_PARA_MAX], ub[N_PARA_MAX];
	double Chi_SQ;
	int i, j, Pos;

	n_Para = 10*n_Phi + 1;

	memset(Is_Para_Fixed, 0, sizeof(int)*n_Para);
//	memset(lb, 0, sizeof(double)*n_Para);
	lb[n_Phi*10] = -HUGE_VAL;
	for(i=0; i<n_Phi; i++)	{
		Pos = 10*i;
		for(j=0; j<10; j+=2)	{	// ten parameters
			lb[Pos+j] = -15.0;
			ub[Pos+j] = 15.0;
		}
		for(j=1; j<10; j+=2)	{	// ten parameters
			lb[Pos+j] = -M_PI;
			ub[Pos+j] = +M_PI;
		}

	}
	ub[n_Phi*10] = HUGE_VAL;


	opt = nlopt_create(NLOPT_LN_COBYLA, n_Para);

	nlopt_set_lower_bounds(opt, lb);
	nlopt_set_upper_bounds(opt, ub);
	nlopt_set_min_objective(opt, Callback_Eval_Gradient, NULL);
	nlopt_set_xtol_rel(opt, 1e-6);


	Collect_Torsion_Parameters();
	Assign_Torsion_Parameters();
	Cal_E_MM_Scaned();
	Cal_E_MM_Rotamer();
	Para_List[n_Phi*10] = E_Scan_QM_Mean-E_Scan_MM_Mean;	// energy shift
	E_Shift_0 = fabs(E_Scan_QM_Mean-E_Scan_MM_Mean);
	for(i=0; i<n_Phi; i++)	{
		Pos = 10*i;

		for(j=1; j<10; j+=2)	{	// the phase is fixed
			Is_Para_Fixed[Pos+j] = 1;
		}
	}

	Read_Parameters("saved-para.dat");

	Chi_SQ_Min = 1.0E100;
	memcpy(Para_Best, Para_List, sizeof(double)*n_Para);

	Chi_SQ = CalObjectiveFunction(Para_List, 0);
	if(ProgID == 0)	{
		fprintf(fFitting, "Iteration %4d  Chi^2 = %.8lf  Chi^2(1D) = %.8lf  Chi^2(rotamer) = %.8lf\n", 
			0, Chi_SQ, Chi_SQ_1D, Chi_SQ_Rotamer);
		fflush(fFitting);
	}

	FailCount = 0;
  int ret= nlopt_optimize(opt, Para_List, &Chi_SQ);
	if (ret < 0) {
		printf("nlopt failed! :%d\n", ret);
	}
	else {
		printf("Chi_SQ_min = %lf  Chi_SQ = %lf\n", Chi_SQ_Min, Chi_SQ);
	}


	memcpy(Para_List, Para_Best, sizeof(double)*n_Para);
	CalObjectiveFunction(Para_List, 1);

	nlopt_destroy(opt);

	return Chi_SQ_Min;
}
Example #12
0
double Geoometry_Optimization_With_Constraint(CMol *pMol)
{

	return (pMol->Cal_E(0));


	nlopt_opt opt, opt_AugLag;
	int Return_Opt, nAtom, nDim, i, iPos;
	double E_min, x[MAX_NUM_ATOM*3];

	Mol_ESP.Restrain_All_Torsions(1);	// apply soft restraints on all soft dihedrals

	nAtom = pMol->nAtom;
	nDim = 3 * nAtom;

	printf("\nMM before:\t");
	for(i=0; i<n_Phi; i++)	{
		printf("%3d %3d %3d %3d %8.4lf\n", DihList[i][0], DihList[i][1], DihList[i][2], DihList[i][3], 
			Mol_ESP.Query_Dihedral(DihList[i][0], DihList[i][1], DihList[i][2], DihList[i][3], 0, NULL));
	}

	double AUG_LAG_ICM_TOL = 1.0E-17;
	opt_AugLag = nlopt_create(NLOPT_LD_LBFGS_GEO, nDim);
	nlopt_set_min_objective(opt_AugLag, func_Geo_Opt_Constraint, pMol);

//	pMol->SavePdb("opt-free.pdb");

	for(i=0; i<pMol->nBond_Fixed; i++)	{
		int ret=0;
		pMol->Geo_Fix_r0[i].pMol = pMol;
		ret =nlopt_add_equality_constraint(opt_AugLag, Geo_Constraint_Bond, &(pMol->Geo_Fix_r0[i]), 2.0E-6);
		printf(" Fixing bond %d\n", i );
	}

	for(i=0; i<pMol->nAngle_Fixed; i++)	{
		int ret=0;
		pMol->Geo_Fix_theta0[i].pMol = pMol;
		ret=nlopt_add_equality_constraint(opt_AugLag, Geo_Constraint_Angle, &(pMol->Geo_Fix_theta0[i]), 2.0E-6);
		printf(" Fixing angle %d\n", i );
	}

	for(i=0; i<pMol->nPhi_Fixed; i++)	{
		int ret=0;
		pMol->Geo_Fix_phi0[i].pMol = pMol;
		ret=nlopt_add_equality_constraint(opt_AugLag, Geo_Constraint_Dihedral, &(pMol->Geo_Fix_phi0[i]), 2.0E-6);
		printf(" Fixing phi %d : %d\n", i, ret );
	}


	nlopt_set_xtol_rel(opt_AugLag, 2E-7);
	
	
//	opt = nlopt_create(NLOPT_LD_LBFGS_GEO, nDim);
//	opt = nlopt_create(NLOPT_LN_COBYLA, nDim);
//	nlopt_set_min_objective(opt, func_Geo_Opt_Constraint, pMol);
//	nlopt_set_xtol_rel(opt, 1E-7);

//	nlopt_set_local_optimizer(opt_AugLag, opt);
//	nlopt_destroy(opt);

	for(i=0; i<nAtom; i++)	{
		iPos = 3*i;
		x[iPos  ] = pMol->x[i];
		x[iPos+1] = pMol->y[i];
		x[iPos+2] = pMol->z[i];
	}
	
	Iter_Opt_Constrained = 0;
	Return_Opt = nlopt_optimize(opt_AugLag, x, &E_min);
	if (Return_Opt < 0) {
		printf("nlopt failed : %d\n", Return_Opt);
	}
//	else {
//		printf("After constrained optimization, E = %12.6lf\n", E_min);
//	}

	nlopt_destroy(opt_AugLag);

	printf("MM after:\t");
	for(i=0; i<n_Phi; i++)	{
		printf("%3d %3d %3d %3d %8.4lf\n", DihList[i][0], DihList[i][1], DihList[i][2], DihList[i][3], 
			Mol_ESP.Query_Dihedral(DihList[i][0], DihList[i][1], DihList[i][2], DihList[i][3], 0, NULL));
	}

	Mol_ESP.Restrain_All_Torsions(0);	// lift soft restraints on all soft dihedrals

	return (pMol->Cal_E(0));
}
Example #13
0
double Optimize_Torsion_Parameters(void)
{
	double lb[N_TOR_PARA] = { 
		-20.0, -PI, 
		-20.0, -PI, 
		-20.0, -PI, 
		-20.0, -PI, 
		-20.0, -PI, 
    -100 };
	double ub[N_TOR_PARA] = { 
		+20.0, +PI, 
		+20.0, +PI, 
		+20.0, +PI, 
		+20.0, +PI, 
		+20.0, +PI, 
    +100 };



//	double ub[N_TOR_PARA] = { 8.0, 8.0, 8.0, 8.0, 8.0, 8.0, 8.0, 8.0, 8.0, 8.0,  100.0 }; /* lower bounds */
	nlopt_opt opt;
	double Chi_SQ;
	int i, j;

	Chi_SQ_Min = 1.0E100;

	for(i=0; i<NRUN; i++)	{ // was 32
		ActiveRun = i;
		Chi_SQ_Min_Local[i] = 1.0E200;
		FailCount = Iteration = 0;

//		for(j=0; j<5; j++)	{
//			Para_Tor[2*j] = 0.1+1.0*rand(iseed);
//		}

		for( i=0; i<11; i++ ) { 
			Para_Tor[i]     = 0.; 
			IsPara_Fixed[i] = 0.;
		} // inital values		
		
		//opt = nlopt_create(NLOPT_LD_LBFGS, N_TOR_PARA);

    printf("=== Fitting ===\n" );
		opt = nlopt_create(NLOPT_LN_COBYLA, N_TOR_PARA);

		nlopt_set_lower_bounds(opt, lb);
		nlopt_set_upper_bounds(opt, ub);
		nlopt_set_min_objective(opt, Callback_Eval_Gradient, NULL);
		nlopt_set_xtol_rel(opt, 1e-8);

		if (nlopt_optimize(opt, Para_Tor, &Chi_SQ) < 0) {
			printf("nlopt didn't converge %lf %lf\n Please check carefully the rotamer results\n",Chi_SQ_Min, Chi_SQ);
		}
		else {
			printf("Chi_SQ_min = %lf  Chi_SQ = %lf\n", Chi_SQ_Min, Chi_SQ);
		}
		nlopt_destroy(opt);
	}

	memcpy(Para_Tor, Para_Best, sizeof(double)*N_TOR_PARA);
	CalObjectiveFunction_Tor(Para_Tor);



	return Chi_SQ_Min;
}
Example #14
0
static int test_function(int ifunc)
{
    nlopt_opt opt;
    testfunc func;
    int i, iter;
    double *x, minf, minf_max, f0, *xtabs, *lb, *ub;
    nlopt_result ret;
    double start = nlopt_seconds();
    int total_count = 0, max_count = 0, min_count = 1 << 30;
    double total_err = 0, max_err = 0;
    bounds_wrap_data bw;

    if (ifunc < 0 || ifunc >= NTESTFUNCS) {
        fprintf(stderr, "testopt: invalid function %d\n", ifunc);
        listfuncs(stderr);
        return 0;
    }
    func = testfuncs[ifunc];
    x = (double *) malloc(sizeof(double) * func.n * 5);
    if (!x) {
        fprintf(stderr, "testopt: Out of memory!\n");
        return 0;
    }

    lb = x + func.n * 3;
    ub = lb + func.n;
    xtabs = x + func.n * 2;
    bw.lb = lb;
    bw.ub = ub;
    bw.f = func.f;
    bw.f_data = func.f_data;

    for (i = 0; i < func.n; ++i)
        xtabs[i] = xtol_abs;
    minf_max = minf_max_delta > (-HUGE_VAL) ? minf_max_delta + func.minf : (-HUGE_VAL);

    printf("-----------------------------------------------------------\n");
    printf("Optimizing %s (%d dims) using %s algorithm\n", func.name, func.n, nlopt_algorithm_name(algorithm));
    printf("lower bounds at lb = [");
    for (i = 0; i < func.n; ++i)
        printf(" %g", func.lb[i]);
    printf("]\n");
    printf("upper bounds at ub = [");
    for (i = 0; i < func.n; ++i)
        printf(" %g", func.ub[i]);
    printf("]\n");
    memcpy(lb, func.lb, func.n * sizeof(double));
    memcpy(ub, func.ub, func.n * sizeof(double));
    for (i = 0; i < func.n; ++i)
        if (fix_bounds[i]) {
            printf("fixing bounds for dim[%d] to xmin[%d]=%g\n", i, i, func.xmin[i]);
            lb[i] = ub[i] = func.xmin[i];
        }
    if (force_constraints) {
        for (i = 0; i < func.n; ++i) {
            if (nlopt_iurand(2) == 0)
                ub[i] = nlopt_urand(lb[i], func.xmin[i]);
            else
                lb[i] = nlopt_urand(func.xmin[i], ub[i]);
        }
        printf("adjusted lower bounds at lb = [");
        for (i = 0; i < func.n; ++i)
            printf(" %g", lb[i]);
        printf("]\n");
        printf("adjusted upper bounds at ub = [");
        for (i = 0; i < func.n; ++i)
            printf(" %g", ub[i]);
        printf("]\n");
    }

    if (fabs(func.f(func.n, func.xmin, 0, func.f_data) - func.minf) > 1e-8) {
        fprintf(stderr, "BUG: function does not achieve given lower bound!\n");
        fprintf(stderr, "f(%g", func.xmin[0]);
        for (i = 1; i < func.n; ++i)
            fprintf(stderr, ", %g", func.xmin[i]);
        fprintf(stderr, ") = %0.16g instead of %0.16g, |diff| = %g\n", func.f(func.n, func.xmin, 0, func.f_data), func.minf, fabs(func.f(func.n, func.xmin, 0, func.f_data) - func.minf));
        free(x);
        return 0;
    }

    for (iter = 0; iter < iterations; ++iter) {
        double val;
        testfuncs_counter = 0;

        printf("Starting guess x = [");
        for (i = 0; i < func.n; ++i) {
            if (center_start)
                x[i] = (ub[i] + lb[i]) * 0.5;
            else if (xinit_tol < 0) {   /* random starting point near center of box */
                double dx = (ub[i] - lb[i]) * 0.25;
                double xm = 0.5 * (ub[i] + lb[i]);
                x[i] = nlopt_urand(xm - dx, xm + dx);
            } else {
                x[i] = nlopt_urand(-xinit_tol, xinit_tol)
                    + (1 + nlopt_urand(-xinit_tol, xinit_tol)) * func.xmin[i];
                if (x[i] > ub[i])
                    x[i] = ub[i];
                else if (x[i] < lb[i])
                    x[i] = lb[i];
            }
            printf(" %g", x[i]);
        }
        printf("]\n");
        f0 = func.f(func.n, x, x + func.n, func.f_data);
        printf("Starting function value = %g\n", f0);

        if (iter == 0 && testfuncs_verbose && func.has_gradient) {
            printf("checking gradient:\n");
            for (i = 0; i < func.n; ++i) {
                double f;
                x[i] *= 1 + 1e-6;
                f = func.f(func.n, x, NULL, func.f_data);
                x[i] /= 1 + 1e-6;
                printf("  grad[%d] = %g vs. numerical derivative %g\n", i, x[i + func.n], (f - f0) / (x[i] * 1e-6));
            }
        }

        testfuncs_counter = 0;
        opt = nlopt_create(algorithm, func.n);
        nlopt_set_min_objective(opt, bounds_wrap_func, &bw);
        nlopt_set_lower_bounds(opt, lb);
        nlopt_set_upper_bounds(opt, ub);
        nlopt_set_stopval(opt, minf_max);
        nlopt_set_ftol_rel(opt, ftol_rel);
        nlopt_set_ftol_abs(opt, ftol_abs);
        nlopt_set_xtol_rel(opt, xtol_rel);
        nlopt_set_xtol_abs(opt, xtabs);
        nlopt_set_maxeval(opt, maxeval);
        nlopt_set_maxtime(opt, maxtime);
        ret = nlopt_optimize(opt, x, &minf);
        printf("finished after %g seconds.\n", nlopt_seconds() - start);
        printf("return code %d from nlopt_minimize\n", ret);
        if (ret < 0 && ret != NLOPT_ROUNDOFF_LIMITED && ret != NLOPT_FORCED_STOP) {
            fprintf(stderr, "testopt: error in nlopt_minimize\n");
            free(x);
            return 0;
        }
        printf("Found minimum f = %g after %d evaluations (numevals = %d).\n", minf, testfuncs_counter, nlopt_get_numevals(opt));
        nlopt_destroy(opt);
        total_count += testfuncs_counter;
        if (testfuncs_counter > max_count)
            max_count = testfuncs_counter;
        if (testfuncs_counter < min_count)
            min_count = testfuncs_counter;
        printf("Minimum at x = [");
        for (i = 0; i < func.n; ++i)
            printf(" %g", x[i]);
        printf("]\n");
        if (func.minf == 0)
            printf("|f - minf| = %g\n", fabs(minf - func.minf));
        else
            printf("|f - minf| = %g, |f - minf| / |minf| = %e\n", fabs(minf - func.minf), fabs(minf - func.minf) / fabs(func.minf));
        total_err += fabs(minf - func.minf);
        if (fabs(minf - func.minf) > max_err)
            max_err = fabs(minf - func.minf);
        printf("vs. global minimum f = %g at x = [", func.minf);
        for (i = 0; i < func.n; ++i)
            printf(" %g", func.xmin[i]);
        printf("]\n");

        val = func.f(func.n, x, NULL, func.f_data);
        if (fabs(val - minf) > 1e-12) {
            fprintf(stderr, "Mismatch %g between returned minf=%g and f(x) = %g\n", minf - val, minf, val);
            free(x);
            return 0;
        }
    }
    if (iterations > 1)
        printf("average #evaluations = %g (%d-%d)\naverage |f-minf| = %g, max |f-minf| = %g\n", total_count * 1.0 / iterations, min_count, max_count, total_err / iterations, max_err);

    free(x);
    return 1;
}
Example #15
0
void omxInvokeNLOPT(double *est, GradientOptimizerContext &goc)
{
	goc.optName = "SLSQP";
	goc.setupSimpleBounds();
	goc.useGradient = true;

	FitContext *fc = goc.fc;
	int oldWanted = fc->wanted;
	fc->wanted = 0;
	omxState *globalState = fc->state;
    
        nlopt_opt opt = nlopt_create(NLOPT_LD_SLSQP, fc->numParam);
	goc.extraData = opt;
        //local_opt = nlopt_create(NLOPT_LD_SLSQP, n); // Subsidiary algorithm
        
        //nlopt_set_local_optimizer(opt, local_opt);
        nlopt_set_lower_bounds(opt, goc.solLB.data());
        nlopt_set_upper_bounds(opt, goc.solUB.data());

	int eq, ieq;
	globalState->countNonlinearConstraints(eq, ieq);

	if (fc->CI) {
		nlopt_set_xtol_rel(opt, 5e-3);
		std::vector<double> tol(fc->numParam, std::numeric_limits<double>::epsilon());
		nlopt_set_xtol_abs(opt, tol.data());
	} else {
		// The *2 is there to roughly equate accuracy with NPSOL.
		nlopt_set_ftol_rel(opt, goc.ControlTolerance * 2);
		nlopt_set_ftol_abs(opt, std::numeric_limits<double>::epsilon());
	}
        
	nlopt_set_min_objective(opt, SLSQP::nloptObjectiveFunction, &goc);

	double feasibilityTolerance = Global->feasibilityTolerance;
	SLSQP::context ctx(goc);
        if (eq + ieq) {
		ctx.origeq = eq;
                if (ieq > 0){
			goc.inequality.resize(ieq);
			std::vector<double> tol(ieq, feasibilityTolerance);
			nlopt_add_inequality_mconstraint(opt, ieq, SLSQP::nloptInequalityFunction, &goc, tol.data());
                }
                
                if (eq > 0){
			goc.equality.resize(eq);
			std::vector<double> tol(eq, feasibilityTolerance);
			nlopt_add_equality_mconstraint(opt, eq, SLSQP::nloptEqualityFunction, &ctx, tol.data());
                }
	}
        
	int priorIterations = fc->iterations;

	int code = nlopt_optimize(opt, est, &fc->fit);
	if (ctx.eqredundent) {
		nlopt_remove_equality_constraints(opt);
		eq -= ctx.eqredundent;
		std::vector<double> tol(eq, feasibilityTolerance);
		nlopt_add_equality_mconstraint(opt, eq, SLSQP::nloptEqualityFunction, &ctx, tol.data());

		code = nlopt_optimize(opt, est, &fc->fit);
	}

	if (goc.verbose >= 2) mxLog("nlopt_optimize returned %d", code);

        nlopt_destroy(opt);

	fc->wanted = oldWanted;

	if (code == NLOPT_INVALID_ARGS) {
		Rf_error("NLOPT invoked with invalid arguments");
	} else if (code == NLOPT_OUT_OF_MEMORY) {
		Rf_error("NLOPT ran out of memory");
	} else if (code == NLOPT_FORCED_STOP) {
		if (fc->iterations - priorIterations <= 1) {
			goc.informOut = INFORM_STARTING_VALUES_INFEASIBLE;
		} else {
			goc.informOut = INFORM_ITERATION_LIMIT;
		}
	} else if (code == NLOPT_ROUNDOFF_LIMITED) {
		if (fc->iterations - priorIterations <= 2) {
			Rf_error("%s: Failed due to singular matrix E or C in LSQ subproblem or "
				 "rank-deficient equality constraint subproblem or "
				 "positive directional derivative in line search", goc.optName);
		} else {
			goc.informOut = INFORM_NOT_AT_OPTIMUM;  // is this correct? TODO
		}
	} else if (code < 0) {
		Rf_error("NLOPT fatal error %d", code);
	} else if (code == NLOPT_MAXEVAL_REACHED) {
		goc.informOut = INFORM_ITERATION_LIMIT;
	} else {
		goc.informOut = INFORM_CONVERGED_OPTIMUM;
	}
}
Example #16
0
double Optimize_Torsion_Parameters(void)
{
	double lb[N_TOR_PARA] = { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, -100.0 }; /* lower bounds */
	double ub[N_TOR_PARA] = { 12.0, 12.0, 12.0, 12.0, 12.0, 12.0, 12.0, 12.0, 12.0, 12.0,  100.0 }; /* lower bounds */

//	double ub[N_TOR_PARA] = { 8.0, 8.0, 8.0, 8.0, 8.0, 8.0, 8.0, 8.0, 8.0, 8.0,  100.0 }; /* lower bounds */
	double Phase[32][5]={{0, 0, 0, 0, 0}, {0, 0, 0, 0, PI}, {0, 0, 0, PI, 0}, {0, 0, 0, PI, PI}, {0, 0, PI, 0, 0}, {0, 0, PI, 0, PI}, {0, 0, PI, PI, 0}, {0, 0, PI, PI, PI}, {0, PI, 0, 0, 0}, {0, PI, 0, 0, PI}, 
						{0, PI, 0, PI, 0}, {0, PI, 0, PI, PI}, {0, PI, PI, 0, 0}, {0, PI, PI, 0, PI}, {0, PI, PI, PI, 0}, {0, PI, PI, PI, PI}, {PI, 0, 0, 0, 0}, {PI, 0, 0, 0, PI}, {PI, 0, 0, PI, 0}, {PI, 0, 0, PI, PI},
						{PI, 0, PI, 0, 0}, {PI, 0, PI, 0, PI}, {PI, 0, PI, PI, 0}, {PI, 0, PI, PI, PI}, {PI, PI, 0, 0, 0}, {PI, PI, 0, 0, PI}, {PI, PI, 0, PI, 0}, {PI, PI, 0, PI, PI}, {PI, PI, PI, 0, 0}, {PI, PI, PI, 0, PI}, 
						{PI, PI, PI, PI, 0}, {PI, PI, PI, PI, PI}};
	nlopt_opt opt;
	double Chi_SQ;
	int i, j;

	Chi_SQ_Min = 1.0E100;

	iseed = time(NULL);
	
	for(i=0; i<32; i++)	{
		ActiveRun = i;
		Chi_SQ_Min_Local[i] = 1.0E200;
		FailCount = Iteration = 0;

//		for(j=0; j<5; j++)	{
//			Para_Tor[2*j] = 0.1+1.0*rand(iseed);
//		}
		
		Para_Tor[0] = Para_Tor[2] = Para_Tor[4] = Para_Tor[6] = Para_Tor[8] = 1.0;


		for(j=0; j<5; j++)	{
			Para_Tor[2*j+1] = Phase[i][j];
			lb[2*j+1] = Para_Tor[2*j+1];
			ub[2*j+1] = Para_Tor[2*j+1];
		}
//		Para_Tor[1] = Para_Tor[3] = Para_Tor[5] = Para_Tor[7] = Para_Tor[9] = 0.0;
		Para_Tor[10] = 0.0;
		
		IsPara_Fixed[1] = IsPara_Fixed[3] = IsPara_Fixed[5] = IsPara_Fixed[7] = IsPara_Fixed[9] = 1;

		//for test
//		IsPara_Fixed[6] = 1;	Para_Tor[6] = 0.0;

		
		opt = nlopt_create(NLOPT_LD_LBFGS, N_TOR_PARA);

		nlopt_set_lower_bounds(opt, lb);
		nlopt_set_upper_bounds(opt, ub);
		nlopt_set_min_objective(opt, Callback_Eval_Gradient, NULL);
		nlopt_set_xtol_rel(opt, 1e-8);

		if (nlopt_optimize(opt, Para_Tor, &Chi_SQ) < 0) {
			printf("nlopt didn't converge %lf %lf\n Please check carefully the rotamer results\n",Chi_SQ_Min, Chi_SQ);
		}
		else {
			printf("Chi_SQ_min = %lf  Chi_SQ = %lf\n", Chi_SQ_Min, Chi_SQ);
		}
		nlopt_destroy(opt);
	}

	memcpy(Para_Tor, Para_Best, sizeof(double)*N_TOR_PARA);
	CalObjectiveFunction_Tor(Para_Tor);



	return Chi_SQ_Min;
}
int solve(const int *i, const int *j, const double *a, const double* b,const int num_constraints,   double *phi_dist_approximate, const double *phi_lb, const int s , const double constraint_eps, const double solution_convergence_eps, double * phi_result ) 
{
    if(num_constraints > s){
        std::cout<< "SHIT! too many constraints!";
        throw(0);
    }

    nlopt_opt opt;

    // use local optimization
    opt = nlopt_create(NLOPT_LD_SLSQP, s);
    //opt = nlopt_create(NLOPT_GN_ISRES, s);

    // set lower bound to not less than phi before damge evolution
    double *lb = new double[s];
    for(int k=0;k<s;k++) lb[k]=phi_lb[k];

    nlopt_set_lower_bounds(opt, lb);

    my_function_data func_data={phi_dist_approximate, s};
    nlopt_set_min_objective(opt, myfunc, &func_data);

    std::vector<my_constraint_data> data(num_constraints);
    for(int k=0;k<num_constraints;k++) {
        data[k].a=a[k];
        data[k].b=b[k];
        data[k].i=i[k];
        data[k].j=j[k];
        data[k].s=s;
        nlopt_add_equality_constraint(opt,myconstraint, &data[k],constraint_eps);
    }
    nlopt_set_xtol_rel(opt, solution_convergence_eps);

    double *x = new double[s];

    for(int k=0;k<s;k++) {
        x[k]= (phi_dist_approximate[k]<lb[k]) ? lb[k]:phi_dist_approximate[k];  // need to let the initialial guess in bound!!
    }
    double minf;
    
    int result= nlopt_optimize(opt,x,&minf);

    if(result > 0){
        for(int k=0; k<s;k++){
            phi_result[k]=x[k];
        }
    }
    else{
        std::cout<<"FATAL ERROR! Can't solve optimization.\n";
        std::cout<<result<<std::endl;
        std::cout<<minf<<std::endl;
    }

    nlopt_destroy(opt);


    delete []lb;
    delete []x;
    return result;

}
int main(int argc, char *argv[]) {

    int opt=0, verb=0;
    int max_harm = 64, max_lag=0;
    int isub = 1;
    while ((opt=getopt(argc,argv,"hvI:H:L:"))!=-1) {
        switch (opt) {
            case 'v':
                verb++;
                break;
            case 'I':
                isub = atoi(optarg);
                break;
            case 'H':
                max_harm = atoi(optarg);
                break;
            case 'L':
                max_lag = atoi(optarg);
                break;
            case 'h':
                usage();
                exit(0);
                break;
        }
    }

    if (optind==argc) {
        usage();
        exit(1);
    }

    int i, rv;

    /* Open file */
    fitsfile *f;
    int status;
    fits_open_file(&f, argv[optind], READONLY, &status);
    fits_error_check_fatal();

    /* Get basic dims */
    struct cyclic_work w;
    cyclic_load_params(f, &w, &status);
    fits_error_check_fatal();
    if (verb) { 
        printf("Read nphase=%d npol=%d nchan=%d\n", 
                w.nphase, w.npol, w.nchan);
        fflush(stdout);
    }
    int orig_npol = w.npol;
    w.npol = 1;

    /* Init FFTs */
    fftwf_init_threads();
    fftwf_plan_with_nthreads(4);
    if (verb) { printf("Planning FFTs\n"); fflush(stdout); }
#define WF "/home/pdemores/share/cyclic_wisdom.dat"
    FILE *wf = fopen(WF,"r");
    if (wf!=NULL) { fftwf_import_wisdom_from_file(wf); fclose(wf); }
    rv = cyclic_init_ffts(&w);
    if (rv) {
        fprintf(stderr, "Error planning ffts (rv=%d)\n", rv);
        exit(1);
    }
    wf = fopen(WF,"w");
    if (wf!=NULL) { fftwf_export_wisdom_to_file(wf); fclose(wf); }

    /* Alloc some stuff */
    struct periodic_spectrum raw;
    struct cyclic_spectrum cs, model_cs;
    struct filter_time ht;
    struct filter_freq hf;
    struct profile_phase pp;
    struct profile_harm ph;

    raw.nphase = pp.nphase = w.nphase;
    raw.nchan = cs.nchan = hf.nchan = w.nchan;
    cs.nharm = ph.nharm =  w.nharm;
    ht.nlag = w.nlag;
    raw.npol = orig_npol;
    cs.npol = 1;

    model_cs.nchan = cs.nchan;
    model_cs.nharm = cs.nharm;
    model_cs.npol = cs.npol;

    cyclic_alloc_ps(&raw);
    cyclic_alloc_cs(&cs);
    cyclic_alloc_cs(&model_cs);
    filter_alloc_time(&ht);
    filter_alloc_freq(&hf);
    profile_alloc_phase(&pp);
    profile_alloc_harm(&ph);

#if 0  // XXX not implemented yet
    /* Check bounds */
    if (max_harm > w.nharm) { max_harm = w.nharm; }
    if (max_lag > w.nlag/2) { max_lag = w.nlag/2; }
    if (verb) {
        printf("Using max of %d harmonics and %d lags\n", max_harm, max_lag);
    }
#endif

    /* Load data */
    cyclic_load_ps(f, &raw, isub, &status);
    fits_error_check_fatal();

    /* Add polns w/o calibration */
    cyclic_pscrunch_ps(&raw, 1.0, 1.0);

    /* Initialize H, profile guesses */
    cyclic_fscrunch_ps(&pp, &raw);
    profile_phase2harm(&pp, &ph, &w);
    ht.data[0] = 1.0;
    for (i=1; i<ht.nlag; i++) { ht.data[i] = 0.0; }
    filter_profile_norm(&ht, &ph, w.nharm);
    profile_harm2phase(&ph, &pp, &w);

    /* convert input data to cyclic spectrum */
    cyclic_ps2cs(&raw, &cs, &w);

    /* could output initial profile */

    /* Fill in data struct for nlopt */
    struct cyclic_data cdata;
    cdata.cs = &cs;
    cdata.s0 = &ph;
    cdata.ht = &ht;
    cdata.model_cs = &model_cs;
    cdata.w = &w;

    /* Set up minimizer */
    const int dim = 2*(w.nharm-1) + 2*w.nlag; /* number of free params */
    printf("number of fit params = %d\n", dim);
    nlopt_opt op;
    op = nlopt_create(NLOPT_LN_COBYLA, dim);
    nlopt_set_min_objective(op, cyclic_ms_difference_nlopt, &cdata);
    nlopt_set_xtol_rel(op, 1e-4);

    /* Set up initial params */
    double *x = (double *)malloc(sizeof(double) * dim);
    double *xtmp = x;
    for (i=1; i<ph.nharm; i++) { 
        xtmp[0] = creal(ph.data[i]);
        xtmp[1] = cimag(ph.data[i]);
        xtmp += 2;
    }
    for (i=0; i<ht.nlag; i++) { 
        xtmp[0] = creal(ht.data[i]);
        xtmp[1] = cimag(ht.data[i]);
        xtmp += 2;
    }

    /* Run optimization */
    double min;
    if (nlopt_optimize(op, x, &min)) {
        fprintf(stderr, "nlopt_optimize failed\n");
        exit(1);
    }

    /* TODO: some kind of output */

    /* All done :) */
    nlopt_destroy(op);
    exit(0);
}
/* unlike nlopt_optimize() below, only handles minimization case */
static nlopt_result nlopt_optimize_(nlopt_opt opt, double *x, double *minf)
{
     const double *lb, *ub;
     nlopt_algorithm algorithm;
     nlopt_func f; void *f_data;
     unsigned n, i;
     int ni;
     nlopt_stopping stop;

     if (!opt || !x || !minf || !opt->f
	 || opt->maximize) return NLOPT_INVALID_ARGS;

     /* reset stopping flag */
     nlopt_set_force_stop(opt, 0);
     opt->force_stop_child = NULL;
     
     /* copy a few params to local vars for convenience */
     n = opt->n;
     ni = (int) n; /* most of the subroutines take "int" arg */
     lb = opt->lb; ub = opt->ub;
     algorithm = opt->algorithm;
     f = opt->f; f_data = opt->f_data;

     if (n == 0) { /* trivial case: no degrees of freedom */
	  *minf = opt->f(n, x, NULL, opt->f_data);
	  return NLOPT_SUCCESS;
     }

     *minf = HUGE_VAL;
     
     /* make sure rand generator is inited */
     nlopt_srand_time_default(); /* default is non-deterministic */

     /* check bound constraints */
     for (i = 0; i < n; ++i)
	  if (lb[i] > ub[i] || x[i] < lb[i] || x[i] > ub[i])
	       return NLOPT_INVALID_ARGS;

     stop.n = n;
     stop.minf_max = opt->stopval;
     stop.ftol_rel = opt->ftol_rel;
     stop.ftol_abs = opt->ftol_abs;
     stop.xtol_rel = opt->xtol_rel;
     stop.xtol_abs = opt->xtol_abs;
     stop.nevals = 0;
     stop.maxeval = opt->maxeval;
     stop.maxtime = opt->maxtime;
     stop.start = nlopt_seconds();
     stop.force_stop = &(opt->force_stop);

     switch (algorithm) {
	 case NLOPT_GN_DIRECT:
	 case NLOPT_GN_DIRECT_L: 
	 case NLOPT_GN_DIRECT_L_RAND: 
	      if (!finite_domain(n, lb, ub)) return NLOPT_INVALID_ARGS;
	      return cdirect(ni, f, f_data, 
			     lb, ub, x, minf, &stop, 0.0, 
			     (algorithm != NLOPT_GN_DIRECT)
			     + 3 * (algorithm == NLOPT_GN_DIRECT_L_RAND 
				    ? 2 : (algorithm != NLOPT_GN_DIRECT))
			     + 9 * (algorithm == NLOPT_GN_DIRECT_L_RAND 
				    ? 1 : (algorithm != NLOPT_GN_DIRECT)));
	      
	 case NLOPT_GN_DIRECT_NOSCAL:
	 case NLOPT_GN_DIRECT_L_NOSCAL: 
	 case NLOPT_GN_DIRECT_L_RAND_NOSCAL: 
	      if (!finite_domain(n, lb, ub)) return NLOPT_INVALID_ARGS;
	      return cdirect_unscaled(ni, f, f_data, lb, ub, x, minf, 
				      &stop, 0.0, 
				      (algorithm != NLOPT_GN_DIRECT)
				      + 3 * (algorithm == NLOPT_GN_DIRECT_L_RAND ? 2 : (algorithm != NLOPT_GN_DIRECT))
				      + 9 * (algorithm == NLOPT_GN_DIRECT_L_RAND ? 1 : (algorithm != NLOPT_GN_DIRECT)));
	      
	 case NLOPT_GN_ORIG_DIRECT:
	 case NLOPT_GN_ORIG_DIRECT_L: {
	      direct_return_code dret;
	      if (!finite_domain(n, lb, ub)) return NLOPT_INVALID_ARGS;
	      opt->work = malloc(sizeof(double) *
				 nlopt_max_constraint_dim(opt->m,
							  opt->fc));
	      if (!opt->work) return NLOPT_OUT_OF_MEMORY;
	      dret = direct_optimize(f_direct, opt, ni, lb, ub, x, minf,
				     stop.maxeval, -1,
				     stop.start, stop.maxtime,
				     0.0, 0.0,
				     pow(stop.xtol_rel, (double) n), -1.0,
				     stop.force_stop,
				     stop.minf_max, 0.0,
				     NULL, 
				     algorithm == NLOPT_GN_ORIG_DIRECT
				     ? DIRECT_ORIGINAL
				     : DIRECT_GABLONSKY);
	      free(opt->work); opt->work = NULL;
	      switch (dret) {
		  case DIRECT_INVALID_BOUNDS:
		  case DIRECT_MAXFEVAL_TOOBIG:
		  case DIRECT_INVALID_ARGS:
		       return NLOPT_INVALID_ARGS;
		  case DIRECT_INIT_FAILED:
		  case DIRECT_SAMPLEPOINTS_FAILED:
		  case DIRECT_SAMPLE_FAILED:
		       return NLOPT_FAILURE;
		  case DIRECT_MAXFEVAL_EXCEEDED:
		  case DIRECT_MAXITER_EXCEEDED:
		       return NLOPT_MAXEVAL_REACHED;
		  case DIRECT_MAXTIME_EXCEEDED:
		       return NLOPT_MAXTIME_REACHED;
		  case DIRECT_GLOBAL_FOUND:
		       return NLOPT_MINF_MAX_REACHED;
		  case DIRECT_VOLTOL:
		  case DIRECT_SIGMATOL:
		       return NLOPT_XTOL_REACHED;
		  case DIRECT_OUT_OF_MEMORY:
		       return NLOPT_OUT_OF_MEMORY;
		  case DIRECT_FORCED_STOP:
		       return NLOPT_FORCED_STOP;
	      }
	      break;
	 }

	 case NLOPT_GD_STOGO:
	 case NLOPT_GD_STOGO_RAND:
#ifdef WITH_CXX
	      if (!finite_domain(n, lb, ub)) return NLOPT_INVALID_ARGS;
	      if (!stogo_minimize(ni, f, f_data, x, minf, lb, ub, &stop,
				  algorithm == NLOPT_GD_STOGO
				  ? 0 : (int) POP(2*n)))
		   return NLOPT_FAILURE;
	      break;
#else
	      return NLOPT_INVALID_ARGS;
#endif

#if 0
	      /* lacking a free/open-source license, we no longer use
		 Rowan's code, and instead use by "sbplx" re-implementation */
	 case NLOPT_LN_SUBPLEX: {
	      int iret, freedx = 0;
	      if (!opt->dx) {
		   freedx = 1;
		   if (nlopt_set_default_initial_step(opt, x) != NLOPT_SUCCESS)
			return NLOPT_OUT_OF_MEMORY;
	      }		       
	      iret = nlopt_subplex(f_bound, minf, x, n, opt, &stop, opt->dx);
	      if (freedx) { free(opt->dx); opt->dx = NULL; }
	      switch (iret) {
		  case -2: return NLOPT_INVALID_ARGS;
		  case -20: return NLOPT_FORCED_STOP;
		  case -10: return NLOPT_MAXTIME_REACHED;
		  case -1: return NLOPT_MAXEVAL_REACHED;
		  case 0: return NLOPT_XTOL_REACHED;
		  case 1: return NLOPT_SUCCESS;
		  case 2: return NLOPT_MINF_MAX_REACHED;
		  case 20: return NLOPT_FTOL_REACHED;
		  case -200: return NLOPT_OUT_OF_MEMORY;
		  default: return NLOPT_FAILURE; /* unknown return code */
	      }
	      break;
	 }
#endif

	 case NLOPT_LN_PRAXIS: {
	      double step;
	      if (initial_step(opt, x, &step) != NLOPT_SUCCESS)
		   return NLOPT_OUT_OF_MEMORY;
	      return praxis_(0.0, DBL_EPSILON, 
			     step, ni, x, f_bound, opt, &stop, minf);
	 }

	 case NLOPT_LD_LBFGS: 
	      return luksan_plis(ni, f, f_data, lb, ub, x, minf, 
				 &stop, opt->vector_storage);

	 case NLOPT_LD_VAR1: 
	 case NLOPT_LD_VAR2: 
	      return luksan_plip(ni, f, f_data, lb, ub, x, minf, 
				 &stop, opt->vector_storage,
				 algorithm == NLOPT_LD_VAR1 ? 1 : 2);

	 case NLOPT_LD_TNEWTON: 
	 case NLOPT_LD_TNEWTON_RESTART: 
	 case NLOPT_LD_TNEWTON_PRECOND: 
	 case NLOPT_LD_TNEWTON_PRECOND_RESTART: 
	      return luksan_pnet(ni, f, f_data, lb, ub, x, minf,
				 &stop, opt->vector_storage,
				 1 + (algorithm - NLOPT_LD_TNEWTON) % 2,
				 1 + (algorithm - NLOPT_LD_TNEWTON) / 2);

	 case NLOPT_GN_CRS2_LM:
	      if (!finite_domain(n, lb, ub)) return NLOPT_INVALID_ARGS;
	      return crs_minimize(ni, f, f_data, lb, ub, x, minf, &stop, 
				  (int) POP(0), 0);

	 case NLOPT_G_MLSL:
	 case NLOPT_G_MLSL_LDS:
	 case NLOPT_GN_MLSL:
	 case NLOPT_GD_MLSL:
	 case NLOPT_GN_MLSL_LDS:
	 case NLOPT_GD_MLSL_LDS: {
	      nlopt_opt local_opt = opt->local_opt;
	      nlopt_result ret;
	      if (!finite_domain(n, lb, ub)) return NLOPT_INVALID_ARGS;
	      if (!local_opt && (algorithm == NLOPT_G_MLSL 
				 || algorithm == NLOPT_G_MLSL_LDS))
		   return NLOPT_INVALID_ARGS;
	      if (!local_opt) { /* default */
		   nlopt_algorithm local_alg = (algorithm == NLOPT_GN_MLSL ||
						algorithm == NLOPT_GN_MLSL_LDS)
			? nlopt_local_search_alg_nonderiv
			: nlopt_local_search_alg_deriv;
		   /* don't call MLSL recursively! */
		   if (local_alg >= NLOPT_GN_MLSL
		       && local_alg <= NLOPT_GD_MLSL_LDS)
			local_alg = (algorithm == NLOPT_GN_MLSL ||
				     algorithm == NLOPT_GN_MLSL_LDS)
			     ? NLOPT_LN_COBYLA : NLOPT_LD_MMA;
		   local_opt = nlopt_create(local_alg, n);
		   if (!local_opt) return NLOPT_FAILURE;
		   nlopt_set_ftol_rel(local_opt, opt->ftol_rel);
		   nlopt_set_ftol_abs(local_opt, opt->ftol_abs);
		   nlopt_set_xtol_rel(local_opt, opt->xtol_rel);
		   nlopt_set_xtol_abs(local_opt, opt->xtol_abs);
		   nlopt_set_maxeval(local_opt, nlopt_local_search_maxeval);
	      }
	      if (opt->dx) nlopt_set_initial_step(local_opt, opt->dx);
	      for (i = 0; i < n && stop.xtol_abs[i] > 0; ++i) ;
	      if (local_opt->ftol_rel <= 0 && local_opt->ftol_abs <= 0 &&
		  local_opt->xtol_rel <= 0 && i < n) {
		   /* it is not sensible to call MLSL without *some*
		      nonzero tolerance for the local search */
		   nlopt_set_ftol_rel(local_opt, 1e-15);
		   nlopt_set_xtol_rel(local_opt, 1e-7);
	      }
	      opt->force_stop_child = local_opt;
	      ret = mlsl_minimize(ni, f, f_data, lb, ub, x, minf, &stop,
				  local_opt, (int) POP(0),
				  algorithm >= NLOPT_GN_MLSL_LDS &&
				  algorithm != NLOPT_G_MLSL);
	      opt->force_stop_child = NULL;
	      if (!opt->local_opt) nlopt_destroy(local_opt);
	      return ret;
	 }

	 case NLOPT_LD_MMA: case NLOPT_LD_CCSAQ: {
	      nlopt_opt dual_opt;
	      nlopt_result ret;
#define LO(param, def) (opt->local_opt ? opt->local_opt->param : (def))
	      dual_opt = nlopt_create(LO(algorithm,
					 nlopt_local_search_alg_deriv),
				      nlopt_count_constraints(opt->m,
							      opt->fc));
	      if (!dual_opt) return NLOPT_FAILURE;
	      nlopt_set_ftol_rel(dual_opt, LO(ftol_rel, 1e-14));
	      nlopt_set_ftol_abs(dual_opt, LO(ftol_abs, 0.0));
	      nlopt_set_maxeval(dual_opt, LO(maxeval, 100000));
#undef LO

	      if (algorithm == NLOPT_LD_MMA)
		   ret = mma_minimize(n, f, f_data, opt->m, opt->fc,
				      lb, ub, x, minf, &stop, dual_opt);
	      else
		   ret = ccsa_quadratic_minimize(
			n, f, f_data, opt->m, opt->fc, opt->pre,
			lb, ub, x, minf, &stop, dual_opt);
	      nlopt_destroy(dual_opt);
	      return ret;
	 }

	 case NLOPT_LN_COBYLA: {
	      nlopt_result ret;
	      int freedx = 0;
	      if (!opt->dx) {
		   freedx = 1;
		   if (nlopt_set_default_initial_step(opt, x) != NLOPT_SUCCESS)
			return NLOPT_OUT_OF_MEMORY;
	      }
	      return cobyla_minimize(n, f, f_data, 
				     opt->m, opt->fc,
				     opt->p, opt->h,
				     lb, ub, x, minf, &stop,
				     opt->dx);
	      if (freedx) { free(opt->dx); opt->dx = NULL; }
	      return ret;
	 }
				     
	 case NLOPT_LN_NEWUOA: {
	      double step;
	      if (initial_step(opt, x, &step) != NLOPT_SUCCESS)
		   return NLOPT_OUT_OF_MEMORY;
	      return newuoa(ni, 2*n+1, x, 0, 0, step,
			    &stop, minf, f_noderiv, opt);
	 }
				     
	 case NLOPT_LN_NEWUOA_BOUND: {
	      double step;
	      if (initial_step(opt, x, &step) != NLOPT_SUCCESS)
		   return NLOPT_OUT_OF_MEMORY;
	      return newuoa(ni, 2*n+1, x, lb, ub, step,
			    &stop, minf, f_noderiv, opt);
	 }

	 case NLOPT_LN_BOBYQA: {
	      nlopt_result ret;
	      int freedx = 0;
	      if (!opt->dx) {
		   freedx = 1;
		   if (nlopt_set_default_initial_step(opt, x) != NLOPT_SUCCESS)
			return NLOPT_OUT_OF_MEMORY;
	      }
	      ret = bobyqa(ni, 2*n+1, x, lb, ub, opt->dx,
			   &stop, minf, opt->f, opt->f_data);
	      if (freedx) { free(opt->dx); opt->dx = NULL; }
	      return ret;
	 }

	 case NLOPT_LN_NELDERMEAD: 
	 case NLOPT_LN_SBPLX: 
	 {
	      nlopt_result ret;
	      int freedx = 0;
	      if (!opt->dx) {
		   freedx = 1;
		   if (nlopt_set_default_initial_step(opt, x) != NLOPT_SUCCESS)
			return NLOPT_OUT_OF_MEMORY;
	      }
	      if (algorithm == NLOPT_LN_NELDERMEAD)
		   ret= nldrmd_minimize(ni,f,f_data,lb,ub,x,minf,opt->dx,&stop);
	      else
		   ret= sbplx_minimize(ni,f,f_data,lb,ub,x,minf,opt->dx,&stop);
	      if (freedx) { free(opt->dx); opt->dx = NULL; }
	      return ret;
	 }

	 case NLOPT_AUGLAG:
	 case NLOPT_AUGLAG_EQ:
	 case NLOPT_LN_AUGLAG:
	 case NLOPT_LN_AUGLAG_EQ:
	 case NLOPT_LD_AUGLAG:
	 case NLOPT_LD_AUGLAG_EQ: {
	      nlopt_opt local_opt = opt->local_opt;
	      nlopt_result ret;
	      if ((algorithm == NLOPT_AUGLAG || algorithm == NLOPT_AUGLAG_EQ)
		  && !local_opt)
		   return NLOPT_INVALID_ARGS;
	      if (!local_opt) { /* default */
		   local_opt = nlopt_create(
			algorithm == NLOPT_LN_AUGLAG || 
			algorithm == NLOPT_LN_AUGLAG_EQ
			? nlopt_local_search_alg_nonderiv
			: nlopt_local_search_alg_deriv, n);
		   if (!local_opt) return NLOPT_FAILURE;
		   nlopt_set_ftol_rel(local_opt, opt->ftol_rel);
		   nlopt_set_ftol_abs(local_opt, opt->ftol_abs);
		   nlopt_set_xtol_rel(local_opt, opt->xtol_rel);
		   nlopt_set_xtol_abs(local_opt, opt->xtol_abs);
		   nlopt_set_maxeval(local_opt, nlopt_local_search_maxeval);
	      }
	      if (opt->dx) nlopt_set_initial_step(local_opt, opt->dx);
	      opt->force_stop_child = local_opt;
	      ret = auglag_minimize(ni, f, f_data, 
				    opt->m, opt->fc, 
				    opt->p, opt->h,
				    lb, ub, x, minf, &stop,
				    local_opt,
				    algorithm == NLOPT_AUGLAG_EQ
				    || algorithm == NLOPT_LN_AUGLAG_EQ
				    || algorithm == NLOPT_LD_AUGLAG_EQ);
	      opt->force_stop_child = NULL;
	      if (!opt->local_opt) nlopt_destroy(local_opt);
	      return ret;
	 }

	 case NLOPT_GN_ISRES:
	      if (!finite_domain(n, lb, ub)) return NLOPT_INVALID_ARGS;
	      return isres_minimize(ni, f, f_data, 
				    (int) (opt->m), opt->fc,
				    (int) (opt->p), opt->h,
				    lb, ub, x, minf, &stop,
				    (int) POP(0));

// 	case NLOPT_GN_ESCH:
// 	      if (!finite_domain(n, lb, ub)) return NLOPT_INVALID_ARGS;
// 	      return chevolutionarystrategy(n, f, f_data, 
// 					    lb, ub, x, minf, &stop,
// 					    (unsigned) POP(0),
// 					    (unsigned) (POP(0)*1.5));

	 case NLOPT_LD_SLSQP:
	      return nlopt_slsqp(n, f, f_data,
				 opt->m, opt->fc,
				 opt->p, opt->h,
				 lb, ub, x, minf, &stop);
				     
	 default:
	      return NLOPT_INVALID_ARGS;
     }

     return NLOPT_SUCCESS; /* never reached */
}
Example #20
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);
}