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