TPCCLIB
Loading...
Searching...
No Matches
powell.c File Reference

Powell function minimization routines. More...

#include "libtpcmodel.h"

Go to the source code of this file.

Functions

int powell (double *p, double *delta, int parNr, double ftol, int *iterNr, double *fret, double(*_fun)(int, double *, void *), void *fundata, int verbose)
 

Variables

int POWELL_LINMIN_MAXIT =100
 

Detailed Description

Powell function minimization routines.

Author
Vesa Oikonen

Based on Numerical recipes in C (Press et al.).

Definition in file powell.c.

Function Documentation

◆ powell()

int powell ( double * p,
double * delta,
int parNr,
double ftol,
int * iterNr,
double * fret,
double(* _fun )(int, double *, void *),
void * fundata,
int verbose )

Powell function minimization routine.

Returns
Returns 0, if successful, 1 if required tolerance was not reached, 2 if initial guess does not give finite function value, 3 if final function value is NaN or infinite, and >3 in case of another error.
See also
simplex, tgo, bobyqa, nlopt1D
Parameters
pInitial guess and final set of parameters
deltaInitial changes for parameters, ==0 if fixed
parNrNr of parameters
ftolFractional tolerance (for WSS); 0<ftol<1
iterNrMax nr of iterations, and nr of required iters
fretFunction return value (WSS) at minimum
_funFunction to minimize (must return the WSS)
fundataPointer to data which is passed on to the function; NULL if not needed
verboseVerbose level; if zero, then nothing is printed into stdout or stderr

Definition at line 43 of file powell.c.

