8 double const beta = 0.5;
12 double nelder_mead(
func_t f,
int n,
double * x,
double const * steps,
double mxrngy,
double mxrngx,
int mxiter)
15 auto s =
new double [n*(n+1)];
16 for (
int i = 0; i<n; i++) s[i] = x[i];
17 for (
int i = 0; i<n; i++) {
19 for (
int j = 0; j<n; j++) d[j] = x[j];
23 auto y =
new double [n+1];
24 for (
int i = 0; i<=n; i ++) y[i] = (*f)(s+i*n);
27 auto xc =
new double [n];
28 auto xn =
new double [n];
29 auto x2 =
new double [n];
46 for (
int i = 2; i<=n; i++) {
47 if (y[i]<y[li]) li = i;
48 else if (y[i]>y[hi]) {
52 else if (y[i]>y[ni]) ni = i;
56 if (y[hi]-y[li]<mxrngy) {
58 for (
int i = 0; i<n; i++) {
62 for (
int j = 0; j<=n; j++) {
74 for (
int i = 0; i<n; i++) {
76 for (
int j = 0; j<=n; j++) xc[i] += s[j*n+i];
77 xc[i] = (xc[i]-sh[i])/n;
78 xn[i] = xc[i]+(xc[i]-sh[i])*
alpha;
84 for (
int i = 0; i<n; i++) x2[i] = xc[i]+(xc[i]-sh[i])*
gamma;
88 for (
int i = 0; i<n; i++) sh[i] = x2[i];
92 for (
int i = 0; i<n; i++) sh[i] = xn[i];
97 for (
int i = 0; i<n; i++) sh[i] = xn[i];
103 for (
int i = 0; i<n; i++) x2[i] = xc[i]+(xn[i]-xc[i])*
beta;
104 double y2 = (*f)(x2);
107 for (
int i = 0; i<n; i++) sh[i] = x2[i];
111 for (
int i = 0; i<n; i++) sh[i] = xn[i];
116 for (
int i = 0; i<n; i++) xn[i] = xc[i]+(sh[i]-xc[i])*
beta;
120 for (
int i = 0; i<n; i++) sh[i] = xn[i];
124 for (
int i = 0; i<=n; i++) {
127 for (
int j = 0; j<n; j++) d[j] = sl[j]+(d[j]-sl[j])*
delta;
134 }
while (iter<mxiter);
135 for (
int i = 0; i<n; i++) x[i] = s[li*n+i];
double const alpha
reflection coefficient
double nelder_mead(func_t f, int n, double *x, double const *steps, double mxrngy, double mxrngx, int mxiter)
Perform minimization with Nelder–Mead method.
double() func_t(double const *)
Function type with a number of parameters.
double const gamma
contraction coefficient
double const beta
expansion coefficient
double const delta
shrinking coefficient
Nelder–Mead method for function minimization.