85static int simplex(
double (*objfunc)(
double[],
void *params),
double start[],
int n,
double EPSILON,
double scale,
86 int maxiter,
void (*constrain)(
double[],
int n),
void *params)
93 int i = 0, j = 0,
m, row;
108 double fsum, favg, s, cent;
113 v = (
double **)malloc(
sizeof(
double *) * (n + 1));
114 f = (
double *)malloc(
sizeof(
double) * (n + 1));
115 vr = (
double *)malloc(
sizeof(
double) * n);
116 ve = (
double *)malloc(
sizeof(
double) * n);
117 vc = (
double *)malloc(
sizeof(
double) * n);
118 vm = (
double *)malloc(
sizeof(
double) * n);
121 for(i = 0; i <= n; i++)
123 v[i] = (
double *)malloc(
sizeof(
double) * n);
129 pn = scale * (sqrt(n + 1) - 1 + n) / (n * sqrt(2));
130 qn = scale * (sqrt(n + 1) - 1) / (n * sqrt(2));
132 for(i = 0; i < n; i++)
137 for(i = 1; i <= n; i++)
139 for(j = 0; j < n; j++)
143 v[i][j] = pn + start[j];
147 v[i][j] = qn + start[j];
152 if(constrain != NULL)
157 for(j = 0; j <= n; j++)
159 f[j] = objfunc(v[j], params);
164 printf(
"Initial Values\n");
165 for(j = 0; j <= n; j++)
167 for(i = 0; i < n; i++)
169 printf(
"%f %f\n", v[j][i],
f[j]);
175 for(itr = 1; itr <= maxiter; itr++)
179 for(j = 0; j <= n; j++)
189 for(j = 0; j <= n; j++)
199 for(j = 0; j <= n; j++)
201 if(
f[j] >
f[vh] &&
f[j] <
f[vg])
208 for(j = 0; j <= n - 1; j++)
211 for(
m = 0;
m <= n;
m++)
222 for(j = 0; j <= n - 1; j++)
225 vr[j] = vm[j] +
NMS_ALPHA * (vm[j] - v[vg][j]);
227 if(constrain != NULL)
231 fr = objfunc(vr, params);
233 if(fr <
f[vh] && fr >=
f[vs])
235 for(j = 0; j <= n - 1; j++)
245 for(j = 0; j <= n - 1; j++)
248 ve[j] = vm[j] +
NMS_GAMMA * (vr[j] - vm[j]);
250 if(constrain != NULL)
254 fe = objfunc(ve, params);
262 for(j = 0; j <= n - 1; j++)
270 for(j = 0; j <= n - 1; j++)
281 if(fr <
f[vg] && fr >=
f[vh])
284 for(j = 0; j <= n - 1; j++)
287 vc[j] = vm[j] +
NMS_BETA * (vr[j] - vm[j]);
289 if(constrain != NULL)
293 fc = objfunc(vc, params);
298 for(j = 0; j <= n - 1; j++)
301 vc[j] = vm[j] -
NMS_BETA * (vm[j] - v[vg][j]);
303 if(constrain != NULL)
307 fc = objfunc(vc, params);
313 for(j = 0; j <= n - 1; j++)
326 for(row = 0; row <= n; row++)
330 for(j = 0; j <= n - 1; j++)
332 v[row][j] = v[vs][j] + (v[row][j] - v[vs][j]) / 2.0;
336 if(constrain != NULL)
340 f[vg] = objfunc(v[vg], params);
341 if(constrain != NULL)
345 f[vh] = objfunc(v[vh], params);
350 printf(
"Iteration %d\n", itr);
351 for(j = 0; j <= n; j++)
353 for(i = 0; i < n; i++)
355 printf(
"%f %f\n", v[j][i],
f[j]);
361 for(j = 0; j <= n; j++)
365 favg = fsum / (n + 1);
367 for(j = 0; j <= n; j++)
369 s += pow((
f[j] - favg), 2.0) / (n);
378 for(j = 0; j <= n; j++)
386 printf(
"The minimum was found at\n");
387 for(j = 0; j < n; j++)
389 printf(
"%e\n", v[vs][j]);
392 double min = objfunc(v[vs], params);
393 printf(
"The minimum value is %f\n", min);
394 printf(
"%d Iterations through program\n", itr);
396 for(j = 0; j < n; j++)
406 for(i = 0; i <= n; i++)
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)
Definition ashift_nmsimplex.c:85
static float f(const float t, const float c, const float x)
Definition graduatednd.c:209