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 #define min(a,b) ((a) <= (b) ? (a) : (b))
45 #define max(a,b) ((a) >= (b) ? (a) : (b))
46 #define abs(x) ((x) >= 0 ? (x) : -(x))
48 /**************************************************************************/
49 /* SGJ, 2008: NLopt-style interface function: */
57 ptrdiff_t fc_datum_size;
59 const double *lb, *ub;
62 static int func_wrap(int n, int m, double *x, double *f, double *con,
66 func_wrap_state *s = (func_wrap_state *) state;
67 double *xtmp = s->xtmp;
68 const double *lb = s->lb, *ub = s->ub;
70 /* in nlopt, we guarante that the function is never evaluated outside
71 the lb and ub bounds, so we need force this with xtmp */
72 for (j = 0; j < n; ++j) {
73 if (x[j] < lb[j]) xtmp[j] = lb[j];
74 else if (x[j] > ub[j]) xtmp[j] = ub[j];
78 *f = s->f(n, xtmp, NULL, s->f_data);
79 for (i = 0; i < s->m_orig; ++i)
80 con[i] = -s->fc(n, xtmp, NULL, s->fc_data + i * s->fc_datum_size);
81 for (j = 0; j < n; ++j) {
82 if (!nlopt_isinf(lb[j]))
83 con[i++] = x[j] - lb[j];
84 if (!nlopt_isinf(ub[j]))
85 con[i++] = ub[j] - x[j];
87 if (m != i) return 1; /* ... bug?? */
91 nlopt_result cobyla_minimize(int n, nlopt_func f, void *f_data,
93 void *fc_data_, ptrdiff_t fc_datum_size,
94 const double *lb, const double *ub, /* bounds */
95 double *x, /* in: initial guess, out: minimizer */
104 s.f = f; s.f_data = f_data;
106 s.fc = fc; s.fc_data = (char*) fc_data_; s.fc_datum_size = fc_datum_size;
107 s.lb = lb; s.ub = ub;
108 s.xtmp = (double *) malloc(sizeof(double) * n);
109 if (!s.xtmp) return NLOPT_OUT_OF_MEMORY;
111 /* add constraints for lower/upper bounds (if any) */
112 for (j = 0; j < n; ++j) {
113 if (!nlopt_isinf(lb[j]))
115 if (!nlopt_isinf(ub[j]))
119 ret = cobyla(n, m, x, minf, rhobegin, stop, COBYLA_MSG_NONE,
126 /**************************************************************************/
128 static nlopt_result cobylb(int *n, int *m, int *mpp, double *x, double *minf, double *rhobeg,
129 nlopt_stopping *stop, int *iprint, double *con, double *sim,
130 double *simi, double *datmat, double *a, double *vsig, double *veta,
131 double *sigbar, double *dx, double *w, int *iact, cobyla_function *calcfc,
133 static void trstlp(int *n, int *m, double *a, double *b, double *rho,
134 double *dx, int *ifull, int *iact, double *z__, double *zdota, double *vmultc,
135 double *sdirn, double *dxnew, double *vmultd);
137 /* ------------------------------------------------------------------------ */
139 nlopt_result cobyla(int n, int m, double *x, double *minf, double rhobeg, nlopt_stopping *stop, int iprint,
140 cobyla_function *calcfc, void *state)
142 int icon, isim, isigb, idatm, iveta, isimi, ivsig, iwork, ia, idx, mpp;
148 * This subroutine minimizes an objective function F(X) subject to M
149 * inequality constraints on X, where X is a vector of variables that has
150 * N components. The algorithm employs linear approximations to the
151 * objective and constraint functions, the approximations being formed by
152 * linear interpolation at N+1 points in the space of the variables.
153 * We regard these interpolation points as vertices of a simplex. The
154 * parameter RHO controls the size of the simplex and it is reduced
155 * automatically from RHOBEG to RHOEND. For each RHO the subroutine tries
156 * to achieve a good vector of variables for the current size, and then
157 * RHO is reduced until the value RHOEND is reached. Therefore RHOBEG and
158 * RHOEND should be set to reasonable initial changes to and the required
159 * accuracy in the variables respectively, but this accuracy should be
160 * viewed as a subject for experimentation because it is not guaranteed.
161 * The subroutine has an advantage over many of its competitors, however,
162 * which is that it treats each constraint individually when calculating
163 * a change to the variables, instead of lumping the constraints together
164 * into a single penalty function. The name of the subroutine is derived
165 * from the phrase Constrained Optimization BY Linear Approximations.
167 * The user must set the values of N, M, RHOBEG and RHOEND, and must
168 * provide an initial vector of variables in X. Further, the value of
169 * IPRINT should be set to 0, 1, 2 or 3, which controls the amount of
170 * printing during the calculation. Specifically, there is no output if
171 * IPRINT=0 and there is output only at the end of the calculation if
172 * IPRINT=1. Otherwise each new value of RHO and SIGMA is printed.
173 * Further, the vector of variables and some function information are
174 * given either when RHO is reduced or when each new value of F(X) is
175 * computed in the cases IPRINT=2 or IPRINT=3 respectively. Here SIGMA
176 * is a penalty parameter, it being assumed that a change to X is an
177 * improvement if it reduces the merit function
178 * F(X)+SIGMA*MAX(0.0,-C1(X),-C2(X),...,-CM(X)),
179 * where C1,C2,...,CM denote the constraint functions that should become
180 * nonnegative eventually, at least to the precision of RHOEND. In the
181 * printed output the displayed term that is multiplied by SIGMA is
182 * called MAXCV, which stands for 'MAXimum Constraint Violation'. The
183 * argument MAXFUN is an int variable that must be set by the user to a
184 * limit on the number of calls of CALCFC, the purpose of this routine being
185 * given below. The value of MAXFUN will be altered to the number of calls
186 * of CALCFC that are made. The arguments W and IACT provide real and
187 * int arrays that are used as working space. Their lengths must be at
188 * least N*(3*N+2*M+11)+4*M+6 and M+1 respectively.
190 * In order to define the objective and constraint functions, we require
191 * a subroutine that has the name and arguments
192 * SUBROUTINE CALCFC (N,M,X,F,CON)
193 * DIMENSION X(*),CON(*) .
194 * The values of N and M are fixed and have been defined already, while
195 * X is now the current vector of variables. The subroutine should return
196 * the objective and constraint functions at X in F and CON(1),CON(2),
197 * ...,CON(M). Note that we are trying to adjust X so that F(X) is as
198 * small as possible subject to the constraint functions being nonnegative.
200 * Partition the working space array W to provide the storage that is needed
201 * for the main calculation.
208 if (iprint>=1) fprintf(stderr, "cobyla: N==0.\n");
209 return NLOPT_SUCCESS;
214 if (iprint>=1) fprintf(stderr, "cobyla: N<0 or M<0.\n");
215 return NLOPT_INVALID_ARGS;
218 /* workspace allocation */
219 w = malloc((n*(3*n+2*m+11)+4*m+6)*sizeof(*w));
222 if (iprint>=1) fprintf(stderr, "cobyla: memory allocation error.\n");
223 return NLOPT_OUT_OF_MEMORY;
225 iact = malloc((m+1)*sizeof(*iact));
228 if (iprint>=1) fprintf(stderr, "cobyla: memory allocation error.\n");
230 return NLOPT_OUT_OF_MEMORY;
233 /* Parameter adjustments */
242 isimi = isim + n * n + n;
243 idatm = isimi + n * n;
244 ia = idatm + n * mpp + mpp;
245 ivsig = ia + m * n + n;
250 rc = cobylb(&n, &m, &mpp, &x[1], minf, &rhobeg, stop, &iprint,
251 &w[icon], &w[isim], &w[isimi], &w[idatm], &w[ia], &w[ivsig], &w[iveta],
252 &w[isigb], &w[idx], &w[iwork], &iact[1], calcfc, state);
254 /* Parameter adjustments (reverse) */
264 /* ------------------------------------------------------------------------- */
265 static nlopt_result cobylb(int *n, int *m, int *mpp, double
266 *x, double *minf, double *rhobeg, nlopt_stopping *stop, int *iprint,
267 double *con, double *sim, double *simi,
268 double *datmat, double *a, double *vsig, double *veta,
269 double *sigbar, double *dx, double *w, int *iact, cobyla_function *calcfc,
272 /* System generated locals */
273 int sim_dim1, sim_offset, simi_dim1, simi_offset, datmat_dim1,
274 datmat_offset, a_dim1, a_offset, i__1, i__2, i__3;
277 /* Local variables */
278 double alpha, delta, denom, tempa, barmu;
279 double beta, cmin = 0.0, cmax = 0.0;
280 double cvmaxm, dxsign, prerem = 0.0;
281 double edgmax, pareta, prerec = 0.0, phimin, parsig = 0.0;
283 double phi, rho, sum = 0.0;
284 double ratio, vmold, parmu, error, vmnew;
285 double resmax, cvmaxp;
286 double resnew, trured;
287 double temp, wsig, f;
296 int mp, np, iz, ibrnch;
297 int nbest, ifull, iptem, jdrop;
298 nlopt_result rc = NLOPT_SUCCESS;
301 /* SGJ, 2008: compute rhoend from NLopt stop info */
302 rhoend = stop->xtol_rel * (*rhobeg);
303 for (j = 0; j < *n; ++j)
304 if (rhoend < stop->xtol_abs[j])
305 rhoend = stop->xtol_abs[j];
307 /* SGJ, 2008: added code to keep track of minimum feasible function val */
310 /* Set the initial values of some parameters. The last column of SIM holds */
311 /* the optimal vertex of the current simplex, and the preceding N columns */
312 /* hold the displacements from the optimal vertex to the other vertices. */
313 /* Further, SIMI holds the inverse of the matrix that is contained in the */
314 /* first N columns of SIM. */
316 /* Parameter adjustments */
318 a_offset = 1 + a_dim1 * 1;
321 simi_offset = 1 + simi_dim1 * 1;
324 sim_offset = 1 + sim_dim1 * 1;
327 datmat_offset = 1 + datmat_dim1 * 1;
328 datmat -= datmat_offset;
351 "cobyla: the initial value of RHO is %12.6E and PARMU is set to zero.\n",
356 for (i__ = 1; i__ <= i__1; ++i__) {
357 sim[i__ + np * sim_dim1] = x[i__];
359 for (j = 1; j <= i__2; ++j) {
360 sim[i__ + j * sim_dim1] = 0.;
361 simi[i__ + j * simi_dim1] = 0.;
363 sim[i__ + i__ * sim_dim1] = rho;
364 simi[i__ + i__ * simi_dim1] = temp;
369 /* Make the next call of the user-supplied subroutine CALCFC. These */
370 /* instructions are also used for calling CALCFC during the iterations of */
374 if (nlopt_stop_evals(stop)) rc = NLOPT_MAXEVAL_REACHED;
375 else if (nlopt_stop_time(stop)) rc = NLOPT_MAXTIME_REACHED;
376 if (rc != NLOPT_SUCCESS) goto L600;
379 if (calcfc(*n, *m, &x[1], &f, &con[1], state))
382 fprintf(stderr, "cobyla: user requested end of minimization.\n");
391 for (k = 1; k <= i__1; ++k) {
392 d__1 = resmax, d__2 = -con[k];
393 resmax = max(d__1,d__2);
397 /* SGJ, 2008: check for minf_max reached by feasible point */
398 if (f < stop->minf_max && resmax <= 0) {
399 rc = NLOPT_MINF_MAX_REACHED;
400 goto L620; /* not L600 because we want to use current x, f, resmax */
403 if (stop->nevals == *iprint - 1 || *iprint == 3) {
404 fprintf(stderr, "cobyla: NFVALS = %4d, F =%13.6E, MAXCV =%13.6E\n",
405 stop->nevals, f, resmax);
407 fprintf(stderr, "cobyla: X =");
408 for (i__ = 1; i__ <= i__1; ++i__) {
409 if (i__>1) fprintf(stderr, " ");
410 fprintf(stderr, "%13.6E", x[i__]);
414 for (i__ = iptemp; i__ <= i__1; ++i__) {
415 if (!((i__-1) % 4)) fprintf(stderr, "\ncobyla: ");
416 fprintf(stderr, "%15.6E", x[i__]);
419 fprintf(stderr, "\n");
427 /* Set the recently calculated function values in a column of DATMAT. This */
428 /* array has a column for each vertex of the current simplex, the entries of */
429 /* each column being the values of the constraint functions (if any) */
430 /* followed by the objective function and the greatest constraint violation */
434 for (k = 1; k <= i__1; ++k) {
435 datmat[k + jdrop * datmat_dim1] = con[k];
437 if (stop->nevals > np) {
441 /* Exchange the new vertex of the initial simplex with the optimal vertex if */
442 /* necessary. Then, if the initial simplex is not complete, pick its next */
443 /* vertex and calculate the function values there. */
446 if (datmat[mp + np * datmat_dim1] <= f) {
447 x[jdrop] = sim[jdrop + np * sim_dim1];
448 } else { /* improvement in function val */
449 sim[jdrop + np * sim_dim1] = x[jdrop];
451 for (k = 1; k <= i__1; ++k) {
452 datmat[k + jdrop * datmat_dim1] = datmat[k + np * datmat_dim1]
454 datmat[k + np * datmat_dim1] = con[k];
457 for (k = 1; k <= i__1; ++k) {
458 sim[jdrop + k * sim_dim1] = -rho;
461 for (i__ = k; i__ <= i__2; ++i__) {
462 temp -= simi[i__ + k * simi_dim1];
464 simi[jdrop + k * simi_dim1] = temp;
468 if (stop->nevals <= *n) {
469 jdrop = stop->nevals;
476 /* Identify the optimal vertex of the current simplex. */
479 phimin = datmat[mp + np * datmat_dim1] + parmu * datmat[*mpp + np *
483 for (j = 1; j <= i__1; ++j) {
484 temp = datmat[mp + j * datmat_dim1] + parmu * datmat[*mpp + j *
489 } else if (temp == phimin && parmu == 0.) {
490 if (datmat[*mpp + j * datmat_dim1] < datmat[*mpp + nbest *
497 /* Switch the best vertex into pole position if it is not there already, */
498 /* and also update SIM, SIMI and DATMAT. */
502 for (i__ = 1; i__ <= i__1; ++i__) {
503 temp = datmat[i__ + np * datmat_dim1];
504 datmat[i__ + np * datmat_dim1] = datmat[i__ + nbest * datmat_dim1]
506 datmat[i__ + nbest * datmat_dim1] = temp;
509 for (i__ = 1; i__ <= i__1; ++i__) {
510 temp = sim[i__ + nbest * sim_dim1];
511 sim[i__ + nbest * sim_dim1] = 0.;
512 sim[i__ + np * sim_dim1] += temp;
515 for (k = 1; k <= i__2; ++k) {
516 sim[i__ + k * sim_dim1] -= temp;
517 tempa -= simi[k + i__ * simi_dim1];
519 simi[nbest + i__ * simi_dim1] = tempa;
523 /* Make an error return if SIGI is a poor approximation to the inverse of */
524 /* the leading N by N submatrix of SIG. */
528 for (i__ = 1; i__ <= i__1; ++i__) {
530 for (j = 1; j <= i__2; ++j) {
536 for (k = 1; k <= i__3; ++k) if (sim[k + j * sim_dim1] != 0) {
537 temp += simi[i__ + k * simi_dim1] * sim[k + j * sim_dim1];
539 d__1 = error, d__2 = abs(temp);
540 error = max(d__1,d__2);
545 fprintf(stderr, "cobyla: rounding errors are becoming damaging.\n");
551 /* Calculate the coefficients of the linear approximations to the objective */
552 /* and constraint functions, placing minus the objective function gradient */
553 /* after the constraint gradients in the array A. The vector W is used for */
557 for (k = 1; k <= i__2; ++k) {
558 con[k] = -datmat[k + np * datmat_dim1];
560 for (j = 1; j <= i__1; ++j) {
561 w[j] = datmat[k + j * datmat_dim1] + con[k];
564 for (i__ = 1; i__ <= i__1; ++i__) {
567 for (j = 1; j <= i__3; ++j) {
568 temp += w[j] * simi[j + i__ * simi_dim1];
573 a[i__ + k * a_dim1] = temp;
577 /* Calculate the values of sigma and eta, and set IFLAG=0 if the current */
578 /* simplex is not acceptable. */
581 parsig = alpha * rho;
584 for (j = 1; j <= i__1; ++j) {
588 for (i__ = 1; i__ <= i__2; ++i__) {
589 d__1 = simi[j + i__ * simi_dim1];
591 d__1 = sim[i__ + j * sim_dim1];
594 vsig[j] = 1. / sqrt(wsig);
595 veta[j] = sqrt(weta);
596 if (vsig[j] < parsig || veta[j] > pareta) {
601 /* If a new vertex is needed to improve acceptability, then decide which */
602 /* vertex to drop from the simplex. */
604 if (ibrnch == 1 || iflag == 1) {
610 for (j = 1; j <= i__1; ++j) {
611 if (veta[j] > temp) {
618 for (j = 1; j <= i__1; ++j) {
619 if (vsig[j] < temp) {
626 /* Calculate the step to the new vertex and its sign. */
628 temp = gamma_ * rho * vsig[jdrop];
630 for (i__ = 1; i__ <= i__1; ++i__) {
631 dx[i__] = temp * simi[jdrop + i__ * simi_dim1];
636 for (k = 1; k <= i__1; ++k) {
639 for (i__ = 1; i__ <= i__2; ++i__) {
640 sum += a[i__ + k * a_dim1] * dx[i__];
643 temp = datmat[k + np * datmat_dim1];
644 d__1 = cvmaxp, d__2 = -sum - temp;
645 cvmaxp = max(d__1,d__2);
646 d__1 = cvmaxm, d__2 = sum - temp;
647 cvmaxm = max(d__1,d__2);
651 if (parmu * (cvmaxp - cvmaxm) > sum + sum) {
655 /* Update the elements of SIM and SIMI, and set the next X. */
659 for (i__ = 1; i__ <= i__1; ++i__) {
660 dx[i__] = dxsign * dx[i__];
661 sim[i__ + jdrop * sim_dim1] = dx[i__];
662 temp += simi[jdrop + i__ * simi_dim1] * dx[i__];
665 for (i__ = 1; i__ <= i__1; ++i__) {
666 simi[jdrop + i__ * simi_dim1] /= temp;
669 for (j = 1; j <= i__1; ++j) {
673 for (i__ = 1; i__ <= i__2; ++i__) {
674 temp += simi[j + i__ * simi_dim1] * dx[i__];
677 for (i__ = 1; i__ <= i__2; ++i__) {
678 simi[j + i__ * simi_dim1] -= temp * simi[jdrop + i__ *
682 x[j] = sim[j + np * sim_dim1] + dx[j];
686 /* Calculate DX=x(*)-x(0). Branch if the length of DX is less than 0.5*RHO. */
690 izdota = iz + *n * *n;
693 idxnew = isdirn + *n;
695 trstlp(n, m, &a[a_offset], &con[1], &rho, &dx[1], &ifull, &iact[1], &w[
696 iz], &w[izdota], &w[ivmc], &w[isdirn], &w[idxnew], &w[ivmd]);
700 for (i__ = 1; i__ <= i__1; ++i__) {
704 if (temp < rho * .25 * rho) {
710 /* Predict the change to F and the new maximum constraint violation if the */
711 /* variables are altered from x(0) to x(0)+DX. */
716 for (k = 1; k <= i__1; ++k) {
719 for (i__ = 1; i__ <= i__2; ++i__) {
720 sum -= a[i__ + k * a_dim1] * dx[i__];
723 resnew = max(resnew,sum);
727 /* Increase PARMU if necessary and branch back if this change alters the */
728 /* optimal vertex. Otherwise PREREM and PREREC will be set to the predicted */
729 /* reductions in the merit function and the maximum constraint violation */
733 prerec = datmat[*mpp + np * datmat_dim1] - resnew;
735 barmu = sum / prerec;
737 if (parmu < barmu * 1.5) {
740 fprintf(stderr, "cobyla: increase in PARMU to %12.6E\n", parmu);
742 phi = datmat[mp + np * datmat_dim1] + parmu * datmat[*mpp + np *
745 for (j = 1; j <= i__1; ++j) {
746 temp = datmat[mp + j * datmat_dim1] + parmu * datmat[*mpp + j *
751 if (temp == phi && parmu == 0.f) {
752 if (datmat[*mpp + j * datmat_dim1] < datmat[*mpp + np *
759 prerem = parmu * prerec - sum;
761 /* Calculate the constraint and objective functions at x(*). Then find the */
762 /* actual reduction in the merit function. */
765 for (i__ = 1; i__ <= i__1; ++i__) {
766 x[i__] = sim[i__ + np * sim_dim1] + dx[i__];
771 vmold = datmat[mp + np * datmat_dim1] + parmu * datmat[*mpp + np *
773 vmnew = f + parmu * resmax;
774 trured = vmold - vmnew;
775 if (parmu == 0. && f == datmat[mp + np * datmat_dim1]) {
777 trured = datmat[*mpp + np * datmat_dim1] - resmax;
780 /* Begin the operations that decide whether x(*) should replace one of the */
781 /* vertices of the current simplex, the change being mandatory if TRURED is */
782 /* positive. Firstly, JDROP is set to the index of the vertex that is to be */
791 for (j = 1; j <= i__1; ++j) {
794 for (i__ = 1; i__ <= i__2; ++i__) {
795 temp += simi[j + i__ * simi_dim1] * dx[i__];
802 sigbar[j] = temp * vsig[j];
805 /* Calculate the value of ell. */
807 edgmax = delta * rho;
810 for (j = 1; j <= i__1; ++j) {
811 if (sigbar[j] >= parsig || sigbar[j] >= vsig[j]) {
816 for (i__ = 1; i__ <= i__2; ++i__) {
817 d__1 = dx[i__] - sim[i__ + j * sim_dim1];
835 /* Revise the simplex by updating the elements of SIM, SIMI and DATMAT. */
839 for (i__ = 1; i__ <= i__1; ++i__) {
840 sim[i__ + jdrop * sim_dim1] = dx[i__];
841 temp += simi[jdrop + i__ * simi_dim1] * dx[i__];
844 for (i__ = 1; i__ <= i__1; ++i__) {
845 simi[jdrop + i__ * simi_dim1] /= temp;
848 for (j = 1; j <= i__1; ++j) {
852 for (i__ = 1; i__ <= i__2; ++i__) {
853 temp += simi[j + i__ * simi_dim1] * dx[i__];
856 for (i__ = 1; i__ <= i__2; ++i__) {
857 simi[j + i__ * simi_dim1] -= temp * simi[jdrop + i__ *
863 for (k = 1; k <= i__1; ++k) {
864 datmat[k + jdrop * datmat_dim1] = con[k];
867 /* Branch back for further iterations with the current RHO. */
869 if (trured > 0. && trured >= prerem * .1) {
878 /* SGJ, 2008: convergence tests for function vals; note that current
879 best val is stored in datmat[mp + np * datmat_dim1], or in f if
880 ifull == 1, and prev. best is in *minf. This seems like a
881 sensible place to put the convergence test, as it is the same
882 place that Powell checks the x tolerance (rho > rhoend). */
884 double fbest = ifull == 1 ? f : datmat[mp + np * datmat_dim1];
885 if (fbest < *minf && nlopt_stop_ftol(stop, fbest, *minf)) {
886 rc = NLOPT_FTOL_REACHED;
892 /* Otherwise reduce RHO if it is not at its least value and reset PARMU. */
896 if (rho <= rhoend * 1.5) {
902 for (k = 1; k <= i__1; ++k) {
903 cmin = datmat[k + np * datmat_dim1];
906 for (i__ = 1; i__ <= i__2; ++i__) {
907 d__1 = cmin, d__2 = datmat[k + i__ * datmat_dim1];
908 cmin = min(d__1,d__2);
909 d__1 = cmax, d__2 = datmat[k + i__ * datmat_dim1];
910 cmax = max(d__1,d__2);
912 if (k <= *m && cmin < cmax * .5) {
913 temp = max(cmax,0.) - cmin;
917 denom = min(denom,temp);
923 } else if (cmax - cmin < parmu * denom) {
924 parmu = (cmax - cmin) / denom;
928 fprintf(stderr, "cobyla: reduction in RHO to %12.6E and PARMU =%13.6E\n",
932 fprintf(stderr, "cobyla: NFVALS = %4d, F =%13.6E, MAXCV =%13.6E\n",
933 stop->nevals, datmat[mp + np * datmat_dim1], datmat[*mpp + np * datmat_dim1]);
935 fprintf(stderr, "cobyla: X =");
937 for (i__ = 1; i__ <= i__1; ++i__) {
938 if (i__>1) fprintf(stderr, " ");
939 fprintf(stderr, "%13.6E", sim[i__ + np * sim_dim1]);
943 for (i__ = iptemp; i__ <= i__1; ++i__) {
944 if (!((i__-1) % 4)) fprintf(stderr, "\ncobyla: ");
945 fprintf(stderr, "%15.6E", x[i__]);
948 fprintf(stderr, "\n");
953 rc = NLOPT_XTOL_REACHED;
955 /* Return the best calculated values of the variables. */
958 fprintf(stderr, "cobyla: normal return.\n");
965 for (i__ = 1; i__ <= i__1; ++i__) {
966 x[i__] = sim[i__ + np * sim_dim1];
968 f = datmat[mp + np * datmat_dim1];
969 resmax = datmat[*mpp + np * datmat_dim1];
973 fprintf(stderr, "cobyla: NFVALS = %4d, F =%13.6E, MAXCV =%13.6E\n",
974 stop->nevals, f, resmax);
976 fprintf(stderr, "cobyla: X =");
977 for (i__ = 1; i__ <= i__1; ++i__) {
978 if (i__>1) fprintf(stderr, " ");
979 fprintf(stderr, "%13.6E", x[i__]);
983 for (i__ = iptemp; i__ <= i__1; ++i__) {
984 if (!((i__-1) % 4)) fprintf(stderr, "\ncobyla: ");
985 fprintf(stderr, "%15.6E", x[i__]);
988 fprintf(stderr, "\n");
993 /* ------------------------------------------------------------------------- */
994 static void trstlp(int *n, int *m, double *a,
995 double *b, double *rho, double *dx, int *ifull,
996 int *iact, double *z__, double *zdota, double *vmultc,
997 double *sdirn, double *dxnew, double *vmultd)
999 /* System generated locals */
1000 int a_dim1, a_offset, z_dim1, z_offset, i__1, i__2;
1003 /* Local variables */
1004 double alpha, tempa;
1006 double optnew, stpful, sum, tot, acca, accb;
1007 double ratio, vsave, zdotv, zdotw, dd;
1009 double sp, ss, resold = 0.0, zdvabs, zdwabs, sumabs, resmax, optold;
1013 int iout, i__, j, k;
1017 int nact, icon = 0, mcon;
1021 /* This subroutine calculates an N-component vector DX by applying the */
1022 /* following two stages. In the first stage, DX is set to the shortest */
1023 /* vector that minimizes the greatest violation of the constraints */
1024 /* A(1,K)*DX(1)+A(2,K)*DX(2)+...+A(N,K)*DX(N) .GE. B(K), K=2,3,...,M, */
1025 /* subject to the Euclidean length of DX being at most RHO. If its length is */
1026 /* strictly less than RHO, then we use the resultant freedom in DX to */
1027 /* minimize the objective function */
1028 /* -A(1,M+1)*DX(1)-A(2,M+1)*DX(2)-...-A(N,M+1)*DX(N) */
1029 /* subject to no increase in any greatest constraint violation. This */
1030 /* notation allows the gradient of the objective function to be regarded as */
1031 /* the gradient of a constraint. Therefore the two stages are distinguished */
1032 /* by MCON .EQ. M and MCON .GT. M respectively. It is possible that a */
1033 /* degeneracy may prevent DX from attaining the target length RHO. Then the */
1034 /* value IFULL=0 would be set, but usually IFULL=1 on return. */
1036 /* In general NACT is the number of constraints in the active set and */
1037 /* IACT(1),...,IACT(NACT) are their indices, while the remainder of IACT */
1038 /* contains a permutation of the remaining constraint indices. Further, Z is */
1039 /* an orthogonal matrix whose first NACT columns can be regarded as the */
1040 /* result of Gram-Schmidt applied to the active constraint gradients. For */
1041 /* J=1,2,...,NACT, the number ZDOTA(J) is the scalar product of the J-th */
1042 /* column of Z with the gradient of the J-th active constraint. DX is the */
1043 /* current vector of variables and here the residuals of the active */
1044 /* constraints should be zero. Further, the active constraints have */
1045 /* nonnegative Lagrange multipliers that are held at the beginning of */
1046 /* VMULTC. The remainder of this vector holds the residuals of the inactive */
1047 /* constraints at DX, the ordering of the components of VMULTC being in */
1048 /* agreement with the permutation of the indices of the constraints that is */
1049 /* in IACT. All these residuals are nonnegative, which is achieved by the */
1050 /* shift RESMAX that makes the least residual zero. */
1052 /* Initialize Z and some other variables. The value of RESMAX will be */
1053 /* appropriate to DX=0, while ICON will be the index of a most violated */
1054 /* constraint if RESMAX is positive. Usually during the first stage the */
1055 /* vector SDIRN gives a search direction that reduces all the active */
1056 /* constraint violations by one simultaneously. */
1058 /* Parameter adjustments */
1060 z_offset = 1 + z_dim1 * 1;
1063 a_offset = 1 + a_dim1 * 1;
1080 for (i__ = 1; i__ <= i__1; ++i__) {
1082 for (j = 1; j <= i__2; ++j) {
1083 z__[i__ + j * z_dim1] = 0.;
1085 z__[i__ + i__ * z_dim1] = 1.;
1090 for (k = 1; k <= i__1; ++k) {
1091 if (b[k] > resmax) {
1097 for (k = 1; k <= i__1; ++k) {
1099 vmultc[k] = resmax - b[k];
1106 for (i__ = 1; i__ <= i__1; ++i__) {
1110 /* End the current stage of the calculation if 3 consecutive iterations */
1111 /* have either failed to reduce the best calculated value of the objective */
1112 /* function or to increase the number of active constraints since the best */
1113 /* value was calculated. This strategy prevents cycling, but there is a */
1114 /* remote possibility that it will cause premature termination. */
1125 for (i__ = 1; i__ <= i__1; ++i__) {
1126 optnew -= dx[i__] * a[i__ + mcon * a_dim1];
1129 if (icount == 0 || optnew < optold) {
1133 } else if (nact > nactx) {
1143 /* If ICON exceeds NACT, then we add the constraint with index IACT(ICON) to */
1144 /* the active set. Apply Givens rotations so that the last N-NACT-1 columns */
1145 /* of Z are orthogonal to the gradient of the new constraint, a scalar */
1146 /* product being set to zero if its nonzero value could be due to computer */
1147 /* rounding errors. The array DXNEW is used for working space. */
1154 for (i__ = 1; i__ <= i__1; ++i__) {
1155 dxnew[i__] = a[i__ + kk * a_dim1];
1164 for (i__ = 1; i__ <= i__1; ++i__) {
1165 temp = z__[i__ + k * z_dim1] * dxnew[i__];
1169 acca = spabs + abs(sp) * .1;
1170 accb = spabs + abs(sp) * .2;
1171 if (spabs >= acca || acca >= accb) {
1178 temp = sqrt(sp * sp + tot * tot);
1183 for (i__ = 1; i__ <= i__1; ++i__) {
1184 temp = alpha * z__[i__ + k * z_dim1] + beta * z__[i__ + kp *
1186 z__[i__ + kp * z_dim1] = alpha * z__[i__ + kp * z_dim1] -
1187 beta * z__[i__ + k * z_dim1];
1188 z__[i__ + k * z_dim1] = temp;
1195 /* Add the new constraint if this can be done without a deletion from the */
1201 vmultc[icon] = vmultc[nact];
1206 /* The next instruction is reached if a deletion has to be made from the */
1207 /* active set in order to make room for the new active constraint, because */
1208 /* the new constraint gradient is a linear combination of the gradients of */
1209 /* the old active constraints. Set the elements of VMULTD to the multipliers */
1210 /* of the linear combination. Further, set IOUT to the index of the */
1211 /* constraint to be deleted, but branch if no suitable index can be found. */
1219 for (i__ = 1; i__ <= i__1; ++i__) {
1220 temp = z__[i__ + k * z_dim1] * dxnew[i__];
1222 zdvabs += abs(temp);
1224 acca = zdvabs + abs(zdotv) * .1;
1225 accb = zdvabs + abs(zdotv) * .2;
1226 if (zdvabs < acca && acca < accb) {
1227 temp = zdotv / zdota[k];
1228 if (temp > 0. && iact[k] <= *m) {
1229 tempa = vmultc[k] / temp;
1230 if (ratio < 0. || tempa < ratio) {
1238 for (i__ = 1; i__ <= i__1; ++i__) {
1239 dxnew[i__] -= temp * a[i__ + kw * a_dim1];
1254 /* Revise the Lagrange multipliers and reorder the active constraints so */
1255 /* that the one to be replaced is at the end of the list. Also calculate the */
1256 /* new value of ZDOTA(NACT) and branch if it is not acceptable. */
1259 for (k = 1; k <= i__1; ++k) {
1260 d__1 = 0., d__2 = vmultc[k] - ratio * vmultd[k];
1261 vmultc[k] = max(d__1,d__2);
1265 vsave = vmultc[icon];
1272 for (i__ = 1; i__ <= i__1; ++i__) {
1273 sp += z__[i__ + k * z_dim1] * a[i__ + kw * a_dim1];
1276 temp = sqrt(sp * sp + d__1 * d__1);
1277 alpha = zdota[kp] / temp;
1279 zdota[kp] = alpha * zdota[k];
1282 for (i__ = 1; i__ <= i__1; ++i__) {
1283 temp = alpha * z__[i__ + kp * z_dim1] + beta * z__[i__ + k *
1285 z__[i__ + kp * z_dim1] = alpha * z__[i__ + k * z_dim1] - beta *
1286 z__[i__ + kp * z_dim1];
1287 z__[i__ + k * z_dim1] = temp;
1290 vmultc[k] = vmultc[kp];
1300 for (i__ = 1; i__ <= i__1; ++i__) {
1301 temp += z__[i__ + nact * z_dim1] * a[i__ + kk * a_dim1];
1308 vmultc[nact] = ratio;
1310 /* Update IACT and ensure that the objective function continues to be */
1311 /* treated as the last active constraint when MCON>M. */
1314 iact[icon] = iact[nact];
1316 if (mcon > *m && kk != mcon) {
1320 for (i__ = 1; i__ <= i__1; ++i__) {
1321 sp += z__[i__ + k * z_dim1] * a[i__ + kk * a_dim1];
1324 temp = sqrt(sp * sp + d__1 * d__1);
1325 alpha = zdota[nact] / temp;
1327 zdota[nact] = alpha * zdota[k];
1330 for (i__ = 1; i__ <= i__1; ++i__) {
1331 temp = alpha * z__[i__ + nact * z_dim1] + beta * z__[i__ + k *
1333 z__[i__ + nact * z_dim1] = alpha * z__[i__ + k * z_dim1] - beta *
1334 z__[i__ + nact * z_dim1];
1335 z__[i__ + k * z_dim1] = temp;
1337 iact[nact] = iact[k];
1340 vmultc[k] = vmultc[nact];
1341 vmultc[nact] = temp;
1344 /* If stage one is in progress, then set SDIRN to the direction of the next */
1345 /* change to the current vector of variables. */
1353 for (i__ = 1; i__ <= i__1; ++i__) {
1354 temp += sdirn[i__] * a[i__ + kk * a_dim1];
1357 temp /= zdota[nact];
1359 for (i__ = 1; i__ <= i__1; ++i__) {
1360 sdirn[i__] -= temp * z__[i__ + nact * z_dim1];
1364 /* Delete the constraint that has the index IACT(ICON) from the active set. */
1369 vsave = vmultc[icon];
1376 for (i__ = 1; i__ <= i__1; ++i__) {
1377 sp += z__[i__ + k * z_dim1] * a[i__ + kk * a_dim1];
1380 temp = sqrt(sp * sp + d__1 * d__1);
1381 alpha = zdota[kp] / temp;
1383 zdota[kp] = alpha * zdota[k];
1386 for (i__ = 1; i__ <= i__1; ++i__) {
1387 temp = alpha * z__[i__ + kp * z_dim1] + beta * z__[i__ + k *
1389 z__[i__ + kp * z_dim1] = alpha * z__[i__ + k * z_dim1] - beta *
1390 z__[i__ + kp * z_dim1];
1391 z__[i__ + k * z_dim1] = temp;
1394 vmultc[k] = vmultc[kp];
1404 /* If stage one is in progress, then set SDIRN to the direction of the next */
1405 /* change to the current vector of variables. */
1412 for (i__ = 1; i__ <= i__1; ++i__) {
1413 temp += sdirn[i__] * z__[i__ + (nact + 1) * z_dim1];
1416 for (i__ = 1; i__ <= i__1; ++i__) {
1417 sdirn[i__] -= temp * z__[i__ + (nact + 1) * z_dim1];
1421 /* Pick the next search direction of stage two. */
1424 temp = 1. / zdota[nact];
1426 for (i__ = 1; i__ <= i__1; ++i__) {
1427 sdirn[i__] = temp * z__[i__ + nact * z_dim1];
1430 /* Calculate the step to the boundary of the trust region or take the step */
1431 /* that reduces RESMAX to zero. The two statements below that include the */
1432 /* factor 1.0E-6 prevent some harmless underflows that occurred in a test */
1433 /* calculation. Further, we skip the step if it could be zero within a */
1434 /* reasonable tolerance for computer rounding errors. */
1441 for (i__ = 1; i__ <= i__1; ++i__) {
1442 if ((d__1 = dx[i__], abs(d__1)) >= *rho * 1e-6f) {
1446 sd += dx[i__] * sdirn[i__];
1453 temp = sqrt(ss * dd);
1454 if (abs(sd) >= temp * 1e-6f) {
1455 temp = sqrt(ss * dd + sd * sd);
1457 stpful = dd / (temp + sd);
1460 acca = step + resmax * .1;
1461 accb = step + resmax * .2;
1462 if (step >= acca || acca >= accb) {
1465 step = min(step,resmax);
1468 /* Set DXNEW to the new variables if STEP is the steplength, and reduce */
1469 /* RESMAX to the corresponding maximum residual if stage one is being done. */
1470 /* Because DXNEW will be changed during the calculation of some Lagrange */
1471 /* multipliers, it will be restored to the following value later. */
1474 for (i__ = 1; i__ <= i__1; ++i__) {
1475 dxnew[i__] = dx[i__] + step * sdirn[i__];
1481 for (k = 1; k <= i__1; ++k) {
1485 for (i__ = 1; i__ <= i__2; ++i__) {
1486 temp -= a[i__ + kk * a_dim1] * dxnew[i__];
1488 resmax = max(resmax,temp);
1492 /* Set VMULTD to the VMULTC vector that would occur if DX became DXNEW. A */
1493 /* device is included to force VMULTD(K)=0.0 if deviations from this value */
1494 /* can be attributed to computer rounding errors. First calculate the new */
1495 /* Lagrange multipliers. */
1502 for (i__ = 1; i__ <= i__1; ++i__) {
1503 temp = z__[i__ + k * z_dim1] * dxnew[i__];
1505 zdwabs += abs(temp);
1507 acca = zdwabs + abs(zdotw) * .1;
1508 accb = zdwabs + abs(zdotw) * .2;
1509 if (zdwabs >= acca || acca >= accb) {
1512 vmultd[k] = zdotw / zdota[k];
1516 for (i__ = 1; i__ <= i__1; ++i__) {
1517 dxnew[i__] -= vmultd[k] * a[i__ + kk * a_dim1];
1523 d__1 = 0., d__2 = vmultd[nact];
1524 vmultd[nact] = max(d__1,d__2);
1527 /* Complete VMULTC by finding the new constraint residuals. */
1530 for (i__ = 1; i__ <= i__1; ++i__) {
1531 dxnew[i__] = dx[i__] + step * sdirn[i__];
1536 for (k = kl; k <= i__1; ++k) {
1538 sum = resmax - b[kk];
1539 sumabs = resmax + (d__1 = b[kk], abs(d__1));
1541 for (i__ = 1; i__ <= i__2; ++i__) {
1542 temp = a[i__ + kk * a_dim1] * dxnew[i__];
1544 sumabs += abs(temp);
1546 acca = sumabs + abs(sum) * .1f;
1547 accb = sumabs + abs(sum) * .2f;
1548 if (sumabs >= acca || acca >= accb) {
1555 /* Calculate the fraction of the step from DX to DXNEW that will be taken. */
1560 for (k = 1; k <= i__1; ++k) {
1561 if (vmultd[k] < 0.) {
1562 temp = vmultc[k] / (vmultc[k] - vmultd[k]);
1570 /* Update DX, VMULTC and RESMAX. */
1574 for (i__ = 1; i__ <= i__1; ++i__) {
1575 dx[i__] = temp * dx[i__] + ratio * dxnew[i__];
1578 for (k = 1; k <= i__1; ++k) {
1579 d__1 = 0., d__2 = temp * vmultc[k] + ratio * vmultd[k];
1580 vmultc[k] = max(d__1,d__2);
1583 resmax = resold + ratio * (resmax - resold);
1586 /* If the full step is not acceptable then begin another iteration. */
1587 /* Otherwise switch to stage two or end the calculation. */
1592 if (step == stpful) {
1602 /* We employ any freedom that may be available to reduce the objective */
1603 /* function before returning a DX whose length is less than RHO. */