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; }
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { unsigned n; double *x, *x0, opt_f; nlopt_result ret; mxArray *x_mx, *mx; user_function_data d, dpre, *dfc = NULL, *dh = NULL; nlopt_opt opt = NULL; CHECK(nrhs == 2 && nlhs <= 3, "wrong number of arguments"); /* options = prhs[0] */ CHECK(mxIsStruct(prhs[0]), "opt must be a struct"); /* x0 = prhs[1] */ CHECK(mxIsDouble(prhs[1]) && !mxIsComplex(prhs[1]) && (mxGetM(prhs[1]) == 1 || mxGetN(prhs[1]) == 1), "x must be real row or column vector"); n = mxGetM(prhs[1]) * mxGetN(prhs[1]), x0 = mxGetPr(prhs[1]); CHECK(opt = make_opt(prhs[0], n), "error initializing nlopt options"); d.neval = 0; d.verbose = (int) struct_val_default(prhs[0], "verbose", 0); d.opt = opt; /* function f = prhs[1] */ mx = struct_funcval(prhs[0], "min_objective"); if (!mx) mx = struct_funcval(prhs[0], "max_objective"); CHECK(mx, "either opt.min_objective or opt.max_objective must exist"); if (mxIsChar(mx)) { CHECK(mxGetString(mx, d.f, FLEN) == 0, "error reading function name string (too long?)"); d.nrhs = 1; d.xrhs = 0; } else { d.prhs[0] = mx; strcpy(d.f, "feval"); d.nrhs = 2; d.xrhs = 1; } d.prhs[d.xrhs] = mxCreateDoubleMatrix(1, n, mxREAL); if ((mx = struct_funcval(prhs[0], "pre"))) { CHECK(mxIsChar(mx) || mxIsFunctionHandle(mx), "pre must contain function handles or function names"); if (mxIsChar(mx)) { CHECK(mxGetString(mx, dpre.f, FLEN) == 0, "error reading function name string (too long?)"); dpre.nrhs = 2; dpre.xrhs = 0; } else { dpre.prhs[0] = mx; strcpy(dpre.f, "feval"); dpre.nrhs = 3; dpre.xrhs = 1; } dpre.verbose = d.verbose > 2; dpre.opt = opt; dpre.neval = 0; dpre.prhs[dpre.xrhs] = d.prhs[d.xrhs]; dpre.prhs[d.xrhs+1] = mxCreateDoubleMatrix(1, n, mxREAL); d.dpre = &dpre; if (struct_funcval(prhs[0], "min_objective")) nlopt_set_precond_min_objective(opt, user_function,user_pre,&d); else nlopt_set_precond_max_objective(opt, user_function,user_pre,&d); } else { dpre.nrhs = 0; if (struct_funcval(prhs[0], "min_objective")) nlopt_set_min_objective(opt, user_function, &d); else nlopt_set_max_objective(opt, user_function, &d); } if ((mx = mxGetField(prhs[0], 0, "fc"))) { int j, m; double *fc_tol; CHECK(mxIsCell(mx), "fc must be a Cell array"); m = mxGetM(mx) * mxGetN(mx);; dfc = (user_function_data *) mxCalloc(m, sizeof(user_function_data)); fc_tol = struct_arrval(prhs[0], "fc_tol", m, NULL); for (j = 0; j < m; ++j) { mxArray *fc = mxGetCell(mx, j); CHECK(mxIsChar(fc) || mxIsFunctionHandle(fc), "fc must contain function handles or function names"); if (mxIsChar(fc)) { CHECK(mxGetString(fc, dfc[j].f, FLEN) == 0, "error reading function name string (too long?)"); dfc[j].nrhs = 1; dfc[j].xrhs = 0; } else { dfc[j].prhs[0] = fc; strcpy(dfc[j].f, "feval"); dfc[j].nrhs = 2; dfc[j].xrhs = 1; } dfc[j].verbose = d.verbose > 1; dfc[j].opt = opt; dfc[j].neval = 0; dfc[j].prhs[dfc[j].xrhs] = d.prhs[d.xrhs]; CHECK(nlopt_add_inequality_constraint(opt, user_function, dfc + j, fc_tol ? fc_tol[j] : 0) > 0, "nlopt error adding inequality constraint"); } } if ((mx = mxGetField(prhs[0], 0, "h"))) { int j, m; double *h_tol; CHECK(mxIsCell(mx), "h must be a Cell array"); m = mxGetM(mx) * mxGetN(mx);; dh = (user_function_data *) mxCalloc(m, sizeof(user_function_data)); h_tol = struct_arrval(prhs[0], "h_tol", m, NULL); for (j = 0; j < m; ++j) { mxArray *h = mxGetCell(mx, j); CHECK(mxIsChar(h) || mxIsFunctionHandle(h), "h must contain function handles or function names"); if (mxIsChar(h)) { CHECK(mxGetString(h, dh[j].f, FLEN) == 0, "error reading function name string (too long?)"); dh[j].nrhs = 1; dh[j].xrhs = 0; } else { dh[j].prhs[0] = h; strcpy(dh[j].f, "feval"); dh[j].nrhs = 2; dh[j].xrhs = 1; } dh[j].verbose = d.verbose > 1; dh[j].opt = opt; dh[j].neval = 0; dh[j].prhs[dh[j].xrhs] = d.prhs[d.xrhs]; CHECK(nlopt_add_equality_constraint(opt, user_function, dh + j, h_tol ? h_tol[j] : 0) > 0, "nlopt error adding equality constraint"); } } x_mx = mxCreateDoubleMatrix(mxGetM(prhs[1]), mxGetN(prhs[1]), mxREAL); x = mxGetPr(x_mx); memcpy(x, x0, sizeof(double) * n); ret = nlopt_optimize(opt, x, &opt_f); mxFree(dh); mxFree(dfc); mxDestroyArray(d.prhs[d.xrhs]); if (dpre.nrhs > 0) mxDestroyArray(dpre.prhs[d.xrhs+1]); nlopt_destroy(opt); plhs[0] = x_mx; if (nlhs > 1) { plhs[1] = mxCreateDoubleMatrix(1, 1, mxREAL); *(mxGetPr(plhs[1])) = opt_f; } if (nlhs > 2) { plhs[2] = mxCreateDoubleMatrix(1, 1, mxREAL); *(mxGetPr(plhs[2])) = (int) ret; } }
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)); }
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; }