-
Notifications
You must be signed in to change notification settings - Fork 1
/
simplex.c
213 lines (183 loc) · 4.37 KB
/
simplex.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "lib.h"
double** create_simplex(int n)
{
int i,j;
double MAX=1.0; /* in every dimension the lenght vector will be one unit */
double **simplex=(double**)malloc((n+1)*sizeof(double*)); /* initialize simplex as a 2D matrix */
for(i=0;i<(n+1);i++)
{
simplex[i]=(double*)malloc(n*sizeof(double));
}
/* initialize simplex */
for(i=0;i<(n+1);i++)
{
for(j=0;j<n;j++)
{
if(i>0 && (j==(i-1)))
simplex[i][j]=MAX;
else
simplex[i][j]=0.0;
}
}
return simplex;
}
double* minimize(double x[], double y[], int n, int k)
{
double **simplex=create_simplex(n); /* create a new simplex */
double *function_values=(double*)malloc((n+1)*sizeof(double)); /* function values for a new simplex */
function_values=call_functions(x,y,simplex,n,k); /* store function values for every vertex of a simplex in an array */
int worst, second_worst, best; /* variable to store indecies for worst, second_word and best vortex */
double min, max; /* max and min values for function values */
int i,j; /* indecies for for loop */
double* centroid=(double*)malloc(n*sizeof(double)); /* centroid */
double alpha=1, beta=0.5, gamma=2, delta=0.5; /* scalar parameteras */
double *vertex_r=(double*)malloc(n*sizeof(double)); /* reflection point */
double f_r; /* function value at the reflection point */
double *vertex_e=(double*)malloc(n*sizeof(double)); /* expansion point */
double f_e; /* function value at the expansion point */
double *vertex_c=(double*)malloc(n*sizeof(double)); /* contraction point */
double f_c; /* function value at the contraction point */
int intterations_max=5000;
int itteration=0;
double conv_test; /* check every 42 itterations if function has changde less than 0.01% */
double diff=42.0;
do
{
/* 1. step Ordering*/
worst=0, second_worst=0, best=0;
min=function_values[0], max=function_values[0];
for(i=1;i<(n+1);i++)
{
if(min>=function_values[i])
{
min=function_values[i];
best=i;
}
if(max<=function_values[i])
{
max=function_values[i];
second_worst=worst;
worst=i;
}
}
/* 2. step Centroid */
for(i=0;i<n;i++)
centroid[i]=0.0;
for (i=0; i<n; i++)
{
for(j=0;j<(n+1);j++)
{
if(j!=worst)
{
centroid[i]+=simplex[j][i];
}
}
centroid[i]=centroid[i]/n;
}
/* 3. Step Transformation */
/* Reflect */
for(i=0;i<n;i++)
{
vertex_r[i]=centroid[i]+alpha*(centroid[i]-simplex[worst][i]);
}
f_r=objective_function(x,y,vertex_r,n,k);
if(function_values[best]<=f_r && f_r<function_values[second_worst])
{
for(i=0;i<n;i++)
{
simplex[worst][i]=vertex_r[i];
}
function_values[worst]=f_r;
}
else if (f_r<function_values[best]) /* Expand */
{
for(i=0;i<n;i++)
{
vertex_e[i]=centroid[i]+gamma*(vertex_r[i]-centroid[i]);
}
f_e=objective_function(x,y,vertex_e,n,k);
if(f_e<f_r)
{
for(i=0;i<n;i++)
{
simplex[worst][i]=vertex_e[i];
}
function_values[worst]=f_e;
}
else if(f_e>=f_r)
{
for(i=0;i<n;i++)
{
simplex[worst][i]=vertex_e[i];
}
function_values[worst]=f_e;
}
}
else if (f_r>=function_values[second_worst]) /* Contract */
{
if(function_values[second_worst]<=f_r && f_r<function_values[worst]) /* Outside */
{
for(i=0;i<n;i++)
{
vertex_c[i]=centroid[i]+beta*(vertex_r[i]-centroid[i]);
}
f_c=objective_function(x,y,vertex_c,n,k);
if(f_c<=f_r)
{
for(i=0;i<n;i++)
{
simplex[worst][i]=vertex_c[i];
}
function_values[worst]=f_c;
}
}
else if(f_r>=function_values[worst]) /* Inside */
{
for(i=0;i<n;i++)
{
vertex_c[i]=centroid[i]+beta*(simplex[worst][i]-centroid[i]);
}
f_c=objective_function(x,y,vertex_c,n,k);
if(f_c<function_values[worst])
{
for(i=0;i<n;i++)
{
simplex[worst][i]=vertex_c[i];
}
function_values[worst]=f_c;
}
}
else /* Shrink */
{
for(i=0;i<(n+1);i++)
{
if(i!=best)
{
for(j=0;i<n;j++)
{
simplex[i][j]=simplex[best][j]+delta*(simplex[i][j]-simplex[best][j]);
}
}
}
function_values=call_functions(x,y,simplex,n,k);
}
}
if(itteration%42==0)
{
if(itteration>0)
{
diff=fabs(conv_test-function_values[best]);
}
conv_test=function_values[best];
}
itteration++;
} while (itteration<intterations_max && diff>0.0001);
if(itteration==intterations_max)
{
printf("The algorithm has not converge.\n");
}
return simplex[best];
}