15 for (j=1;j<=ndim;j++) {\
16 for (sum=0.0,i=1;i<=mpts;i++) sum += p[i][j];\
22 #define SWAP(a,b) {swap=(a);(a)=(b);(b)=swap;}
39 int ndim,
double ftol,
int nfunk)
43 int i,ihi,ilo,inhi,j,mpts=ndim+1;
44 double rtol,
sum,swap,ysave,ytry;
51 ihi = y[1]>y[2] ? (inhi=2,1) : (inhi=1,2);
52 for (i=1;i<=mpts;i++) {
53 if (y[i] <= y[ilo]) ilo=i;
57 }
else if (y[i] > y[inhi] && i != ihi) inhi=i;
59 rtol=2.0*
fabs(y[ihi]-y[ilo])/(
fabs(y[ihi])+
fabs(y[ilo]));
62 for (i=1;i<=ndim;i++)
SWAP(p[1][i],p[ilo][i])
67 cerr <<
"NMAX exceeded" <<
endl;
70 ytry=
amxxx(p,y,psum,ndim,ihi,-1.0);
72 ytry=
amxxx(p,y,psum,ndim,ihi,2.0);
73 else if (ytry >= y[inhi]) {
75 ytry=
amxxx(p,y,psum,ndim,ihi,0.5);
77 for (i=1;i<=mpts;i++) {
80 p[i][j]=psum[j]=0.5*(p[i][j]+p[ilo][j]);
108 const dvector& _psum,
int ndim,
int ihi,
double fac)
114 double fac1,fac2,ytry;
119 for (j=1;j<=ndim;j++) ptry[j]=psum[j]*fac1-p[ihi][j]*fac2;
130 for (j=1;j<=ndim;j++) {
131 psum[j] += ptry[j]-p[ihi][j];
151 double *ynewlo,
double reqmin,
double delta,
int *icount,
int *numres,
290 dn = ( double ) ( n );
292 dnn = ( double ) ( nn );
301 for ( i = 0; i < n; i++ )
314 *icount = *icount + 1;
316 for ( j = 0; j < n; j++ )
319 start[j] = start[j] + step[j] * del;
320 for ( i = 0; i < n; i++ )
333 *icount = *icount + 1;
343 for ( i = 1; i < nn; i++ )
355 if ( kcount <= *icount )
362 for ( i = 1; i < nn; i++ )
364 if ( *ynewlo < y[i] )
373 for ( i = 0; i < n; i++ )
376 for ( j = 0; j < nn; j++ )
385 for ( i = 0; i < n; i++ )
387 pstar[i] = pbar[i] + rcoeff * ( pbar[i] - p[i+ihi*n] );
398 *icount = *icount + 1;
403 for ( i = 0; i < n; i++ )
405 p2star[i] = pbar[i] + ecoeff * ( pstar[i] - pbar[i] );
416 *icount = *icount + 1;
419 if ( ystar < y2star )
421 for ( i = 0; i < n; i++ )
423 p[i+ihi*n] = pstar[i];
431 for ( i = 0; i < n; i++ )
433 p[i+ihi*n] = p2star[i];
443 for ( i = 0; i < nn; i++ )
453 for ( i = 0; i < n; i++ )
455 p[i+ihi*n] = pstar[i];
463 for ( i = 0; i < n; i++ )
465 p2star[i] = pbar[i] + ccoeff * ( p[i+ihi*n] - pbar[i] );
476 *icount = *icount + 1;
479 if ( y[ihi] < y2star )
481 for ( j = 0; j < nn; j++ )
483 for ( i = 0; i < n; i++ )
485 p[i+j*n] = ( p[i+j*n] + p[i+ilo*n] ) * 0.5;
497 *icount = *icount + 1;
502 for ( i = 1; i < nn; i++ )
516 for ( i = 0; i < n; i++ )
518 p[i+ihi*n] = p2star[i];
527 for ( i = 0; i < n; i++ )
529 p2star[i] = pbar[i] + ccoeff * ( pstar[i] - pbar[i] );
540 *icount = *icount + 1;
543 if ( y2star <= ystar )
545 for ( i = 0; i < n; i++ )
547 p[i+ihi*n] = p2star[i];
553 for ( i = 0; i < n; i++ )
555 p[i+ihi*n] = pstar[i];
576 if ( *icount <= kcount )
581 for ( i = 0; i < nn; i++ )
588 for ( i = 0; i < nn; i++ )
590 z = z +
pow ( y[i] - x, 2 );
601 for ( i = 0; i < n; i++ )
603 xmin[i] = p[i+ilo*n];
607 if ( kcount < *icount )
615 for ( i = 0; i < n; i++ )
618 xmin[i] = xmin[i] + del;
628 *icount = *icount + 1;
634 xmin[i] = xmin[i] - del - del;
644 *icount = *icount + 1;
650 xmin[i] = xmin[i] + del;
659 for ( i = 0; i < n; i++ )
664 *numres = *numres + 1;
Vector of double precision numbers.
int indexmin() const
Get minimum valid index.
void neldmead(int n, dvector &_start, dvector &_xmin, double *ynewlo, double reqmin, double delta, int *icount, int *numres, int *ifault)
Nelder-Mead simplex alogrithm.
double sum(const d3_array &darray)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
df1_two_variable fabs(const df1_two_variable &x)
static dvariable reset(const dvar_vector &x)
void adamoeba(const dmatrix &p, const dvector &y, int ndim, double ftol, int maxfn)
Nelder-Mead simplex alogrithm.
prnstream & endl(prnstream &)
static objective_function_value * pobjfun
Description not yet available.
Description not yet available.
dvector & shift(int min)
Shift valid range of subscripts.
double amxxx(const dmatrix &p, const dvector &y, const dvector &psum, int ndim, int ihi, double fac)
Description not yet available.
#define SWAP(a, b)
Reverses a and b.
dvector value(const df1_one_vector &v)
virtual void userfunction(void)=0
#define GET_PSUM
Computes psum.
Fundamental data type for reverse mode automatic differentiation.
d3_array pow(const d3_array &m, int e)
Description not yet available.