-
Notifications
You must be signed in to change notification settings - Fork 0
/
betaRoute.cpp
557 lines (445 loc) · 16.6 KB
/
betaRoute.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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
/*
* BetaCome: betacome of future network routing
*
* Code to accompany the competition:
* huawei software challenge : future network routing,
* Yuanyuan Qin, Song Tang,2016
*
* Copyright (C) 2016 Yuanyuan Qin, Peking University, Shenzhen, China
*
* This file is part of BetaCome.
*
* BetaCome is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* BetaCome is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with BetaCome. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "betaRoute.h"
#include "assist.h"
#include <iostream>
#include <vector>
#include <list>
#include <cassert>
#include <queue>
using namespace std;
BetaRoute::BetaRoute()
{
}
BetaRoute::~BetaRoute()
{
}
//new algorithm of compute path
int BetaRoute::cmPath(GraphVector &graph, VPrimeToUse &vPrime, Path &path)
{
//reset path,if path isn't empty,we random the order of vprime
if (!(path.getPathList().empty()))
{
//reset path to empty
path.resetSelf();
//reset graph node to unused
path.setAllNodesUnused();
//re-arrange in random inoutdegree order,still meet scc order
vPrime.refreshVPrime(graph, path);
//vPrime.randomOrder();//////////////??????????here i do not write this func
}
#ifdef DEBUG
vPrime.printVprime();
#endif
int repairPieceFlag = false;
int firstNode = 0;//used for search of bfsPathPiece
int secondNode = 0;//used for search of bfsPathPiece
int algorithmFlag;//result of algorithm
int crackFlag = NOCRACK;//flag index that whether we bringing in cracked piece
int pieceFirstNode = 0;
int pieceBeforeFirstNode = 0;
int pieceLastNode = 0;
int pieceAfterLastNode = 0;
int timeCnt = 0;
list<PathPiece> &pathList = path.getPathList();
//when there is no vprime in path
if (path.getVPrimeSize() == 0)
{
//find a path from start to end
path.bfsPathPiece(graph, path.getVStart(), path.getVEnd(), false);
//according to pathpiece we searched, add path piece or repair path piece
path.createPieceList(graph, path.getVStart(), path.getVEnd(), path.getPathList().end());
#ifdef DEBUG
cout << "@_@ cmpath find a path: " << endl;
path.printPath();
#endif
return HAVESOLUTION;//we find a path
}
//when there is no piece in path
if (path.getPathList().empty())
{
firstNode = path.getVStart();
secondNode = vPrime.begin()->getPointId();
//because this is the first node,not point has been used
path.bfsPathPiece(graph, firstNode, secondNode, false);
//according to pathpiece we searched, add path piece or repair path piece
path.createPieceList(graph, firstNode, secondNode, path.getPathList().end());
#ifdef DEBUG
cout << "add first piece, firstNode: " << firstNode << " ,secondNode: " << secondNode << endl;
path.printPath();
#endif
}
while (1)
{
#ifdef DEBUG
if (path.checkUsedProperty(graph) == WRONG)
{
path.printAllNodeInPath();
assert(0);
}
#endif
#ifndef DEBUG
timeCnt++;
if (!(timeCnt & 0x0000000f))//every xx times,check time
{
timeMeasure.timeAll = getTime();
if (timeMeasure.timeAll>9200)
{
cout << "cmpath end for time out............................." << endl;
return NOSOLUTION;
}
}
#endif
//when maybe have cracked piece
if (crackFlag != NOCRACK)
{
//deal with cracked pieces first
list<PathPiece>::iterator iterTemp;
for (iterTemp = pathList.begin(); iterTemp != pathList.end(); ++iterTemp)
{
pieceFirstNode = (*iterTemp).getFirstNode();
pieceLastNode = (*iterTemp).getLastNode();
if ((*iterTemp).getLinkProperty() == CRACKED)
{
if ((path.getCrackTimes()[pieceFirstNode][pieceLastNode]<CRACKLIMITS))
{
//由于如果删除了piece,当前的迭代器会失效,所以需要从头开始,无法避免
path.connectTwoNodes(graph, pieceFirstNode, pieceLastNode,true);
#ifdef DEBUG
cout << "repair cracked piece<CRACKLIMITS may with new crack, firstNode: " << pieceFirstNode << " ,secondNode: " << pieceLastNode << endl;
path.printPath();
path.printAllNodeInPath();
#endif
repairPieceFlag = true;//flag==1表示对crack边进行了修复
break;//jump out of for loop,still in while loop
}
else if ((iterTemp == pathList.begin()) || (iterTemp == (--(pathList.end()))))
{
//for: Vstart x b->n
if (iterTemp == pathList.begin())
{
auto iter = iterTemp; ++iter;
pieceAfterLastNode = (*iter).getLastNode();
auto iterPiece = iterTemp;
iterPiece = path.deleteOnePiece(graph, iterPiece, true);
iterPiece = path.deleteOnePiece(graph, iterPiece, true);
vPrime.insertVPrime(graph, path, pieceLastNode);
//here firstnode is vstart
path.insertCrackPiece(graph, pieceFirstNode, pieceAfterLastNode, iterPiece);
#ifdef DEBUG
cout << "delete 1 point to vprime2use and new crack happen, VStart: "
<< pieceFirstNode << " ,secondNode: " << pieceAfterLastNode << endl;
path.printPath();
path.printAllNodeInPath();
#endif
repairPieceFlag = true;//because of new crack,we should check from begining
break;//jump out of for loop,still in while loop
}
else//for: m->a x Vend
{
auto iter = iterTemp; --iter;
pieceBeforeFirstNode = (*iter).getFirstNode();
auto iterPiece = iter;
iterPiece = path.deleteOnePiece(graph, iterPiece, true);
iterPiece = path.deleteOnePiece(graph, iterPiece, true);
vPrime.insertVPrime(graph, path, pieceFirstNode);
//here pieceLastNode is vend
path.insertCrackPiece(graph, pieceBeforeFirstNode, pieceLastNode, iterPiece);
#ifdef DEBUG
cout << "delete 1 point to vprime2use and new crack happen, firstNode: "
<< pieceBeforeFirstNode << " ,VEnd: " << pieceLastNode << endl;
path.printPath();
path.printAllNodeInPath();
#endif
repairPieceFlag = true;//because of new crack,we should check from begining
break;//jump out of for loop,still in while loop
}
}
else//bigger than cracklimits
{
//we should remove a or b vprime point,and add it to list vprimeToUse
//the cracked piece is like: m->a x b->n
auto iter = iterTemp; --iter;
pieceBeforeFirstNode = (*iter).getFirstNode();
path.setPieceNodeFalse(graph, iter);//set v points in piece m->a false
//if find a piecelist of m~b
if (path.bfsPathPiece(graph, pieceBeforeFirstNode, pieceLastNode, false) == FOUND)
{
//remove vprime point and add to vprimeToUse
auto iterIn = path.deleteOneVPrime(graph, pieceFirstNode);//iterIn should points to b->n
vPrime.insertVPrime(graph, path, pieceFirstNode);
//attention: must prevent invalid of iterTemp
iterTemp = path.createPieceList(graph, pieceBeforeFirstNode, pieceLastNode, iterIn);
#ifdef DEBUG
cout << "repair cracked piece>CRACKLIMITS(1), firstNode: " << pieceBeforeFirstNode << " ,secondNode: " << pieceLastNode << endl;
path.printPath();
path.printAllNodeInPath();
#endif
continue;
}
else//for: m->a x b->n, can not find a path of m->b,find path a->n
{
path.setPieceNodeTrue(graph, iter);//set v points in piece m->a true
iter = iterTemp; ++iter;
pieceAfterLastNode = (*iter).getLastNode();
path.setPieceNodeFalse(graph, iter);//set v points in piece b->n false
//if find a piecelist of a~n
if (path.bfsPathPiece(graph, pieceFirstNode, pieceAfterLastNode, false) == FOUND)
{
//remove vprime point and add to vprimeToUse
auto iterIn = path.deleteOneVPrime(graph, pieceLastNode);
vPrime.insertVPrime(graph, path, pieceLastNode);
//iterIn add after sub for preventing invalid of iterTemp
iterTemp = path.createPieceList(graph, pieceFirstNode, pieceAfterLastNode, iterIn);
#ifdef DEBUG
cout << "repair cracked piece>CRACKLIMITS(2), firstNode: " << pieceFirstNode << " ,secondNode: " << pieceAfterLastNode << endl;
path.printPath();
path.printAllNodeInPath();
#endif
continue;
}
else // m->a x b->n,find piecelist of a~n
{
iter = iterTemp; --iter;
path.setPieceNodeFalse(graph, iter);//set v points in piece m->a false
//if find a piecelist of m~n
if (path.bfsPathPiece(graph, pieceBeforeFirstNode, pieceAfterLastNode, false) == FOUND)
{
auto iterIn = iterTemp; --iterIn;
//remove vprime point and add to vprimeToUse
iterIn = path.deleteOnePiece(graph, iterIn, true);
iterIn = path.deleteOnePiece(graph, iterIn, true);
iterIn = path.deleteOnePiece(graph, iterIn, true);
vPrime.insertVPrime(graph, path, pieceFirstNode);
vPrime.insertVPrime(graph, path, pieceLastNode);
iterTemp = path.createPieceList(graph, pieceBeforeFirstNode, pieceAfterLastNode, iterIn);
#ifdef DEBUG
cout << "repair cracked piece>CRACKLIMITS(3), firstNode: " << pieceBeforeFirstNode << " ,secondNode: " << pieceAfterLastNode << endl;
path.printPath();
path.printAllNodeInPath();
#endif
continue;
}
else//if can not,insert a crack piece of m x n,delete m->a x b->n,and add a b to vprime
{
auto iterPiece = iterTemp; --iterPiece;
iterPiece = path.deleteOnePiece(graph, iterPiece, true);
iterPiece = path.deleteOnePiece(graph, iterPiece, true);
iterPiece = path.deleteOnePiece(graph, iterPiece, true);
vPrime.insertVPrime(graph, path, pieceFirstNode);
vPrime.insertVPrime(graph, path, pieceLastNode);
path.insertCrackPiece(graph, pieceBeforeFirstNode, pieceAfterLastNode, iterPiece);
#ifdef DEBUG
cout << "delete 2 point to vprime2use and new crack happen, firstNode: "
<< pieceFirstNode << " ,secondNode: " << pieceAfterLastNode << endl;
path.printPath();
path.printAllNodeInPath();
#endif
repairPieceFlag = true;//because of new crack,we should check from begining
break;//jump out of for loop,still in while loop
}
}
}
}
}//if cracked
}//for loop
}
if (repairPieceFlag == true)//如果对cracked边进行了修复,可能还有其他cracked边,需要返回重新用for loop检查
{
repairPieceFlag = false;
continue;
}
else//if there are no cracked pieces in path
{
crackFlag = NOCRACK;
firstNode = path.getLastNode();
//delete all used vprime points, these vprime points may still in vprime
//time complexity O(50)
vPrime.deleteUsedVPrime(graph, path);
//if vprime is empty
if (vPrime.empty())
{
//if lastnode is vend, over
if (path.getLastNode() == path.getVEnd())
{
return HAVESOLUTION;//we find a path
}
else
{
//set lastnode to be vend
if (path.connectTwoNodes(graph, path.getLastNode(), path.getVEnd(), true) == FOUND)
{
#ifdef DEBUG
cout << "@_@ cmpath find a path, link to end: " << path.getVEnd() << endl;
path.printPath();
path.printAllNodeInPath();
#endif
return HAVESOLUTION;//we find a path
}
else
{
#ifdef DEBUG
cout << "@_@ cmpath find a path with crack, link to end: " << path.getVEnd() << endl;
path.printPath();
path.printAllNodeInPath();
#endif
//crack piece,continue
crackFlag = HAVECRACK;
continue;
}
}
}
//2: if begin's scc is biger than lastnode of path,or have add vend to path
//use "vprime insert" algorithm
if ((path.getLastNode() == path.getVEnd()) || (vPrime.begin()->getSccNum() > graph.getNode(path.getLastNode()).getSccNum()))
{
algorithmFlag = ALGORITHMFAILURE;
auto iterEnd = vPrime.curSccEnd(graph, path);
for (auto iterTemp = vPrime.begin(); iterTemp != iterEnd; ++iterTemp)
{
if (path.insertVPrimeToPath(graph, (*iterTemp).getPointId(),false) == ALGORITHMSUCCESS)
{
#ifdef DEBUG
cout << "insertVPrimeToPath no crack happen(up),vprime: " << (*iterTemp).getPointId() << endl;
path.printPath();
path.printAllNodeInPath();
#endif
//delete vprime we have used
vPrime.deleteVPrime(iterTemp);
//flag = success,we do not need to run other algorithm
algorithmFlag = ALGORITHMSUCCESS;
//get out of for loop
break;
}
}
//if algorthim 1 and 2, both are useless
//4: we have to use violent insert
if (algorithmFlag == ALGORITHMFAILURE)
{
auto iterEnd = vPrime.curSccEnd(graph, path);
for (auto iterTemp = vPrime.begin(); iterTemp != iterEnd; ++iterTemp)
{
if (path.insertVPrimeToPath(graph, (*iterTemp).getPointId(),true) == ALGORITHMSUCCESS)
{
#ifdef DEBUG
cout << "insertVPrimeToPath crack happen,vprime: " << (*iterTemp).getPointId() << endl;
path.printPath();
path.printAllNodeInPath();
#endif
//delete vprime we have used
vPrime.deleteVPrime(iterTemp);
//flag = success,we do not need to run other algorithm
algorithmFlag = ALGORITHMSUCCESS;
crackFlag = HAVECRACK;
//get out of for loop
break;
}
}
#ifdef DEBUG
assert(algorithmFlag == ALGORITHMSUCCESS);//this must happen,but may not
#endif
}
}
else
{
//1: if begin's scc is not biger than lastnode of path
//use "later vprime in advance" algorithm
algorithmFlag = ALGORITHMFAILURE;
auto iterEnd = vPrime.curSccEnd(graph, path);
for (auto iterTemp = vPrime.begin(); iterTemp != iterEnd; ++iterTemp)
{
secondNode = (*iterTemp).getPointId();
if (path.bfsPathPiece(graph, firstNode, secondNode, false) == FOUND)
{
path.createPieceList(graph, firstNode, secondNode, path.getPathList().end());
//delete vprime we have used
vPrime.deleteVPrime(iterTemp);
//flag = success,we do not need to run other algorithm
algorithmFlag = ALGORITHMSUCCESS;
#ifdef DEBUG
cout << "later vprime in advance, firstNode: " << firstNode << " ,secondNode: " << secondNode << endl;
path.printPath();
path.printAllNodeInPath();
#endif
//get out of for loop
break;
}
}
//2: if "later vprime in advance" algorithm is useless
//use "vprime insert" algorithm instead
if (algorithmFlag == ALGORITHMFAILURE)
{
auto iterEnd = vPrime.curSccEnd(graph, path);
for (auto iterTemp = vPrime.begin(); iterTemp != iterEnd; ++iterTemp)
{
if (path.insertVPrimeToPath(graph, (*iterTemp).getPointId(),false) == ALGORITHMSUCCESS)
{
#ifdef DEBUG
cout << "insertVPrimeToPath no crack happen(down),vprime: " << (*iterTemp).getPointId() << endl;
path.printPath();
path.printAllNodeInPath();
#endif
//delete vprime we have used
vPrime.deleteVPrime(iterTemp);
//flag = success,we do not need to run other algorithm
algorithmFlag = ALGORITHMSUCCESS;
//get out of for loop
break;
}
}
}
//if algorthim 1 and 2, both are useless
//3: here we have to use violent bfs
if (algorithmFlag == ALGORITHMFAILURE)
{
//use used points to search pathpiece
if (path.bfsPathPiece(graph, firstNode, secondNode, true) == NOTFOUND)
{
return NOSOLUTION;
}
//deal with used points and pathpiece associated with
//restore:used/pathpieceone/pathpiecetwo
path.dealUsedNodes(graph, firstNode, secondNode);
path.createPieceList(graph, firstNode, secondNode, path.getPathList().end());
crackFlag = HAVECRACK;
#ifdef DEBUG
cout << "bfs with crack happening,firstNode: " << firstNode << " ,secondNode: " << secondNode << endl;
path.printPath();
path.printAllNodeInPath();
#endif
}
}
}//if there are no cracked pieces in path
}//while
//return MAYBESOLUTION;
}
int BetaRoute::addSuperLink(PathPiece &pPiece)
{
pPiece.setLinkProperty(SUPERLINK);
pPiece.setWeight(INFINITEWEIGHT);
pPiece.getLinkNodesVector().clear();
return EXIT_SUCCESS;
}