-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathevaluation.c
305 lines (231 loc) · 7.01 KB
/
evaluation.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
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#include<string.h>
#include<time.h>
#include "VNS.h"
#include "HLCio/advio.h"
#include "HLCio/dataframe.h"
#define GVNS_STOPPING_CONDITION 300
// #define EVALUATION_FACTOR 20
/// the quick sort algorithm is taken from geeksforgeeks platform!
void swap(int* a, int* b)
{
int t = *a;
*a = *b;
*b = t;
}
int partition (int arr[], int xi[], int low, int high)
{
int pivot = arr[high]; // pivot
int i = (low - 1); // Index of smaller element
for (int j = low; j <= high- 1; j++)
{
if (arr[j] < pivot)
{
i++;
swap(&arr[i], &arr[j]);
swap(&xi[i], &xi[j]);
}
}
swap(&arr[i + 1], &arr[high]);
swap(&xi[i + 1], &xi[high]);
return (i + 1);
}
void quickSort(int arr[], int xi[], int low, int high)
{
if (low < high)
{
int pi = partition(arr, xi, low, high);
quickSort(arr, xi, low, pi - 1);
quickSort(arr, xi, pi + 1, high);
}
}
POLICY LPT(){
POLICY x = arrcreate(500);
int s = vns_config.ds->len_cols;
int arr[s];
int xi[s];
for (int i = 0; i < s; i++){
arr[i] = vns_config.ds->data[1][i].node.Int;
xi[i] = i;
}
quickSort(arr, xi, 0, s);
for(int i = 0; i<x.node.Arr->size; i++){
x.node.Arr->data[i].type = DF_ELEMENT_TInt;
x.node.Arr->data[i].node.Int = xi[i];
}
return x;
}
CMP_RESULT min(DF_ELEMENT e1, DF_ELEMENT e2, int i, int j){
CMP_RESULT r;
if(e1.node.Int < e2.node.Int){
r.best = e1;
r.index = i;
}
else{
r.best = e2;
r.index = j;
}
return r;
}
CMP_RESULT max(DF_ELEMENT e1, DF_ELEMENT e2, int i, int j){
CMP_RESULT r;
if(e1.node.Int > e2.node.Int){
r.best = e1;
r.index = i;
}
else{
r.best = e2;
r.index = j;
}
return r;
}
OPT_VAL f(POLICY x){
OPT_VAL ov;
DF_ELEMENT loader;
loader.node.Int = 0;
DF_ELEMENT initval;
initval.type = DF_ELEMENT_TInt;
initval.node.Int = 0;
DF_ELEMENT machines = arrinit(vns_config.other_params[1], initval);
for(int i = 0;i<x.node.Arr->size; i++){
CMP_RESULT mn = arrcmp(&machines, min);
CMP_RESULT mx = max(mn.best, loader, 0, 0);
loader.node.Int = mx.best.node.Int + vns_config.ds->data[1][x.node.Arr->data[i].node.Int].node.Int;
machines.node.Arr->data[mn.index].node.Int = loader.node.Int + vns_config.ds->data[0][x.node.Arr->data[i].node.Int].node.Int;
}
CMP_RESULT mx = arrcmp(&machines, max);
ov = mx.best;
arrfree(&machines);
return ov;
}
CMP_RESULT cmp(POLICY x, POLICY bx, int i, int j){
CMP_RESULT r;
if(vns_config.f(x).node.Int < vns_config.f(bx).node.Int){
r.best = df_element_copy(x);
r.index = i;
}
else{
r.best = df_element_copy(bx);
r.index = j;
}
return r;
}
POLICY* s1(POLICY x, int i){
return block_swapping(x, 10, 0, i);
}
POLICY* s2(POLICY x, int i){
return block_swapping(x, 4, 30, i);
}
POLICY* s3(POLICY x, int i){
return block_swapping(x, 1, 100, i);
}
POLICY* s4(POLICY x, int i){
return block_reversing(x, 20, i);
}
POLICY* s5(POLICY x, int i){
return block_reversing(x, 30, i);
}
POLICY* s6(POLICY x, int i){
return block_reversing(x, 10, i);
}
int main(int argc, char *args[]){
char *filename = args[1];
int EVALUATION_FACTOR = atoi(args[2]);
vns_config.f = f;
vns_config.cmp_optimality = cmp;
vns_config.local_search = first_improvement;
// vns_config.STOCHASTIC_LR_FAILURE_LIMIT = 300;
FILE *fds = fopen(filename, "r");
if(fds == NULL)
exit(EXIT_FAILURE);
// load the number of commodities and the number of drivers :
vns_config.other_params = (int*)malloc(sizeof(int) * 2);
char* fline = get_line(fds);
vns_config.other_params[0] = atoi(strtok(fline, " "));
vns_config.other_params[1] = atoi(strtok(NULL, " "));
free(fline);
rewind(fds);
printf("\n %d ", vns_config.other_params[0]);
// load the commodities processing time / set up time :
vns_config.ds = csv_to_df(fds, 1, "\t");
// retyping the data to int :
df_retype(vns_config.ds, DF_ELEMENT_TInt, 0);
// display_df(vns_config.ds, 0);
// initiating the solution with LONGEST PROCESSING TIME first :
// POLICY bx = LPT();
POLICY bx = arrcreate(vns_config.other_params[0]);
for(int i = 0; i<bx.node.Arr->size; i++){
bx.node.Arr->data[i].type = DF_ELEMENT_TInt;
bx.node.Arr->data[i].node.Int = i;
}
OPT_VAL ov = vns_config.f(bx);
printf(" %d ", ov.node.Int);
// creating the first neighborhood structure set :
NEIGHBORHOOD_STRUCTURES N1 = neistructs(3);
N1[0] = s1;
N1[1] = s2;
N1[2] = s3;
// creating the second neighborhood structure set :
NEIGHBORHOOD_STRUCTURES N2 = neistructs(3);
N2[0] = s4;
N2[1] = s5;
N2[2] = s6;
int ev_counter = EVALUATION_FACTOR;
DF_ELEMENT EVA_ARR_OV = arrcreate(EVALUATION_FACTOR);
DF_ELEMENT EVA_ARR_ET = arrcreate(EVALUATION_FACTOR);
while(ev_counter > 0){
printf("\n iter %d\n", EVALUATION_FACTOR - ev_counter);
int i;
double total_time;
clock_t start, end;
start = clock();
//time count starts
srand(time(NULL));
// running GVNS :
POLICY x = GVNS(bx, N2, N1, 3, 3, 15);
arrshow(&x);
printf("here");
ov = vns_config.f(x);
end = clock();
total_time = ((double) (end - start)) / CLOCKS_PER_SEC;
EVA_ARR_OV.node.Arr->data[EVALUATION_FACTOR - ev_counter].node.Int = ov.node.Int;
EVA_ARR_OV.node.Arr->data[EVALUATION_FACTOR - ev_counter].type = DF_ELEMENT_TInt;
EVA_ARR_ET.node.Arr->data[EVALUATION_FACTOR - ev_counter].node.Int = (int) total_time;
EVA_ARR_ET.node.Arr->data[EVALUATION_FACTOR - ev_counter].type = DF_ELEMENT_TInt;
arrfree(&x);
ev_counter--;
}
printf("\nEVALUATION FACTOR : %d\n", EVALUATION_FACTOR);
CMP_RESULT cr = arrcmp(&EVA_ARR_OV, min);
printf("\nOV MIN : %d ", cr.best.node.Int);
cr = arrcmp(&EVA_ARR_OV, max);
printf("\nOV MAX : %d ", cr.best.node.Int);
int avg = 0;
int sum = 0;
for (int i = 0; i < EVALUATION_FACTOR; i++){
sum += EVA_ARR_OV.node.Arr->data[i].node.Int;
}
avg = sum/EVALUATION_FACTOR;
printf("\nOV AVG : %d ", avg);
cr = arrcmp(&EVA_ARR_ET, min);
printf("\nET MIN : %d ", cr.best.node.Int);
cr = arrcmp(&EVA_ARR_ET, max);
printf("\nET MAX : %d ", cr.best.node.Int);
avg = 0;
sum = 0;
for (int i = 0; i < EVALUATION_FACTOR; i++){
sum += EVA_ARR_ET.node.Arr->data[i].node.Int;
}
avg = sum/EVALUATION_FACTOR;
printf("\nET AVG : %d ", avg);
arrfree(&EVA_ARR_OV);
arrfree(&EVA_ARR_ET);
free(N1);
free(N2);
arrfree(&bx);
fclose(fds);
fVNS();
exit(EXIT_SUCCESS);
}