-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
essInit.c
341 lines (269 loc) · 11.9 KB
/
essInit.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
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
#include "ess.h"
void init_defaultSettings(eSSType *eSSParams){
eSSParams->iter = 1;
eSSParams->debug = 0;
eSSParams->perform_warm_start = 0;
eSSParams->init_with_user_guesses = 0;
eSSParams->collect_stats = 0;
eSSParams->save_output = 1;
eSSParams->perform_local_search = 0;
eSSParams->max_iter = 0;
eSSParams->max_eval = 0;
eSSParams->max_time = 0;
eSSParams->local_SolverMethod = '0';
}
/**
* Request heap memory for all the variables needed based on their size.
* So, the init_sampleParams should be called before.
*/
void init_essParams(eSSType *eSSParams){
/**
* Initializing stats matrices and boundary matrices
*/
eSSParams->stats = (Stats *)malloc(sizeof(Stats));
eSSParams->stats->n_successful_goBeyond = 0;
eSSParams->stats->n_local_search_performed = 0;
eSSParams->stats->n_successful_localSearch = 0;
eSSParams->stats->n_local_search_iterations = 0;
eSSParams->stats->n_total_stuck = 0;
eSSParams->stats->n_successful_recombination = 0;
eSSParams->stats->n_refSet_randomized = 0;
eSSParams->stats->n_duplicate_found = 0;
eSSParams->stats->n_flatzone_detected = 0;
eSSParams->refSet = (Set*)malloc(sizeof(Set));
eSSParams->refSet->size = eSSParams->n_refSet;
allocate_Set(eSSParams, eSSParams->refSet);
eSSParams->scatterSet = (Set*)malloc(sizeof(Set));
eSSParams->scatterSet->size = eSSParams->n_scatterSet;
allocate_Set(eSSParams, eSSParams->scatterSet);
eSSParams->childsSet = (Set*)malloc(sizeof(Set));
eSSParams->childsSet->size = eSSParams->n_childsSet;
allocate_Set(eSSParams, eSSParams->childsSet);
eSSParams->candidateSet = (Set*)malloc(sizeof(Set));
eSSParams->candidateSet->size = eSSParams->n_candidateSet;
allocate_Set(eSSParams, eSSParams->candidateSet);
eSSParams->localSearchCandidateSet = (Set *)malloc(sizeof(Set));
eSSParams->localSearchCandidateSet->size = eSSParams->n_refSet;
allocate_Set(eSSParams, eSSParams->localSearchCandidateSet);
eSSParams->archiveSet = (Set *)malloc(sizeof(Set));
eSSParams->archiveSet->size = eSSParams->n_archiveSet;
allocate_Set(eSSParams, eSSParams->archiveSet);
eSSParams->best = (Individual*)malloc(sizeof(Individual));
eSSParams->stats->freqs_matrix = (int **)malloc(eSSParams->n_params * sizeof(int *));
eSSParams->stats->probs_matrix = (double **)malloc(eSSParams->n_params * sizeof(double *));
// Generating the sub regions matrices
eSSParams->min_boundary_matrix = (double **)malloc( eSSParams->n_params * sizeof(double *));
eSSParams->max_boundary_matrix = (double **)malloc( eSSParams->n_params * sizeof(double *));
for (int i = 0; i < eSSParams->n_params; ++i)
{
eSSParams->min_boundary_matrix[i] = (double *)malloc((eSSParams->n_sub_regions) * sizeof(double));
eSSParams->max_boundary_matrix[i] = (double *)malloc((eSSParams->n_sub_regions) * sizeof(double));
eSSParams->stats->freqs_matrix[i] = (int *)malloc(eSSParams->n_sub_regions * sizeof(int));
eSSParams->stats->probs_matrix[i] = (double *)malloc(eSSParams->n_sub_regions * sizeof(double));
for (int j = 1; j <= eSSParams->n_sub_regions; ++j)
{
/* Building the sub region matrices */ // One matrix would be enough but for clarity I use two.
eSSParams->min_boundary_matrix[i][j - 1] = eSSParams->min_real_var[i] + ((eSSParams->max_real_var[i] - eSSParams->min_real_var[i]) / eSSParams->n_sub_regions) * (j - 1);
eSSParams->max_boundary_matrix[i][j - 1] = eSSParams->min_real_var[i] + ((eSSParams->max_real_var[i] - eSSParams->min_real_var[i]) / eSSParams->n_sub_regions) * j;
/* Building the freqs_matrix: frequently_matrix */
eSSParams->stats->freqs_matrix[i][j - 1] = 1;
eSSParams->stats->probs_matrix[i][j - 1] = 1./(eSSParams->n_sub_regions); // Since all values are 1.
}
}
}
void init_report_files(eSSType *eSSParams){
char mode = 'w';
if (eSSParams->perform_warm_start)
mode = 'a';
refSet_history_file = fopen("ref_set_history_file.out", &mode);
best_sols_history_file = fopen("best_sols_history_file.out", &mode);
freqs_matrix_file = fopen("freqs_matrix_history.out", &mode);
stats_file = fopen("stats_file.csv", &mode);
ref_set_stats_history_file = fopen("ref_set_stats_history_file.csv", &mode);
archive_set_file = fopen("archive_set_file.csv", &mode);
// TODO: Check the mode
}
void init_stats(eSSType *eSSParams){
}
/**
* Generate scatterSet by first generating one solution in each subRegion and then expanding
* the set by preserving the diversity.
* @param eSSParams
* @param inp
* @param out
*/
void init_scatterSet(eSSType *eSSParams, void* inp, void *out){
// Generating Scatter Set with the size of p
// The first `p` members are uniformly selected from all subRegions
for (int k = 0; k < eSSParams->n_sub_regions; ++k)
for (int i = 0; i < eSSParams->n_params; ++i)
eSSParams->scatterSet->members[k].params[i] = rndreal(eSSParams->min_boundary_matrix[i][k], eSSParams->max_boundary_matrix[i][k]);
// `a` indicates the index of selected subRegions that the new value should be generated from.
int a = 0;
double probs_sum = 0;
double rnd = 0;
/* Generate new item to add to scatterSet
eSSParams->scatterSet->members[eSSParams->p + k] is going to be generated */
for (int k = 0; k < eSSParams->scatterSet->size - eSSParams->n_sub_regions; ++k)
{
for (int i = 0; i < eSSParams->n_params; ++i)
{
rnd = rndreal(0, 1);
for (int j = 0; j < eSSParams->n_sub_regions; ++j)
{
/* Compute `probs` and find the index */
probs_sum += eSSParams->stats->probs_matrix[i][j];
if ( rnd <= probs_sum ){
a = j;
// Updating the f matrix, and prob_matrix
eSSParams->stats->freqs_matrix[i][a]++;
// Updating the eSSParams->stats->probs_matrix
// IMPROVE: This could be rewritten much nicer
double f_col_prob = 0;
for (int t = 0; t < eSSParams->n_sub_regions; ++t){
f_col_prob += (1. / eSSParams->stats->freqs_matrix[i][t]);
}
eSSParams->stats->probs_matrix[i][a] = (1. / eSSParams->stats->freqs_matrix[i][a] ) / (f_col_prob);
probs_sum = 0;
break;
}
}
// subRegoins index is selected now: `a`
eSSParams->scatterSet->members[eSSParams->n_sub_regions + k].params[i] = rndreal(eSSParams->min_boundary_matrix[i][a], eSSParams->max_boundary_matrix[i][a] );
}
} // Scatter Set is now extended...
}
/**
* Initialize the refSet using the diversity selection routine. Note that all the Individual
* are selecting from the scatterSet which is already evaluated.
* @param eSSParams
* @param inp
* @param out
*/
void init_refSet(eSSType *eSSParams, void* inp, void *out){
printf("Forming the refSet...\n");
int m = eSSParams->scatterSet->size;
int b = eSSParams->refSet->size;
int h = b / 2; // max_elite
// Sorting the scatterSet
quickSort_Set(eSSParams, eSSParams->scatterSet, 0, eSSParams->scatterSet->size - 1, 'c');
for (int i = 0; i < h; ++i){
// Copying first `h` best items of the scatterSet with respect to the cost
copy_Ind(eSSParams, &(eSSParams->refSet->members[i]), &(eSSParams->scatterSet->members[i]));
}
/************************************************************/
/* Expanding the refSet by applying the diversity distance */
/************************************************************/
// dist_matrix will store the distances between the rest of scatterSet and the refSet
double **dist_matrix = (double **)malloc( (m - h) * sizeof(double *));;
for (int i = 0; i < (m - h); ++i)
dist_matrix[i] = (double *)malloc( (b) * sizeof(double));
double *min_dists = (double *)malloc((m - h) * sizeof(double));
int min_index = 0;
int max_index = 0;
int i = 0;
for (int k = h; k < b; ++k, --m) // Iterate through the refSet for expanding
{ // --m is reduce the depth that the second `for` should go through it
// since the array is shifted after each iteration
for (i = h; i < m; ++i) // Iterate through the rest of scatterSet
{
for (int j = 0; j < k; ++j) // Iterate trhough the selected members of refSet
{
/* Computing the distance */
dist_matrix[i - (b / 2)][j] = euclidean_distance(eSSParams, &(eSSParams->scatterSet->members[i]), &(eSSParams->refSet->members[j]));
}
// Find the minimum of each row into a new variable, then finding it's max
min_dists[i - (b / 2)] = min(dist_matrix[i - (b / 2)], k, &min_index);
}
// Now, the min_dists of vectors to all the members of R is computed,
// we could compute the maximum of them and also it's index
// Finding the place of the max/min distance
max(min_dists, i - (b / 2), &max_index);
// Adding the vector in max_index to the refSet
copy_Ind(eSSParams, &(eSSParams->refSet->members[k]), &(eSSParams->scatterSet->members[h + max_index]));
// After assignment, the rest of should be shift into the left form the position that the
// vector is selected
// It could be done more complicated!
delete_and_shift(eSSParams, eSSParams->scatterSet, eSSParams->n_scatterSet, (b / 2) + max_index);
}
/**
* It checks if the user initial guesses are available, if so, then add them to the
* end of the refSet.
*/
if (eSSParams->init_with_user_guesses){
FILE *user_initial_guesses_file = fopen("init_guesses.csv", "r");
int i = 15 ;
char line[4098];
/**
* Read the `init_guesses` file and import 5 entries from it to the refSet
*/
while (fgets(line, 4098, user_initial_guesses_file) && (i < eSSParams->n_refSet))
{
double row[eSSParams->n_params];
char* tmp = strdup(line);
parse_double_row(eSSParams, tmp, row);
for (int j = 0; j < eSSParams->n_params; ++j){
eSSParams->refSet->members[i].params[j] = row[j];
}
evaluate_Individual(eSSParams, &(eSSParams->refSet->members[i]), inp, out);
free(tmp);
i++;
}
}
free(min_dists);
for (int i = 0; i < (eSSParams->n_scatterSet - b/2); ++i){
free(dist_matrix[i]);
}
free(dist_matrix);
}
void init_perform_warm_start(eSSType *eSSParams){
int i;
char line[4098];
printf("Loading the data to perform warm start...\n");
printf("Loaded refSet: \n");
// Read refSet
i = 0;
FILE* refSetStream = fopen("ref_set_final.csv", "r");
while (fgets(line, 4098, refSetStream) && (i < eSSParams->n_refSet))
{
double row[eSSParams->n_params + 1];
char* tmp = strdup(line);
parse_double_row(eSSParams, tmp, row);
for (int j = 0; j < eSSParams->n_params; ++j){
eSSParams->refSet->members[i].params[j] = row[j];
}
eSSParams->refSet->members[i].cost = row[eSSParams->n_params];
free(tmp);
i++;
}
eSSParams->best = (Individual *)malloc(sizeof(Individual));
eSSParams->best = &(eSSParams->refSet->members[0]); // The first members of ref_set is always the best
print_Set(eSSParams, eSSParams->refSet);
print_Ind(eSSParams, eSSParams->best);
// Read freqMat
// i = 0;
// FILE* freqMatStream = fopen("freq_mat_final.csv", "r");
// while (fgets(line, 4098, freqMatStream) && (i < eSSParams->n_params ))
// {
// int row[eSSParams->p];
// char* tmp = strdup(line);
// parse_int_row(eSSParams, tmp, row);
// memcpy(eSSParams->freqs_matrix[i], (int*)row, eSSParams->p * sizeof(int));
// free(tmp);
// i++;
// }
// // print_int_matrix(eSSParams, eSSParams->freqs_matrix, eSSParams->n_params, eSSParams->p);
// // Read probMat
// i = 0;
// FILE* probMatStream = fopen("prob_mat_final.csv", "r");
// while (fgets(line, 4098, probMatStream))
// {
// double row[eSSParams->p];
// char* tmp = strdup(line);
// parse_double_row(eSSParams, tmp, row);
// memcpy(eSSParams->probs_matrix[i], row, eSSParams->p * sizeof(double));
// free(tmp);
// i++;
// }
// print_double_matrix(eSSParams, eSSParams->probs_matrix, eSSParams->n_params, eSSParams->p);
}