2 Downloaded from http://www.netlib.org/opt/subplex.tgz
4 README file for SUBPLEX
7 subplex - subspace-searching simplex method for unconstrained
11 Subplex is a subspace-searching simplex method for the
12 unconstrained optimization of general multivariate functions.
13 Like the Nelder-Mead simplex method it generalizes, the subplex
14 method is well suited for optimizing noisy objective functions.
15 The number of function evaluations required for convergence
16 typically increases only linearly with the problem size, so for
17 most applications the subplex method is much more efficient than
21 To build subplex on UNIX systems, edit the Makefile as necessary
26 This will create a linkable library named subplex.a and a
27 demonstration executable named demo.
30 To run subplex on a simple objective function type:
34 To run subplex on other problems, edit a copy of the sample driver
39 Oak Ridge National Laboratory
40 Mathematical Sciences Section
41 P.O. Box 2008, Bldg. 6012
42 Oak Ridge, TN 37831-6367
46 Email: na.rowan@na-net.ornl.gov
49 T. Rowan, "Functional Stability Analysis of Numerical Algorithms",
50 Ph.D. thesis, Department of Computer Sciences, University of Texas
54 Please send comments, suggestions, or bug reports to
55 na.rowan@na-net.ornl.gov.
64 typedef double doublereal;
71 typedef subplex_func D_fp;
73 #define max(a,b) ((a) > (b) ? (a) : (b))
74 #define min(a,b) ((a) < (b) ? (a) : (b))
75 #define abs(x) fabs(x)
77 /****************************************************************************/
78 /****************************************************************************/
80 /* dasum.f -- translated by f2c (version 19991025).
81 You must link the resulting object file with the libraries:
82 -lf2c -lm (in that order)
85 static doublereal dasum_(integer *n, doublereal *dx, integer *incx)
87 /* System generated locals */
89 doublereal ret_val, d__1, d__2, d__3, d__4, d__5, d__6;
97 /* takes the sum of the absolute values. */
98 /* uses unrolled loops for increment equal to one. */
99 /* jack dongarra, linpack, 3/11/78. */
100 /* modified to correct problem with negative increment, 8/21/90. */
103 /* Parameter adjustments */
116 /* code for increment not equal to 1 */
120 ix = (-(*n) + 1) * *incx + 1;
123 for (i__ = 1; i__ <= i__1; ++i__) {
124 dtemp += (d__1 = dx[ix], abs(d__1));
131 /* code for increment equal to 1 */
142 for (i__ = 1; i__ <= i__1; ++i__) {
143 dtemp += (d__1 = dx[i__], abs(d__1));
152 for (i__ = mp1; i__ <= i__1; i__ += 6) {
153 dtemp = dtemp + (d__1 = dx[i__], abs(d__1)) + (d__2 = dx[i__ + 1],
154 abs(d__2)) + (d__3 = dx[i__ + 2], abs(d__3)) + (d__4 = dx[i__
155 + 3], abs(d__4)) + (d__5 = dx[i__ + 4], abs(d__5)) + (d__6 =
156 dx[i__ + 5], abs(d__6));
164 /* daxpy.f -- translated by f2c (version 19991025).
165 You must link the resulting object file with the libraries:
166 -lf2c -lm (in that order)
169 static int daxpy_(integer *n, doublereal *da, doublereal *dx,
170 integer *incx, doublereal *dy, integer *incy)
172 /* System generated locals */
175 /* Local variables */
176 integer i__, m, ix, iy, mp1;
179 /* constant times a vector plus a vector. */
180 /* uses unrolled loops for increments equal to one. */
181 /* jack dongarra, linpack, 3/11/78. */
184 /* Parameter adjustments */
195 if (*incx == 1 && *incy == 1) {
199 /* code for unequal increments or equal increments */
205 ix = (-(*n) + 1) * *incx + 1;
208 iy = (-(*n) + 1) * *incy + 1;
211 for (i__ = 1; i__ <= i__1; ++i__) {
212 dy[iy] += *da * dx[ix];
219 /* code for both increments equal to 1 */
230 for (i__ = 1; i__ <= i__1; ++i__) {
231 dy[i__] += *da * dx[i__];
240 for (i__ = mp1; i__ <= i__1; i__ += 4) {
241 dy[i__] += *da * dx[i__];
242 dy[i__ + 1] += *da * dx[i__ + 1];
243 dy[i__ + 2] += *da * dx[i__ + 2];
244 dy[i__ + 3] += *da * dx[i__ + 3];
250 /* dcopy.f -- translated by f2c (version 19991025).
251 You must link the resulting object file with the libraries:
252 -lf2c -lm (in that order)
255 static int dcopy_(integer *n, const doublereal *dx, integer *incx,
256 doublereal *dy, integer *incy)
258 /* System generated locals */
261 /* Local variables */
262 integer i__, m, ix, iy, mp1;
265 /* copies a vector, x, to a vector, y. */
266 /* uses unrolled loops for increments equal to one. */
267 /* jack dongarra, linpack, 3/11/78. */
270 /* Parameter adjustments */
278 if (*incx == 1 && *incy == 1) {
282 /* code for unequal increments or equal increments */
288 ix = (-(*n) + 1) * *incx + 1;
291 iy = (-(*n) + 1) * *incy + 1;
294 for (i__ = 1; i__ <= i__1; ++i__) {
302 /* code for both increments equal to 1 */
313 for (i__ = 1; i__ <= i__1; ++i__) {
323 for (i__ = mp1; i__ <= i__1; i__ += 7) {
325 dy[i__ + 1] = dx[i__ + 1];
326 dy[i__ + 2] = dx[i__ + 2];
327 dy[i__ + 3] = dx[i__ + 3];
328 dy[i__ + 4] = dx[i__ + 4];
329 dy[i__ + 5] = dx[i__ + 5];
330 dy[i__ + 6] = dx[i__ + 6];
336 /* dscal.f -- translated by f2c (version 19991025).
337 You must link the resulting object file with the libraries:
338 -lf2c -lm (in that order)
341 static int dscal_(integer *n, doublereal *da, doublereal *dx,
344 /* System generated locals */
347 /* Local variables */
348 integer i__, m, ix, mp1;
351 /* scales a vector by a constant. */
352 /* uses unrolled loops for increment equal to one. */
353 /* jack dongarra, linpack, 3/11/78. */
354 /* modified to correct problem with negative increment, 8/21/90. */
357 /* Parameter adjustments */
368 /* code for increment not equal to 1 */
372 ix = (-(*n) + 1) * *incx + 1;
375 for (i__ = 1; i__ <= i__1; ++i__) {
376 dx[ix] = *da * dx[ix];
382 /* code for increment equal to 1 */
393 for (i__ = 1; i__ <= i__1; ++i__) {
394 dx[i__] = *da * dx[i__];
403 for (i__ = mp1; i__ <= i__1; i__ += 5) {
404 dx[i__] = *da * dx[i__];
405 dx[i__ + 1] = *da * dx[i__ + 1];
406 dx[i__ + 2] = *da * dx[i__ + 2];
407 dx[i__ + 3] = *da * dx[i__ + 3];
408 dx[i__ + 4] = *da * dx[i__ + 4];
414 /* dist.f -- translated by f2c (version 19991025).
415 You must link the resulting object file with the libraries:
416 -lf2c -lm (in that order)
419 static doublereal dist_(integer *n, doublereal *x, doublereal *y)
421 /* System generated locals */
423 doublereal ret_val, d__1;
425 /* Local variables */
427 doublereal scale, absxmy, sum;
431 /* Coded by Tom Rowan */
432 /* Department of Computer Sciences */
433 /* University of Texas at Austin */
435 /* dist calculates the distance between the points x,y. */
439 /* n - number of components */
441 /* x - point in n-space */
443 /* y - point in n-space */
445 /* local variables */
448 /* subroutines and functions */
452 /* ----------------------------------------------------------- */
454 /* Parameter adjustments */
459 absxmy = (d__1 = x[1] - y[1], abs(d__1));
461 sum = absxmy * absxmy;
468 for (i__ = 2; i__ <= i__1; ++i__) {
469 absxmy = (d__1 = x[i__] - y[i__], abs(d__1));
470 if (absxmy <= scale) {
471 /* Computing 2nd power */
472 d__1 = absxmy / scale;
475 /* Computing 2nd power */
476 d__1 = scale / absxmy;
477 sum = sum * (d__1 * d__1) + 1.;
482 ret_val = scale * sqrt(sum);
486 /* calcc.f -- translated by f2c (version 19991025).
487 You must link the resulting object file with the libraries:
488 -lf2c -lm (in that order)
491 /* Table of constant values */
493 static doublereal c_b3 = 0.;
494 static integer c__0 = 0;
495 static integer c__1 = 1;
496 static doublereal c_b7 = 1.;
498 static int calcc_(integer *ns, doublereal *s, integer *ih, integer *
499 inew, logical *updatc, doublereal *c__)
501 /* System generated locals */
502 integer s_dim1, s_offset, i__1;
505 /* Local variables */
508 /* Coded by Tom Rowan */
509 /* Department of Computer Sciences */
510 /* University of Texas at Austin */
512 /* calcc calculates the centroid of the simplex without the */
513 /* vertex with highest function value. */
517 /* ns - subspace dimension */
519 /* s - double precision work space of dimension .ge. */
520 /* ns*(ns+3) used to store simplex */
522 /* ih - index to vertex with highest function value */
524 /* inew - index to new point */
526 /* updatc - logical switch */
527 /* = .true. : update centroid */
528 /* = .false. : calculate centroid from scratch */
530 /* c - centroid of the simplex without vertex with */
531 /* highest function value */
535 /* c - new centroid */
537 /* local variables */
540 /* subroutines and functions */
544 /* ----------------------------------------------------------- */
546 /* Parameter adjustments */
549 s_offset = 1 + s_dim1 * 1;
558 for (i__ = 1; i__ <= i__1; ++i__) {
559 c__[i__] += (s[i__ + *inew * s_dim1] - s[i__ + *ih * s_dim1]) / *
564 dcopy_(ns, &c_b3, &c__0, &c__[1], &c__1);
566 for (j = 1; j <= i__1; ++j) {
568 daxpy_(ns, &c_b7, &s[j * s_dim1 + 1], &c__1, &c__[1], &c__1);
573 dscal_(ns, &d__1, &c__[1], &c__1);
578 /* order.f -- translated by f2c (version 19991025).
579 You must link the resulting object file with the libraries:
580 -lf2c -lm (in that order)
583 static int order_(integer *npts, doublereal *fs, integer *il,
584 integer *is, integer *ih)
586 /* System generated locals */
589 /* Local variables */
594 /* Coded by Tom Rowan */
595 /* Department of Computer Sciences */
596 /* University of Texas at Austin */
598 /* order determines the indices of the vertices with the */
599 /* lowest, second highest, and highest function values. */
603 /* npts - number of points in simplex */
605 /* fs - double precision vector of function values of */
608 /* il - index to vertex with lowest function value */
612 /* il - new index to vertex with lowest function value */
614 /* is - new index to vertex with second highest */
617 /* ih - new index to vertex with highest function value */
619 /* local variables */
622 /* subroutines and functions */
626 /* ----------------------------------------------------------- */
628 /* Parameter adjustments */
634 if (fs[j] >= fs[*il]) {
642 i__1 = il0 + *npts - 2;
643 for (i__ = il0 + 1; i__ <= i__1; ++i__) {
645 if (fs[j] >= fs[*ih]) {
648 } else if (fs[j] > fs[*is]) {
650 } else if (fs[j] < fs[*il]) {
658 /* partx.f -- translated by f2c (version 19991025).
659 You must link the resulting object file with the libraries:
660 -lf2c -lm (in that order)
663 /* Common Block Declarations */
666 doublereal alpha, beta, gamma, delta, psi, omega;
667 integer nsmin, nsmax, irepl, ifxsw;
668 doublereal bonus, fstop;
669 integer nfstop, nfxe;
670 doublereal fxstat[4], ftest;
671 logical minf, initx, newx;
674 #define usubc_1 usubc_
676 static int partx_(integer *n, integer *ip, doublereal *absdx,
677 integer *nsubs, integer *nsvals)
679 /* System generated locals */
682 /* Local variables */
683 static integer i__, nleft, nused;
684 static doublereal as1max, gapmax, asleft, as1, as2;
685 static integer ns1, ns2;
686 static doublereal gap;
690 /* Coded by Tom Rowan */
691 /* Department of Computer Sciences */
692 /* University of Texas at Austin */
694 /* partx partitions the vector x by grouping components of */
695 /* similar magnitude of change. */
699 /* n - number of components (problem dimension) */
701 /* ip - permutation vector */
703 /* absdx - vector of magnitude of change in x */
705 /* nsvals - integer array dimensioned .ge. int(n/nsmin) */
709 /* nsubs - number of subspaces */
711 /* nsvals - integer array of subspace dimensions */
717 /* local variables */
721 /* subroutines and functions */
725 /* ----------------------------------------------------------- */
727 /* Parameter adjustments */
738 for (i__ = 2; i__ <= i__1; ++i__) {
739 asleft += absdx[i__];
746 i__1 = usubc_1.nsmin - 1;
747 for (i__ = 1; i__ <= i__1; ++i__) {
748 as1 += absdx[ip[nused + i__]];
752 i__1 = min(usubc_1.nsmax,nleft);
753 for (ns1 = usubc_1.nsmin; ns1 <= i__1; ++ns1) {
754 as1 += absdx[ip[nused + ns1]];
757 if (ns2 >= ((ns2 - 1) / usubc_1.nsmax + 1) * usubc_1.nsmin) {
759 gap = as1 / ns1 - as2 / ns2;
762 nsvals[*nsubs] = ns1;
767 if (as1 / ns1 > gapmax) {
768 nsvals[*nsubs] = ns1;
774 nused += nsvals[*nsubs];
782 /* sortd.f -- translated by f2c (version 19991025).
783 You must link the resulting object file with the libraries:
784 -lf2c -lm (in that order)
787 static int sortd_(integer *n, doublereal *xkey, integer *ix)
789 /* System generated locals */
792 /* Local variables */
793 integer ixip1, i__, ilast, iswap, ifirst, ixi;
797 /* Coded by Tom Rowan */
798 /* Department of Computer Sciences */
799 /* University of Texas at Austin */
801 /* sortd uses the shakersort method to sort an array of keys */
802 /* in decreasing order. The sort is performed implicitly by */
803 /* modifying a vector of indices. */
805 /* For nearly sorted arrays, sortd requires O(n) comparisons. */
806 /* for completely unsorted arrays, sortd requires O(n**2) */
807 /* comparisons and will be inefficient unless n is small. */
811 /* n - number of components */
813 /* xkey - double precision vector of keys */
815 /* ix - integer vector of indices */
819 /* ix - indices satisfy xkey(ix(i)) .ge. xkey(ix(i+1)) */
820 /* for i = 1,...,n-1 */
822 /* local variables */
825 /* ----------------------------------------------------------- */
827 /* Parameter adjustments */
836 if (ifirst <= ilast) {
838 for (i__ = ifirst; i__ <= i__1; ++i__) {
841 if (xkey[ixi] < xkey[ixip1]) {
850 for (i__ = ilast; i__ >= i__1; --i__) {
853 if (xkey[ixi] < xkey[ixip1]) {
866 /* newpt.f -- translated by f2c (version 19991025).
867 You must link the resulting object file with the libraries:
868 -lf2c -lm (in that order)
871 static int newpt_(integer *ns, doublereal *coef, doublereal *xbase,
872 doublereal *xold, logical *new__, doublereal *xnew, logical *small)
874 /* System generated locals */
877 /* Local variables */
885 /* Coded by Tom Rowan */
886 /* Department of Computer Sciences */
887 /* University of Texas at Austin */
889 /* newpt performs reflections, expansions, contractions, and */
890 /* shrinkages (massive contractions) by computing: */
892 /* xbase + coef * (xbase - xold) */
894 /* The result is stored in xnew if new .eq. .true., */
895 /* in xold otherwise. */
897 /* use : coef .gt. 0 to reflect */
898 /* coef .lt. 0 to expand, contract, or shrink */
902 /* ns - number of components (subspace dimension) */
904 /* coef - one of four simplex method coefficients */
906 /* xbase - double precision ns-vector representing base */
909 /* xold - double precision ns-vector representing old */
912 /* new - logical switch */
913 /* = .true. : store result in xnew */
914 /* = .false. : store result in xold, xnew is not */
919 /* xold - unchanged if new .eq. .true., contains new */
920 /* point otherwise */
922 /* xnew - double precision ns-vector representing new */
923 /* point if new .eq. .true., not referenced */
926 /* small - logical flag */
927 /* = .true. : coincident points */
928 /* = .false. : otherwise */
930 /* local variables */
933 /* subroutines and functions */
937 /* ----------------------------------------------------------- */
939 /* Parameter adjustments */
949 for (i__ = 1; i__ <= i__1; ++i__) {
950 xnew[i__] = xbase[i__] + *coef * (xbase[i__] - xold[i__]);
951 eqbase = eqbase && xnew[i__] == xbase[i__];
952 eqold = eqold && xnew[i__] == xold[i__];
957 for (i__ = 1; i__ <= i__1; ++i__) {
959 xold[i__] = xbase[i__] + *coef * (xbase[i__] - xold[i__]);
960 eqbase = eqbase && xold[i__] == xbase[i__];
961 eqold = eqold && xold[i__] == xoldi;
965 *small = eqbase || eqold;
969 /* start.f -- translated by f2c (version 19991025).
970 You must link the resulting object file with the libraries:
971 -lf2c -lm (in that order)
974 static int start_(integer *n, doublereal *x, doublereal *step,
975 integer *ns, integer *ips, doublereal *s, logical *small)
977 /* System generated locals */
978 integer s_dim1, s_offset, i__1;
980 /* Local variables */
984 /* Coded by Tom Rowan */
985 /* Department of Computer Sciences */
986 /* University of Texas at Austin */
988 /* start creates the initial simplex for simplx minimization. */
992 /* n - problem dimension */
994 /* x - current best point */
996 /* step - stepsizes for corresponding components of x */
998 /* ns - subspace dimension */
1000 /* ips - permutation vector */
1005 /* s - first ns+1 columns contain initial simplex */
1007 /* small - logical flag */
1008 /* = .true. : coincident points */
1009 /* = .false. : otherwise */
1011 /* local variables */
1014 /* subroutines and functions */
1019 /* ----------------------------------------------------------- */
1021 /* Parameter adjustments */
1026 s_offset = 1 + s_dim1 * 1;
1031 for (i__ = 1; i__ <= i__1; ++i__) {
1032 s[i__ + s_dim1] = x[ips[i__]];
1036 for (j = 2; j <= i__1; ++j) {
1037 dcopy_(ns, &s[s_dim1 + 1], &c__1, &s[j * s_dim1 + 1], &c__1);
1038 s[j - 1 + j * s_dim1] = s[j - 1 + s_dim1] + step[ips[j - 1]];
1042 /* check for coincident points */
1045 for (j = 2; j <= i__1; ++j) {
1046 if (s[j - 1 + j * s_dim1] == s[j - 1 + s_dim1]) {
1054 /* coincident points */
1061 /* fstats.f -- translated by f2c (version 19991025).
1062 You must link the resulting object file with the libraries:
1063 -lf2c -lm (in that order)
1066 static int fstats_(doublereal *fx, integer *ifxwt, logical *reset)
1068 /* System generated locals */
1069 doublereal d__1, d__2, d__3;
1071 /* Local variables */
1072 static doublereal fscale;
1074 static doublereal f1sv;
1078 /* Coded by Tom Rowan */
1079 /* Department of Computer Sciences */
1080 /* University of Texas at Austin */
1082 /* fstats modifies the common /usubc/ variables nfxe,fxstat. */
1086 /* fx - most recent evaluation of f at best x */
1088 /* ifxwt - integer weight for fx */
1090 /* reset - logical switch */
1091 /* = .true. : initialize nfxe,fxstat */
1092 /* = .false. : update nfxe,fxstat */
1098 /* local variables */
1102 /* subroutines and functions */
1106 /* ----------------------------------------------------------- */
1109 usubc_1.nfxe = *ifxwt;
1110 usubc_1.fxstat[0] = *fx;
1111 usubc_1.fxstat[1] = *fx;
1112 usubc_1.fxstat[2] = *fx;
1113 usubc_1.fxstat[3] = 0.;
1116 f1sv = usubc_1.fxstat[0];
1117 usubc_1.nfxe += *ifxwt;
1118 usubc_1.fxstat[0] += *ifxwt * (*fx - usubc_1.fxstat[0]) /
1120 usubc_1.fxstat[1] = max(usubc_1.fxstat[1],*fx);
1121 usubc_1.fxstat[2] = min(usubc_1.fxstat[2],*fx);
1123 d__1 = abs(usubc_1.fxstat[1]), d__2 = abs(usubc_1.fxstat[2]), d__1 =
1125 fscale = max(d__1,1.);
1126 /* Computing 2nd power */
1127 d__1 = usubc_1.fxstat[3] / fscale;
1128 /* Computing 2nd power */
1129 d__2 = (usubc_1.fxstat[0] - f1sv) / fscale;
1130 /* Computing 2nd power */
1131 d__3 = (*fx - usubc_1.fxstat[0]) / fscale;
1132 usubc_1.fxstat[3] = fscale * sqrt(((nsv - 1) * (d__1 * d__1) + nsv * (
1133 d__2 * d__2) + *ifxwt * (d__3 * d__3)) / (usubc_1.nfxe - 1));
1138 /* evalf.f -- translated by f2c (version 19991025).
1139 You must link the resulting object file with the libraries:
1140 -lf2c -lm (in that order)
1143 /* Common Block Declarations */
1146 doublereal fbonus, sfstop, sfbest;
1150 #define isubc_1 isubc_
1152 static logical c_true = TRUE_;
1153 static logical c_false = FALSE_;
1155 static int evalf_(D_fp f,void*fdata, integer *ns, integer *ips, doublereal *xs,
1156 integer *n, doublereal *x, doublereal *sfx, integer *nfe)
1158 /* System generated locals */
1161 /* Local variables */
1163 static doublereal fx;
1164 static logical newbst;
1166 /* Coded by Tom Rowan */
1167 /* Department of Computer Sciences */
1168 /* University of Texas at Austin */
1170 /* evalf evaluates the function f at a point defined by x */
1171 /* with ns of its components replaced by those in xs. */
1175 /* f - user supplied function f(n,x) to be optimized */
1177 /* ns - subspace dimension */
1179 /* ips - permutation vector */
1181 /* xs - double precision ns-vector to be mapped to x */
1183 /* n - problem dimension */
1185 /* x - double precision n-vector */
1187 /* nfe - number of function evaluations */
1191 /* sfx - signed value of f evaluated at x */
1193 /* nfe - incremented number of function evaluations */
1201 /* local variables */
1205 /* subroutines and functions */
1208 /* ----------------------------------------------------------- */
1210 /* Parameter adjustments */
1217 for (i__ = 1; i__ <= i__1; ++i__) {
1218 x[ips[i__]] = xs[i__];
1221 usubc_1.newx = isubc_1.new__ || usubc_1.irepl != 2;
1222 fx = (*f)(*n, &x[1], fdata);
1223 if (usubc_1.irepl == 0) {
1229 } else if (isubc_1.new__) {
1232 newbst = fx < usubc_1.ftest;
1235 newbst = fx > usubc_1.ftest;
1237 if (usubc_1.initx || newbst) {
1238 if (usubc_1.irepl == 1) {
1239 fstats_(&fx, &c__1, &c_true);
1242 isubc_1.sfbest = *sfx;
1245 if (usubc_1.irepl == 1) {
1246 fstats_(&fx, &c__1, &c_false);
1247 fx = usubc_1.fxstat[usubc_1.ifxsw - 1];
1249 usubc_1.ftest = fx + isubc_1.fbonus * usubc_1.fxstat[3];
1251 *sfx = usubc_1.ftest;
1252 isubc_1.sfbest = fx;
1254 *sfx = -usubc_1.ftest;
1255 isubc_1.sfbest = -fx;
1262 /* simplx.f -- translated by f2c (version 19991025).
1263 You must link the resulting object file with the libraries:
1264 -lf2c -lm (in that order)
1267 static int simplx_(D_fp f, void *fdata, integer *n, doublereal *step, integer *
1268 ns, integer *ips, integer *maxnfe, logical *cmode, doublereal *x,
1269 doublereal *fx, integer *nfe, doublereal *s, doublereal *fs, integer *
1272 /* System generated locals */
1273 integer s_dim1, s_offset, i__1;
1274 doublereal d__1, d__2;
1276 /* Local variables */
1277 static integer inew;
1278 static integer npts;
1279 static integer i__, j;
1280 static integer icent;
1281 static logical small;
1282 static integer itemp;
1283 static doublereal fc, fe;
1284 static integer ih, il;
1285 static doublereal fr;
1287 static logical updatc;
1288 static doublereal dum, tol;
1292 /* Coded by Tom Rowan */
1293 /* Department of Computer Sciences */
1294 /* University of Texas at Austin */
1296 /* simplx uses the Nelder-Mead simplex method to minimize the */
1297 /* function f on a subspace. */
1301 /* f - function to be minimized, declared external in */
1302 /* calling routine */
1304 /* n - problem dimension */
1306 /* step - stepsizes for corresponding components of x */
1308 /* ns - subspace dimension */
1310 /* ips - permutation vector */
1312 /* maxnfe - maximum number of function evaluations */
1314 /* cmode - logical switch */
1315 /* = .true. : continuation of previous call */
1316 /* = .false. : first call */
1318 /* x - starting guess for minimum */
1320 /* fx - value of f at x */
1322 /* nfe - number of function evaluations */
1324 /* s - double precision work array of dimension .ge. */
1325 /* ns*(ns+3) used to store simplex */
1327 /* fs - double precision work array of dimension .ge. */
1328 /* ns+1 used to store function values of simplex */
1333 /* x - computed minimum */
1335 /* fx - value of f at x */
1337 /* nfe - incremented number of function evaluations */
1339 /* iflag - error flag */
1340 /* = -1 : maxnfe exceeded */
1341 /* = 0 : simplex reduced by factor of psi */
1342 /* = 1 : limit of machine precision */
1343 /* = 2 : reached fstop */
1351 /* local variables */
1355 /* subroutines and functions */
1360 /* ----------------------------------------------------------- */
1362 /* Parameter adjustments */
1367 s_offset = 1 + s_dim1 * 1;
1379 start_(n, &x[1], &step[1], ns, &ips[1], &s[s_offset], &small);
1384 if (usubc_1.irepl > 0) {
1385 isubc_1.new__ = FALSE_;
1386 evalf_((D_fp)f,fdata, ns, &ips[1], &s[s_dim1 + 1], n, &x[1], &fs[1], nfe);
1390 isubc_1.new__ = TRUE_;
1392 for (j = 2; j <= i__1; ++j) {
1393 evalf_((D_fp)f, fdata,ns, &ips[1], &s[j * s_dim1 + 1], n, &x[1], &fs[j],
1398 order_(&npts, &fs[1], &il, &is, &ih);
1399 tol = usubc_1.psi * dist_(ns, &s[ih * s_dim1 + 1], &s[il * s_dim1 + 1]);
1404 calcc_(ns, &s[s_offset], &ih, &inew, &updatc, &s[icent * s_dim1 + 1]);
1410 newpt_(ns, &usubc_1.alpha, &s[icent * s_dim1 + 1], &s[ih * s_dim1 + 1], &
1411 c_true, &s[itemp * s_dim1 + 1], &small);
1415 evalf_((D_fp)f,fdata, ns, &ips[1], &s[itemp * s_dim1 + 1], n, &x[1], &fr, nfe);
1420 d__1 = -usubc_1.gamma;
1421 newpt_(ns, &d__1, &s[icent * s_dim1 + 1], &s[itemp * s_dim1 + 1], &
1422 c_true, &s[ih * s_dim1 + 1], &small);
1426 evalf_((D_fp)f,fdata, ns, &ips[1], &s[ih * s_dim1 + 1], n, &x[1], &fe, nfe);
1430 dcopy_(ns, &s[itemp * s_dim1 + 1], &c__1, &s[ih * s_dim1 + 1], &
1434 } else if (fr < fs[is]) {
1436 /* accept reflected point */
1438 dcopy_(ns, &s[itemp * s_dim1 + 1], &c__1, &s[ih * s_dim1 + 1], &c__1);
1445 d__1 = -usubc_1.beta;
1446 newpt_(ns, &d__1, &s[icent * s_dim1 + 1], &s[ih * s_dim1 + 1], &
1447 c_true, &s[itemp * s_dim1 + 1], &small);
1449 d__1 = -usubc_1.beta;
1450 newpt_(ns, &d__1, &s[icent * s_dim1 + 1], &s[itemp * s_dim1 + 1],
1451 &c_false, &dum, &small);
1456 evalf_((D_fp)f,fdata, ns, &ips[1], &s[itemp * s_dim1 + 1], n, &x[1], &fc,
1459 d__1 = fr, d__2 = fs[ih];
1460 if (fc < min(d__1,d__2)) {
1461 dcopy_(ns, &s[itemp * s_dim1 + 1], &c__1, &s[ih * s_dim1 + 1], &
1466 /* shrink simplex */
1469 for (j = 1; j <= i__1; ++j) {
1471 d__1 = -usubc_1.delta;
1472 newpt_(ns, &d__1, &s[il * s_dim1 + 1], &s[j * s_dim1 + 1],
1473 &c_false, &dum, &small);
1477 evalf_((D_fp)f,fdata, ns, &ips[1], &s[j * s_dim1 + 1], n, &x[1],
1485 order_(&npts, &fs[1], &il, &is, &ih);
1487 /* check termination */
1490 if (usubc_1.irepl == 0) {
1493 *fx = isubc_1.sfbest;
1496 if (usubc_1.nfstop > 0 && *fx <= isubc_1.sfstop && usubc_1.nfxe >=
1499 } else if (*nfe >= *maxnfe) {
1501 } else if (dist_(ns, &s[ih * s_dim1 + 1], &s[il * s_dim1 + 1]) <= tol ||
1508 /* end main loop, return best point */
1511 for (i__ = 1; i__ <= i__1; ++i__) {
1512 x[ips[i__]] = s[i__ + il * s_dim1];
1518 /* subopt.f -- translated by f2c (version 19991025).
1519 You must link the resulting object file with the libraries:
1520 -lf2c -lm (in that order)
1523 static int subopt_(integer *n)
1527 /* Coded by Tom Rowan */
1528 /* Department of Computer Sciences */
1529 /* University of Texas at Austin */
1531 /* subopt sets options for subplx. */
1535 /* n - problem dimension */
1542 /* subroutines and functions */
1546 /* ----------------------------------------------------------- */
1548 /* *********************************************************** */
1549 /* simplex method strategy parameters */
1550 /* *********************************************************** */
1552 /* alpha - reflection coefficient */
1557 /* beta - contraction coefficient */
1558 /* 0 .lt. beta .lt. 1 */
1562 /* gamma - expansion coefficient */
1567 /* delta - shrinkage (massive contraction) coefficient */
1568 /* 0 .lt. delta .lt. 1 */
1572 /* *********************************************************** */
1573 /* subplex method strategy parameters */
1574 /* *********************************************************** */
1576 /* psi - simplex reduction coefficient */
1577 /* 0 .lt. psi .lt. 1 */
1581 /* omega - step reduction coefficient */
1582 /* 0 .lt. omega .lt. 1 */
1586 /* nsmin and nsmax specify a range of subspace dimensions. */
1587 /* In addition to satisfying 1 .le. nsmin .le. nsmax .le. n, */
1588 /* nsmin and nsmax must be chosen so that n can be expressed */
1589 /* as a sum of positive integers where each of these integers */
1590 /* ns(i) satisfies nsmin .le. ns(i) .ge. nsmax. */
1592 /* nsmin*ceil(n/nsmax) .le. n must be true. */
1594 /* nsmin - subspace dimension minimum */
1596 usubc_1.nsmin = min(2,*n);
1598 /* nsmax - subspace dimension maximum */
1600 usubc_1.nsmax = min(5,*n);
1602 /* *********************************************************** */
1603 /* subplex method special cases */
1604 /* *********************************************************** */
1605 /* nelder-mead simplex method with periodic restarts */
1606 /* nsmin = nsmax = n */
1607 /* *********************************************************** */
1608 /* nelder-mead simplex method */
1609 /* nsmin = nsmax = n, psi = small positive */
1610 /* *********************************************************** */
1612 /* irepl, ifxsw, and bonus deal with measurement replication. */
1613 /* Objective functions subject to large amounts of noise can */
1614 /* cause an optimization method to halt at a false optimum. */
1615 /* An expensive solution to this problem is to evaluate f */
1616 /* several times at each point and return the average (or max */
1617 /* or min) of these trials as the function value. subplx */
1618 /* performs measurement replication only at the current best */
1619 /* point. The longer a point is retained as best, the more */
1620 /* accurate its function value becomes. */
1622 /* The common variable nfxe contains the number of function */
1623 /* evaluations at the current best point. fxstat contains the */
1624 /* mean, max, min, and standard deviation of these trials. */
1626 /* irepl - measurement replication switch */
1627 /* irepl = 0, 1, or 2 */
1628 /* = 0 : no measurement replication */
1629 /* = 1 : subplx performs measurement replication */
1630 /* = 2 : user performs measurement replication */
1631 /* (This is useful when optimizing on the mean, */
1632 /* max, or min of trials is insufficient. Common */
1633 /* variable initx is true for first function */
1634 /* evaluation. newx is true for first trial at */
1635 /* this point. The user uses subroutine fstats */
1636 /* within his objective function to maintain */
1637 /* fxstat. By monitoring newx, the user can tell */
1638 /* whether to return the function evaluation */
1639 /* (newx = .true.) or to use the new function */
1640 /* evaluation to refine the function evaluation */
1641 /* of the current best point (newx = .false.). */
1642 /* The common variable ftest gives the function */
1643 /* value that a new point must beat to be */
1644 /* considered the new best point.) */
1648 /* ifxsw - measurement replication optimization switch */
1649 /* ifxsw = 1, 2, or 3 */
1650 /* = 1 : retain mean of trials as best function value */
1651 /* = 2 : retain max */
1652 /* = 3 : retain min */
1656 /* Since the current best point will also be the most */
1657 /* accurately evaluated point whenever irepl .gt. 0, a bonus */
1658 /* should be added to the function value of the best point */
1659 /* so that the best point is not replaced by a new point */
1660 /* that only appears better because of noise. */
1661 /* subplx uses bonus to determine how many multiples of */
1662 /* fxstat(4) should be added as a bonus to the function */
1663 /* evaluation. (The bonus is adjusted automatically by */
1664 /* subplx when ifxsw or minf is changed.) */
1666 /* bonus - measurement replication bonus coefficient */
1667 /* bonus .ge. 0 (normally, bonus = 0 or 1) */
1668 /* = 0 : bonus not used */
1669 /* = 1 : bonus used */
1673 /* nfstop = 0 : f(x) is not tested against fstop */
1674 /* = 1 : if f(x) has reached fstop, subplx returns */
1676 /* = 2 : (only valid when irepl .gt. 0) */
1677 /* if f(x) has reached fstop and */
1678 /* nfxe .gt. nfstop, subplx returns iflag = 2 */
1682 /* fstop - f target value */
1683 /* Its usage is determined by the value of nfstop. */
1685 /* minf - logical switch */
1686 /* = .true. : subplx performs minimization */
1687 /* = .false. : subplx performs maximization */
1689 usubc_1.minf = TRUE_;
1693 /* setstp.f -- translated by f2c (version 19991025).
1694 You must link the resulting object file with the libraries:
1695 -lf2c -lm (in that order)
1698 static double d_sign(doublereal *x, doublereal *y)
1700 return copysign(*x, *y);
1703 static int setstp_(integer *nsubs, integer *n, doublereal *deltax,
1706 /* System generated locals */
1708 doublereal d__1, d__2, d__3;
1710 /* Builtin functions */
1711 /* double d_sign(doublereal *, doublereal *); */
1713 /* Local variables */
1715 static doublereal stpfac;
1719 /* Coded by Tom Rowan */
1720 /* Department of Computer Sciences */
1721 /* University of Texas at Austin */
1723 /* setstp sets the stepsizes for the corresponding components */
1724 /* of the solution vector. */
1728 /* nsubs - number of subspaces */
1730 /* n - number of components (problem dimension) */
1732 /* deltax - vector of change in solution vector */
1734 /* step - stepsizes for corresponding components of */
1735 /* solution vector */
1739 /* step - new stepsizes */
1745 /* local variables */
1749 /* subroutines and functions */
1754 /* ----------------------------------------------------------- */
1758 /* Parameter adjustments */
1766 d__3 = dasum_(n, &deltax[1], &c__1) / dasum_(n, &step[1], &c__1);
1767 d__1 = max(d__3,usubc_1.omega), d__2 = 1. / usubc_1.omega;
1768 stpfac = min(d__1,d__2);
1770 stpfac = usubc_1.psi;
1772 dscal_(n, &stpfac, &step[1], &c__1);
1774 /* reorient simplex */
1777 for (i__ = 1; i__ <= i__1; ++i__) {
1778 if (deltax[i__] != 0.) {
1779 step[i__] = d_sign(&step[i__], &deltax[i__]);
1781 step[i__] = -step[i__];
1788 /* subplx.f -- translated by f2c (version 19991025).
1789 You must link the resulting object file with the libraries:
1790 -lf2c -lm (in that order)
1793 static int subplx_(D_fp f, void *fdata, integer *n, doublereal *tol, integer *
1794 maxnfe, integer *mode, const doublereal *scale, doublereal *x, doublereal *
1795 fx, integer *nfe, doublereal *work, integer *iwork, integer *iflag)
1797 /* Initialized data */
1799 static doublereal bnsfac[6] /* was [3][2] */ = { -1.,-2.,0.,1.,0.,2. };
1801 /* System generated locals */
1803 doublereal d__1, d__2, d__3, d__4, d__5, d__6;
1805 /* Local variables */
1807 static logical cmode;
1808 static integer istep;
1809 static doublereal xpscl;
1810 static integer nsubs, ipptr;
1811 static integer isptr;
1812 static integer ns, insfnl, ifsptr;
1813 static integer insptr;
1814 static integer istptr;
1815 static doublereal scl, dum;
1817 static doublereal sfx;
1821 /* Coded by Tom Rowan */
1822 /* Department of Computer Sciences */
1823 /* University of Texas at Austin */
1825 /* subplx uses the subplex method to solve unconstrained */
1826 /* optimization problems. The method is well suited for */
1827 /* optimizing objective functions that are noisy or are */
1828 /* discontinuous at the solution. */
1830 /* subplx sets default optimization options by calling the */
1831 /* subroutine subopt. The user can override these defaults */
1832 /* by calling subopt prior to calling subplx, changing the */
1833 /* appropriate common variables, and setting the value of */
1834 /* mode as indicated below. */
1836 /* By default, subplx performs minimization. */
1840 /* f - user supplied function f(n,x) to be optimized, */
1841 /* declared external in calling routine */
1843 /* n - problem dimension */
1845 /* tol - relative error tolerance for x (tol .ge. 0.) */
1847 /* maxnfe - maximum number of function evaluations */
1849 /* mode - integer mode switch with binary expansion */
1850 /* (bit 1) (bit 0) : */
1851 /* bit 0 = 0 : first call to subplx */
1852 /* = 1 : continuation of previous call */
1853 /* bit 1 = 0 : use default options */
1854 /* = 1 : user set options */
1856 /* scale - scale and initial stepsizes for corresponding */
1857 /* components of x */
1858 /* (If scale(1) .lt. 0., */
1859 /* abs(scale(1)) is used for all components of x, */
1860 /* and scale(2),...,scale(n) are not referenced.) */
1862 /* x - starting guess for optimum */
1864 /* work - double precision work array of dimension .ge. */
1865 /* 2*n + nsmax*(nsmax+4) + 1 */
1866 /* (nsmax is set in subroutine subopt. */
1867 /* default: nsmax = min(5,n)) */
1869 /* iwork - integer work array of dimension .ge. */
1870 /* n + int(n/nsmin) */
1871 /* (nsmin is set in subroutine subopt. */
1872 /* default: nsmin = min(2,n)) */
1876 /* x - computed optimum */
1878 /* fx - value of f at x */
1880 /* nfe - number of function evaluations */
1882 /* iflag - error flag */
1883 /* = -2 : invalid input */
1884 /* = -1 : maxnfe exceeded */
1885 /* = 0 : tol satisfied */
1886 /* = 1 : limit of machine precision */
1887 /* = 2 : fstop reached (fstop usage is determined */
1888 /* by values of options minf, nfstop, and */
1889 /* irepl. default: f(x) not tested against */
1891 /* iflag should not be reset between calls to */
1900 /* local variables */
1904 /* subroutines and functions */
1911 /* Parameter adjustments */
1918 /* ----------------------------------------------------------- */
1920 if (*mode % 2 == 0) {
1922 /* first call, check input */
1933 if (scale[1] >= 0.) {
1935 for (i__ = 1; i__ <= i__1; ++i__) {
1936 xpscl = x[i__] + scale[i__];
1937 if (xpscl == x[i__]) {
1943 scl = abs(scale[1]);
1945 for (i__ = 1; i__ <= i__1; ++i__) {
1946 xpscl = x[i__] + scl;
1947 if (xpscl == x[i__]) {
1953 if (*mode / 2 % 2 == 0) {
1956 if (usubc_1.alpha <= 0.) {
1959 if (usubc_1.beta <= 0. || usubc_1.beta >= 1.) {
1962 if (usubc_1.gamma <= 1.) {
1965 if (usubc_1.delta <= 0. || usubc_1.delta >= 1.) {
1968 if (usubc_1.psi <= 0. || usubc_1.psi >= 1.) {
1971 if (usubc_1.omega <= 0. || usubc_1.omega >= 1.) {
1974 if (usubc_1.nsmin < 1 || usubc_1.nsmax < usubc_1.nsmin || *n <
1978 if (*n < ((*n - 1) / usubc_1.nsmax + 1) * usubc_1.nsmin) {
1981 if (usubc_1.irepl < 0 || usubc_1.irepl > 2) {
1984 if (usubc_1.ifxsw < 1 || usubc_1.ifxsw > 3) {
1987 if (usubc_1.bonus < 0.) {
1990 if (usubc_1.nfstop < 0) {
1995 /* initialization */
1998 isptr = istptr + *n;
1999 ifsptr = isptr + usubc_1.nsmax * (usubc_1.nsmax + 3);
2001 if (scale[1] > 0.) {
2002 dcopy_(n, &scale[1], &c__1, &work[1], &c__1);
2003 dcopy_(n, &scale[1], &c__1, &work[istptr], &c__1);
2005 dcopy_(n, &scl, &c__0, &work[1], &c__1);
2006 dcopy_(n, &scl, &c__0, &work[istptr], &c__1);
2009 for (i__ = 1; i__ <= i__1; ++i__) {
2015 if (usubc_1.irepl == 0) {
2016 isubc_1.fbonus = 0.;
2017 } else if (usubc_1.minf) {
2018 isubc_1.fbonus = bnsfac[usubc_1.ifxsw - 1] * usubc_1.bonus;
2020 isubc_1.fbonus = bnsfac[usubc_1.ifxsw + 2] * usubc_1.bonus;
2022 if (usubc_1.nfstop == 0) {
2023 isubc_1.sfstop = 0.;
2024 } else if (usubc_1.minf) {
2025 isubc_1.sfstop = usubc_1.fstop;
2027 isubc_1.sfstop = -usubc_1.fstop;
2031 isubc_1.new__ = TRUE_;
2032 usubc_1.initx = TRUE_;
2033 evalf_((D_fp)f, fdata, &c__0, &iwork[1], &dum, n, &x[1], &sfx, nfe);
2034 usubc_1.initx = FALSE_;
2037 /* continuation of previous call */
2041 isubc_1.sfstop = usubc_1.fstop;
2043 isubc_1.sfstop = -usubc_1.fstop;
2047 } else if (*iflag == -1) {
2050 } else if (*iflag == 0) {
2062 for (i__ = 1; i__ <= i__1; ++i__) {
2063 work[i__] = (d__1 = work[i__], abs(d__1));
2066 sortd_(n, &work[1], &iwork[1]);
2067 partx_(n, &iwork[1], &work[1], &nsubs, &iwork[insptr]);
2068 dcopy_(n, &x[1], &c__1, &work[1], &c__1);
2070 insfnl = insptr + nsubs - 1;
2078 simplx_((D_fp)f, fdata, n, &work[istptr], &ns, &iwork[ipptr], maxnfe, &cmode, &x[
2079 1], &sfx, nfe, &work[isptr], &work[ifsptr], iflag);
2090 /* end simplex loop */
2093 for (i__ = 1; i__ <= i__1; ++i__) {
2094 work[i__] = x[i__] - work[i__];
2098 /* check termination */
2103 for (i__ = 1; i__ <= i__1; ++i__) {
2105 d__4 = (d__2 = work[i__], abs(d__2)), d__5 = (d__1 = work[istep], abs(
2106 d__1)) * usubc_1.psi;
2108 d__6 = (d__3 = x[i__], abs(d__3));
2109 if (max(d__4,d__5) / max(d__6,1.) > *tol) {
2110 setstp_(&nsubs, n, &work[1], &work[istptr]);
2117 /* end subplex loop */
2135 /****************************************************************************/
2136 /****************************************************************************/
2138 /* front-end for subplex routines */
2140 /* Wrapper around f2c'ed subplx_ routine, for multidimensinal
2141 unconstrained optimization:
2145 f: function f(n,x,fdata) to be optimized
2146 n: problem dimension
2147 fmin: (output) value of f at minimum
2148 x[n]: (input) starting guess position, (output) computed minimum
2149 fdata: data pointer passed to f
2150 tol: relative error tolerance for x
2151 maxnfe: maximum number of function evaluations
2152 fmin_max, use_fmin_max: if use_fmin_max, stop when f <= fmin_max
2153 scale[n]: (input) scale & initial stepsizes for components of x
2154 (if *scale < 0, |*scale| is used for all components)
2157 = -2 : invalid input
2158 = -1 : maxnfe exceeded
2160 = 1 : limit of machine precision
2161 = 2 : fstop reached (fstop usage is determined by values of
2162 options minf, nfstop, and irepl. default: f(x) not
2163 tested against fstop)
2165 int subplex(subplex_func f, double *fmin, double *x, int n, void *fdata,
2166 double tol, int maxnfe,
2167 double fmin_max, int use_fmin_max,
2168 const double *scale)
2170 int mode = 0, *iwork, nsmax, nsmin, errflag, nfe;
2175 work = (double*) malloc(sizeof(double) * (2*n + nsmax*(nsmax+4) + 1));
2176 iwork = (int*) malloc(sizeof(int) * (n + n/nsmin + 1));
2177 if (!work || !iwork) {
2178 fprintf(stderr, "subplex: error, out of memory!\n");
2182 if (use_fmin_max) { /* stop when fmin_max is reached */
2185 usubc_1.fstop = fmin_max;
2189 subplx_(f,fdata, &n,
2190 &tol, &maxnfe, &mode,
2193 work, iwork, &errflag);