62 {
63 int pbIterNr;
64 int i, j, ibig, iterMax, fixed[MAX_PARAMETERS];
65 double xi[MAX_PARAMETERS][MAX_PARAMETERS]; /* Matrix for directions */
66 double pt[MAX_PARAMETERS], ptt[MAX_PARAMETERS], xit[MAX_PARAMETERS];
67 double del, fp, fptt, t;
68 double origp[MAX_PARAMETERS];
69 int ftol_reached=0;
70
71
72 if(verbose>0) printf("in powell(,,%d,%g,%d,,)\n", parNr, ftol, *iterNr);
73 if(verbose>1) {
74 printf("Initial parameter guesses and deltas:\n");
75 for(i=0; i<parNr; i++) printf(" %g %g\n", p[i], delta[i]);
76 }
77 *fret=nan("");
78 if(p==NULL) return(11);
79 if(delta==NULL) return(12);
80 if(parNr<1) return(21);
81 if(ftol<=0.0) return(22);
82 if(ftol>=1.0) return(23);
83 if((*iterNr)<1) return(24);
84
85 /* SetUp */
86 _powellFunc=_fun;
87 _powellFuncData=fundata;
88 iterMax=*iterNr; /* save the max nr of iterations */
89 _powell_ncom=parNr;
90 /* Function value at initial point */
91 _powell_func_calls=1;
92 *fret=(*_powellFunc)(parNr, p, _powellFuncData);
93 if(verbose>10) printf("initial point fret=%g\n", *fret);
94 if(!isfinite(*fret)) {
95 if(verbose>0) printf("in powell(): objf failed at initial point.\n");
96 *fret=nan(""); return(2);
97 }
98 /* Save the initial point (pt[] will be changed later) */
99 for(j=0; j<parNr; j++) origp[j]=pt[j]=p[j];
100 /* Check which parameters are fixed */
101 for(i=0; i<parNr; i++) if(fabs(delta[i])<1.0e-20) fixed[i]=1; else fixed[i]=0;
102
103 /* Initiate matrix for directions */
104 for(i=0; i<parNr; i++)
105 for(j=0; j<parNr; j++)
106 if(i==j) xi[i][j]=delta[i]; else xi[i][j]=0.0;
107
108 /* Iterate */
109 for(*iterNr=1; ; (*iterNr)++) {
110 if(verbose>2) printf(" iteration %d\n", *iterNr);
111 fp=*fret; ibig=0; del=0.0; /* largest function decrease */
112
113 /* In each iteration, loop over all directions in the set */
114 for(i=0; i<parNr; i++) {
115 if(fixed[i]) continue; /* do nothing with fixed parameters */
116 for(j=0; j<parNr; j++) if(fixed[j]) xit[j]=0.0; else xit[j]=xi[j][i];
117 fptt=*fret;
118 /* minimize along direction xit */
119 pbIterNr=POWELL_LINMIN_MAXIT;
120 _powell_linmin(p, xit, parNr, fret, &pbIterNr);
121 if(verbose>3) printf("iterNr in _powell_linmin() with p%d: %d\n",
122 i, pbIterNr);
123 if(fabs(fptt-(*fret))>del) {del=fabs(fptt-(*fret)); ibig=i;}
124 }
125 if(verbose>20) printf("fret=%g fp=%g\n", *fret, fp);
126
127 /* Check if done */
128#if(0)
129 if(2.0*fabs(fp-(*fret)) <= ftol*(fabs(fp)+fabs(*fret))) break;
130#else
131 if(2.0*fabs(fp-(*fret)) <= ftol*(fabs(fp)+fabs(*fret))) {
132 if(ftol_reached>0 || (*iterNr)>=iterMax) break; else ftol_reached++;
133 } else ftol_reached=0;
134#endif
135 if((*iterNr)>=iterMax) {
136 if(verbose>0) printf("max iterations nr exceeded in powell().\n");
137 break;
138 }
139 /* Construct the extrapolated point and the average direction moved */
140 for(j=0; j<parNr; j++) {
141 ptt[j]=2.0*p[j]-pt[j]; xit[j]=p[j]-pt[j];
142 pt[j]=p[j]; /* save the old starting point */
143 }
144 fptt=(*_powellFunc)(parNr, ptt, _powellFuncData); _powell_func_calls++;
145 if(fptt<fp) {
146 t=2.0*(fp-2.0*(*fret)+fptt)*_powell_sqr(fp-(*fret)-del)-del*_powell_sqr(fp-fptt);
147 if(t<0.0) {
148 pbIterNr=POWELL_LINMIN_MAXIT;
149 _powell_linmin(p, xit, parNr, fret, &pbIterNr);
150 if(verbose>3) printf("iterNr in _powell_linmin(): %d\n", pbIterNr);
151 for(j=0; j<parNr; j++) {
152 xi[j][ibig]=xi[j][parNr-1]; xi[j][parNr-1]=xit[j];}
153 }
154 }
155 } /* next iteration */
156 if(verbose>1) {
157 printf("iterNr := %d\n", *iterNr);
158 printf("nr of function calls := %d\n", _powell_func_calls);
159 }
160
161 if(isnan(*fret) || !isfinite(*fret)) {
162 if(verbose>10) printf("powell() fails and returns the initial point.\n");
163 if(verbose>11) {
164 if(isnan(*fret)) printf(" fret := NaN\n");
165 if(isfinite(*fret)) printf(" fret := overflow/underflow\n");
166 }
167 // if failed, then return initial guess
168 for(j=0; j<parNr; j++) p[j]=origp[j];
169 // and call function again so that any data saved there is correct
170 *fret=(*_powellFunc)(parNr, p, _powellFuncData);
171 return(3);
172 }
173 // and call function again so that any data saved there is correct
174 *fret=(*_powellFunc)(parNr, p, _powellFuncData);
175 if((*iterNr)>=iterMax) return(1);
176 if(verbose>0) printf("out of powell() in good order.\n");
177 return(0);
178}
#define MAX_PARAMETERS
Definition libtpcmodel.h:31
int POWELL_LINMIN_MAXIT
Definition powell.c:11

Referenced by bootstrap(), and tgo().

Variable Documentation

◆ POWELL_LINMIN_MAXIT

int POWELL_LINMIN_MAXIT =100

Max iterations for linear minimization inside powell()

Definition at line 11 of file powell.c.

Referenced by powell(), and tgo().