1 /* cobyla : contrained optimization by linear approximation */
4 * Copyright (c) 1992, Michael J. D. Powell (M.J.D.Powell@damtp.cam.ac.uk)
5 * Copyright (c) 2004, Jean-Sebastien Roy (js@jeannot.org)
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sublicense, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
15 * The above copyright notice and this permission notice shall be included
16 * in all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
21 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
22 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 * This software is a C version of COBYLA2, a contrained optimization by linear
29 * approximation package developed by Michael J. D. Powell in Fortran.
31 * The original source code can be found at :
32 * http://plato.la.asu.edu/topics/problems/nlores.html
35 static char const rcsid[] =
36 "@(#) $Jeannot: cobyla.c,v 1.11 2004/04/18 09:51:36 js Exp $";
44 /* SGJ, 2008: modified COBYLA code to take explicit account of bound
45 constraints. Since bound constraints are linear, these should
46 already be handled exactly when COBYLA optimizes it linear model.
47 However, they were not handled properly when COBYLA created its
48 initial simplex, or when COBYLA updated unacceptable simplices.
49 Since NLopt guarantees that the objective will not be evaluated
50 outside the bound constraints, this required us to handle such
51 points by putting a slope discontinuity into the objective &
52 constraints (below), which slows convergence considerably for
53 smooth functions. Instead, handling them explicitly prevents this
55 #define ENFORCE_BOUNDS 1
57 #define min(a,b) ((a) <= (b) ? (a) : (b))
58 #define max(a,b) ((a) >= (b) ? (a) : (b))
59 #define abs(x) ((x) >= 0 ? (x) : -(x))
61 /**************************************************************************/
62 /* SGJ, 2008: NLopt-style interface function: */
70 const double *lb, *ub;
73 static int func_wrap(int n, int m, double *x, double *f, double *con,
77 func_wrap_state *s = (func_wrap_state *) state;
78 double *xtmp = s->xtmp;
79 const double *lb = s->lb, *ub = s->ub;
81 /* in nlopt, we guarante that the function is never evaluated outside
82 the lb and ub bounds, so we need force this with xtmp ... note
83 that this leads to discontinuity in the first derivative, which
84 slows convergence if we don't enable the ENFORCE_BOUNDS feature
86 for (j = 0; j < n; ++j) {
87 if (x[j] < lb[j]) xtmp[j] = lb[j];
88 else if (x[j] > ub[j]) xtmp[j] = ub[j];
92 *f = s->f(n, xtmp, NULL, s->f_data);
93 for (i = 0; i < s->m_orig; ++i)
94 con[i] = -s->fc[i].f(n, xtmp, NULL, s->fc[i].f_data);
95 for (j = 0; j < n; ++j) {
96 if (!nlopt_isinf(lb[j]))
97 con[i++] = x[j] - lb[j];
98 if (!nlopt_isinf(ub[j]))
99 con[i++] = ub[j] - x[j];
101 if (m != i) return 1; /* ... bug?? */
105 nlopt_result cobyla_minimize(int n, nlopt_func f, void *f_data,
106 int m, nlopt_constraint *fc,
107 const double *lb, const double *ub, /* bounds */
108 double *x, /* in: initial guess, out: minimizer */
110 nlopt_stopping *stop,
117 s.f = f; s.f_data = f_data;
120 s.lb = lb; s.ub = ub;
121 s.xtmp = (double *) malloc(sizeof(double) * n);
122 if (!s.xtmp) return NLOPT_OUT_OF_MEMORY;
124 /* add constraints for lower/upper bounds (if any) */
125 for (j = 0; j < n; ++j) {
126 if (!nlopt_isinf(lb[j]))
128 if (!nlopt_isinf(ub[j]))
132 ret = cobyla(n, m, x, minf, rhobegin, stop, lb, ub, COBYLA_MSG_NONE,
135 /* make sure e.g. rounding errors didn't push us slightly out of bounds */
136 for (j = 0; j < n; ++j) {
137 if (x[j] < lb[j]) x[j] = lb[j];
138 if (x[j] > ub[j]) x[j] = ub[j];
145 /**************************************************************************/
147 static nlopt_result cobylb(int *n, int *m, int *mpp, double *x, double *minf, double *rhobeg,
148 nlopt_stopping *stop, const double *lb, const double *ub, int *iprint, double *con, double *sim,
149 double *simi, double *datmat, double *a, double *vsig, double *veta,
150 double *sigbar, double *dx, double *w, int *iact, cobyla_function *calcfc,
152 static void trstlp(int *n, int *m, double *a, double *b, double *rho,
153 double *dx, int *ifull, int *iact, double *z__, double *zdota, double *vmultc,
154 double *sdirn, double *dxnew, double *vmultd);
156 /* ------------------------------------------------------------------------ */
158 nlopt_result cobyla(int n, int m, double *x, double *minf, double rhobeg, nlopt_stopping *stop, const double *lb, const double *ub, int iprint,
159 cobyla_function *calcfc, void *state)
161 int icon, isim, isigb, idatm, iveta, isimi, ivsig, iwork, ia, idx, mpp;
167 * This subroutine minimizes an objective function F(X) subject to M
168 * inequality constraints on X, where X is a vector of variables that has
169 * N components. The algorithm employs linear approximations to the
170 * objective and constraint functions, the approximations being formed by
171 * linear interpolation at N+1 points in the space of the variables.
172 * We regard these interpolation points as vertices of a simplex. The
173 * parameter RHO controls the size of the simplex and it is reduced
174 * automatically from RHOBEG to RHOEND. For each RHO the subroutine tries
175 * to achieve a good vector of variables for the current size, and then
176 * RHO is reduced until the value RHOEND is reached. Therefore RHOBEG and
177 * RHOEND should be set to reasonable initial changes to and the required
178 * accuracy in the variables respectively, but this accuracy should be
179 * viewed as a subject for experimentation because it is not guaranteed.
180 * The subroutine has an advantage over many of its competitors, however,
181 * which is that it treats each constraint individually when calculating
182 * a change to the variables, instead of lumping the constraints together
183 * into a single penalty function. The name of the subroutine is derived
184 * from the phrase Constrained Optimization BY Linear Approximations.
186 * The user must set the values of N, M, RHOBEG and RHOEND, and must
187 * provide an initial vector of variables in X. Further, the value of
188 * IPRINT should be set to 0, 1, 2 or 3, which controls the amount of
189 * printing during the calculation. Specifically, there is no output if
190 * IPRINT=0 and there is output only at the end of the calculation if
191 * IPRINT=1. Otherwise each new value of RHO and SIGMA is printed.
192 * Further, the vector of variables and some function information are
193 * given either when RHO is reduced or when each new value of F(X) is
194 * computed in the cases IPRINT=2 or IPRINT=3 respectively. Here SIGMA
195 * is a penalty parameter, it being assumed that a change to X is an
196 * improvement if it reduces the merit function
197 * F(X)+SIGMA*MAX(0.0,-C1(X),-C2(X),...,-CM(X)),
198 * where C1,C2,...,CM denote the constraint functions that should become
199 * nonnegative eventually, at least to the precision of RHOEND. In the
200 * printed output the displayed term that is multiplied by SIGMA is
201 * called MAXCV, which stands for 'MAXimum Constraint Violation'. The
202 * argument MAXFUN is an int variable that must be set by the user to a
203 * limit on the number of calls of CALCFC, the purpose of this routine being
204 * given below. The value of MAXFUN will be altered to the number of calls
205 * of CALCFC that are made. The arguments W and IACT provide real and
206 * int arrays that are used as working space. Their lengths must be at
207 * least N*(3*N+2*M+11)+4*M+6 and M+1 respectively.
209 * In order to define the objective and constraint functions, we require
210 * a subroutine that has the name and arguments
211 * SUBROUTINE CALCFC (N,M,X,F,CON)
212 * DIMENSION X(*),CON(*) .
213 * The values of N and M are fixed and have been defined already, while
214 * X is now the current vector of variables. The subroutine should return
215 * the objective and constraint functions at X in F and CON(1),CON(2),
216 * ...,CON(M). Note that we are trying to adjust X so that F(X) is as
217 * small as possible subject to the constraint functions being nonnegative.
219 * Partition the working space array W to provide the storage that is needed
220 * for the main calculation.
227 if (iprint>=1) fprintf(stderr, "cobyla: N==0.\n");
228 return NLOPT_SUCCESS;
233 if (iprint>=1) fprintf(stderr, "cobyla: N<0 or M<0.\n");
234 return NLOPT_INVALID_ARGS;
237 /* workspace allocation */
238 w = (double*) malloc((n*(3*n+2*m+11)+4*m+6)*sizeof(*w));
241 if (iprint>=1) fprintf(stderr, "cobyla: memory allocation error.\n");
242 return NLOPT_OUT_OF_MEMORY;
244 iact = (int*)malloc((m+1)*sizeof(*iact));
247 if (iprint>=1) fprintf(stderr, "cobyla: memory allocation error.\n");
249 return NLOPT_OUT_OF_MEMORY;
252 /* Parameter adjustments */
262 isimi = isim + n * n + n;
263 idatm = isimi + n * n;
264 ia = idatm + n * mpp + mpp;
265 ivsig = ia + m * n + n;
270 rc = cobylb(&n, &m, &mpp, &x[1], minf, &rhobeg, stop, &lb[1], &ub[1], &iprint,
271 &w[icon], &w[isim], &w[isimi], &w[idatm], &w[ia], &w[ivsig], &w[iveta],
272 &w[isigb], &w[idx], &w[iwork], &iact[1], calcfc, state);
274 /* Parameter adjustments (reverse) */
284 /* ------------------------------------------------------------------------- */
285 static nlopt_result cobylb(int *n, int *m, int *mpp,
286 double *x, double *minf, double *rhobeg,
287 nlopt_stopping *stop, const double *lb, const double *ub,
288 int *iprint, double *con, double *sim, double *simi,
289 double *datmat, double *a, double *vsig, double *veta,
290 double *sigbar, double *dx, double *w, int *iact, cobyla_function *calcfc,
293 /* System generated locals */
294 int sim_dim1, sim_offset, simi_dim1, simi_offset, datmat_dim1,
295 datmat_offset, a_dim1, a_offset, i__1, i__2, i__3;
298 /* Local variables */
299 double alpha, delta, denom, tempa, barmu;
300 double beta, cmin = 0.0, cmax = 0.0;
301 double cvmaxm, dxsign, prerem = 0.0;
302 double edgmax, pareta, prerec = 0.0, phimin, parsig = 0.0;
304 double phi, rho, sum = 0.0;
305 double ratio, vmold, parmu, error, vmnew;
306 double resmax, cvmaxp;
307 double resnew, trured;
308 double temp, wsig, f;
317 int mp, np, iz, ibrnch;
318 int nbest, ifull, iptem, jdrop;
319 nlopt_result rc = NLOPT_SUCCESS;
322 /* SGJ, 2008: compute rhoend from NLopt stop info */
323 rhoend = stop->xtol_rel * (*rhobeg);
324 for (j = 0; j < *n; ++j)
325 if (rhoend < stop->xtol_abs[j])
326 rhoend = stop->xtol_abs[j];
328 /* SGJ, 2008: added code to keep track of minimum feasible function val */
331 /* Set the initial values of some parameters. The last column of SIM holds */
332 /* the optimal vertex of the current simplex, and the preceding N columns */
333 /* hold the displacements from the optimal vertex to the other vertices. */
334 /* Further, SIMI holds the inverse of the matrix that is contained in the */
335 /* first N columns of SIM. */
337 /* Parameter adjustments */
339 a_offset = 1 + a_dim1 * 1;
342 simi_offset = 1 + simi_dim1 * 1;
345 sim_offset = 1 + sim_dim1 * 1;
348 datmat_offset = 1 + datmat_dim1 * 1;
349 datmat -= datmat_offset;
373 "cobyla: the initial value of RHO is %12.6E and PARMU is set to zero.\n",
378 for (i__ = 1; i__ <= i__1; ++i__) {
380 sim[i__ + np * sim_dim1] = x[i__];
382 for (j = 1; j <= i__2; ++j) {
383 sim[i__ + j * sim_dim1] = 0.;
384 simi[i__ + j * simi_dim1] = 0.;
388 /* SGJ: make sure step rhocur stays inside [lb,ub] */
389 if (x[i__] + rhocur > ub[i__]) {
390 if (x[i__] - rhocur >= lb[i__])
392 else if (ub[i__] - x[i__] > x[i__] - lb[i__])
393 rhocur = 0.5 * (ub[i__] - x[i__]);
395 rhocur = 0.5 * (x[i__] - lb[i__]);
398 sim[i__ + i__ * sim_dim1] = rhocur;
399 simi[i__ + i__ * simi_dim1] = 1.0 / rhocur;
404 /* Make the next call of the user-supplied subroutine CALCFC. These */
405 /* instructions are also used for calling CALCFC during the iterations of */
408 /* SGJ comment: why the hell couldn't he just use a damn subroutine?
409 #*&!%*@ Fortran-66 spaghetti code */
412 if (nlopt_stop_evals(stop)) rc = NLOPT_MAXEVAL_REACHED;
413 else if (nlopt_stop_time(stop)) rc = NLOPT_MAXTIME_REACHED;
414 if (rc != NLOPT_SUCCESS) goto L600;
417 if (calcfc(*n, *m, &x[1], &f, &con[1], state))
420 fprintf(stderr, "cobyla: user requested end of minimization.\n");
429 for (k = 1; k <= i__1; ++k) {
430 d__1 = resmax, d__2 = -con[k];
431 resmax = max(d__1,d__2);
435 /* SGJ, 2008: check for minf_max reached by feasible point */
436 if (f < stop->minf_max && resmax <= 0) {
437 rc = NLOPT_MINF_MAX_REACHED;
438 goto L620; /* not L600 because we want to use current x, f, resmax */
441 if (stop->nevals == *iprint - 1 || *iprint == 3) {
442 fprintf(stderr, "cobyla: NFVALS = %4d, F =%13.6E, MAXCV =%13.6E\n",
443 stop->nevals, f, resmax);
445 fprintf(stderr, "cobyla: X =");
446 for (i__ = 1; i__ <= i__1; ++i__) {
447 if (i__>1) fprintf(stderr, " ");
448 fprintf(stderr, "%13.6E", x[i__]);
452 for (i__ = iptemp; i__ <= i__1; ++i__) {
453 if (!((i__-1) % 4)) fprintf(stderr, "\ncobyla: ");
454 fprintf(stderr, "%15.6E", x[i__]);
457 fprintf(stderr, "\n");
465 /* Set the recently calculated function values in a column of DATMAT. This */
466 /* array has a column for each vertex of the current simplex, the entries of */
467 /* each column being the values of the constraint functions (if any) */
468 /* followed by the objective function and the greatest constraint violation */
472 for (k = 1; k <= i__1; ++k) {
473 datmat[k + jdrop * datmat_dim1] = con[k];
475 if (stop->nevals > np) {
479 /* Exchange the new vertex of the initial simplex with the optimal vertex if */
480 /* necessary. Then, if the initial simplex is not complete, pick its next */
481 /* vertex and calculate the function values there. */
484 if (datmat[mp + np * datmat_dim1] <= f) {
485 x[jdrop] = sim[jdrop + np * sim_dim1];
486 } else { /* improvement in function val */
487 double rhocur = x[jdrop] - sim[jdrop + np * sim_dim1];
488 /* SGJ: use rhocur instead of rho. In original code, rhocur == rho
489 always, but I want to change this to ensure that simplex points
490 fall within [lb,ub]. */
491 sim[jdrop + np * sim_dim1] = x[jdrop];
493 for (k = 1; k <= i__1; ++k) {
494 datmat[k + jdrop * datmat_dim1] = datmat[k + np * datmat_dim1]
496 datmat[k + np * datmat_dim1] = con[k];
499 for (k = 1; k <= i__1; ++k) {
500 sim[jdrop + k * sim_dim1] = -rhocur;
503 for (i__ = k; i__ <= i__2; ++i__) {
504 temp -= simi[i__ + k * simi_dim1];
506 simi[jdrop + k * simi_dim1] = temp;
510 if (stop->nevals <= *n) { /* evaluating initial simplex */
511 jdrop = stop->nevals;
512 /* SGJ: was += rho, but using sim[jdrop,jdrop] enforces consistency
513 if we change the stepsize above to stay in [lb,ub]. */
514 x[jdrop] += sim[jdrop + jdrop * sim_dim1];
520 /* Identify the optimal vertex of the current simplex. */
523 phimin = datmat[mp + np * datmat_dim1] + parmu * datmat[*mpp + np *
527 for (j = 1; j <= i__1; ++j) {
528 temp = datmat[mp + j * datmat_dim1] + parmu * datmat[*mpp + j *
533 } else if (temp == phimin && parmu == 0.) {
534 if (datmat[*mpp + j * datmat_dim1] < datmat[*mpp + nbest *
541 /* Switch the best vertex into pole position if it is not there already, */
542 /* and also update SIM, SIMI and DATMAT. */
546 for (i__ = 1; i__ <= i__1; ++i__) {
547 temp = datmat[i__ + np * datmat_dim1];
548 datmat[i__ + np * datmat_dim1] = datmat[i__ + nbest * datmat_dim1]
550 datmat[i__ + nbest * datmat_dim1] = temp;
553 for (i__ = 1; i__ <= i__1; ++i__) {
554 temp = sim[i__ + nbest * sim_dim1];
555 sim[i__ + nbest * sim_dim1] = 0.;
556 sim[i__ + np * sim_dim1] += temp;
559 for (k = 1; k <= i__2; ++k) {
560 sim[i__ + k * sim_dim1] -= temp;
561 tempa -= simi[k + i__ * simi_dim1];
563 simi[nbest + i__ * simi_dim1] = tempa;
567 /* Make an error return if SIGI is a poor approximation to the inverse of */
568 /* the leading N by N submatrix of SIG. */
572 for (i__ = 1; i__ <= i__1; ++i__) {
574 for (j = 1; j <= i__2; ++j) {
580 for (k = 1; k <= i__3; ++k) if (sim[k + j * sim_dim1] != 0) {
581 temp += simi[i__ + k * simi_dim1] * sim[k + j * sim_dim1];
583 d__1 = error, d__2 = abs(temp);
584 error = max(d__1,d__2);
589 fprintf(stderr, "cobyla: rounding errors are becoming damaging.\n");
595 /* Calculate the coefficients of the linear approximations to the objective */
596 /* and constraint functions, placing minus the objective function gradient */
597 /* after the constraint gradients in the array A. The vector W is used for */
601 for (k = 1; k <= i__2; ++k) {
602 con[k] = -datmat[k + np * datmat_dim1];
604 for (j = 1; j <= i__1; ++j) {
605 w[j] = datmat[k + j * datmat_dim1] + con[k];
608 for (i__ = 1; i__ <= i__1; ++i__) {
611 for (j = 1; j <= i__3; ++j) {
612 temp += w[j] * simi[j + i__ * simi_dim1];
617 a[i__ + k * a_dim1] = temp;
621 /* Calculate the values of sigma and eta, and set IFLAG=0 if the current */
622 /* simplex is not acceptable. */
625 parsig = alpha * rho;
628 for (j = 1; j <= i__1; ++j) {
632 for (i__ = 1; i__ <= i__2; ++i__) {
633 d__1 = simi[j + i__ * simi_dim1];
635 d__1 = sim[i__ + j * sim_dim1];
638 vsig[j] = 1. / sqrt(wsig);
639 veta[j] = sqrt(weta);
640 if (vsig[j] < parsig || veta[j] > pareta) {
645 /* If a new vertex is needed to improve acceptability, then decide which */
646 /* vertex to drop from the simplex. */
648 if (ibrnch == 1 || iflag == 1) {
654 for (j = 1; j <= i__1; ++j) {
655 if (veta[j] > temp) {
662 for (j = 1; j <= i__1; ++j) {
663 if (vsig[j] < temp) {
670 /* Calculate the step to the new vertex and its sign. */
672 temp = gamma_ * rho * vsig[jdrop];
674 for (i__ = 1; i__ <= i__1; ++i__) {
675 dx[i__] = temp * simi[jdrop + i__ * simi_dim1];
680 for (k = 1; k <= i__1; ++k) {
683 for (i__ = 1; i__ <= i__2; ++i__) {
684 sum += a[i__ + k * a_dim1] * dx[i__];
687 temp = datmat[k + np * datmat_dim1];
688 d__1 = cvmaxp, d__2 = -sum - temp;
689 cvmaxp = max(d__1,d__2);
690 d__1 = cvmaxm, d__2 = sum - temp;
691 cvmaxm = max(d__1,d__2);
695 if (parmu * (cvmaxp - cvmaxm) > sum + sum) {
699 /* Update the elements of SIM and SIMI, and set the next X. */
703 for (i__ = 1; i__ <= i__1; ++i__) {
704 dx[i__] = dxsign * dx[i__];
705 /* SGJ: make sure dx step says in [lb,ub] */
708 double xi = sim[i__ + np * sim_dim1];
710 if (xi + dx[i__] > ub[i__])
712 if (xi + dx[i__] < lb[i__]) {
713 if (xi - dx[i__] <= ub[i__])
715 else { /* try again with halved step */
722 sim[i__ + jdrop * sim_dim1] = dx[i__];
723 temp += simi[jdrop + i__ * simi_dim1] * dx[i__];
726 for (i__ = 1; i__ <= i__1; ++i__) {
727 simi[jdrop + i__ * simi_dim1] /= temp;
730 for (j = 1; j <= i__1; ++j) {
734 for (i__ = 1; i__ <= i__2; ++i__) {
735 temp += simi[j + i__ * simi_dim1] * dx[i__];
738 for (i__ = 1; i__ <= i__2; ++i__) {
739 simi[j + i__ * simi_dim1] -= temp * simi[jdrop + i__ *
743 x[j] = sim[j + np * sim_dim1] + dx[j];
747 /* Calculate DX=x(*)-x(0). Branch if the length of DX is less than 0.5*RHO. */
751 izdota = iz + *n * *n;
754 idxnew = isdirn + *n;
756 trstlp(n, m, &a[a_offset], &con[1], &rho, &dx[1], &ifull, &iact[1], &w[
757 iz], &w[izdota], &w[ivmc], &w[isdirn], &w[idxnew], &w[ivmd]);
759 /* SGJ: since the bound constraints are linear, we should never get
760 a dx that lies outside the [lb,ub] constraints here, but we'll
761 enforce this anyway just to be paranoid */
763 for (i__ = 1; i__ <= i__1; ++i__) {
764 double xi = sim[i__ + np * sim_dim1];
765 if (xi + dx[i__] > ub[i__]) dx[i__] = ub[i__] - xi;
766 if (xi + dx[i__] < lb[i__]) dx[i__] = xi - lb[i__];
772 for (i__ = 1; i__ <= i__1; ++i__) {
776 if (temp < rho * .25 * rho) {
782 /* Predict the change to F and the new maximum constraint violation if the */
783 /* variables are altered from x(0) to x(0)+DX. */
788 for (k = 1; k <= i__1; ++k) {
791 for (i__ = 1; i__ <= i__2; ++i__) {
792 sum -= a[i__ + k * a_dim1] * dx[i__];
795 resnew = max(resnew,sum);
799 /* Increase PARMU if necessary and branch back if this change alters the */
800 /* optimal vertex. Otherwise PREREM and PREREC will be set to the predicted */
801 /* reductions in the merit function and the maximum constraint violation */
805 prerec = datmat[*mpp + np * datmat_dim1] - resnew;
807 barmu = sum / prerec;
809 if (parmu < barmu * 1.5) {
812 fprintf(stderr, "cobyla: increase in PARMU to %12.6E\n", parmu);
814 phi = datmat[mp + np * datmat_dim1] + parmu * datmat[*mpp + np *
817 for (j = 1; j <= i__1; ++j) {
818 temp = datmat[mp + j * datmat_dim1] + parmu * datmat[*mpp + j *
823 if (temp == phi && parmu == 0.f) {
824 if (datmat[*mpp + j * datmat_dim1] < datmat[*mpp + np *
831 prerem = parmu * prerec - sum;
833 /* Calculate the constraint and objective functions at x(*). Then find the */
834 /* actual reduction in the merit function. */
837 for (i__ = 1; i__ <= i__1; ++i__) {
838 x[i__] = sim[i__ + np * sim_dim1] + dx[i__];
843 vmold = datmat[mp + np * datmat_dim1] + parmu * datmat[*mpp + np *
845 vmnew = f + parmu * resmax;
846 trured = vmold - vmnew;
847 if (parmu == 0. && f == datmat[mp + np * datmat_dim1]) {
849 trured = datmat[*mpp + np * datmat_dim1] - resmax;
852 /* Begin the operations that decide whether x(*) should replace one of the */
853 /* vertices of the current simplex, the change being mandatory if TRURED is */
854 /* positive. Firstly, JDROP is set to the index of the vertex that is to be */
863 for (j = 1; j <= i__1; ++j) {
866 for (i__ = 1; i__ <= i__2; ++i__) {
867 temp += simi[j + i__ * simi_dim1] * dx[i__];
874 sigbar[j] = temp * vsig[j];
877 /* Calculate the value of ell. */
879 edgmax = delta * rho;
882 for (j = 1; j <= i__1; ++j) {
883 if (sigbar[j] >= parsig || sigbar[j] >= vsig[j]) {
888 for (i__ = 1; i__ <= i__2; ++i__) {
889 d__1 = dx[i__] - sim[i__ + j * sim_dim1];
907 /* Revise the simplex by updating the elements of SIM, SIMI and DATMAT. */
911 for (i__ = 1; i__ <= i__1; ++i__) {
912 sim[i__ + jdrop * sim_dim1] = dx[i__];
913 temp += simi[jdrop + i__ * simi_dim1] * dx[i__];
916 for (i__ = 1; i__ <= i__1; ++i__) {
917 simi[jdrop + i__ * simi_dim1] /= temp;
920 for (j = 1; j <= i__1; ++j) {
924 for (i__ = 1; i__ <= i__2; ++i__) {
925 temp += simi[j + i__ * simi_dim1] * dx[i__];
928 for (i__ = 1; i__ <= i__2; ++i__) {
929 simi[j + i__ * simi_dim1] -= temp * simi[jdrop + i__ *
935 for (k = 1; k <= i__1; ++k) {
936 datmat[k + jdrop * datmat_dim1] = con[k];
939 /* Branch back for further iterations with the current RHO. */
941 if (trured > 0. && trured >= prerem * .1) {
942 /* SGJ, 2010: following a suggestion in the SAS manual (which
943 mentions a similar modification to COBYLA, although they didn't
944 publish their source code), increase rho if predicted reduction
945 is sufficiently close to the actual reduction. Otherwise,
946 COBLYA seems to easily get stuck making very small steps.
947 Also require iflag != 0 (i.e., acceptable simplex), again
948 following SAS suggestion (otherwise I get convergence failure
950 if (trured >= prerem * 0.9 && trured <= prerem * 1.1 && iflag) {
961 /* SGJ, 2008: convergence tests for function vals; note that current
962 best val is stored in datmat[mp + np * datmat_dim1], or in f if
963 ifull == 1, and previous best is in *minf. This seems like a
964 sensible place to put the convergence test, as it is the same
965 place that Powell checks the x tolerance (rho > rhoend). */
967 double fbest = ifull == 1 ? f : datmat[mp + np * datmat_dim1];
968 if (fbest < *minf && nlopt_stop_ftol(stop, fbest, *minf)) {
969 rc = NLOPT_FTOL_REACHED;
975 /* Otherwise reduce RHO if it is not at its least value and reset PARMU. */
979 if (rho <= rhoend * 1.5) {
985 for (k = 1; k <= i__1; ++k) {
986 cmin = datmat[k + np * datmat_dim1];
989 for (i__ = 1; i__ <= i__2; ++i__) {
990 d__1 = cmin, d__2 = datmat[k + i__ * datmat_dim1];
991 cmin = min(d__1,d__2);
992 d__1 = cmax, d__2 = datmat[k + i__ * datmat_dim1];
993 cmax = max(d__1,d__2);
995 if (k <= *m && cmin < cmax * .5) {
996 temp = max(cmax,0.) - cmin;
1000 denom = min(denom,temp);
1006 } else if (cmax - cmin < parmu * denom) {
1007 parmu = (cmax - cmin) / denom;
1011 fprintf(stderr, "cobyla: reduction in RHO to %12.6E and PARMU =%13.6E\n",
1015 fprintf(stderr, "cobyla: NFVALS = %4d, F =%13.6E, MAXCV =%13.6E\n",
1016 stop->nevals, datmat[mp + np * datmat_dim1], datmat[*mpp + np * datmat_dim1]);
1018 fprintf(stderr, "cobyla: X =");
1020 for (i__ = 1; i__ <= i__1; ++i__) {
1021 if (i__>1) fprintf(stderr, " ");
1022 fprintf(stderr, "%13.6E", sim[i__ + np * sim_dim1]);
1026 for (i__ = iptemp; i__ <= i__1; ++i__) {
1027 if (!((i__-1) % 4)) fprintf(stderr, "\ncobyla: ");
1028 fprintf(stderr, "%15.6E", x[i__]);
1031 fprintf(stderr, "\n");
1036 rc = NLOPT_XTOL_REACHED;
1038 /* Return the best calculated values of the variables. */
1041 fprintf(stderr, "cobyla: normal return.\n");
1048 for (i__ = 1; i__ <= i__1; ++i__) {
1049 x[i__] = sim[i__ + np * sim_dim1];
1051 f = datmat[mp + np * datmat_dim1];
1052 resmax = datmat[*mpp + np * datmat_dim1];
1056 fprintf(stderr, "cobyla: NFVALS = %4d, F =%13.6E, MAXCV =%13.6E\n",
1057 stop->nevals, f, resmax);
1059 fprintf(stderr, "cobyla: X =");
1060 for (i__ = 1; i__ <= i__1; ++i__) {
1061 if (i__>1) fprintf(stderr, " ");
1062 fprintf(stderr, "%13.6E", x[i__]);
1066 for (i__ = iptemp; i__ <= i__1; ++i__) {
1067 if (!((i__-1) % 4)) fprintf(stderr, "\ncobyla: ");
1068 fprintf(stderr, "%15.6E", x[i__]);
1071 fprintf(stderr, "\n");
1076 /* ------------------------------------------------------------------------- */
1077 static void trstlp(int *n, int *m, double *a,
1078 double *b, double *rho, double *dx, int *ifull,
1079 int *iact, double *z__, double *zdota, double *vmultc,
1080 double *sdirn, double *dxnew, double *vmultd)
1082 /* System generated locals */
1083 int a_dim1, a_offset, z_dim1, z_offset, i__1, i__2;
1086 /* Local variables */
1087 double alpha, tempa;
1089 double optnew, stpful, sum, tot, acca, accb;
1090 double ratio, vsave, zdotv, zdotw, dd;
1092 double sp, ss, resold = 0.0, zdvabs, zdwabs, sumabs, resmax, optold;
1096 int iout, i__, j, k;
1100 int nact, icon = 0, mcon;
1104 /* This subroutine calculates an N-component vector DX by applying the */
1105 /* following two stages. In the first stage, DX is set to the shortest */
1106 /* vector that minimizes the greatest violation of the constraints */
1107 /* A(1,K)*DX(1)+A(2,K)*DX(2)+...+A(N,K)*DX(N) .GE. B(K), K=2,3,...,M, */
1108 /* subject to the Euclidean length of DX being at most RHO. If its length is */
1109 /* strictly less than RHO, then we use the resultant freedom in DX to */
1110 /* minimize the objective function */
1111 /* -A(1,M+1)*DX(1)-A(2,M+1)*DX(2)-...-A(N,M+1)*DX(N) */
1112 /* subject to no increase in any greatest constraint violation. This */
1113 /* notation allows the gradient of the objective function to be regarded as */
1114 /* the gradient of a constraint. Therefore the two stages are distinguished */
1115 /* by MCON .EQ. M and MCON .GT. M respectively. It is possible that a */
1116 /* degeneracy may prevent DX from attaining the target length RHO. Then the */
1117 /* value IFULL=0 would be set, but usually IFULL=1 on return. */
1119 /* In general NACT is the number of constraints in the active set and */
1120 /* IACT(1),...,IACT(NACT) are their indices, while the remainder of IACT */
1121 /* contains a permutation of the remaining constraint indices. Further, Z is */
1122 /* an orthogonal matrix whose first NACT columns can be regarded as the */
1123 /* result of Gram-Schmidt applied to the active constraint gradients. For */
1124 /* J=1,2,...,NACT, the number ZDOTA(J) is the scalar product of the J-th */
1125 /* column of Z with the gradient of the J-th active constraint. DX is the */
1126 /* current vector of variables and here the residuals of the active */
1127 /* constraints should be zero. Further, the active constraints have */
1128 /* nonnegative Lagrange multipliers that are held at the beginning of */
1129 /* VMULTC. The remainder of this vector holds the residuals of the inactive */
1130 /* constraints at DX, the ordering of the components of VMULTC being in */
1131 /* agreement with the permutation of the indices of the constraints that is */
1132 /* in IACT. All these residuals are nonnegative, which is achieved by the */
1133 /* shift RESMAX that makes the least residual zero. */
1135 /* Initialize Z and some other variables. The value of RESMAX will be */
1136 /* appropriate to DX=0, while ICON will be the index of a most violated */
1137 /* constraint if RESMAX is positive. Usually during the first stage the */
1138 /* vector SDIRN gives a search direction that reduces all the active */
1139 /* constraint violations by one simultaneously. */
1141 /* Parameter adjustments */
1143 z_offset = 1 + z_dim1 * 1;
1146 a_offset = 1 + a_dim1 * 1;
1163 for (i__ = 1; i__ <= i__1; ++i__) {
1165 for (j = 1; j <= i__2; ++j) {
1166 z__[i__ + j * z_dim1] = 0.;
1168 z__[i__ + i__ * z_dim1] = 1.;
1173 for (k = 1; k <= i__1; ++k) {
1174 if (b[k] > resmax) {
1180 for (k = 1; k <= i__1; ++k) {
1182 vmultc[k] = resmax - b[k];
1189 for (i__ = 1; i__ <= i__1; ++i__) {
1193 /* End the current stage of the calculation if 3 consecutive iterations */
1194 /* have either failed to reduce the best calculated value of the objective */
1195 /* function or to increase the number of active constraints since the best */
1196 /* value was calculated. This strategy prevents cycling, but there is a */
1197 /* remote possibility that it will cause premature termination. */
1208 for (i__ = 1; i__ <= i__1; ++i__) {
1209 optnew -= dx[i__] * a[i__ + mcon * a_dim1];
1212 if (icount == 0 || optnew < optold) {
1216 } else if (nact > nactx) {
1226 /* If ICON exceeds NACT, then we add the constraint with index IACT(ICON) to */
1227 /* the active set. Apply Givens rotations so that the last N-NACT-1 columns */
1228 /* of Z are orthogonal to the gradient of the new constraint, a scalar */
1229 /* product being set to zero if its nonzero value could be due to computer */
1230 /* rounding errors. The array DXNEW is used for working space. */
1237 for (i__ = 1; i__ <= i__1; ++i__) {
1238 dxnew[i__] = a[i__ + kk * a_dim1];
1247 for (i__ = 1; i__ <= i__1; ++i__) {
1248 temp = z__[i__ + k * z_dim1] * dxnew[i__];
1252 acca = spabs + abs(sp) * .1;
1253 accb = spabs + abs(sp) * .2;
1254 if (spabs >= acca || acca >= accb) {
1261 temp = sqrt(sp * sp + tot * tot);
1266 for (i__ = 1; i__ <= i__1; ++i__) {
1267 temp = alpha * z__[i__ + k * z_dim1] + beta * z__[i__ + kp *
1269 z__[i__ + kp * z_dim1] = alpha * z__[i__ + kp * z_dim1] -
1270 beta * z__[i__ + k * z_dim1];
1271 z__[i__ + k * z_dim1] = temp;
1278 /* Add the new constraint if this can be done without a deletion from the */
1284 vmultc[icon] = vmultc[nact];
1289 /* The next instruction is reached if a deletion has to be made from the */
1290 /* active set in order to make room for the new active constraint, because */
1291 /* the new constraint gradient is a linear combination of the gradients of */
1292 /* the old active constraints. Set the elements of VMULTD to the multipliers */
1293 /* of the linear combination. Further, set IOUT to the index of the */
1294 /* constraint to be deleted, but branch if no suitable index can be found. */
1302 for (i__ = 1; i__ <= i__1; ++i__) {
1303 temp = z__[i__ + k * z_dim1] * dxnew[i__];
1305 zdvabs += abs(temp);
1307 acca = zdvabs + abs(zdotv) * .1;
1308 accb = zdvabs + abs(zdotv) * .2;
1309 if (zdvabs < acca && acca < accb) {
1310 temp = zdotv / zdota[k];
1311 if (temp > 0. && iact[k] <= *m) {
1312 tempa = vmultc[k] / temp;
1313 if (ratio < 0. || tempa < ratio) {
1321 for (i__ = 1; i__ <= i__1; ++i__) {
1322 dxnew[i__] -= temp * a[i__ + kw * a_dim1];
1337 /* Revise the Lagrange multipliers and reorder the active constraints so */
1338 /* that the one to be replaced is at the end of the list. Also calculate the */
1339 /* new value of ZDOTA(NACT) and branch if it is not acceptable. */
1342 for (k = 1; k <= i__1; ++k) {
1343 d__1 = 0., d__2 = vmultc[k] - ratio * vmultd[k];
1344 vmultc[k] = max(d__1,d__2);
1348 vsave = vmultc[icon];
1355 for (i__ = 1; i__ <= i__1; ++i__) {
1356 sp += z__[i__ + k * z_dim1] * a[i__ + kw * a_dim1];
1359 temp = sqrt(sp * sp + d__1 * d__1);
1360 alpha = zdota[kp] / temp;
1362 zdota[kp] = alpha * zdota[k];
1365 for (i__ = 1; i__ <= i__1; ++i__) {
1366 temp = alpha * z__[i__ + kp * z_dim1] + beta * z__[i__ + k *
1368 z__[i__ + kp * z_dim1] = alpha * z__[i__ + k * z_dim1] - beta *
1369 z__[i__ + kp * z_dim1];
1370 z__[i__ + k * z_dim1] = temp;
1373 vmultc[k] = vmultc[kp];
1383 for (i__ = 1; i__ <= i__1; ++i__) {
1384 temp += z__[i__ + nact * z_dim1] * a[i__ + kk * a_dim1];
1391 vmultc[nact] = ratio;
1393 /* Update IACT and ensure that the objective function continues to be */
1394 /* treated as the last active constraint when MCON>M. */
1397 iact[icon] = iact[nact];
1399 if (mcon > *m && kk != mcon) {
1403 for (i__ = 1; i__ <= i__1; ++i__) {
1404 sp += z__[i__ + k * z_dim1] * a[i__ + kk * a_dim1];
1407 temp = sqrt(sp * sp + d__1 * d__1);
1408 alpha = zdota[nact] / temp;
1410 zdota[nact] = alpha * zdota[k];
1413 for (i__ = 1; i__ <= i__1; ++i__) {
1414 temp = alpha * z__[i__ + nact * z_dim1] + beta * z__[i__ + k *
1416 z__[i__ + nact * z_dim1] = alpha * z__[i__ + k * z_dim1] - beta *
1417 z__[i__ + nact * z_dim1];
1418 z__[i__ + k * z_dim1] = temp;
1420 iact[nact] = iact[k];
1423 vmultc[k] = vmultc[nact];
1424 vmultc[nact] = temp;
1427 /* If stage one is in progress, then set SDIRN to the direction of the next */
1428 /* change to the current vector of variables. */
1436 for (i__ = 1; i__ <= i__1; ++i__) {
1437 temp += sdirn[i__] * a[i__ + kk * a_dim1];
1440 temp /= zdota[nact];
1442 for (i__ = 1; i__ <= i__1; ++i__) {
1443 sdirn[i__] -= temp * z__[i__ + nact * z_dim1];
1447 /* Delete the constraint that has the index IACT(ICON) from the active set. */
1452 vsave = vmultc[icon];
1459 for (i__ = 1; i__ <= i__1; ++i__) {
1460 sp += z__[i__ + k * z_dim1] * a[i__ + kk * a_dim1];
1463 temp = sqrt(sp * sp + d__1 * d__1);
1464 alpha = zdota[kp] / temp;
1466 zdota[kp] = alpha * zdota[k];
1469 for (i__ = 1; i__ <= i__1; ++i__) {
1470 temp = alpha * z__[i__ + kp * z_dim1] + beta * z__[i__ + k *
1472 z__[i__ + kp * z_dim1] = alpha * z__[i__ + k * z_dim1] - beta *
1473 z__[i__ + kp * z_dim1];
1474 z__[i__ + k * z_dim1] = temp;
1477 vmultc[k] = vmultc[kp];
1487 /* If stage one is in progress, then set SDIRN to the direction of the next */
1488 /* change to the current vector of variables. */
1495 for (i__ = 1; i__ <= i__1; ++i__) {
1496 temp += sdirn[i__] * z__[i__ + (nact + 1) * z_dim1];
1499 for (i__ = 1; i__ <= i__1; ++i__) {
1500 sdirn[i__] -= temp * z__[i__ + (nact + 1) * z_dim1];
1504 /* Pick the next search direction of stage two. */
1507 temp = 1. / zdota[nact];
1509 for (i__ = 1; i__ <= i__1; ++i__) {
1510 sdirn[i__] = temp * z__[i__ + nact * z_dim1];
1513 /* Calculate the step to the boundary of the trust region or take the step */
1514 /* that reduces RESMAX to zero. The two statements below that include the */
1515 /* factor 1.0E-6 prevent some harmless underflows that occurred in a test */
1516 /* calculation. Further, we skip the step if it could be zero within a */
1517 /* reasonable tolerance for computer rounding errors. */
1524 for (i__ = 1; i__ <= i__1; ++i__) {
1525 if ((d__1 = dx[i__], abs(d__1)) >= *rho * 1e-6f) {
1529 sd += dx[i__] * sdirn[i__];
1536 temp = sqrt(ss * dd);
1537 if (abs(sd) >= temp * 1e-6f) {
1538 temp = sqrt(ss * dd + sd * sd);
1540 stpful = dd / (temp + sd);
1543 acca = step + resmax * .1;
1544 accb = step + resmax * .2;
1545 if (step >= acca || acca >= accb) {
1548 step = min(step,resmax);
1551 /* Set DXNEW to the new variables if STEP is the steplength, and reduce */
1552 /* RESMAX to the corresponding maximum residual if stage one is being done. */
1553 /* Because DXNEW will be changed during the calculation of some Lagrange */
1554 /* multipliers, it will be restored to the following value later. */
1557 for (i__ = 1; i__ <= i__1; ++i__) {
1558 dxnew[i__] = dx[i__] + step * sdirn[i__];
1564 for (k = 1; k <= i__1; ++k) {
1568 for (i__ = 1; i__ <= i__2; ++i__) {
1569 temp -= a[i__ + kk * a_dim1] * dxnew[i__];
1571 resmax = max(resmax,temp);
1575 /* Set VMULTD to the VMULTC vector that would occur if DX became DXNEW. A */
1576 /* device is included to force VMULTD(K)=0.0 if deviations from this value */
1577 /* can be attributed to computer rounding errors. First calculate the new */
1578 /* Lagrange multipliers. */
1585 for (i__ = 1; i__ <= i__1; ++i__) {
1586 temp = z__[i__ + k * z_dim1] * dxnew[i__];
1588 zdwabs += abs(temp);
1590 acca = zdwabs + abs(zdotw) * .1;
1591 accb = zdwabs + abs(zdotw) * .2;
1592 if (zdwabs >= acca || acca >= accb) {
1595 vmultd[k] = zdotw / zdota[k];
1599 for (i__ = 1; i__ <= i__1; ++i__) {
1600 dxnew[i__] -= vmultd[k] * a[i__ + kk * a_dim1];
1606 d__1 = 0., d__2 = vmultd[nact];
1607 vmultd[nact] = max(d__1,d__2);
1610 /* Complete VMULTC by finding the new constraint residuals. */
1613 for (i__ = 1; i__ <= i__1; ++i__) {
1614 dxnew[i__] = dx[i__] + step * sdirn[i__];
1619 for (k = kl; k <= i__1; ++k) {
1621 sum = resmax - b[kk];
1622 sumabs = resmax + (d__1 = b[kk], abs(d__1));
1624 for (i__ = 1; i__ <= i__2; ++i__) {
1625 temp = a[i__ + kk * a_dim1] * dxnew[i__];
1627 sumabs += abs(temp);
1629 acca = sumabs + abs(sum) * .1f;
1630 accb = sumabs + abs(sum) * .2f;
1631 if (sumabs >= acca || acca >= accb) {
1638 /* Calculate the fraction of the step from DX to DXNEW that will be taken. */
1643 for (k = 1; k <= i__1; ++k) {
1644 if (vmultd[k] < 0.) {
1645 temp = vmultc[k] / (vmultc[k] - vmultd[k]);
1653 /* Update DX, VMULTC and RESMAX. */
1657 for (i__ = 1; i__ <= i__1; ++i__) {
1658 dx[i__] = temp * dx[i__] + ratio * dxnew[i__];
1661 for (k = 1; k <= i__1; ++k) {
1662 d__1 = 0., d__2 = temp * vmultc[k] + ratio * vmultd[k];
1663 vmultc[k] = max(d__1,d__2);
1666 resmax = resold + ratio * (resmax - resold);
1669 /* If the full step is not acceptable then begin another iteration. */
1670 /* Otherwise switch to stage two or end the calculation. */
1675 if (step == stpful) {
1685 /* We employ any freedom that may be available to reduce the objective */
1686 /* function before returning a DX whose length is less than RHO. */