forked from Dedsec-Xu/Coloring_Problem
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
437 lines (385 loc) · 12.8 KB
/
main.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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define status int
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define OVERFLOW -2
#define Maxiter 1000000
#define DESCOLOR 12
#define maxdelt 1000000
using namespace std;
typedef struct edgeNode
{
int name;
struct edgeNode *next;
} EdgeNode, *EdgeNodePtr;
typedef struct headNode
{
int degree;
EdgeNodePtr Edge;
} HeadNode, *HeadNodePtr;
//typedef struct TabuList{
//int ;
//EdgeNodePtr Edge;
//}HeadNode, *HeadNodePtr;
clock_t tstart, tend;
char debug;
void insertEdge(HeadNodePtr H, int start, int target);
void insertEdge2(HeadNodePtr H, int start, int target);
void TabuSearch(
HeadNodePtr H,
int &delta,
int sol[],
int Tabulist[][DESCOLOR],
int Adjacent_Color_Table[][DESCOLOR],
int size_node, int size_edge, FILE *log);
void FindMove(int &u, int &vi, int &vj, int &delt, int delta, int min_f, int iter, int Tabulist[][DESCOLOR], int Adjacent_Color_Table[][DESCOLOR], int sol[], int size_node);
void MakeMove(HeadNodePtr H, int u, int vi, int vj, int delt, int sol[], int &delta, int &min_f, int iter, int Tabulist[][DESCOLOR], int Adjacent_Color_Table[][DESCOLOR]);
int main()
{
FILE *graphdata; //graphdata in file
FILE *log; //log file
char path[100]; //file directory
int size_node, size_edge; //size of the graph
int n_1, n_2; //two datas
int i, j, tt;
int color;
int TabuTenure;
int max = Maxiter;
EdgeNodePtr Operator;
EdgeNodePtr head;
unsigned int seed = time(NULL);
srand(seed);
//*****************************
// fileopen
//*****************************
//printf("File Directory:");
//scanf("%s",path);
graphdata = fopen("E:\\Workspace\\SMARTLAB\\Coloring_Problem\\instances\\DSJC500.1.col", "r");
log = fopen("log.txt", "w");
color = DESCOLOR;
fscanf(graphdata, "%d %d", &size_node, &size_edge);
fprintf(log, "opened file :\n%d nodes and %d edges.\nRandom seed: %u\n", path, size_node, size_edge, seed);
fprintf(log, "color: %d\nMaxiter: %d\n", color, max);
printf("opened file :\n%d nodes and %d edges.\nRandom seed: %u\n", path, size_node, size_edge, seed);
printf("color: %d\nMaxiter: %d\n", color, max);
printf("DEBUG?(y):");
scanf("%c", &debug);
//int sol[5] = {0, 0, 1, 2, 2};
int sol[size_node];
int Tabulist[size_node][DESCOLOR];
int Adjacent_Color_Table[size_node][DESCOLOR];
int delta = 0;
//*****************************
// prepare head node
//*****************************
HeadNodePtr H = (HeadNodePtr)malloc(size_node * sizeof(HeadNode));
HeadNodePtr H2 = (HeadNodePtr)malloc(size_node * sizeof(HeadNode));
for (i = 0; i < size_node; i++)
{
(H + i)->degree = 0;
(H + i)->Edge = NULL;
(H2 + i)->degree = 0;
(H2 + i)->Edge = NULL;
sol[i] = rand() % color; //color form 0 to color - -1
printf("sol[%d] = %d\n", i, sol[i]);
for (j = 0; j < color; j++)
{
Tabulist[i][j] = 0;
Adjacent_Color_Table[i][j] = 0;
}
}
//*****************************
// create graph
//*****************************
while (!feof(graphdata))
{
fscanf(graphdata, "%d%d", &n_1, &n_2);
//printf("%d %d\n", n_1, n_2);
// printf("%d\t%d\n", n_1, n_2);
insertEdge(H, n_1, n_2);
insertEdge2(H2, n_1, n_2);
}
//*****************************
// initialize Adjacent_Color_Table
//*****************************
int arc;
int thiscolor;
for (i = 0; i < size_node; i++)
{
Operator = (H + i)->Edge;
//printf("\n%d",size_node);
if (Operator != NULL)
{
for (; Operator != NULL;)
{
arc = Operator->name;
thiscolor = sol[arc];
Adjacent_Color_Table[i][thiscolor] += 1;
//printf("\n\nAdjacent_Color_Table[%d][%d] = %d\n", i, thiscolor, Adjacent_Color_Table [i][thiscolor]);
//printf("Adjacent_Color_Table [%d][sol[%d]] = %d;\n", i, arc, Adjacent_Color_Table [i][thiscolor]);
//printf("%d ==== %d\n", i , Operator->name);
if (sol[i] == thiscolor)
{
delta += 1;
printf("+1\n");
}
Operator = Operator->next;
}
}
}
delta /= 2;
/*for(i = 0;i < size_node; i++)
{
printf("\n");
for(j = 0; j < DESCOLOR; j++)
{
printf("%d\t", Adjacent_Color_Table [i][j]);
}
}*/
printf("\n%d\n\n\n\n\n\n\nrunning\n", delta);
TabuSearch(H, delta, sol, Tabulist, Adjacent_Color_Table, size_node, size_edge, log);
int soll = 1;
for (i = 0; i < size_node; i++)
{
Operator = (H2 + i)->Edge;
//printf("\n%d",size_node);
for (; Operator != NULL;)
{
soll *= ((sol[Operator->name] == sol[i]) ? 0 : 1);
//printf("Adjacent_Color_Table [%d][sol[%d]] = %d;\n", i, Operator->name,Adjacent_Color_Table [i][sol[Operator->name]]);
Operator = Operator->next;
}
}
if (soll == 1)
{
printf("\nno conflict\n");
fprintf(log, "\nno conflict\n");
}
else
{
printf("\nconflict!!!1\n");
fprintf(log, "\nconflict!!!\n");
}
printf("\ntime = %f\nEnter to quit:", (double)tend - tstart / CLK_TCK);
fprintf(log, "\ntime = %f", (double)tend - tstart / CLK_TCK);
fclose(graphdata);
fclose(log);
getchar();
getchar();
getchar();
return 0;
}
void insertEdge(HeadNodePtr H, int start, int target)
{
EdgeNodePtr Operator;
(H + start - 1)->degree += 1;
if ((H + start - 1)->Edge == NULL)
{
(H + start - 1)->Edge = (EdgeNodePtr)malloc(sizeof(EdgeNode));
Operator = (H + start - 1)->Edge;
}
else
{
for (Operator = (H + start - 1)->Edge; Operator->next != NULL; Operator = Operator->next)
;
Operator->next = (EdgeNodePtr)malloc(sizeof(EdgeNode));
Operator = Operator->next;
}
Operator->name = target - 1;
Operator->next = NULL;
//printf("%d ==== %d\n", target , start);
//printf("%d %d\n", start - 1, Operator->name);
(H + target - 1)->degree += 1;
if ((H + target - 1)->Edge == NULL)
{
(H + target - 1)->Edge = (EdgeNodePtr)malloc(sizeof(EdgeNode));
Operator = (H + target - 1)->Edge;
}
else
{
for (Operator = (H + target - 1)->Edge; Operator->next != NULL; Operator = Operator->next)
;
Operator->next = (EdgeNodePtr)malloc(sizeof(EdgeNode));
Operator = Operator->next;
}
Operator->name = start - 1;
Operator->next = NULL;
}
void insertEdge2(HeadNodePtr H, int start, int target)
{
EdgeNodePtr Operator;
int i;
(H + start - 1)->degree += 1;
if ((H + start - 1)->Edge == NULL)
{
(H + start - 1)->Edge = (EdgeNodePtr)malloc(sizeof(EdgeNode));
//printf("start %d LOC = %x\n", start - 1, (H + start - 1)->Edge);
Operator = (H + start - 1)->Edge;
Operator->name = target - 1;
Operator->next = NULL;
}
else
{
for (Operator = (H + start - 1)->Edge; Operator->next != NULL; Operator = Operator->next)
{
i++;
}
Operator->next = (EdgeNodePtr)malloc(sizeof(EdgeNode));
Operator = Operator->next;
Operator->name = target - 1;
Operator->next = NULL;
}
}
void TabuSearch(
HeadNodePtr H,
int &delta,
int sol[],
int Tabulist[][DESCOLOR],
int Adjacent_Color_Table[][DESCOLOR],
int size_node, int size_edge, FILE *log)
{
int iter, u, vi, vj, delt, i;
iter = 0;
int min_f = delta;
tstart = clock();
for (; delta != 0; iter++)
{
FindMove(u, vi, vj, delt, delta, min_f, iter,
Tabulist,
Adjacent_Color_Table,
sol,
size_node);
MakeMove(H, u, vi, vj, delt, sol, delta, min_f, iter, Tabulist, Adjacent_Color_Table);
}
tend = clock();
for (i = 0; i < size_node; i++)
{
printf("sol[%d] = %d\n", i, sol[i]);
fprintf(log, "sol[%d] = %d\n", i, sol[i]);
}
}
void FindMove(int &u, int &vi, int &vj, int &delt, int delta, int min_f, int iter, int Tabulist[][DESCOLOR], int Adjacent_Color_Table[][DESCOLOR], int sol[], int size_node)
{
int i, k, vit, vjt;
int prevdelt = 100000;
int prevdeltt = 100000;
u = -1;
int ut = -1;
int c = rand();
int samed = 0, samedt = 0;
int mark = 0;
for (i = 0; i < size_node; i++)
{
if (Adjacent_Color_Table[i][sol[i]] > 0)
{
for (k = 0; k < DESCOLOR; k++)
{
if (k != sol[i])
{
delt = Adjacent_Color_Table[i][k] - Adjacent_Color_Table[i][sol[i]]; //calculate delt value of the move <i, sol[i], k>
//printf("delt:%d = %d[%d][%d] - %d[%d][%d]\n", delt, Adjacent_Color_Table[i][k], i, k, Adjacent_Color_Table[i][sol[i]], i, sol[i]);
if (Tabulist[i][k] > iter)
{
if (delt < prevdeltt)
{
ut = i;
vit = sol[i];
vjt = k;
prevdeltt = delt;
//update the tabu best move;
//printf("update the tabu best move %d\n",delt);
samedt = 0;
}
if (delt == prevdeltt)
{
samedt++;
if (rand() % samedt == 0)
{
ut = i;
vit = sol[i];
vjt = k;
prevdeltt = delt;
//update the tabu best move;
//printf("update the tabu best move %d\n",delt);
}
}
}
else
{
if (delt < prevdelt)
{
u = i;
vi = sol[i];
vj = k;
prevdelt = delt;
//update the non-tabu best move;
//printf("update the non-tabu best move; %d\n",delt);
samed = 0;
mark = 1;
}
if (delt == prevdeltt)
{
samed++;
if (rand() % samed == 0)
{
u = i;
vi = sol[i];
vj = k;
prevdelt = delt;
//update the tabu best move;
//printf("update the tabu best move %d\n",delt);
}
}
}
}
}
}
}
if (prevdeltt + delta < min_f)
{
u = ut;
vi = sol[u];
vj = vjt;
//printf("tabu");
}
else if (!mark)
{
u = rand() % size_node;
vj = (sol[u] + rand() % (DESCOLOR - 1) + 1) % DESCOLOR;
//printf("rand");
}
//printf("%d %d %d\n", u, vi, vj);
vi = sol[u];
delt = Adjacent_Color_Table[u][vj] - Adjacent_Color_Table[u][vi];
//printf("delt:%d = %d[%d][%d] - %d[%d][%d]\n", delt, Adjacent_Color_Table[u][vj], u, vj, Adjacent_Color_Table[u][vi], u, vi);
return;
}
void MakeMove(HeadNodePtr H, int u, int vi, int vj, int delt, int sol[], int &delta, int &min_f, int iter, int Tabulist[][DESCOLOR], int Adjacent_Color_Table[][DESCOLOR])
{
EdgeNodePtr Operator;
sol[u] = vj;
delta += delt;
//if((debug == 'y')||(debug == 'Y') )printf("%d \r", delta);
if (delta < min_f)
{
if ((debug == 'y') || (debug == 'Y'))
printf("%d\n", delta);
min_f = delta;
}
//printf("delta + %d = %d\n", delt, delta);
Tabulist[u][vi] = iter + delta + rand() % 10;
EdgeNodePtr tempPtr = (H + u)->Edge;
while (tempPtr != NULL)
{
--(Adjacent_Color_Table[tempPtr->name][vi]);
++(Adjacent_Color_Table[tempPtr->name][vj]);
//printf("\n\nAdjacent_Color_Table[%d][%d] = %d\n", tempPtr->name, vi, Adjacent_Color_Table[tempPtr->name][vi]);
//printf("Adjacent_Color_Table[%d][%d] = %d\n", tempPtr->name, vj, Adjacent_Color_Table[tempPtr->name][vj]);
tempPtr = tempPtr->next;
}
}