79static int simplex(
double (*objfunc)(
double[],
void *params),
double start[],
int n,
double EPSILON,
double scale,
80 int maxiter,
void (*constrain)(
double[],
int n),
void *params)
87 int i = 0, j = 0,
m, row;
102 double fsum, favg, s, cent;
107 v = (
double **)malloc(
sizeof(
double *) * (n + 1));
108 f = (
double *)malloc(
sizeof(
double) * (n + 1));
109 vr = (
double *)malloc(
sizeof(
double) * n);
110 ve = (
double *)malloc(
sizeof(
double) * n);
111 vc = (
double *)malloc(
sizeof(
double) * n);
112 vm = (
double *)malloc(
sizeof(
double) * n);
115 for(i = 0; i <= n; i++)
117 v[i] = (
double *)malloc(
sizeof(
double) * n);
123 pn = scale * (sqrt(n + 1) - 1 + n) / (n * sqrt(2));
124 qn = scale * (sqrt(n + 1) - 1) / (n * sqrt(2));
126 for(i = 0; i < n; i++)
131 for(i = 1; i <= n; i++)
133 for(j = 0; j < n; j++)
137 v[i][j] = pn + start[j];
141 v[i][j] = qn + start[j];
146 if(constrain != NULL)
151 for(j = 0; j <= n; j++)
153 f[j] = objfunc(v[j], params);
158 printf(
"Initial Values\n");
159 for(j = 0; j <= n; j++)
161 for(i = 0; i < n; i++)
163 printf(
"%f %f\n", v[j][i],
f[j]);
169 for(itr = 1; itr <= maxiter; itr++)
173 for(j = 0; j <= n; j++)
183 for(j = 0; j <= n; j++)
193 for(j = 0; j <= n; j++)
195 if(
f[j] >
f[vh] &&
f[j] <
f[vg])
202 for(j = 0; j <= n - 1; j++)
205 for(
m = 0;
m <= n;
m++)
216 for(j = 0; j <= n - 1; j++)
219 vr[j] = vm[j] +
NMS_ALPHA * (vm[j] - v[vg][j]);
221 if(constrain != NULL)
225 fr = objfunc(vr, params);
227 if(fr <
f[vh] && fr >=
f[vs])
229 for(j = 0; j <= n - 1; j++)
239 for(j = 0; j <= n - 1; j++)
242 ve[j] = vm[j] +
NMS_GAMMA * (vr[j] - vm[j]);
244 if(constrain != NULL)
248 fe = objfunc(ve, params);
256 for(j = 0; j <= n - 1; j++)
264 for(j = 0; j <= n - 1; j++)
275 if(fr <
f[vg] && fr >=
f[vh])
278 for(j = 0; j <= n - 1; j++)
281 vc[j] = vm[j] +
NMS_BETA * (vr[j] - vm[j]);
283 if(constrain != NULL)
287 fc = objfunc(vc, params);
292 for(j = 0; j <= n - 1; j++)
295 vc[j] = vm[j] -
NMS_BETA * (vm[j] - v[vg][j]);
297 if(constrain != NULL)
301 fc = objfunc(vc, params);
307 for(j = 0; j <= n - 1; j++)
320 for(row = 0; row <= n; row++)
324 for(j = 0; j <= n - 1; j++)
326 v[row][j] = v[vs][j] + (v[row][j] - v[vs][j]) / 2.0;
330 if(constrain != NULL)
334 f[vg] = objfunc(v[vg], params);
335 if(constrain != NULL)
339 f[vh] = objfunc(v[vh], params);
344 printf(
"Iteration %d\n", itr);
345 for(j = 0; j <= n; j++)
347 for(i = 0; i < n; i++)
349 printf(
"%f %f\n", v[j][i],
f[j]);
355 for(j = 0; j <= n; j++)
359 favg = fsum / (n + 1);
361 for(j = 0; j <= n; j++)
363 s += pow((
f[j] - favg), 2.0) / (n);
372 for(j = 0; j <= n; j++)
380 printf(
"The minimum was found at\n");
381 for(j = 0; j < n; j++)
383 printf(
"%e\n", v[vs][j]);
386 double min = objfunc(v[vs], params);
387 printf(
"The minimum value is %f\n", min);
388 printf(
"%d Iterations through program\n", itr);
390 for(j = 0; j < n; j++)
400 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:79
static float f(const float t, const float c, const float x)
Definition graduatednd.c:173