8 int auglag_verbose = 1;
10 #define MIN(a,b) ((a) < (b) ? (a) : (b))
11 #define MAX(a,b) ((a) > (b) ? (a) : (b))
13 /***************************************************************************/
16 nlopt_func f; void *f_data;
17 int m; nlopt_constraint *fc;
18 int p; nlopt_constraint *h;
19 double rho, *lambda, *mu;
24 /* the augmented lagrangian objective function */
25 static double auglag(int n, const double *x, double *grad, void *data)
27 auglag_data *d = (auglag_data *) data;
28 double *gradtmp = grad ? d->gradtmp : NULL;
30 const double *lambda = d->lambda, *mu = d->mu;
34 L = d->f(n, x, grad, d->f_data);
36 for (i = 0; i < d->p; ++i) {
38 h = d->h[i].f(n, x, gradtmp, d->h[i].f_data) + lambda[i] / rho;
40 if (grad) for (j = 0; j < n; ++j) grad[j] += (rho * h) * gradtmp[j];
43 for (i = 0; i < d->m; ++i) {
45 fc = d->fc[i].f(n, x, gradtmp, d->fc[i].f_data) + mu[i] / rho;
47 L += 0.5 * rho * fc*fc;
48 if (grad) for (j = 0; j < n; ++j)
49 grad[j] += (rho * fc) * gradtmp[j];
58 /***************************************************************************/
60 nlopt_result auglag_minimize(int n, nlopt_func f, void *f_data,
61 int m, nlopt_constraint *fc,
62 int p, nlopt_constraint *h,
63 const double *lb, const double *ub, /* bounds */
64 double *x, /* in: initial guess, out: minimizer */
67 nlopt_opt sub_opt, int sub_has_fc)
70 nlopt_result ret = NLOPT_SUCCESS;
71 double ICM = HUGE_VAL;
72 double *xcur = NULL, fcur;
75 /* magic parameters from Birgin & Martinez */
76 const double tau = 0.5, gam = 10;
77 const double lam_min = -1e20, lam_max = 1e20, mu_max = 1e20;
79 d.f = f; d.f_data = f_data;
84 /* whether we handle inequality constraints via the augmented
85 Lagrangian penalty function, or directly in the sub-algorithm */
91 ret = nlopt_set_min_objective(sub_opt, auglag, &d); if (ret<0) return ret;
92 ret = nlopt_set_lower_bounds(sub_opt, lb); if (ret<0) return ret;
93 ret = nlopt_set_upper_bounds(sub_opt, ub); if (ret<0) return ret;
94 ret = nlopt_remove_inequality_constraints(sub_opt); if (ret<0) return ret;
95 ret = nlopt_remove_equality_constraints(sub_opt); if (ret<0) return ret;
96 for (i = 0; i < m; ++i) {
97 ret = nlopt_add_inequality_constraint(sub_opt, fc[i].f, fc[i].f_data,
99 if (ret < 0) return ret;
102 xcur = (double *) malloc(sizeof(double) * (2*n + d.p + d.m));
103 if (!xcur) return NLOPT_OUT_OF_MEMORY;
104 memcpy(xcur, x, sizeof(double) * n);
106 d.gradtmp = xcur + n;
107 memset(d.gradtmp, 0, sizeof(double) * (n + d.p + d.m));
108 d.lambda = d.gradtmp + n;
109 d.mu = d.lambda + d.p;
111 /* starting rho suggested by B & M */
115 fcur = f(n, xcur, NULL, f_data);
117 for (i = 0; i < d.p; ++i) {
118 double hi = h[i].f(n, xcur, NULL, d.h[i].f_data);
119 feasible = feasible && fabs(hi) <= h[i].tol;
122 for (i = 0; i < d.m; ++i) {
123 double fci = fc[i].f(n, xcur, NULL, d.fc[i].f_data);
124 feasible = feasible && fci <= fc[i].tol;
125 if (fci > 0) con2 += fci * fci;
127 d.rho = MAX(1e-6, MIN(10, 2 * fabs(*minf) / con2));
136 double prev_ICM = ICM;
138 ret = nlopt_optimize_limited(sub_opt, xcur, minf,
139 stop->maxeval - stop->nevals,
140 stop->maxtime - (nlopt_seconds()
145 fcur = f(n, xcur, NULL, f_data);
149 for (i = 0; i < d.p; ++i) {
150 double hi = h[i].f(n, xcur, NULL, d.h[i].f_data);
151 double newlam = d.lambda[i] + d.rho * hi;
152 feasible = feasible && fabs(hi) <= h[i].tol;
153 ICM = MAX(ICM, fabs(hi));
154 d.lambda[i] = MIN(MAX(lam_min, newlam), lam_max);
156 for (i = 0; i < d.m; ++i) {
157 double fci = fc[i].f(n, xcur, NULL, d.fc[i].f_data);
158 double newmu = d.mu[i] + d.rho * fci;
159 feasible = feasible && fci <= fc[i].tol;
160 ICM = MAX(ICM, fabs(MAX(fci, -d.mu[i] / d.rho)));
161 d.mu[i] = MIN(MAX(0.0, newmu), mu_max);
163 if (ICM > tau * prev_ICM)
166 if (auglag_verbose) {
167 printf("auglag: ICM=%g, rho=%g\nauglag lambda=", ICM, d.rho);
168 for (i = 0; i < d.p; ++i) printf(" %g", d.lambda[i]);
169 printf("\nauglag mu = ");
170 for (i = 0; i < d.m; ++i) printf(" %g", d.mu[i]);
174 /* only check f & x convergence for feasible points...
175 for this to be effective on active constraints, the user
176 must set some nonzero tolerance for each constraint */
177 if (feasible && fcur < *minf) {
179 if (fcur < stop->minf_max) ret = NLOPT_MINF_MAX_REACHED;
180 if (nlopt_stop_ftol(stop, fcur,*minf)) ret = NLOPT_FTOL_REACHED;
181 if (nlopt_stop_x(stop, xcur, x)) ret = NLOPT_XTOL_REACHED;
183 memcpy(x, xcur, sizeof(double) * n);
184 if (ret != NLOPT_SUCCESS) break;
187 if (nlopt_stop_evals(stop)) {ret = NLOPT_MAXEVAL_REACHED; break;}
188 if (nlopt_stop_time(stop)) {ret = NLOPT_MAXTIME_REACHED; break;}
190 /* TODO: use some stopping criterion on ICM? */