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) {
950 /* SGJ, 2008: convergence tests for function vals; note that current
951 best val is stored in datmat[mp + np * datmat_dim1], or in f if
952 ifull == 1, and previous best is in *minf. This seems like a
953 sensible place to put the convergence test, as it is the same
954 place that Powell checks the x tolerance (rho > rhoend). */
956 double fbest = ifull == 1 ? f : datmat[mp + np * datmat_dim1];
957 if (fbest < *minf && nlopt_stop_ftol(stop, fbest, *minf)) {
958 rc = NLOPT_FTOL_REACHED;
964 /* Otherwise reduce RHO if it is not at its least value and reset PARMU. */
968 if (rho <= rhoend * 1.5) {
974 for (k = 1; k <= i__1; ++k) {
975 cmin = datmat[k + np * datmat_dim1];
978 for (i__ = 1; i__ <= i__2; ++i__) {
979 d__1 = cmin, d__2 = datmat[k + i__ * datmat_dim1];
980 cmin = min(d__1,d__2);
981 d__1 = cmax, d__2 = datmat[k + i__ * datmat_dim1];
982 cmax = max(d__1,d__2);
984 if (k <= *m && cmin < cmax * .5) {
985 temp = max(cmax,0.) - cmin;
989 denom = min(denom,temp);
995 } else if (cmax - cmin < parmu * denom) {
996 parmu = (cmax - cmin) / denom;
1000 fprintf(stderr, "cobyla: reduction in RHO to %12.6E and PARMU =%13.6E\n",
1004 fprintf(stderr, "cobyla: NFVALS = %4d, F =%13.6E, MAXCV =%13.6E\n",
1005 stop->nevals, datmat[mp + np * datmat_dim1], datmat[*mpp + np * datmat_dim1]);
1007 fprintf(stderr, "cobyla: X =");
1009 for (i__ = 1; i__ <= i__1; ++i__) {
1010 if (i__>1) fprintf(stderr, " ");
1011 fprintf(stderr, "%13.6E", sim[i__ + np * sim_dim1]);
1015 for (i__ = iptemp; i__ <= i__1; ++i__) {
1016 if (!((i__-1) % 4)) fprintf(stderr, "\ncobyla: ");
1017 fprintf(stderr, "%15.6E", x[i__]);
1020 fprintf(stderr, "\n");
1025 rc = NLOPT_XTOL_REACHED;
1027 /* Return the best calculated values of the variables. */
1030 fprintf(stderr, "cobyla: normal return.\n");
1037 for (i__ = 1; i__ <= i__1; ++i__) {
1038 x[i__] = sim[i__ + np * sim_dim1];
1040 f = datmat[mp + np * datmat_dim1];
1041 resmax = datmat[*mpp + np * datmat_dim1];
1045 fprintf(stderr, "cobyla: NFVALS = %4d, F =%13.6E, MAXCV =%13.6E\n",
1046 stop->nevals, f, resmax);
1048 fprintf(stderr, "cobyla: X =");
1049 for (i__ = 1; i__ <= i__1; ++i__) {
1050 if (i__>1) fprintf(stderr, " ");
1051 fprintf(stderr, "%13.6E", x[i__]);
1055 for (i__ = iptemp; i__ <= i__1; ++i__) {
1056 if (!((i__-1) % 4)) fprintf(stderr, "\ncobyla: ");
1057 fprintf(stderr, "%15.6E", x[i__]);
1060 fprintf(stderr, "\n");
1065 /* ------------------------------------------------------------------------- */
1066 static void trstlp(int *n, int *m, double *a,
1067 double *b, double *rho, double *dx, int *ifull,
1068 int *iact, double *z__, double *zdota, double *vmultc,
1069 double *sdirn, double *dxnew, double *vmultd)
1071 /* System generated locals */
1072 int a_dim1, a_offset, z_dim1, z_offset, i__1, i__2;
1075 /* Local variables */
1076 double alpha, tempa;
1078 double optnew, stpful, sum, tot, acca, accb;
1079 double ratio, vsave, zdotv, zdotw, dd;
1081 double sp, ss, resold = 0.0, zdvabs, zdwabs, sumabs, resmax, optold;
1085 int iout, i__, j, k;
1089 int nact, icon = 0, mcon;
1093 /* This subroutine calculates an N-component vector DX by applying the */
1094 /* following two stages. In the first stage, DX is set to the shortest */
1095 /* vector that minimizes the greatest violation of the constraints */
1096 /* A(1,K)*DX(1)+A(2,K)*DX(2)+...+A(N,K)*DX(N) .GE. B(K), K=2,3,...,M, */
1097 /* subject to the Euclidean length of DX being at most RHO. If its length is */
1098 /* strictly less than RHO, then we use the resultant freedom in DX to */
1099 /* minimize the objective function */
1100 /* -A(1,M+1)*DX(1)-A(2,M+1)*DX(2)-...-A(N,M+1)*DX(N) */
1101 /* subject to no increase in any greatest constraint violation. This */
1102 /* notation allows the gradient of the objective function to be regarded as */
1103 /* the gradient of a constraint. Therefore the two stages are distinguished */
1104 /* by MCON .EQ. M and MCON .GT. M respectively. It is possible that a */
1105 /* degeneracy may prevent DX from attaining the target length RHO. Then the */
1106 /* value IFULL=0 would be set, but usually IFULL=1 on return. */
1108 /* In general NACT is the number of constraints in the active set and */
1109 /* IACT(1),...,IACT(NACT) are their indices, while the remainder of IACT */
1110 /* contains a permutation of the remaining constraint indices. Further, Z is */
1111 /* an orthogonal matrix whose first NACT columns can be regarded as the */
1112 /* result of Gram-Schmidt applied to the active constraint gradients. For */
1113 /* J=1,2,...,NACT, the number ZDOTA(J) is the scalar product of the J-th */
1114 /* column of Z with the gradient of the J-th active constraint. DX is the */
1115 /* current vector of variables and here the residuals of the active */
1116 /* constraints should be zero. Further, the active constraints have */
1117 /* nonnegative Lagrange multipliers that are held at the beginning of */
1118 /* VMULTC. The remainder of this vector holds the residuals of the inactive */
1119 /* constraints at DX, the ordering of the components of VMULTC being in */
1120 /* agreement with the permutation of the indices of the constraints that is */
1121 /* in IACT. All these residuals are nonnegative, which is achieved by the */
1122 /* shift RESMAX that makes the least residual zero. */
1124 /* Initialize Z and some other variables. The value of RESMAX will be */
1125 /* appropriate to DX=0, while ICON will be the index of a most violated */
1126 /* constraint if RESMAX is positive. Usually during the first stage the */
1127 /* vector SDIRN gives a search direction that reduces all the active */
1128 /* constraint violations by one simultaneously. */
1130 /* Parameter adjustments */
1132 z_offset = 1 + z_dim1 * 1;
1135 a_offset = 1 + a_dim1 * 1;
1152 for (i__ = 1; i__ <= i__1; ++i__) {
1154 for (j = 1; j <= i__2; ++j) {
1155 z__[i__ + j * z_dim1] = 0.;
1157 z__[i__ + i__ * z_dim1] = 1.;
1162 for (k = 1; k <= i__1; ++k) {
1163 if (b[k] > resmax) {
1169 for (k = 1; k <= i__1; ++k) {
1171 vmultc[k] = resmax - b[k];
1178 for (i__ = 1; i__ <= i__1; ++i__) {
1182 /* End the current stage of the calculation if 3 consecutive iterations */
1183 /* have either failed to reduce the best calculated value of the objective */
1184 /* function or to increase the number of active constraints since the best */
1185 /* value was calculated. This strategy prevents cycling, but there is a */
1186 /* remote possibility that it will cause premature termination. */
1197 for (i__ = 1; i__ <= i__1; ++i__) {
1198 optnew -= dx[i__] * a[i__ + mcon * a_dim1];
1201 if (icount == 0 || optnew < optold) {
1205 } else if (nact > nactx) {
1215 /* If ICON exceeds NACT, then we add the constraint with index IACT(ICON) to */
1216 /* the active set. Apply Givens rotations so that the last N-NACT-1 columns */
1217 /* of Z are orthogonal to the gradient of the new constraint, a scalar */
1218 /* product being set to zero if its nonzero value could be due to computer */
1219 /* rounding errors. The array DXNEW is used for working space. */
1226 for (i__ = 1; i__ <= i__1; ++i__) {
1227 dxnew[i__] = a[i__ + kk * a_dim1];
1236 for (i__ = 1; i__ <= i__1; ++i__) {
1237 temp = z__[i__ + k * z_dim1] * dxnew[i__];
1241 acca = spabs + abs(sp) * .1;
1242 accb = spabs + abs(sp) * .2;
1243 if (spabs >= acca || acca >= accb) {
1250 temp = sqrt(sp * sp + tot * tot);
1255 for (i__ = 1; i__ <= i__1; ++i__) {
1256 temp = alpha * z__[i__ + k * z_dim1] + beta * z__[i__ + kp *
1258 z__[i__ + kp * z_dim1] = alpha * z__[i__ + kp * z_dim1] -
1259 beta * z__[i__ + k * z_dim1];
1260 z__[i__ + k * z_dim1] = temp;
1267 /* Add the new constraint if this can be done without a deletion from the */
1273 vmultc[icon] = vmultc[nact];
1278 /* The next instruction is reached if a deletion has to be made from the */
1279 /* active set in order to make room for the new active constraint, because */
1280 /* the new constraint gradient is a linear combination of the gradients of */
1281 /* the old active constraints. Set the elements of VMULTD to the multipliers */
1282 /* of the linear combination. Further, set IOUT to the index of the */
1283 /* constraint to be deleted, but branch if no suitable index can be found. */
1291 for (i__ = 1; i__ <= i__1; ++i__) {
1292 temp = z__[i__ + k * z_dim1] * dxnew[i__];
1294 zdvabs += abs(temp);
1296 acca = zdvabs + abs(zdotv) * .1;
1297 accb = zdvabs + abs(zdotv) * .2;
1298 if (zdvabs < acca && acca < accb) {
1299 temp = zdotv / zdota[k];
1300 if (temp > 0. && iact[k] <= *m) {
1301 tempa = vmultc[k] / temp;
1302 if (ratio < 0. || tempa < ratio) {
1310 for (i__ = 1; i__ <= i__1; ++i__) {
1311 dxnew[i__] -= temp * a[i__ + kw * a_dim1];
1326 /* Revise the Lagrange multipliers and reorder the active constraints so */
1327 /* that the one to be replaced is at the end of the list. Also calculate the */
1328 /* new value of ZDOTA(NACT) and branch if it is not acceptable. */
1331 for (k = 1; k <= i__1; ++k) {
1332 d__1 = 0., d__2 = vmultc[k] - ratio * vmultd[k];
1333 vmultc[k] = max(d__1,d__2);
1337 vsave = vmultc[icon];
1344 for (i__ = 1; i__ <= i__1; ++i__) {
1345 sp += z__[i__ + k * z_dim1] * a[i__ + kw * a_dim1];
1348 temp = sqrt(sp * sp + d__1 * d__1);
1349 alpha = zdota[kp] / temp;
1351 zdota[kp] = alpha * zdota[k];
1354 for (i__ = 1; i__ <= i__1; ++i__) {
1355 temp = alpha * z__[i__ + kp * z_dim1] + beta * z__[i__ + k *
1357 z__[i__ + kp * z_dim1] = alpha * z__[i__ + k * z_dim1] - beta *
1358 z__[i__ + kp * z_dim1];
1359 z__[i__ + k * z_dim1] = temp;
1362 vmultc[k] = vmultc[kp];
1372 for (i__ = 1; i__ <= i__1; ++i__) {
1373 temp += z__[i__ + nact * z_dim1] * a[i__ + kk * a_dim1];
1380 vmultc[nact] = ratio;
1382 /* Update IACT and ensure that the objective function continues to be */
1383 /* treated as the last active constraint when MCON>M. */
1386 iact[icon] = iact[nact];
1388 if (mcon > *m && kk != mcon) {
1392 for (i__ = 1; i__ <= i__1; ++i__) {
1393 sp += z__[i__ + k * z_dim1] * a[i__ + kk * a_dim1];
1396 temp = sqrt(sp * sp + d__1 * d__1);
1397 alpha = zdota[nact] / temp;
1399 zdota[nact] = alpha * zdota[k];
1402 for (i__ = 1; i__ <= i__1; ++i__) {
1403 temp = alpha * z__[i__ + nact * z_dim1] + beta * z__[i__ + k *
1405 z__[i__ + nact * z_dim1] = alpha * z__[i__ + k * z_dim1] - beta *
1406 z__[i__ + nact * z_dim1];
1407 z__[i__ + k * z_dim1] = temp;
1409 iact[nact] = iact[k];
1412 vmultc[k] = vmultc[nact];
1413 vmultc[nact] = temp;
1416 /* If stage one is in progress, then set SDIRN to the direction of the next */
1417 /* change to the current vector of variables. */
1425 for (i__ = 1; i__ <= i__1; ++i__) {
1426 temp += sdirn[i__] * a[i__ + kk * a_dim1];
1429 temp /= zdota[nact];
1431 for (i__ = 1; i__ <= i__1; ++i__) {
1432 sdirn[i__] -= temp * z__[i__ + nact * z_dim1];
1436 /* Delete the constraint that has the index IACT(ICON) from the active set. */
1441 vsave = vmultc[icon];
1448 for (i__ = 1; i__ <= i__1; ++i__) {
1449 sp += z__[i__ + k * z_dim1] * a[i__ + kk * a_dim1];
1452 temp = sqrt(sp * sp + d__1 * d__1);
1453 alpha = zdota[kp] / temp;
1455 zdota[kp] = alpha * zdota[k];
1458 for (i__ = 1; i__ <= i__1; ++i__) {
1459 temp = alpha * z__[i__ + kp * z_dim1] + beta * z__[i__ + k *
1461 z__[i__ + kp * z_dim1] = alpha * z__[i__ + k * z_dim1] - beta *
1462 z__[i__ + kp * z_dim1];
1463 z__[i__ + k * z_dim1] = temp;
1466 vmultc[k] = vmultc[kp];
1476 /* If stage one is in progress, then set SDIRN to the direction of the next */
1477 /* change to the current vector of variables. */
1484 for (i__ = 1; i__ <= i__1; ++i__) {
1485 temp += sdirn[i__] * z__[i__ + (nact + 1) * z_dim1];
1488 for (i__ = 1; i__ <= i__1; ++i__) {
1489 sdirn[i__] -= temp * z__[i__ + (nact + 1) * z_dim1];
1493 /* Pick the next search direction of stage two. */
1496 temp = 1. / zdota[nact];
1498 for (i__ = 1; i__ <= i__1; ++i__) {
1499 sdirn[i__] = temp * z__[i__ + nact * z_dim1];
1502 /* Calculate the step to the boundary of the trust region or take the step */
1503 /* that reduces RESMAX to zero. The two statements below that include the */
1504 /* factor 1.0E-6 prevent some harmless underflows that occurred in a test */
1505 /* calculation. Further, we skip the step if it could be zero within a */
1506 /* reasonable tolerance for computer rounding errors. */
1513 for (i__ = 1; i__ <= i__1; ++i__) {
1514 if ((d__1 = dx[i__], abs(d__1)) >= *rho * 1e-6f) {
1518 sd += dx[i__] * sdirn[i__];
1525 temp = sqrt(ss * dd);
1526 if (abs(sd) >= temp * 1e-6f) {
1527 temp = sqrt(ss * dd + sd * sd);
1529 stpful = dd / (temp + sd);
1532 acca = step + resmax * .1;
1533 accb = step + resmax * .2;
1534 if (step >= acca || acca >= accb) {
1537 step = min(step,resmax);
1540 /* Set DXNEW to the new variables if STEP is the steplength, and reduce */
1541 /* RESMAX to the corresponding maximum residual if stage one is being done. */
1542 /* Because DXNEW will be changed during the calculation of some Lagrange */
1543 /* multipliers, it will be restored to the following value later. */
1546 for (i__ = 1; i__ <= i__1; ++i__) {
1547 dxnew[i__] = dx[i__] + step * sdirn[i__];
1553 for (k = 1; k <= i__1; ++k) {
1557 for (i__ = 1; i__ <= i__2; ++i__) {
1558 temp -= a[i__ + kk * a_dim1] * dxnew[i__];
1560 resmax = max(resmax,temp);
1564 /* Set VMULTD to the VMULTC vector that would occur if DX became DXNEW. A */
1565 /* device is included to force VMULTD(K)=0.0 if deviations from this value */
1566 /* can be attributed to computer rounding errors. First calculate the new */
1567 /* Lagrange multipliers. */
1574 for (i__ = 1; i__ <= i__1; ++i__) {
1575 temp = z__[i__ + k * z_dim1] * dxnew[i__];
1577 zdwabs += abs(temp);
1579 acca = zdwabs + abs(zdotw) * .1;
1580 accb = zdwabs + abs(zdotw) * .2;
1581 if (zdwabs >= acca || acca >= accb) {
1584 vmultd[k] = zdotw / zdota[k];
1588 for (i__ = 1; i__ <= i__1; ++i__) {
1589 dxnew[i__] -= vmultd[k] * a[i__ + kk * a_dim1];
1595 d__1 = 0., d__2 = vmultd[nact];
1596 vmultd[nact] = max(d__1,d__2);
1599 /* Complete VMULTC by finding the new constraint residuals. */
1602 for (i__ = 1; i__ <= i__1; ++i__) {
1603 dxnew[i__] = dx[i__] + step * sdirn[i__];
1608 for (k = kl; k <= i__1; ++k) {
1610 sum = resmax - b[kk];
1611 sumabs = resmax + (d__1 = b[kk], abs(d__1));
1613 for (i__ = 1; i__ <= i__2; ++i__) {
1614 temp = a[i__ + kk * a_dim1] * dxnew[i__];
1616 sumabs += abs(temp);
1618 acca = sumabs + abs(sum) * .1f;
1619 accb = sumabs + abs(sum) * .2f;
1620 if (sumabs >= acca || acca >= accb) {
1627 /* Calculate the fraction of the step from DX to DXNEW that will be taken. */
1632 for (k = 1; k <= i__1; ++k) {
1633 if (vmultd[k] < 0.) {
1634 temp = vmultc[k] / (vmultc[k] - vmultd[k]);
1642 /* Update DX, VMULTC and RESMAX. */
1646 for (i__ = 1; i__ <= i__1; ++i__) {
1647 dx[i__] = temp * dx[i__] + ratio * dxnew[i__];
1650 for (k = 1; k <= i__1; ++k) {
1651 d__1 = 0., d__2 = temp * vmultc[k] + ratio * vmultd[k];
1652 vmultc[k] = max(d__1,d__2);
1655 resmax = resold + ratio * (resmax - resold);
1658 /* If the full step is not acceptable then begin another iteration. */
1659 /* Otherwise switch to stage two or end the calculation. */
1664 if (step == stpful) {
1674 /* We employ any freedom that may be available to reduce the objective */
1675 /* function before returning a DX whose length is less than RHO. */