83#ifndef DT_MATH_NELDER_MEAD_SIMPLEX_H
84#define DT_MATH_NELDER_MEAD_SIMPLEX_H
103 int maxiter,
void (*constrain)(
double[],
int n),
void *
params)
110 int i = 0, j = 0,
m,
row;
125 double fsum, favg, s, cent;
130 v = (
double **)malloc(
sizeof(
double *) * (
n + 1));
131 f = (
double *)malloc(
sizeof(
double) * (
n + 1));
132 vr = (
double *)malloc(
sizeof(
double) *
n);
133 ve = (
double *)malloc(
sizeof(
double) *
n);
134 vc = (
double *)malloc(
sizeof(
double) *
n);
135 vm = (
double *)malloc(
sizeof(
double) *
n);
138 for(
i = 0;
i <=
n;
i++)
140 v[
i] = (
double *)malloc(
sizeof(
double) *
n);
146 pn = scale * (sqrt(
n + 1) - 1 +
n) / (
n * sqrt(2));
147 qn = scale * (sqrt(
n + 1) - 1) / (
n * sqrt(2));
149 for(
i = 0;
i <
n;
i++)
154 for(
i = 1;
i <=
n;
i++)
156 for(j = 0; j <
n; j++)
160 v[
i][j] = pn + start[j];
164 v[
i][j] = qn + start[j];
174 for(j = 0; j <=
n; j++)
181 printf(
"Initial Values\n");
182 for(j = 0; j <=
n; j++)
184 for(
i = 0;
i <
n;
i++)
186 printf(
"%f %f\n",
v[j][
i],
f[j]);
192 for(itr = 1; itr <= maxiter; itr++)
196 for(j = 0; j <=
n; j++)
206 for(j = 0; j <=
n; j++)
216 for(j = 0; j <=
n; j++)
218 if(
f[j] >
f[vh] &&
f[j] <
f[vg])
225 for(j = 0; j <=
n - 1; j++)
228 for(
m = 0;
m <=
n;
m++)
239 for(j = 0; j <=
n - 1; j++)
242 vr[j] = vm[j] +
NMS_ALPHA * (vm[j] -
v[vg][j]);
250 if(fr <
f[vh] && fr >=
f[vs])
252 for(j = 0; j <=
n - 1; j++)
262 for(j = 0; j <=
n - 1; j++)
265 ve[j] = vm[j] +
NMS_GAMMA * (vr[j] - vm[j]);
279 for(j = 0; j <=
n - 1; j++)
287 for(j = 0; j <=
n - 1; j++)
298 if(fr <
f[vg] && fr >=
f[vh])
301 for(j = 0; j <=
n - 1; j++)
304 vc[j] = vm[j] +
NMS_BETA * (vr[j] - vm[j]);
315 for(j = 0; j <=
n - 1; j++)
318 vc[j] = vm[j] -
NMS_BETA * (vm[j] -
v[vg][j]);
330 for(j = 0; j <=
n - 1; j++)
347 for(j = 0; j <=
n - 1; j++)
349 v[
row][j] =
v[vs][j] + (
v[
row][j] -
v[vs][j]) / 2.0;
367 printf(
"Iteration %d\n", itr);
368 for(j = 0; j <=
n; j++)
370 for(
i = 0;
i <
n;
i++)
372 printf(
"%f %f\n",
v[j][
i],
f[j]);
378 for(j = 0; j <=
n; j++)
382 favg = fsum / (
n + 1);
384 for(j = 0; j <=
n; j++)
386 s += pow((
f[j] - favg), 2.0) / (
n);
395 for(j = 0; j <=
n; j++)
403 printf(
"The minimum was found at\n");
404 for(j = 0; j <
n; j++)
406 printf(
"%e\n",
v[vs][j]);
410 printf(
"The minimum value is %f\n",
min);
411 printf(
"%d Iterations through program\n", itr);
413 for(j = 0; j <
n; j++)
423 for(
i = 0;
i <=
n;
i++)
static const float const float const float min
static const dt_iop_drawlayer_params_t * params
#define IS_NULL_PTR(p)
C is way too permissive with !=, == and if(var) checks, which can mean too many things depending on w...
#define dt_free(ptr)
g_free() ptr and set it to NULL, skipping both if it is already NULL.
static int simplex(double(*objfunc)(double[], void *params), double start[], int n, double EPSILON, double scale, int maxiter, void(*constrain)(double[], int n), void *params)