-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSOMA_Sequence.cpp
362 lines (338 loc) · 9.23 KB
/
SOMA_Sequence.cpp
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
#include "SOMA_Sequence.h"
#include <cstdlib>
#include <ctime>
#include <stdio.h>
#include <iostream>
#include "TimeAnalysis.h"
#if __linux__
#else
#include <math.h>
#endif
#include <iostream>
#include <cstdint>
#include <cstring>
using namespace std;
SOMA_Sequence::SOMA_Sequence(void)
{
OP0_THRESHOLD = RANGE*0.0001f;
f.SetTestfunction2(TEST_FUNCTION_ID);
Individuals_cost = (float*)malloc(POP_SIZE * sizeof(float));
Individuals_status = (bool*)malloc(POP_SIZE * sizeof(bool));
Individuals_isChanged = (bool*)malloc(POP_SIZE * sizeof(bool));
for (int i = 0; i < POP_SIZE; i++)
{
Individuals_status[i] = true;
Individuals_isChanged = false;
}
Individuals = (float*)malloc(POP_SIZE *DIMENSION* sizeof(float));
PRT_Vectors = (float*)malloc(POP_SIZE *DIMENSION * sizeof(float));
BOUNDS = (float*)malloc(2 *DIMENSION * sizeof(float));
}
SOMA_Sequence::~SOMA_Sequence(void)
{
}
void SOMA_Sequence::RanDomIndividuals()
{
//f.SetTestfunction2(TEST_FUNCTION_ID);
Clear();
srand(time(NULL)+PROCESS_ID);
for(int i =0;i<POP_SIZE;i++)
{
TVector vec_temp;
for(int j =0;j<DIMENSION;j++)
{
Individuals[i*DIMENSION+j] = ((rand() % 10000)/10000.0f)*(BOUNDS[2*j+1] - BOUNDS[2*j]) + BOUNDS[2*j];
}
}
}
void SOMA_Sequence::RanDomIndividuals(float *target, float factor)
{
Clear();
float step;
srand(time(NULL) + PROCESS_ID);
for (int i = 0; i<POP_SIZE; i++)
{
TVector vec_temp;
for (int j = 0; j<DIMENSION; j++)
{
step = (BOUNDS[2 * j + 1] - BOUNDS[2 * j])*factor;
//Individuals[i*DIMENSION + j] = ((rand() % 10000) / 10000.0f)*(BOUNDS[2 * j + 1] - BOUNDS[2 * j]) + BOUNDS[2 * j];
Individuals[i*DIMENSION + j] = target[j] + ((rand() % 10000) / 10000.0f - 0.5f)*step;
if (Individuals[i*DIMENSION + j] > BOUNDS[2 * j + 1]) Individuals[i*DIMENSION + j] = BOUNDS[2 * j + 1];
else if (Individuals[i*DIMENSION + j] < BOUNDS[2 * j ]) Individuals[i*DIMENSION + j] = BOUNDS[2 * j ];
}
}
}
void SOMA_Sequence::FindLeader()
{
//float min_cost = f.Schwefel_(&Individuals[LEADER_INDEX*DIMENSION]);
float min_cost = f.testfunction_(&Individuals[LEADER_INDEX*DIMENSION]);
LEADER_INDEX = LEADER_INDEX;
for(int i =0;i<POP_SIZE;i++)
{
//float temp = f.Schwefel_(&Individuals[i*DIMENSION]);
float temp = f.testfunction_(&Individuals[i*DIMENSION]);
Individuals_cost[i] = temp;
//if(temp < min_cost)
if (temp > min_cost)
{
min_cost = temp;
LEADER_INDEX=i;
}
if (OP0 && Individuals_status[i])
{
float d = 0;
for (int j = i + 1; j < POP_SIZE; j++)
{
if (Individuals_status[j])
{
d = 0;
for (int d = 0; d < DIMENSION; d++)
d += fabs(Individuals[i*DIMENSION+d] - Individuals[j*DIMENSION + d]);
if (d < OP0_THRESHOLD)
{
Individuals_status[j] = false;
break;
}
}
}
}
}
if(last_leader_cost != min_cost)
{
if (MY_DEVICE_ID == 0 && IS_WRITE_MIGRATIONS)
{
TOutput::getInstance().WriteMigration(Individuals, Individuals_cost, LEADER_INDEX);
}
last_leader_cost = min_cost;
}
}
TVector SOMA_Sequence::Get_PRT_Vectors()
{
TVector tvector_temp;
for(int j =0;j<DIMENSION;j++)
{
if(rand()/32767.0f <PRT_THRESHOLD)
tvector_temp.A[j] =1;
}
return tvector_temp;
}
float* SOMA_Sequence::Get_PRT_Vectors_(float *f)
{
bool is_all_zero = true;
for (int j = 0; j<DIMENSION; j++)
{
if (rand() / 32767.0f < PRT_THRESHOLD)
{
f[j] = 1;
is_all_zero = false;
}
else f[j] = 0;
}
if (is_all_zero)
f[((int)rand()) % DIMENSION] = 1;
return f;
}
bool SOMA_Sequence::Is_InBounds(TVector in)
{
for(int j =0;j<DIMENSION;j++)
{
if (in.A[j] <BOUNDS[j]) return false;
if(in.A[j] >BOUNDS[j+1]) return false;
}
return true;
}
bool SOMA_Sequence::Is_InBounds(float* in)
{
//if (in[0] * in[1] < 0)
//int asd = 23;
for (int j = 0; j<DIMENSION; j++)
{
if (in[j] <BOUNDS[j*2]) return false;
if (in[j] >BOUNDS[j*2 + 1]) return false;
}
return true;
}
void SOMA_Sequence::DoSOMAWithSchwefel()
{
if (WORLD_SIZE == 1) TimeAnalysis::getInstance().Begin_ProcessCPU();
LEADER_INDEX=0;
FindLeader();
bool is_have_change = false;
float* current_vec = (float*)malloc(DIMENSION * sizeof(float));
float* current_leader_temp;
float* Individuals_J_temp = (float*)malloc(DIMENSION * sizeof(float));
int j_leader_count = 0;
for(int i =0;i<MIGRATIONS;i++)
{
is_have_change = false;
for(int j =0;j<POP_SIZE;j++)
{
j_leader_count = 0;
if (OP0 && !Individuals_status[j]) continue;
float cost_temp = 0;
Get_PRT_Vectors_(&(PRT_Vectors[j*DIMENSION]));
float Individuals_cost_min = Individuals_cost[j];
std::memcpy(Individuals_J_temp,&Individuals[j*DIMENSION],DIMENSION*sizeof(float));
if (SOMA_TYPE == 0) // ALl to Leader
{
current_leader_temp = &Individuals[LEADER_INDEX*DIMENSION];
RunSOMAPath(j, current_leader_temp, current_vec, &is_have_change, Individuals_J_temp, &Individuals_cost_min);
}
// j to t
else if(SOMA_TYPE ==1) // All to All
{
for (int t = 0; t < POP_SIZE; t++)
{
if (t == j) continue;
if (OP0 && !Individuals_status[t]) continue;
if (OP1 && Individuals_cost[j] > Individuals_cost[t] - OP1_THRESHOLD) continue;
if (OP2)
{
if (Individuals_cost[j] > Individuals_cost[t] - OP1_THRESHOLD) continue;
if (j_leader_count > OP2_N) continue;
j_leader_count++;
}
current_leader_temp = &Individuals[t*DIMENSION];
RunSOMAPath(j, current_leader_temp, current_vec, &is_have_change, Individuals_J_temp, &Individuals_cost_min);
}
}
memcpy(&Individuals[j*DIMENSION] , Individuals_J_temp, DIMENSION*sizeof(float));
Individuals_cost[j] = Individuals_cost_min;
}
FindLeader();
if (!is_have_change)
{
if (WORLD_SIZE == 1)
{
printf("break at mirgration: %d \n", i);
printf("result for me: (%f,%f,%f,%f) -> (%f,%f,%f)\n", BOUNDS[0], BOUNDS[1], BOUNDS[2], BOUNDS[3], Individuals[LEADER_INDEX*DIMENSION], Individuals[LEADER_INDEX*DIMENSION + 1], Individuals_cost[LEADER_INDEX]);
TimeAnalysis::getInstance().NUM_MIGRATION = i;
for (int d = 0; d < DIMENSION; d++)
{
TimeAnalysis::getInstance().POSITION[d] = Individuals[LEADER_INDEX*DIMENSION+d];
}
TimeAnalysis::getInstance().VALUE = Individuals_cost[LEADER_INDEX];
}
break;
}
}
TimeAnalysis::getInstance().End_ProcessCPU();
}
float* SOMA_Sequence::RunSOMAPath(int index,float *target, float* current_vec, bool *is_have_change,float *Individuals_J_temp, float* Individuals_cost_min)
{
float cost_temp = 0;
//float Individuals_cost_min = Individuals_cost[index];
for (float t = STEP; t <= PATH_LENGTH; t += STEP)
{
for (int d = 0; d < DIMENSION; d++)
{
current_vec[d] = Individuals[index*DIMENSION + d]
+ (target[d] - Individuals[index*DIMENSION + d])
*t*PRT_Vectors[index*DIMENSION + d];
}
if (!Is_InBounds(current_vec)) continue;
//cost_temp = f.Schwefel_(current_vec);
cost_temp = f.testfunction_(current_vec);
if (cost_temp>(*Individuals_cost_min))
{
*is_have_change = true;
for (int d = 0; d < DIMENSION; d++)
{
Individuals_J_temp[d] = current_vec[d];
}
(*Individuals_cost_min) = cost_temp;
}
}
return NULL;
}
void SOMA_Sequence::Test_PrintIndividuals()
{
printf("Individuals: ");
for (int i = 0; i<POP_SIZE; i+= DIMENSION)
{
printf(" (%f,%f) ", Individuals[i], Individuals[i+1]);
}
printf("\n");
}
void SOMA_Sequence::Test_PrintBounds()
{
printf("Bounds: ");
for (int i = 0; i<DIMENSION; i ++)
{
printf(" (%f,%f) ", BOUNDS[2*i], BOUNDS[2 * i+1]);
}
printf("\n");
}
void SOMA_Sequence::SetBounds(float min, float max)
{
for (int i = 0; i<DIMENSION; i++)
{
BOUNDS[i*2] = min;
BOUNDS[i*2 +1] = max;
}
if (WORLD_SIZE == 1)
{
TimeAnalysis::getInstance().SetBound(min, max);
}
}
void SOMA_Sequence::SetBounds(float *values)
{
SetBounds(values[0], values[1]);
}
void SOMA_Sequence::SetBounds(float xmin, float xmax, float ymin, float ymax)
{
//SOMAs->SetBounds(xmin, xmax, ymin, ymax);
BOUNDS[0] = xmin;
BOUNDS[1] = xmax;
BOUNDS[2] = ymin;
BOUNDS[3] = ymax;
//printf("setting for me: (%f,%f,%f,%f)\n", BOUNDS[0], BOUNDS[1], BOUNDS[2], BOUNDS[3]);
}
void SOMA_Sequence::SetBounds(int dimension, float min, float max)
{
BOUNDS[dimension*2] = min;
BOUNDS[dimension*2+1] = max;
}
void SOMA_Sequence::Clear()
{
for (int i = 0; i < POP_SIZE; i++)
{
Individuals_status[i] = true;
Individuals_cost[i] = 0;
}
last_leader_cost = 0;
LEADER_INDEX = 0;
}
//void SOMA_Sequence::InitParameters(int argc, char *argv[])
//{
// int i = 1;
// while (i < argc)
// {
// if (strcmp(argv[i], "-pop") == 0)
// {
// POP_SIZE = atoi(argv[++i]);
// }
// else if (strcmp(argv[i], "-step") == 0)
// {
// STEP = atof(argv[++i]);
// }
// else if (strcmp(argv[i], "-plength") == 0)
// {
// PATH_LENGTH = atof(argv[++i]);
// }
// else if (strcmp(argv[i], "-range") == 0)
// {
// RANGE = atof(argv[++i]);
// }
// else if (strcmp(argv[i], "-d") == 0)
// {
// DIMENSION = atoi(argv[++i]);
// }
// else if (strcmp(argv[i], "-grid") == 0)
// {
// GRID_X = GRID_Y = atoi(argv[++i]);
// }
// i++;
// }
//}