chiark / gitweb /
recommend building in a subdir
[nlopt.git] / src / algs / cobyla / cobyla.c
1 /* cobyla : contrained optimization by linear approximation */
2
3 /*
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)
6  * 
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:
14  * 
15  * The above copyright notice and this permission notice shall be included
16  * in all copies or substantial portions of the Software.
17  * 
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.
25  */
26
27 /*
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.
30  * 
31  * The original source code can be found at :
32  * http://plato.la.asu.edu/topics/problems/nlores.html
33  */
34
35 static char const rcsid[] =
36   "@(#) $Jeannot: cobyla.c,v 1.11 2004/04/18 09:51:36 js Exp $";
37
38 #include <stdlib.h>
39 #include <stdio.h>
40 #include <math.h>
41
42 #include "cobyla.h"
43
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
54    problem */
55 #define ENFORCE_BOUNDS 1
56
57 #define MIN2(a,b) ((a) <= (b) ? (a) : (b))
58 #define MAX2(a,b) ((a) >= (b) ? (a) : (b))
59
60 #define U(n) ((unsigned) (n))
61
62 /**************************************************************************/
63 /* SGJ, 2008: NLopt-style interface function: */
64
65 typedef struct {
66      nlopt_func f;
67      void *f_data;
68      unsigned m_orig;
69      nlopt_constraint *fc;
70      unsigned p;
71      nlopt_constraint *h;
72      double *xtmp;
73      double *lb, *ub;
74      double *con_tol, *scale;
75      nlopt_stopping *stop;
76 } func_wrap_state;
77
78 static int func_wrap(int ni, int mi, double *x, double *f, double *con,
79                      func_wrap_state *s)
80 {
81      unsigned n = U(ni);
82      unsigned i, j, k;
83      double *xtmp = s->xtmp;
84      const double *lb = s->lb, *ub = s->ub;
85
86      (void) mi; /* unused */
87
88      /* in nlopt, we guarante that the function is never evaluated outside
89         the lb and ub bounds, so we need force this with xtmp ... note
90         that this leads to discontinuity in the first derivative, which
91         slows convergence if we don't enable the ENFORCE_BOUNDS feature
92         above. */
93      for (j = 0; j < n; ++j) {
94           if (x[j] < lb[j]) xtmp[j] = lb[j];
95           else if (x[j] > ub[j]) xtmp[j] = ub[j];
96           else xtmp[j] = x[j];
97      }
98      nlopt_unscale(n, s->scale, xtmp, xtmp);
99
100      *f = s->f(n, xtmp, NULL, s->f_data);
101      if (nlopt_stop_forced(s->stop)) return 1;
102      i = 0;
103      for (j = 0; j < s->m_orig; ++j) {
104           nlopt_eval_constraint(con + i, NULL, s->fc+j, n, xtmp);
105           if (nlopt_stop_forced(s->stop)) return 1;
106           for (k = 0; k < s->fc[j].m; ++k)
107                con[i + k] = -con[i + k];
108           i += s->fc[j].m;
109      }
110      for (j = 0; j < s->p; ++j) {
111           nlopt_eval_constraint(con + i, NULL, s->h+j, n, xtmp);
112           if (nlopt_stop_forced(s->stop)) return 1;
113           for (k = 0; k < s->h[j].m; ++k)
114                con[(i + s->h[j].m) + k] = -con[i + k];
115           i += 2 * s->h[j].m;
116      }
117      for (j = 0; j < n; ++j) {
118           if (!nlopt_isinf(lb[j]))
119                con[i++] = x[j] - lb[j];
120           if (!nlopt_isinf(ub[j]))
121                con[i++] = ub[j] - x[j];
122      }
123      return 0;
124 }
125
126 /*
127  * Verbosity level
128  */
129 typedef enum {
130   COBYLA_MSG_NONE = 0, /* No messages */
131   COBYLA_MSG_EXIT = 1, /* Exit reasons */
132   COBYLA_MSG_ITER = 2, /* Rho and Sigma changes */
133   COBYLA_MSG_INFO = 3 /* Informational messages */
134 } cobyla_message;
135
136 /*
137  * A function as required by cobyla
138  * state is a void pointer provided to the function at each call
139  *
140  * n     : the number of variables
141  * m     : the number of constraints
142  * x     : on input, then vector of variables (should not be modified)
143  * f     : on output, the value of the function
144  * con   : on output, the value of the constraints (vector of size m)
145  * state : on input, the value of the state variable as provided to cobyla
146  *
147  * COBYLA will try to make all the values of the constraints positive.
148  * So if you want to input a constraint j such as x[i] <= MAX, set:
149  *   con[j] = MAX - x[i]
150  * The function must returns 0 if no error occurs or 1 to immediately end the
151  * minimization.
152  *
153  */
154 typedef int cobyla_function(int n, int m, double *x, double *f, double *con,
155   func_wrap_state *state);
156
157 /*
158  * cobyla : minimize a function subject to constraints
159  *
160  * n         : number of variables (>=0)
161  * m         : number of constraints (>=0)
162  * x         : on input, initial estimate ; on output, the solution
163  * minf      : on output, minimum objective function
164  * rhobeg    : a reasonable initial change to the variables
165  * stop      : the NLopt stopping criteria
166  * lb, ub    : lower and upper bounds on x
167  * message   : see the cobyla_message enum
168  * calcfc    : the function to minimize (see cobyla_function)
169  * state     : used by function (see cobyla_function)
170  *
171  * The cobyla function returns the usual nlopt_result codes.
172  *
173  */
174 extern nlopt_result cobyla(int n, int m, double *x, double *minf, double rhobeg, double rhoend, nlopt_stopping *stop, const double *lb, const double *ub,
175   int message, cobyla_function *calcfc, func_wrap_state *state);
176
177 nlopt_result cobyla_minimize(unsigned n, nlopt_func f, void *f_data,
178                              unsigned m, nlopt_constraint *fc,
179                              unsigned p, nlopt_constraint *h,
180                              const double *lb, const double *ub, /* bounds */
181                              double *x, /* in: initial guess, out: minimizer */
182                              double *minf,
183                              nlopt_stopping *stop,
184                              const double *dx)
185 {
186      unsigned i, j;
187      func_wrap_state s;
188      nlopt_result ret;
189      double rhobeg, rhoend;
190
191      s.f = f; s.f_data = f_data;
192      s.m_orig = m;
193      s.fc = fc; 
194      s.p = p;
195      s.h = h;
196      s.stop = stop;
197      s.lb = s.ub = s.xtmp = s.con_tol = s.scale = NULL;
198
199      s.scale = nlopt_compute_rescaling(n, dx);
200      if (!s.scale) { ret = NLOPT_OUT_OF_MEMORY; goto done; }
201      for (j = 0; j < n; ++j)
202          if (s.scale[j] == 0 || !nlopt_isfinite(s.scale[j])) {
203              nlopt_stop_msg(stop, "invalid scaling %g of dimension %d: possible over/underflow?", s.scale[j], j);
204              ret = NLOPT_INVALID_ARGS; goto done;
205          }
206
207      s.lb = nlopt_new_rescaled(n, s.scale, lb);
208      if (!s.lb) { ret = NLOPT_OUT_OF_MEMORY; goto done; }
209      s.ub = nlopt_new_rescaled(n, s.scale, ub);
210      if (!s.ub) { ret = NLOPT_OUT_OF_MEMORY; goto done; }
211      nlopt_reorder_bounds(n, s.lb, s.ub);
212
213      s.xtmp = (double *) malloc(sizeof(double) * n);
214      if (!s.xtmp) { ret = NLOPT_OUT_OF_MEMORY; goto done; }
215
216      /* SGJ, 2008: compute rhoend from NLopt stop info */
217      rhobeg = fabs(dx[0] / s.scale[0]);
218      rhoend = stop->xtol_rel * (rhobeg);
219      for (j = 0; j < n; ++j)
220           if (rhoend < stop->xtol_abs[j] / fabs(s.scale[j]))
221                rhoend = stop->xtol_abs[j] / fabs(s.scale[j]);
222
223      /* each equality constraint gives two inequality constraints */
224      m = nlopt_count_constraints(m, fc) + 2 * nlopt_count_constraints(p, h);
225
226      /* add constraints for lower/upper bounds (if any) */
227      for (j = 0; j < n; ++j) {
228           if (!nlopt_isinf(lb[j]))
229                ++m;
230           if (!nlopt_isinf(ub[j]))
231                ++m;
232      }
233
234      s.con_tol = (double *) malloc(sizeof(double) * m);
235      if (m && !s.con_tol) { ret = NLOPT_OUT_OF_MEMORY; goto done; }
236
237      for (j = 0; j < m; ++j) s.con_tol[j] = 0;
238      for (j = i = 0; i < s.m_orig; ++i) {
239           unsigned ji = j, jnext = j + fc[i].m;
240           for (; j < jnext; ++j) s.con_tol[j] = fc[i].tol[j - ji];
241      }
242      for (i = 0; i < s.p; ++i) {
243           unsigned ji = j, jnext = j + h[i].m;
244           for (; j < jnext; ++j) s.con_tol[j] = h[i].tol[j - ji];
245           ji = j; jnext = j + h[i].m;
246           for (; j < jnext; ++j) s.con_tol[j] = h[i].tol[j - ji];
247      }
248
249      nlopt_rescale(n, s.scale, x, x);
250      ret = cobyla((int) n, (int) m, x, minf, rhobeg, rhoend,
251                   stop, s.lb, s.ub, COBYLA_MSG_NONE, 
252                   func_wrap, &s);
253      nlopt_unscale(n, s.scale, x, x);
254
255      /* make sure e.g. rounding errors didn't push us slightly out of bounds */
256      for (j = 0; j < n; ++j) {
257           if (x[j] < lb[j]) x[j] = lb[j];
258           if (x[j] > ub[j]) x[j] = ub[j];
259      }
260
261 done:
262      free(s.con_tol);
263      free(s.xtmp);
264      free(s.ub);
265      free(s.lb);
266      free(s.scale);
267      return ret;
268 }
269
270 /**************************************************************************/
271
272 /* SGJ, 2010: I find that it seems to increase robustness of the algorithm
273    if, on "simplex" steps (which are intended to improve the linear
274    independence of the simplex, not improve the objective), we multiply
275    the steps by pseudorandom numbers in [0,1).  Intuitively, pseudorandom
276    steps are likely to avoid any accidental dependency in the simplex
277    vertices.  However, since I still want COBYLA to be a deterministic
278    algorithm, and I don't care all that much about the quality of the
279    randomness here, I implement a simple linear congruential generator
280    rather than use nlopt_urand, and set the initial seed deterministically. */
281
282 #if defined(HAVE_STDINT_H)
283 #  include <stdint.h>
284 #endif
285
286 #ifndef HAVE_UINT32_T
287 #  if SIZEOF_UNSIGNED_LONG == 4
288 typedef unsigned long uint32_t;
289 #  elif SIZEOF_UNSIGNED_INT == 4
290 typedef unsigned int uint32_t;
291 #  else
292 #    error No 32-bit unsigned integer type
293 #  endif
294 #endif
295
296 /* a simple linear congruential generator */
297
298 static uint32_t lcg_rand(uint32_t *seed)
299 {
300      return (*seed = *seed * 1103515245 + 12345);
301 }
302
303 static double lcg_urand(uint32_t *seed, double a, double b)
304 {
305      return a + lcg_rand(seed) * (b - a) / ((uint32_t) -1);
306 }
307
308 /**************************************************************************/
309
310 static nlopt_result cobylb(int *n, int *m, int *mpp, double *x, double *minf, double *rhobeg, double rhoend,
311   nlopt_stopping *stop, const double *lb, const double *ub, int *iprint, double *con, double *sim,
312   double *simi, double *datmat, double *a, double *vsig, double *veta,
313   double *sigbar, double *dx, double *w, int *iact, cobyla_function *calcfc,
314   func_wrap_state *state);
315 static nlopt_result trstlp(int *n, int *m, double *a, double *b, double *rho,
316   double *dx, int *ifull, int *iact, double *z__, double *zdota, double *vmultc,
317   double *sdirn, double *dxnew, double *vmultd);
318
319 /* ------------------------------------------------------------------------ */
320
321 nlopt_result cobyla(int n, int m, double *x, double *minf, double rhobeg, double rhoend, nlopt_stopping *stop, const double *lb, const double *ub, int iprint,
322   cobyla_function *calcfc, func_wrap_state *state)
323 {
324   int icon, isim, isigb, idatm, iveta, isimi, ivsig, iwork, ia, idx, mpp;
325   int *iact;
326   double *w;
327   nlopt_result rc;
328
329 /*
330  * This subroutine minimizes an objective function F(X) subject to M
331  * inequality constraints on X, where X is a vector of variables that has 
332  * N components. The algorithm employs linear approximations to the 
333  * objective and constraint functions, the approximations being formed by 
334  * linear interpolation at N+1 points in the space of the variables. 
335  * We regard these interpolation points as vertices of a simplex. The 
336  * parameter RHO controls the size of the simplex and it is reduced 
337  * automatically from RHOBEG to RHOEND. For each RHO the subroutine tries 
338  * to achieve a good vector of variables for the current size, and then 
339  * RHO is reduced until the value RHOEND is reached. Therefore RHOBEG and 
340  * RHOEND should be set to reasonable initial changes to and the required 
341  * accuracy in the variables respectively, but this accuracy should be 
342  * viewed as a subject for experimentation because it is not guaranteed. 
343  * The subroutine has an advantage over many of its competitors, however, 
344  * which is that it treats each constraint individually when calculating 
345  * a change to the variables, instead of lumping the constraints together 
346  * into a single penalty function. The name of the subroutine is derived 
347  * from the phrase Constrained Optimization BY Linear Approximations. 
348  *
349  * The user must set the values of N, M, RHOBEG and RHOEND, and must 
350  * provide an initial vector of variables in X. Further, the value of 
351  * IPRINT should be set to 0, 1, 2 or 3, which controls the amount of 
352  * printing during the calculation. Specifically, there is no output if 
353  * IPRINT=0 and there is output only at the end of the calculation if 
354  * IPRINT=1. Otherwise each new value of RHO and SIGMA is printed. 
355  * Further, the vector of variables and some function information are 
356  * given either when RHO is reduced or when each new value of F(X) is 
357  * computed in the cases IPRINT=2 or IPRINT=3 respectively. Here SIGMA 
358  * is a penalty parameter, it being assumed that a change to X is an 
359  * improvement if it reduces the merit function 
360  *      F(X)+SIGMA*MAX(0.0,-C1(X),-C2(X),...,-CM(X)), 
361  * where C1,C2,...,CM denote the constraint functions that should become 
362  * nonnegative eventually, at least to the precision of RHOEND. In the 
363  * printed output the displayed term that is multiplied by SIGMA is 
364  * called MAXCV, which stands for 'MAXimum Constraint Violation'. The 
365  * argument MAXFUN is an int variable that must be set by the user to a 
366  * limit on the number of calls of CALCFC, the purpose of this routine being 
367  * given below. The value of MAXFUN will be altered to the number of calls 
368  * of CALCFC that are made. The arguments W and IACT provide real and 
369  * int arrays that are used as working space. Their lengths must be at 
370  * least N*(3*N+2*M+11)+4*M+6 and M+1 respectively. 
371  *
372  * In order to define the objective and constraint functions, we require 
373  * a subroutine that has the name and arguments 
374  *      SUBROUTINE CALCFC (N,M,X,F,CON) 
375  *      DIMENSION X(*),CON(*)  . 
376  * The values of N and M are fixed and have been defined already, while 
377  * X is now the current vector of variables. The subroutine should return 
378  * the objective and constraint functions at X in F and CON(1),CON(2), 
379  * ...,CON(M). Note that we are trying to adjust X so that F(X) is as 
380  * small as possible subject to the constraint functions being nonnegative. 
381  *
382  * Partition the working space array W to provide the storage that is needed 
383  * for the main calculation.
384  */
385
386   *(stop->nevals_p) = 0;
387
388   if (n == 0)
389   {
390     if (iprint>=1) fprintf(stderr, "cobyla: N==0.\n");
391     return NLOPT_SUCCESS;
392   }
393
394   if (n < 0 || m < 0)
395   {
396     if (iprint>=1) fprintf(stderr, "cobyla: N<0 or M<0.\n");
397     return NLOPT_INVALID_ARGS;
398   }
399
400   /* workspace allocation */
401   w = (double*) malloc(U(n*(3*n+2*m+11)+4*m+6)*sizeof(*w));
402   if (w == NULL)
403   {
404     if (iprint>=1) fprintf(stderr, "cobyla: memory allocation error.\n");
405     return NLOPT_OUT_OF_MEMORY;
406   }
407   iact = (int*)malloc(U(m+1)*sizeof(*iact));
408   if (iact == NULL)
409   {
410     if (iprint>=1) fprintf(stderr, "cobyla: memory allocation error.\n");
411     free(w);
412     return NLOPT_OUT_OF_MEMORY;
413   }
414   
415   /* Parameter adjustments */
416   --iact;
417   --w;
418   --x;
419   --lb; --ub;
420
421   /* Function Body */
422   mpp = m + 2;
423   icon = 1;
424   isim = icon + mpp;
425   isimi = isim + n * n + n;
426   idatm = isimi + n * n;
427   ia = idatm + n * mpp + mpp;
428   ivsig = ia + m * n + n;
429   iveta = ivsig + n;
430   isigb = iveta + n;
431   idx = isigb + n;
432   iwork = idx + n;
433   rc = cobylb(&n, &m, &mpp, &x[1], minf, &rhobeg, rhoend, stop, &lb[1], &ub[1], &iprint,
434       &w[icon], &w[isim], &w[isimi], &w[idatm], &w[ia], &w[ivsig], &w[iveta],
435       &w[isigb], &w[idx], &w[iwork], &iact[1], calcfc, state);
436
437   /* Parameter adjustments (reverse) */
438   ++iact;
439   ++w;
440
441   free(w);
442   free(iact);
443   
444   return rc;
445 } /* cobyla */
446
447 /* ------------------------------------------------------------------------- */
448 static nlopt_result cobylb(int *n, int *m, int *mpp,
449    double *x, double *minf, double *rhobeg, double rhoend,
450     nlopt_stopping *stop, const double *lb, const double *ub,
451     int *iprint, double *con, double *sim, double *simi, 
452     double *datmat, double *a, double *vsig, double *veta,
453      double *sigbar, double *dx, double *w, int *iact, cobyla_function *calcfc,
454      func_wrap_state *state)
455 {
456   /* System generated locals */
457   int sim_dim1, sim_offset, simi_dim1, simi_offset, datmat_dim1, 
458       datmat_offset, a_dim1, a_offset, i__1, i__2, i__3;
459   double d__1, d__2;
460
461   /* Local variables */
462   double alpha, delta, denom, tempa, barmu;
463   double beta, cmin = 0.0, cmax = 0.0;
464   double cvmaxm, dxsign, prerem = 0.0;
465   double edgmax, pareta, prerec = 0.0, phimin, parsig = 0.0;
466   double gamma_;
467   double phi, rho, sum = 0.0;
468   double ratio, vmold, parmu, error, vmnew;
469   double resmax, cvmaxp;
470   double resnew, trured;
471   double temp, wsig, f;
472   double weta;
473   int i__, j, k, l;
474   int idxnew;
475   int iflag = 0;
476   int iptemp;
477   int isdirn, izdota;
478   int ivmc;
479   int ivmd;
480   int mp, np, iz, ibrnch;
481   int nbest, ifull = 0, iptem, jdrop;
482   nlopt_result rc = NLOPT_SUCCESS;
483   uint32_t seed = (uint32_t) (*n + *m); /* arbitrary deterministic LCG seed */
484   int feasible;
485
486   /* SGJ, 2008: added code to keep track of minimum feasible function val */
487   *minf = HUGE_VAL;
488
489 /* Set the initial values of some parameters. The last column of SIM holds */
490 /* the optimal vertex of the current simplex, and the preceding N columns */
491 /* hold the displacements from the optimal vertex to the other vertices. */
492 /* Further, SIMI holds the inverse of the matrix that is contained in the */
493 /* first N columns of SIM. */
494
495   /* Parameter adjustments */
496   a_dim1 = *n;
497   a_offset = 1 + a_dim1 * 1;
498   a -= a_offset;
499   simi_dim1 = *n;
500   simi_offset = 1 + simi_dim1 * 1;
501   simi -= simi_offset;
502   sim_dim1 = *n;
503   sim_offset = 1 + sim_dim1 * 1;
504   sim -= sim_offset;
505   datmat_dim1 = *mpp;
506   datmat_offset = 1 + datmat_dim1 * 1;
507   datmat -= datmat_offset;
508   --x;
509   --con;
510   --vsig;
511   --veta;
512   --sigbar;
513   --dx;
514   --w;
515   --iact;
516   --lb; --ub;
517
518   /* Function Body */
519   iptem = MIN2(*n,4);
520   iptemp = iptem + 1;
521   np = *n + 1;
522   mp = *m + 1;
523   alpha = .25;
524   beta = 2.1;
525   gamma_ = .5;
526   delta = 1.1;
527   rho = *rhobeg;
528   parmu = 0.;
529   if (*iprint >= 2) {
530     fprintf(stderr,
531       "cobyla: the initial value of RHO is %12.6E and PARMU is set to zero.\n",
532       rho);
533   }
534   temp = 1. / rho;
535   i__1 = *n;
536   for (i__ = 1; i__ <= i__1; ++i__) {
537     double rhocur;
538     sim[i__ + np * sim_dim1] = x[i__];
539     i__2 = *n;
540     for (j = 1; j <= i__2; ++j) {
541       sim[i__ + j * sim_dim1] = 0.;
542       simi[i__ + j * simi_dim1] = 0.;
543     }
544     rhocur = rho;
545 #if ENFORCE_BOUNDS
546     /* SGJ: make sure step rhocur stays inside [lb,ub] */
547     if (x[i__] + rhocur > ub[i__]) {
548          if (x[i__] - rhocur >= lb[i__])
549               rhocur = -rhocur;
550          else if (ub[i__] - x[i__] > x[i__] - lb[i__])
551               rhocur = 0.5 * (ub[i__] - x[i__]);
552          else
553               rhocur = 0.5 * (x[i__] - lb[i__]);
554     }
555 #endif
556     sim[i__ + i__ * sim_dim1] = rhocur;
557     simi[i__ + i__ * simi_dim1] = 1.0 / rhocur;
558   }
559   jdrop = np;
560   ibrnch = 0;
561
562 /* Make the next call of the user-supplied subroutine CALCFC. These */
563 /* instructions are also used for calling CALCFC during the iterations of */
564 /* the algorithm. */
565
566   /* SGJ comment: why the hell couldn't he just use a damn subroutine?
567      #*&!%*@ Fortran-66 spaghetti code */
568
569 L40:
570   if (nlopt_stop_forced(stop)) rc = NLOPT_FORCED_STOP;
571   else if (*(stop->nevals_p) > 0) {
572        if (nlopt_stop_evals(stop)) rc = NLOPT_MAXEVAL_REACHED;
573        else if (nlopt_stop_time(stop)) rc = NLOPT_MAXTIME_REACHED;
574   }
575   if (rc != NLOPT_SUCCESS) goto L600;
576
577   ++ *(stop->nevals_p);
578   if (calcfc(*n, *m, &x[1], &f, &con[1], state))
579   {
580     if (*iprint >= 1) {
581       fprintf(stderr, "cobyla: user requested end of minimization.\n");
582     }
583     rc = NLOPT_FORCED_STOP;
584     goto L600;
585   }
586
587   resmax = 0.;
588   feasible = 1; /* SGJ, 2010 */
589   if (*m > 0) {
590     i__1 = *m;
591     for (k = 1; k <= i__1; ++k) {
592       d__1 = resmax, d__2 = -con[k];
593       resmax = MAX2(d__1,d__2);
594       if (d__2 > state->con_tol[k-1])
595            feasible = 0; /* SGJ, 2010 */
596     }
597   }
598
599   /* SGJ, 2008: check for minf_max reached by feasible point */
600   if (f < stop->minf_max && feasible) {
601        rc = NLOPT_MINF_MAX_REACHED;
602        goto L620; /* not L600 because we want to use current x, f, resmax */
603   }
604
605   if (*(stop->nevals_p) == *iprint - 1 || *iprint == 3) {
606     fprintf(stderr, "cobyla: NFVALS = %4d, F =%13.6E, MAXCV =%13.6E\n",
607             *(stop->nevals_p), f, resmax);
608     i__1 = iptem;
609     fprintf(stderr, "cobyla: X =");
610     for (i__ = 1; i__ <= i__1; ++i__) {
611       if (i__>1) fprintf(stderr, "  ");
612       fprintf(stderr, "%13.6E", x[i__]);
613     }
614     if (iptem < *n) {
615       i__1 = *n;
616       for (i__ = iptemp; i__ <= i__1; ++i__) {
617         if (!((i__-1) % 4)) fprintf(stderr, "\ncobyla:  ");
618         fprintf(stderr, "%15.6E", x[i__]);
619       }
620     }
621     fprintf(stderr, "\n");
622   }
623   con[mp] = f;
624   con[*mpp] = resmax;
625   if (ibrnch == 1) {
626     goto L440;
627   }
628
629 /* Set the recently calculated function values in a column of DATMAT. This */
630 /* array has a column for each vertex of the current simplex, the entries of */
631 /* each column being the values of the constraint functions (if any) */
632 /* followed by the objective function and the greatest constraint violation */
633 /* at the vertex. */
634
635   i__1 = *mpp;
636   for (k = 1; k <= i__1; ++k) {
637     datmat[k + jdrop * datmat_dim1] = con[k];
638   }
639   if (*(stop->nevals_p) > np) {
640     goto L130;
641   }
642
643 /* Exchange the new vertex of the initial simplex with the optimal vertex if */
644 /* necessary. Then, if the initial simplex is not complete, pick its next */
645 /* vertex and calculate the function values there. */
646
647   if (jdrop <= *n) {
648     if (datmat[mp + np * datmat_dim1] <= f) {
649       x[jdrop] = sim[jdrop + np * sim_dim1];
650     } else { /* improvement in function val */
651       double rhocur = x[jdrop] - sim[jdrop + np * sim_dim1];
652       /* SGJ: use rhocur instead of rho.  In original code, rhocur == rho
653               always, but I want to change this to ensure that simplex points
654               fall within [lb,ub]. */
655       sim[jdrop + np * sim_dim1] = x[jdrop];
656       i__1 = *mpp;
657       for (k = 1; k <= i__1; ++k) {
658         datmat[k + jdrop * datmat_dim1] = datmat[k + np * datmat_dim1]
659             ;
660         datmat[k + np * datmat_dim1] = con[k];
661       }
662       i__1 = jdrop;
663       for (k = 1; k <= i__1; ++k) {
664         sim[jdrop + k * sim_dim1] = -rhocur;
665         temp = 0.f;
666         i__2 = jdrop;
667         for (i__ = k; i__ <= i__2; ++i__) {
668           temp -= simi[i__ + k * simi_dim1];
669         }
670         simi[jdrop + k * simi_dim1] = temp;
671       }
672     }
673   }
674   if (*(stop->nevals_p) <= *n) { /* evaluating initial simplex */
675     jdrop = *(stop->nevals_p);
676     /* SGJ: was += rho, but using sim[jdrop,jdrop] enforces consistency
677             if we change the stepsize above to stay in [lb,ub]. */
678     x[jdrop] += sim[jdrop + jdrop * sim_dim1];
679     goto L40;
680   }
681 L130:
682   ibrnch = 1;
683
684 /* Identify the optimal vertex of the current simplex. */
685
686 L140:
687   phimin = datmat[mp + np * datmat_dim1] + parmu * datmat[*mpp + np * 
688       datmat_dim1];
689   nbest = np;
690   i__1 = *n;
691   for (j = 1; j <= i__1; ++j) {
692     temp = datmat[mp + j * datmat_dim1] + parmu * datmat[*mpp + j * 
693         datmat_dim1];
694     if (temp < phimin) {
695       nbest = j;
696       phimin = temp;
697     } else if (temp == phimin && parmu == 0.) {
698       if (datmat[*mpp + j * datmat_dim1] < datmat[*mpp + nbest * 
699           datmat_dim1]) {
700         nbest = j;
701       }
702     }
703   }
704
705 /* Switch the best vertex into pole position if it is not there already, */
706 /* and also update SIM, SIMI and DATMAT. */
707
708   if (nbest <= *n) {
709     i__1 = *mpp;
710     for (i__ = 1; i__ <= i__1; ++i__) {
711       temp = datmat[i__ + np * datmat_dim1];
712       datmat[i__ + np * datmat_dim1] = datmat[i__ + nbest * datmat_dim1]
713           ;
714       datmat[i__ + nbest * datmat_dim1] = temp;
715     }
716     i__1 = *n;
717     for (i__ = 1; i__ <= i__1; ++i__) {
718       temp = sim[i__ + nbest * sim_dim1];
719       sim[i__ + nbest * sim_dim1] = 0.;
720       sim[i__ + np * sim_dim1] += temp;
721       tempa = 0.;
722       i__2 = *n;
723       for (k = 1; k <= i__2; ++k) {
724         sim[i__ + k * sim_dim1] -= temp;
725         tempa -= simi[k + i__ * simi_dim1];
726       }
727       simi[nbest + i__ * simi_dim1] = tempa;
728     }
729   }
730
731 /* Make an error return if SIGI is a poor approximation to the inverse of */
732 /* the leading N by N submatrix of SIG. */
733
734   error = 0.;
735   i__1 = *n;
736   for (i__ = 1; i__ <= i__1; ++i__) {
737     i__2 = *n;
738     for (j = 1; j <= i__2; ++j) {
739       temp = 0.;
740       if (i__ == j) {
741         temp += -1.;
742       }
743       i__3 = *n;
744       for (k = 1; k <= i__3; ++k) if (sim[k + j * sim_dim1] != 0) {
745         temp += simi[i__ + k * simi_dim1] * sim[k + j * sim_dim1];
746       }
747       d__1 = error, d__2 = fabs(temp);
748       error = MAX2(d__1,d__2);
749     }
750   }
751   if (error > .1) {
752     if (*iprint >= 1) {
753       fprintf(stderr, "cobyla: rounding errors are becoming damaging.\n");
754     }
755     rc = NLOPT_ROUNDOFF_LIMITED;
756     goto L600;
757   }
758
759 /* Calculate the coefficients of the linear approximations to the objective */
760 /* and constraint functions, placing minus the objective function gradient */
761 /* after the constraint gradients in the array A. The vector W is used for */
762 /* working space. */
763
764   i__2 = mp;
765   for (k = 1; k <= i__2; ++k) {
766     con[k] = -datmat[k + np * datmat_dim1];
767     i__1 = *n;
768     for (j = 1; j <= i__1; ++j) {
769       w[j] = datmat[k + j * datmat_dim1] + con[k];
770     }
771     i__1 = *n;
772     for (i__ = 1; i__ <= i__1; ++i__) {
773       temp = 0.;
774       i__3 = *n;
775       for (j = 1; j <= i__3; ++j) {
776         temp += w[j] * simi[j + i__ * simi_dim1];
777       }
778       if (k == mp) {
779         temp = -temp;
780       }
781       a[i__ + k * a_dim1] = temp;
782     }
783   }
784
785 /* Calculate the values of sigma and eta, and set IFLAG=0 if the current */
786 /* simplex is not acceptable. */
787
788   iflag = 1;
789   parsig = alpha * rho;
790   pareta = beta * rho;
791   i__1 = *n;
792   for (j = 1; j <= i__1; ++j) {
793     wsig = 0.;
794     weta = 0.;
795     i__2 = *n;
796     for (i__ = 1; i__ <= i__2; ++i__) {
797       d__1 = simi[j + i__ * simi_dim1];
798       wsig += d__1 * d__1;
799       d__1 = sim[i__ + j * sim_dim1];
800       weta += d__1 * d__1;
801     }
802     vsig[j] = 1. / sqrt(wsig);
803     veta[j] = sqrt(weta);
804     if (vsig[j] < parsig || veta[j] > pareta) {
805       iflag = 0;
806     }
807   }
808
809 /* If a new vertex is needed to improve acceptability, then decide which */
810 /* vertex to drop from the simplex. */
811
812   if (ibrnch == 1 || iflag == 1) {
813     goto L370;
814   }
815   jdrop = 0;
816   temp = pareta;
817   i__1 = *n;
818   for (j = 1; j <= i__1; ++j) {
819     if (veta[j] > temp) {
820       jdrop = j;
821       temp = veta[j];
822     }
823   }
824   if (jdrop == 0) {
825     i__1 = *n;
826     for (j = 1; j <= i__1; ++j) {
827       if (vsig[j] < temp) {
828         jdrop = j;
829         temp = vsig[j];
830       }
831     }
832   }
833
834 /* Calculate the step to the new vertex and its sign. */
835
836   temp = gamma_ * rho * vsig[jdrop];
837   i__1 = *n;
838   for (i__ = 1; i__ <= i__1; ++i__) {
839     dx[i__] = temp * simi[jdrop + i__ * simi_dim1];
840   }
841   cvmaxp = 0.;
842   cvmaxm = 0.;
843   i__1 = mp;
844   for (k = 1; k <= i__1; ++k) {
845     sum = 0.;
846     i__2 = *n;
847     for (i__ = 1; i__ <= i__2; ++i__) {
848       sum += a[i__ + k * a_dim1] * dx[i__];
849     }
850     if (k < mp) {
851       temp = datmat[k + np * datmat_dim1];
852       d__1 = cvmaxp, d__2 = -sum - temp;
853       cvmaxp = MAX2(d__1,d__2);
854       d__1 = cvmaxm, d__2 = sum - temp;
855       cvmaxm = MAX2(d__1,d__2);
856     }
857   }
858   dxsign = 1.;
859   if (parmu * (cvmaxp - cvmaxm) > sum + sum) {
860     dxsign = -1.;
861   }
862
863 /* Update the elements of SIM and SIMI, and set the next X. */
864
865   temp = 0.;
866   i__1 = *n;
867   for (i__ = 1; i__ <= i__1; ++i__) {
868     /* SGJ, 2010: pseudo-randomize simplex steps (see LCG comments above) */
869     dx[i__] = dxsign * dx[i__] * lcg_urand(&seed, 0.01, 1);
870     /* SGJ: make sure dx step says in [lb,ub] */
871 #if ENFORCE_BOUNDS
872     {
873          double xi = sim[i__ + np * sim_dim1];
874     fixdx:
875          if (xi + dx[i__] > ub[i__])
876               dx[i__] = -dx[i__];
877          if (xi + dx[i__] < lb[i__]) {
878               if (xi - dx[i__] <= ub[i__])
879                    dx[i__] = -dx[i__];
880               else { /* try again with halved step */
881                    dx[i__] *= 0.5;
882                    goto fixdx;
883               }
884          }
885     }
886 #endif
887     sim[i__ + jdrop * sim_dim1] = dx[i__];
888     temp += simi[jdrop + i__ * simi_dim1] * dx[i__];
889   }
890   i__1 = *n;
891   for (i__ = 1; i__ <= i__1; ++i__) {
892     simi[jdrop + i__ * simi_dim1] /= temp;
893   }
894   i__1 = *n;
895   for (j = 1; j <= i__1; ++j) {
896     if (j != jdrop) {
897       temp = 0.;
898       i__2 = *n;
899       for (i__ = 1; i__ <= i__2; ++i__) {
900         temp += simi[j + i__ * simi_dim1] * dx[i__];
901       }
902       i__2 = *n;
903       for (i__ = 1; i__ <= i__2; ++i__) {
904         simi[j + i__ * simi_dim1] -= temp * simi[jdrop + i__ * 
905             simi_dim1];
906       }
907     }
908     x[j] = sim[j + np * sim_dim1] + dx[j];
909   }
910   goto L40;
911
912 /* Calculate DX=x(*)-x(0). Branch if the length of DX is less than 0.5*RHO. */
913
914 L370:
915   iz = 1;
916   izdota = iz + *n * *n;
917   ivmc = izdota + *n;
918   isdirn = ivmc + mp;
919   idxnew = isdirn + *n;
920   ivmd = idxnew + *n;
921   rc = trstlp(n, m, &a[a_offset], &con[1], &rho, &dx[1], &ifull, &iact[1], &w[
922       iz], &w[izdota], &w[ivmc], &w[isdirn], &w[idxnew], &w[ivmd]);
923   if (rc != NLOPT_SUCCESS) goto L600;
924 #if ENFORCE_BOUNDS
925   /* SGJ: since the bound constraints are linear, we should never get
926      a dx that lies outside the [lb,ub] constraints here, but we'll
927      enforce this anyway just to be paranoid */
928   i__1 = *n;
929   for (i__ = 1; i__ <= i__1; ++i__) {
930        double xi = sim[i__ + np * sim_dim1];
931        if (xi + dx[i__] > ub[i__]) dx[i__] = ub[i__] - xi;
932        if (xi + dx[i__] < lb[i__]) dx[i__] = xi - lb[i__];
933   }
934 #endif
935   if (ifull == 0) {
936     temp = 0.;
937     i__1 = *n;
938     for (i__ = 1; i__ <= i__1; ++i__) {
939       d__1 = dx[i__];
940       temp += d__1 * d__1;
941     }
942     if (temp < rho * .25 * rho) {
943       ibrnch = 1;
944       goto L550;
945     }
946   }
947
948 /* Predict the change to F and the new maximum constraint violation if the */
949 /* variables are altered from x(0) to x(0)+DX. */
950
951   resnew = 0.;
952   con[mp] = 0.;
953   i__1 = mp;
954   for (k = 1; k <= i__1; ++k) {
955     sum = con[k];
956     i__2 = *n;
957     for (i__ = 1; i__ <= i__2; ++i__) {
958       sum -= a[i__ + k * a_dim1] * dx[i__];
959     }
960     if (k < mp) {
961       resnew = MAX2(resnew,sum);
962     }
963   }
964
965 /* Increase PARMU if necessary and branch back if this change alters the */
966 /* optimal vertex. Otherwise PREREM and PREREC will be set to the predicted */
967 /* reductions in the merit function and the maximum constraint violation */
968 /* respectively. */
969
970   barmu = 0.;
971   prerec = datmat[*mpp + np * datmat_dim1] - resnew;
972   if (prerec > 0.) {
973     barmu = sum / prerec;
974   }
975   if (parmu < barmu * 1.5) {
976     parmu = barmu * 2.;
977     if (*iprint >= 2) {
978       fprintf(stderr, "cobyla: increase in PARMU to %12.6E\n", parmu);
979     }
980     phi = datmat[mp + np * datmat_dim1] + parmu * datmat[*mpp + np * 
981         datmat_dim1];
982     i__1 = *n;
983     for (j = 1; j <= i__1; ++j) {
984       temp = datmat[mp + j * datmat_dim1] + parmu * datmat[*mpp + j * 
985           datmat_dim1];
986       if (temp < phi) {
987         goto L140;
988       }
989       if (temp == phi && parmu == 0.f) {
990         if (datmat[*mpp + j * datmat_dim1] < datmat[*mpp + np * 
991             datmat_dim1]) {
992           goto L140;
993         }
994       }
995     }
996   }
997   prerem = parmu * prerec - sum;
998
999 /* Calculate the constraint and objective functions at x(*). Then find the */
1000 /* actual reduction in the merit function. */
1001
1002   i__1 = *n;
1003   for (i__ = 1; i__ <= i__1; ++i__) {
1004     x[i__] = sim[i__ + np * sim_dim1] + dx[i__];
1005   }
1006   ibrnch = 1;
1007   goto L40;
1008 L440:
1009   vmold = datmat[mp + np * datmat_dim1] + parmu * datmat[*mpp + np * 
1010       datmat_dim1];
1011   vmnew = f + parmu * resmax;
1012   trured = vmold - vmnew;
1013   if (parmu == 0. && f == datmat[mp + np * datmat_dim1]) {
1014     prerem = prerec;
1015     trured = datmat[*mpp + np * datmat_dim1] - resmax;
1016   }
1017
1018 /* Begin the operations that decide whether x(*) should replace one of the */
1019 /* vertices of the current simplex, the change being mandatory if TRURED is */
1020 /* positive. Firstly, JDROP is set to the index of the vertex that is to be */
1021 /* replaced. */
1022
1023   ratio = 0.;
1024   if (trured <= 0.f) {
1025     ratio = 1.f;
1026   }
1027   jdrop = 0;
1028   i__1 = *n;
1029   for (j = 1; j <= i__1; ++j) {
1030     temp = 0.;
1031     i__2 = *n;
1032     for (i__ = 1; i__ <= i__2; ++i__) {
1033       temp += simi[j + i__ * simi_dim1] * dx[i__];
1034     }
1035     temp = fabs(temp);
1036     if (temp > ratio) {
1037       jdrop = j;
1038       ratio = temp;
1039     }
1040     sigbar[j] = temp * vsig[j];
1041   }
1042
1043 /* Calculate the value of ell. */
1044
1045   edgmax = delta * rho;
1046   l = 0;
1047   i__1 = *n;
1048   for (j = 1; j <= i__1; ++j) {
1049     if (sigbar[j] >= parsig || sigbar[j] >= vsig[j]) {
1050       temp = veta[j];
1051       if (trured > 0.) {
1052         temp = 0.;
1053         i__2 = *n;
1054         for (i__ = 1; i__ <= i__2; ++i__) {
1055           d__1 = dx[i__] - sim[i__ + j * sim_dim1];
1056           temp += d__1 * d__1;
1057         }
1058         temp = sqrt(temp);
1059       }
1060       if (temp > edgmax) {
1061         l = j;
1062         edgmax = temp;
1063       }
1064     }
1065   }
1066   if (l > 0) {
1067     jdrop = l;
1068   }
1069   if (jdrop == 0) {
1070     goto L550;
1071   }
1072
1073 /* Revise the simplex by updating the elements of SIM, SIMI and DATMAT. */
1074
1075   temp = 0.;
1076   i__1 = *n;
1077   for (i__ = 1; i__ <= i__1; ++i__) {
1078     sim[i__ + jdrop * sim_dim1] = dx[i__];
1079     temp += simi[jdrop + i__ * simi_dim1] * dx[i__];
1080   }
1081   i__1 = *n;
1082   for (i__ = 1; i__ <= i__1; ++i__) {
1083     simi[jdrop + i__ * simi_dim1] /= temp;
1084   }
1085   i__1 = *n;
1086   for (j = 1; j <= i__1; ++j) {
1087     if (j != jdrop) {
1088       temp = 0.;
1089       i__2 = *n;
1090       for (i__ = 1; i__ <= i__2; ++i__) {
1091         temp += simi[j + i__ * simi_dim1] * dx[i__];
1092       }
1093       i__2 = *n;
1094       for (i__ = 1; i__ <= i__2; ++i__) {
1095         simi[j + i__ * simi_dim1] -= temp * simi[jdrop + i__ * 
1096             simi_dim1];
1097       }
1098     }
1099   }
1100   i__1 = *mpp;
1101   for (k = 1; k <= i__1; ++k) {
1102     datmat[k + jdrop * datmat_dim1] = con[k];
1103   }
1104
1105 /* Branch back for further iterations with the current RHO. */
1106
1107   if (trured > 0. && trured >= prerem * .1) {
1108     /* SGJ, 2010: following a suggestion in the SAS manual (which
1109        mentions a similar modification to COBYLA, although they didn't
1110        publish their source code), increase rho if predicted reduction
1111        is sufficiently close to the actual reduction.  Otherwise,
1112        COBLYA seems to easily get stuck making very small steps. 
1113        Also require iflag != 0 (i.e., acceptable simplex), again
1114        following SAS suggestion (otherwise I get convergence failure
1115        in some cases.) */
1116     if (trured >= prerem * 0.9 && trured <= prerem * 1.1 && iflag) {
1117          rho *= 2.0;
1118     }
1119     goto L140;
1120   }
1121 L550:
1122   if (iflag == 0) {
1123     ibrnch = 0;
1124     goto L140;
1125   }
1126
1127   /* SGJ, 2008: convergence tests for function vals; note that current
1128      best val is stored in datmat[mp + np * datmat_dim1], or in f if
1129      ifull == 1, and previous best is in *minf.  This seems like a
1130      sensible place to put the convergence test, as it is the same
1131      place that Powell checks the x tolerance (rho > rhoend). */
1132   {
1133        double fbest = ifull == 1 ? f : datmat[mp + np * datmat_dim1];
1134        if (fbest < *minf && nlopt_stop_ftol(stop, fbest, *minf)) {
1135             rc = NLOPT_FTOL_REACHED;
1136             goto L600;
1137        }
1138        *minf = fbest;
1139   }
1140
1141 /* Otherwise reduce RHO if it is not at its least value and reset PARMU. */
1142
1143   if (rho > rhoend) {
1144     rho *= .5;
1145     if (rho <= rhoend * 1.5) {
1146       rho = rhoend;
1147     }
1148     if (parmu > 0.) {
1149       denom = 0.;
1150       i__1 = mp;
1151       for (k = 1; k <= i__1; ++k) {
1152         cmin = datmat[k + np * datmat_dim1];
1153         cmax = cmin;
1154         i__2 = *n;
1155         for (i__ = 1; i__ <= i__2; ++i__) {
1156           d__1 = cmin, d__2 = datmat[k + i__ * datmat_dim1];
1157           cmin = MIN2(d__1,d__2);
1158           d__1 = cmax, d__2 = datmat[k + i__ * datmat_dim1];
1159           cmax = MAX2(d__1,d__2);
1160         }
1161         if (k <= *m && cmin < cmax * .5) {
1162           temp = MAX2(cmax,0.) - cmin;
1163           if (denom <= 0.) {
1164             denom = temp;
1165           } else {
1166             denom = MIN2(denom,temp);
1167           }
1168         }
1169       }
1170       if (denom == 0.) {
1171         parmu = 0.;
1172       } else if (cmax - cmin < parmu * denom) {
1173         parmu = (cmax - cmin) / denom;
1174       }
1175     }
1176     if (*iprint >= 2) {
1177       fprintf(stderr, "cobyla: reduction in RHO to %12.6E and PARMU =%13.6E\n",
1178         rho, parmu);
1179     }
1180     if (*iprint == 2) {
1181       fprintf(stderr, "cobyla: NFVALS = %4d, F =%13.6E, MAXCV =%13.6E\n",
1182         *(stop->nevals_p), datmat[mp + np * datmat_dim1], datmat[*mpp + np * datmat_dim1]);
1183
1184       fprintf(stderr, "cobyla: X =");
1185       i__1 = iptem;
1186       for (i__ = 1; i__ <= i__1; ++i__) {
1187         if (i__>1) fprintf(stderr, "  ");
1188         fprintf(stderr, "%13.6E", sim[i__ + np * sim_dim1]);
1189       }
1190       if (iptem < *n) {
1191         i__1 = *n;
1192         for (i__ = iptemp; i__ <= i__1; ++i__) {
1193           if (!((i__-1) % 4)) fprintf(stderr, "\ncobyla:  ");
1194           fprintf(stderr, "%15.6E", x[i__]);
1195         }
1196       }
1197       fprintf(stderr, "\n");
1198     }
1199     goto L140;
1200   }
1201   else /* rho <= rhoend */
1202        rc = rhoend > 0 ? NLOPT_XTOL_REACHED : NLOPT_ROUNDOFF_LIMITED;
1203
1204 /* Return the best calculated values of the variables. */
1205
1206   if (*iprint >= 1) {
1207     fprintf(stderr, "cobyla: normal return.\n");
1208   }
1209   if (ifull == 1) {
1210     goto L620;
1211   }
1212 L600:
1213   i__1 = *n;
1214   for (i__ = 1; i__ <= i__1; ++i__) {
1215     x[i__] = sim[i__ + np * sim_dim1];
1216   }
1217   f = datmat[mp + np * datmat_dim1];
1218   resmax = datmat[*mpp + np * datmat_dim1];
1219 L620:
1220   *minf = f;
1221   if (*iprint >= 1) {
1222     fprintf(stderr, "cobyla: NFVALS = %4d, F =%13.6E, MAXCV =%13.6E\n",
1223             *(stop->nevals_p), f, resmax);
1224     i__1 = iptem;
1225     fprintf(stderr, "cobyla: X =");
1226     for (i__ = 1; i__ <= i__1; ++i__) {
1227       if (i__>1) fprintf(stderr, "  ");
1228       fprintf(stderr, "%13.6E", x[i__]);
1229     }
1230     if (iptem < *n) {
1231       i__1 = *n;
1232       for (i__ = iptemp; i__ <= i__1; ++i__) {
1233         if (!((i__-1) % 4)) fprintf(stderr, "\ncobyla:  ");
1234         fprintf(stderr, "%15.6E", x[i__]);
1235       }
1236     }
1237     fprintf(stderr, "\n");
1238   }
1239   return rc;
1240 } /* cobylb */
1241
1242 /* ------------------------------------------------------------------------- */
1243 static nlopt_result trstlp(int *n, int *m, double *a, 
1244     double *b, double *rho, double *dx, int *ifull, 
1245     int *iact, double *z__, double *zdota, double *vmultc,
1246      double *sdirn, double *dxnew, double *vmultd)
1247 {
1248   /* System generated locals */
1249   int a_dim1, a_offset, z_dim1, z_offset, i__1, i__2;
1250   double d__1, d__2;
1251
1252   /* Local variables */
1253   double alpha, tempa;
1254   double beta;
1255   double optnew, stpful, sum, tot, acca, accb;
1256   double ratio, vsave, zdotv, zdotw, dd;
1257   double sd;
1258   double sp, ss, resold = 0.0, zdvabs, zdwabs, sumabs, resmax, optold;
1259   double spabs;
1260   double temp, step;
1261   int icount;
1262   int iout, i__, j, k;
1263   int isave;
1264   int kk;
1265   int kl, kp, kw;
1266   int nact, icon = 0, mcon;
1267   int nactx = 0;
1268
1269
1270 /* This subroutine calculates an N-component vector DX by applying the */
1271 /* following two stages. In the first stage, DX is set to the shortest */
1272 /* vector that minimizes the greatest violation of the constraints */
1273 /*   A(1,K)*DX(1)+A(2,K)*DX(2)+...+A(N,K)*DX(N) .GE. B(K), K=2,3,...,M, */
1274 /* subject to the Euclidean length of DX being at most RHO. If its length is */
1275 /* strictly less than RHO, then we use the resultant freedom in DX to */
1276 /* minimize the objective function */
1277 /*      -A(1,M+1)*DX(1)-A(2,M+1)*DX(2)-...-A(N,M+1)*DX(N) */
1278 /* subject to no increase in any greatest constraint violation. This */
1279 /* notation allows the gradient of the objective function to be regarded as */
1280 /* the gradient of a constraint. Therefore the two stages are distinguished */
1281 /* by MCON .EQ. M and MCON .GT. M respectively. It is possible that a */
1282 /* degeneracy may prevent DX from attaining the target length RHO. Then the */
1283 /* value IFULL=0 would be set, but usually IFULL=1 on return. */
1284
1285 /* In general NACT is the number of constraints in the active set and */
1286 /* IACT(1),...,IACT(NACT) are their indices, while the remainder of IACT */
1287 /* contains a permutation of the remaining constraint indices. Further, Z is */
1288 /* an orthogonal matrix whose first NACT columns can be regarded as the */
1289 /* result of Gram-Schmidt applied to the active constraint gradients. For */
1290 /* J=1,2,...,NACT, the number ZDOTA(J) is the scalar product of the J-th */
1291 /* column of Z with the gradient of the J-th active constraint. DX is the */
1292 /* current vector of variables and here the residuals of the active */
1293 /* constraints should be zero. Further, the active constraints have */
1294 /* nonnegative Lagrange multipliers that are held at the beginning of */
1295 /* VMULTC. The remainder of this vector holds the residuals of the inactive */
1296 /* constraints at DX, the ordering of the components of VMULTC being in */
1297 /* agreement with the permutation of the indices of the constraints that is */
1298 /* in IACT. All these residuals are nonnegative, which is achieved by the */
1299 /* shift RESMAX that makes the least residual zero. */
1300
1301 /* Initialize Z and some other variables. The value of RESMAX will be */
1302 /* appropriate to DX=0, while ICON will be the index of a most violated */
1303 /* constraint if RESMAX is positive. Usually during the first stage the */
1304 /* vector SDIRN gives a search direction that reduces all the active */
1305 /* constraint violations by one simultaneously. */
1306
1307   /* Parameter adjustments */
1308   z_dim1 = *n;
1309   z_offset = 1 + z_dim1 * 1;
1310   z__ -= z_offset;
1311   a_dim1 = *n;
1312   a_offset = 1 + a_dim1 * 1;
1313   a -= a_offset;
1314   --b;
1315   --dx;
1316   --iact;
1317   --zdota;
1318   --vmultc;
1319   --sdirn;
1320   --dxnew;
1321   --vmultd;
1322
1323   /* Function Body */
1324   *ifull = 1;
1325   mcon = *m;
1326   nact = 0;
1327   resmax = 0.;
1328   i__1 = *n;
1329   for (i__ = 1; i__ <= i__1; ++i__) {
1330     i__2 = *n;
1331     for (j = 1; j <= i__2; ++j) {
1332       z__[i__ + j * z_dim1] = 0.;
1333     }
1334     z__[i__ + i__ * z_dim1] = 1.;
1335     dx[i__] = 0.;
1336   }
1337   if (*m >= 1) {
1338     i__1 = *m;
1339     for (k = 1; k <= i__1; ++k) {
1340       if (b[k] > resmax) {
1341         resmax = b[k];
1342         icon = k;
1343       }
1344     }
1345     i__1 = *m;
1346     for (k = 1; k <= i__1; ++k) {
1347       iact[k] = k;
1348       vmultc[k] = resmax - b[k];
1349     }
1350   }
1351   if (resmax == 0.) {
1352     goto L480;
1353   }
1354   i__1 = *n;
1355   for (i__ = 1; i__ <= i__1; ++i__) {
1356     sdirn[i__] = 0.;
1357   }
1358
1359 /* End the current stage of the calculation if 3 consecutive iterations */
1360 /* have either failed to reduce the best calculated value of the objective */
1361 /* function or to increase the number of active constraints since the best */
1362 /* value was calculated. This strategy prevents cycling, but there is a */
1363 /* remote possibility that it will cause premature termination. */
1364
1365 L60:
1366   optold = 0.;
1367   icount = 0;
1368 L70:
1369   if (mcon == *m) {
1370     optnew = resmax;
1371   } else {
1372     optnew = 0.;
1373     i__1 = *n;
1374     for (i__ = 1; i__ <= i__1; ++i__) {
1375       optnew -= dx[i__] * a[i__ + mcon * a_dim1];
1376     }
1377   }
1378   if (icount == 0 || optnew < optold) {
1379     optold = optnew;
1380     nactx = nact;
1381     icount = 3;
1382   } else if (nact > nactx) {
1383     nactx = nact;
1384     icount = 3;
1385   } else {
1386     --icount;
1387     if (icount == 0) {
1388       goto L490;
1389     }
1390   }
1391
1392 /* If ICON exceeds NACT, then we add the constraint with index IACT(ICON) to */
1393 /* the active set. Apply Givens rotations so that the last N-NACT-1 columns */
1394 /* of Z are orthogonal to the gradient of the new constraint, a scalar */
1395 /* product being set to zero if its nonzero value could be due to computer */
1396 /* rounding errors. The array DXNEW is used for working space. */
1397
1398   if (icon <= nact) {
1399     goto L260;
1400   }
1401   kk = iact[icon];
1402   i__1 = *n;
1403   for (i__ = 1; i__ <= i__1; ++i__) {
1404     dxnew[i__] = a[i__ + kk * a_dim1];
1405   }
1406   tot = 0.;
1407   k = *n;
1408 L100:
1409   if (k > nact) {
1410     sp = 0.;
1411     spabs = 0.;
1412     i__1 = *n;
1413     for (i__ = 1; i__ <= i__1; ++i__) {
1414       temp = z__[i__ + k * z_dim1] * dxnew[i__];
1415       sp += temp;
1416       spabs += fabs(temp);
1417     }
1418     acca = spabs + fabs(sp) * .1;
1419     accb = spabs + fabs(sp) * .2;
1420     if (spabs >= acca || acca >= accb) {
1421       sp = 0.;
1422     }
1423     if (tot == 0.) {
1424       tot = sp;
1425     } else {
1426       kp = k + 1;
1427       temp = sqrt(sp * sp + tot * tot);
1428       alpha = sp / temp;
1429       beta = tot / temp;
1430       tot = temp;
1431       i__1 = *n;
1432       for (i__ = 1; i__ <= i__1; ++i__) {
1433         temp = alpha * z__[i__ + k * z_dim1] + beta * z__[i__ + kp * 
1434             z_dim1];
1435         z__[i__ + kp * z_dim1] = alpha * z__[i__ + kp * z_dim1] - 
1436             beta * z__[i__ + k * z_dim1];
1437         z__[i__ + k * z_dim1] = temp;
1438       }
1439     }
1440     --k;
1441     goto L100;
1442   }
1443
1444 /* Add the new constraint if this can be done without a deletion from the */
1445 /* active set. */
1446
1447   if (tot != 0.) {
1448     ++nact;
1449     zdota[nact] = tot;
1450     vmultc[icon] = vmultc[nact];
1451     vmultc[nact] = 0.;
1452     goto L210;
1453   }
1454
1455 /* The next instruction is reached if a deletion has to be made from the */
1456 /* active set in order to make room for the new active constraint, because */
1457 /* the new constraint gradient is a linear combination of the gradients of */
1458 /* the old active constraints. Set the elements of VMULTD to the multipliers */
1459 /* of the linear combination. Further, set IOUT to the index of the */
1460 /* constraint to be deleted, but branch if no suitable index can be found. */
1461
1462   ratio = -1.;
1463   k = nact;
1464 L130:
1465   zdotv = 0.;
1466   zdvabs = 0.;
1467   i__1 = *n;
1468   for (i__ = 1; i__ <= i__1; ++i__) {
1469     temp = z__[i__ + k * z_dim1] * dxnew[i__];
1470     zdotv += temp;
1471     zdvabs += fabs(temp);
1472   }
1473   acca = zdvabs + fabs(zdotv) * .1;
1474   accb = zdvabs + fabs(zdotv) * .2;
1475   if (zdvabs < acca && acca < accb) {
1476     temp = zdotv / zdota[k];
1477     if (temp > 0. && iact[k] <= *m) {
1478       tempa = vmultc[k] / temp;
1479       if (ratio < 0. || tempa < ratio) {
1480         ratio = tempa;
1481         iout = k;
1482       }
1483     }
1484     if (k >= 2) {
1485       kw = iact[k];
1486       i__1 = *n;
1487       for (i__ = 1; i__ <= i__1; ++i__) {
1488         dxnew[i__] -= temp * a[i__ + kw * a_dim1];
1489       }
1490     }
1491     vmultd[k] = temp;
1492   } else {
1493     vmultd[k] = 0.;
1494   }
1495   --k;
1496   if (k > 0) {
1497     goto L130;
1498   }
1499   if (ratio < 0.) {
1500     goto L490;
1501   }
1502
1503 /* Revise the Lagrange multipliers and reorder the active constraints so */
1504 /* that the one to be replaced is at the end of the list. Also calculate the */
1505 /* new value of ZDOTA(NACT) and branch if it is not acceptable. */
1506
1507   i__1 = nact;
1508   
1509 /* This pragma fixes a known problem compiling with VS2013 or VS2015 in Release */
1510 /* see https://connect.microsoft.com/VisualStudio/feedback/details/1028781/c1001-on-release-build */
1511 #if defined(_MSC_VER) && _MSC_VER >= 1800
1512   #pragma loop(no_vector)
1513 #endif
1514   for (k = 1; k <= i__1; ++k) {
1515     d__1 = 0., d__2 = vmultc[k] - ratio * vmultd[k];
1516     vmultc[k] = MAX2(d__1,d__2);
1517   }
1518   if (icon < nact) {
1519     isave = iact[icon];
1520     vsave = vmultc[icon];
1521     k = icon;
1522 L170:
1523     kp = k + 1;
1524     kw = iact[kp];
1525     sp = 0.;
1526     i__1 = *n;
1527     for (i__ = 1; i__ <= i__1; ++i__) {
1528       sp += z__[i__ + k * z_dim1] * a[i__ + kw * a_dim1];
1529     }
1530     d__1 = zdota[kp];
1531     temp = sqrt(sp * sp + d__1 * d__1);
1532     alpha = zdota[kp] / temp;
1533     beta = sp / temp;
1534     zdota[kp] = alpha * zdota[k];
1535     zdota[k] = temp;
1536     i__1 = *n;
1537     for (i__ = 1; i__ <= i__1; ++i__) {
1538       temp = alpha * z__[i__ + kp * z_dim1] + beta * z__[i__ + k * 
1539           z_dim1];
1540       z__[i__ + kp * z_dim1] = alpha * z__[i__ + k * z_dim1] - beta * 
1541           z__[i__ + kp * z_dim1];
1542       z__[i__ + k * z_dim1] = temp;
1543     }
1544     iact[k] = kw;
1545     vmultc[k] = vmultc[kp];
1546     k = kp;
1547     if (k < nact) {
1548       goto L170;
1549     }
1550     iact[k] = isave;
1551     vmultc[k] = vsave;
1552   }
1553   temp = 0.;
1554   i__1 = *n;
1555   for (i__ = 1; i__ <= i__1; ++i__) {
1556     temp += z__[i__ + nact * z_dim1] * a[i__ + kk * a_dim1];
1557   }
1558   if (temp == 0.) {
1559     goto L490;
1560   }
1561   zdota[nact] = temp;
1562   vmultc[icon] = 0.;
1563   vmultc[nact] = ratio;
1564
1565 /* Update IACT and ensure that the objective function continues to be */
1566 /* treated as the last active constraint when MCON>M. */
1567
1568 L210:
1569   iact[icon] = iact[nact];
1570   iact[nact] = kk;
1571   if (mcon > *m && kk != mcon) {
1572     k = nact - 1;
1573     sp = 0.;
1574     i__1 = *n;
1575     for (i__ = 1; i__ <= i__1; ++i__) {
1576       sp += z__[i__ + k * z_dim1] * a[i__ + kk * a_dim1];
1577     }
1578     d__1 = zdota[nact];
1579     temp = sqrt(sp * sp + d__1 * d__1);
1580     alpha = zdota[nact] / temp;
1581     beta = sp / temp;
1582     zdota[nact] = alpha * zdota[k];
1583     zdota[k] = temp;
1584     i__1 = *n;
1585     for (i__ = 1; i__ <= i__1; ++i__) {
1586       temp = alpha * z__[i__ + nact * z_dim1] + beta * z__[i__ + k * 
1587           z_dim1];
1588       z__[i__ + nact * z_dim1] = alpha * z__[i__ + k * z_dim1] - beta * 
1589           z__[i__ + nact * z_dim1];
1590       z__[i__ + k * z_dim1] = temp;
1591     }
1592     iact[nact] = iact[k];
1593     iact[k] = kk;
1594     temp = vmultc[k];
1595     vmultc[k] = vmultc[nact];
1596     vmultc[nact] = temp;
1597   }
1598
1599 /* If stage one is in progress, then set SDIRN to the direction of the next */
1600 /* change to the current vector of variables. */
1601
1602   if (mcon > *m) {
1603     goto L320;
1604   }
1605   kk = iact[nact];
1606   temp = 0.;
1607   i__1 = *n;
1608   for (i__ = 1; i__ <= i__1; ++i__) {
1609     temp += sdirn[i__] * a[i__ + kk * a_dim1];
1610   }
1611   temp += -1.;
1612   temp /= zdota[nact];
1613   i__1 = *n;
1614   for (i__ = 1; i__ <= i__1; ++i__) {
1615     sdirn[i__] -= temp * z__[i__ + nact * z_dim1];
1616   }
1617   goto L340;
1618
1619 /* Delete the constraint that has the index IACT(ICON) from the active set. */
1620
1621 L260:
1622   if (icon < nact) {
1623     isave = iact[icon];
1624     vsave = vmultc[icon];
1625     k = icon;
1626 L270:
1627     kp = k + 1;
1628     kk = iact[kp];
1629     sp = 0.;
1630     i__1 = *n;
1631     for (i__ = 1; i__ <= i__1; ++i__) {
1632       sp += z__[i__ + k * z_dim1] * a[i__ + kk * a_dim1];
1633     }
1634     d__1 = zdota[kp];
1635     temp = sqrt(sp * sp + d__1 * d__1);
1636     alpha = zdota[kp] / temp;
1637     beta = sp / temp;
1638     zdota[kp] = alpha * zdota[k];
1639     zdota[k] = temp;
1640     i__1 = *n;
1641     for (i__ = 1; i__ <= i__1; ++i__) {
1642       temp = alpha * z__[i__ + kp * z_dim1] + beta * z__[i__ + k * 
1643           z_dim1];
1644       z__[i__ + kp * z_dim1] = alpha * z__[i__ + k * z_dim1] - beta * 
1645           z__[i__ + kp * z_dim1];
1646       z__[i__ + k * z_dim1] = temp;
1647     }
1648     iact[k] = kk;
1649     vmultc[k] = vmultc[kp];
1650     k = kp;
1651     if (k < nact) {
1652       goto L270;
1653     }
1654     iact[k] = isave;
1655     vmultc[k] = vsave;
1656   }
1657   --nact;
1658
1659 /* If stage one is in progress, then set SDIRN to the direction of the next */
1660 /* change to the current vector of variables. */
1661
1662   if (mcon > *m) {
1663     goto L320;
1664   }
1665   temp = 0.;
1666   i__1 = *n;
1667   for (i__ = 1; i__ <= i__1; ++i__) {
1668     temp += sdirn[i__] * z__[i__ + (nact + 1) * z_dim1];
1669   }
1670   i__1 = *n;
1671   for (i__ = 1; i__ <= i__1; ++i__) {
1672     sdirn[i__] -= temp * z__[i__ + (nact + 1) * z_dim1];
1673   }
1674   goto L340;
1675
1676 /* Pick the next search direction of stage two. */
1677
1678 L320:
1679   temp = 1. / zdota[nact];
1680   i__1 = *n;
1681   for (i__ = 1; i__ <= i__1; ++i__) {
1682     sdirn[i__] = temp * z__[i__ + nact * z_dim1];
1683   }
1684
1685 /* Calculate the step to the boundary of the trust region or take the step */
1686 /* that reduces RESMAX to zero. The two statements below that include the */
1687 /* factor 1.0E-6 prevent some harmless underflows that occurred in a test */
1688 /* calculation. Further, we skip the step if it could be zero within a */
1689 /* reasonable tolerance for computer rounding errors. */
1690
1691 L340:
1692   dd = *rho * *rho;
1693   sd = 0.;
1694   ss = 0.;
1695   i__1 = *n;
1696   for (i__ = 1; i__ <= i__1; ++i__) {
1697     if ((d__1 = dx[i__], fabs(d__1)) >= *rho * 1e-6f) {
1698       d__2 = dx[i__];
1699       dd -= d__2 * d__2;
1700     }
1701     sd += dx[i__] * sdirn[i__];
1702     d__1 = sdirn[i__];
1703     ss += d__1 * d__1;
1704   }
1705   if (dd <= 0.) {
1706     goto L490;
1707   }
1708   temp = sqrt(ss * dd);
1709   if (fabs(sd) >= temp * 1e-6f) {
1710     temp = sqrt(ss * dd + sd * sd);
1711   }
1712   stpful = dd / (temp + sd);
1713   step = stpful;
1714   if (mcon == *m) {
1715     acca = step + resmax * .1;
1716     accb = step + resmax * .2;
1717     if (step >= acca || acca >= accb) {
1718       goto L480;
1719     }
1720     step = MIN2(step,resmax);
1721   }
1722
1723   /* SGJ, 2010: check for error here */
1724   if (nlopt_isinf(step)) return NLOPT_ROUNDOFF_LIMITED;
1725
1726 /* Set DXNEW to the new variables if STEP is the steplength, and reduce */
1727 /* RESMAX to the corresponding maximum residual if stage one is being done. */
1728 /* Because DXNEW will be changed during the calculation of some Lagrange */
1729 /* multipliers, it will be restored to the following value later. */
1730
1731   i__1 = *n;
1732   for (i__ = 1; i__ <= i__1; ++i__) {
1733     dxnew[i__] = dx[i__] + step * sdirn[i__];
1734   }
1735   if (mcon == *m) {
1736     resold = resmax;
1737     resmax = 0.;
1738     i__1 = nact;
1739     for (k = 1; k <= i__1; ++k) {
1740       kk = iact[k];
1741       temp = b[kk];
1742       i__2 = *n;
1743       for (i__ = 1; i__ <= i__2; ++i__) {
1744         temp -= a[i__ + kk * a_dim1] * dxnew[i__];
1745       }
1746       resmax = MAX2(resmax,temp);
1747     }
1748   }
1749
1750 /* Set VMULTD to the VMULTC vector that would occur if DX became DXNEW. A */
1751 /* device is included to force VMULTD(K)=0.0 if deviations from this value */
1752 /* can be attributed to computer rounding errors. First calculate the new */
1753 /* Lagrange multipliers. */
1754
1755   k = nact;
1756 L390:
1757   zdotw = 0.;
1758   zdwabs = 0.;
1759   i__1 = *n;
1760   for (i__ = 1; i__ <= i__1; ++i__) {
1761     temp = z__[i__ + k * z_dim1] * dxnew[i__];
1762     zdotw += temp;
1763     zdwabs += fabs(temp);
1764   }
1765   acca = zdwabs + fabs(zdotw) * .1;
1766   accb = zdwabs + fabs(zdotw) * .2;
1767   if (zdwabs >= acca || acca >= accb) {
1768     zdotw = 0.;
1769   }
1770   vmultd[k] = zdotw / zdota[k];
1771   if (k >= 2) {
1772     kk = iact[k];
1773     i__1 = *n;
1774     for (i__ = 1; i__ <= i__1; ++i__) {
1775       dxnew[i__] -= vmultd[k] * a[i__ + kk * a_dim1];
1776     }
1777     --k;
1778     goto L390;
1779   }
1780   if (mcon > *m) {
1781     d__1 = 0., d__2 = vmultd[nact];
1782     vmultd[nact] = MAX2(d__1,d__2);
1783   }
1784
1785 /* Complete VMULTC by finding the new constraint residuals. */
1786
1787   i__1 = *n;
1788   for (i__ = 1; i__ <= i__1; ++i__) {
1789     dxnew[i__] = dx[i__] + step * sdirn[i__];
1790   }
1791   if (mcon > nact) {
1792     kl = nact + 1;
1793     i__1 = mcon;
1794     for (k = kl; k <= i__1; ++k) {
1795       kk = iact[k];
1796       sum = resmax - b[kk];
1797       sumabs = resmax + (d__1 = b[kk], fabs(d__1));
1798       i__2 = *n;
1799       for (i__ = 1; i__ <= i__2; ++i__) {
1800         temp = a[i__ + kk * a_dim1] * dxnew[i__];
1801         sum += temp;
1802         sumabs += fabs(temp);
1803       }
1804       acca = sumabs + fabs(sum) * .1f;
1805       accb = sumabs + fabs(sum) * .2f;
1806       if (sumabs >= acca || acca >= accb) {
1807         sum = 0.f;
1808       }
1809       vmultd[k] = sum;
1810     }
1811   }
1812
1813 /* Calculate the fraction of the step from DX to DXNEW that will be taken. */
1814
1815   ratio = 1.;
1816   icon = 0;
1817   i__1 = mcon;
1818   for (k = 1; k <= i__1; ++k) {
1819     if (vmultd[k] < 0.) {
1820       temp = vmultc[k] / (vmultc[k] - vmultd[k]);
1821       if (temp < ratio) {
1822         ratio = temp;
1823         icon = k;
1824       }
1825     }
1826   }
1827
1828 /* Update DX, VMULTC and RESMAX. */
1829
1830   temp = 1. - ratio;
1831   i__1 = *n;
1832   for (i__ = 1; i__ <= i__1; ++i__) {
1833     dx[i__] = temp * dx[i__] + ratio * dxnew[i__];
1834   }
1835   i__1 = mcon;
1836   for (k = 1; k <= i__1; ++k) {
1837     d__1 = 0., d__2 = temp * vmultc[k] + ratio * vmultd[k];
1838     vmultc[k] = MAX2(d__1,d__2);
1839   }
1840   if (mcon == *m) {
1841     resmax = resold + ratio * (resmax - resold);
1842   }
1843
1844 /* If the full step is not acceptable then begin another iteration. */
1845 /* Otherwise switch to stage two or end the calculation. */
1846
1847   if (icon > 0) {
1848     goto L70;
1849   }
1850   if (step == stpful) {
1851     goto L500;
1852   }
1853 L480:
1854   mcon = *m + 1;
1855   icon = mcon;
1856   iact[mcon] = mcon;
1857   vmultc[mcon] = 0.;
1858   goto L60;
1859
1860 /* We employ any freedom that may be available to reduce the objective */
1861 /* function before returning a DX whose length is less than RHO. */
1862
1863 L490:
1864   if (mcon == *m) {
1865     goto L480;
1866   }
1867   *ifull = 0;
1868 L500:
1869   return NLOPT_SUCCESS;
1870 } /* trstlp */