-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBranchAndBound.cpp
446 lines (350 loc) · 11.5 KB
/
BranchAndBound.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
#include "pch.h"
#include "Graph.h"
#include <queue>
#include <iostream>
#include <iomanip>
#include <numeric>
#include <tuple>
class Node {
public:
vector<int> mIndexes;
vector<bool> mVisited;
double mLength;
double mInferiorHeight;
double mSuperiorHeight;
Node(double mmDijkstraDistance, vector<int> indexes, int nVertex)
: mIndexes(indexes)
, mLength(mmDijkstraDistance)
, mInferiorHeight(0)
, mSuperiorHeight(0)
{
mVisited.resize(nVertex, false);
mVisited[0] = true;
}
Node(double mmDijkstraDistance, vector<int> indexes, int nVertex, double inferiorHeight, double cotaSuperior)
: mIndexes(indexes)
, mLength(mmDijkstraDistance)
, mInferiorHeight(inferiorHeight)
, mSuperiorHeight(cotaSuperior)
{
mVisited.resize(nVertex, false);
mVisited[0] = true;
}
Node(const Node& node)
: mLength(0)
, mInferiorHeight(0)
, mSuperiorHeight(0)
{
mIndexes = node.mIndexes;
mVisited = node.mVisited;
}
};
struct NodeLenghtComparator {
bool operator()(const Node* s1, const Node* s2) {
return s1->mLength > s2->mLength;
}
};
struct NodeHeightComparator {
bool operator()(const Node* s1, const Node* s2) {
return s1->mInferiorHeight > s2->mInferiorHeight;
}
};
class Cell {
public:
double mDijkstraDistance;
list<CEdge*> mPath;
};
using Matrix = vector<vector<Cell>>;
using PriorityQueueByLength = priority_queue<Node*, std::vector<Node*>, NodeLenghtComparator>;
using PriorityQueueByHeight = priority_queue<Node*, std::vector<Node*>, NodeHeightComparator>;
Matrix BuildDistanceMatrix(CGraph& graph, CVisits& visits)
{
Matrix matrix = Matrix(visits.m_Vertices.size() - 1);
int index = 0;
for (auto iOriginVisits = visits.m_Vertices.begin(); iOriginVisits != next(visits.m_Vertices.begin(), visits.m_Vertices.size() - 1); ++iOriginVisits)
{
DijkstraQueue(graph, *iOriginVisits);
(*iOriginVisits)->m_indexMatrix = index;
for (const CVertex* jDestinationVisits : visits.m_Vertices) {
Cell matrixCell;
matrixCell.mDijkstraDistance = jDestinationVisits->m_DijkstraDistance;
if (*iOriginVisits != jDestinationVisits)
{
while (jDestinationVisits != *iOriginVisits)
{
matrixCell.mPath.push_front(jDestinationVisits->m_pDijkstraPrevious);
jDestinationVisits = jDestinationVisits->m_pDijkstraPrevious->m_pOrigin;
}
}
matrix[index].push_back(matrixCell);
}
index++;
}
visits.m_Vertices.back()->m_indexMatrix = index;
return matrix;
}
void LengthBound(Node* topNode, const CVisits& visits, const Matrix& matrix, PriorityQueueByLength& priorityQueue)
{
if (topNode->mIndexes.back() == visits.m_Vertices.back()->m_indexMatrix)
{
return;
}
for (int col = 0; col < visits.m_Vertices.size(); col++)
{
if (col != topNode->mIndexes.back() && !topNode->mVisited[col])
{
Node* newNode = new Node(*topNode);
newNode->mIndexes.push_back(col);
newNode->mLength = topNode->mLength + matrix[topNode->mIndexes.back()][col].mDijkstraDistance;
priorityQueue.push(newNode);
}
}
}
CTrack BuildSolution(CGraph& graph, const Node* topNode, const Matrix& matrix)
{
CTrack solution(&graph, matrix[topNode->mIndexes[0]][topNode->mIndexes[1]].mPath);
for (int element = 1; element < topNode->mIndexes.size() - 1; element++)
{
for (CEdge* iEdge : matrix[topNode->mIndexes[element]][topNode->mIndexes[element + 1]].mPath)
{
solution.m_Edges.push_back(iEdge);
}
}
return solution;
}
CTrack SalesmanTrackBranchAndBound1(CGraph& graph, CVisits& visits)
{
const auto matrix = BuildDistanceMatrix(graph, visits);
PriorityQueueByLength priorityQueue;
for (int col = 1; col < visits.m_Vertices.size(); col++) {
vector<int> pathIndexes;
pathIndexes.push_back(0);
pathIndexes.push_back(col);
priorityQueue.push(new Node(matrix[0][col].mDijkstraDistance, pathIndexes, visits.m_Vertices.size()));
}
while (!priorityQueue.empty())
{
Node* topNode = priorityQueue.top();
priorityQueue.pop();
topNode->mVisited[topNode->mIndexes.back()] = true;
if (topNode->mIndexes.back() != visits.m_Vertices.back()->m_indexMatrix)
{
LengthBound(topNode, visits, matrix, priorityQueue);
continue;
}
bool bound = false;
for (const bool visitVertex : topNode->mVisited)
{
if (!visitVertex)
{
bound = true;
break;
}
}
if (bound)
{
LengthBound(topNode, visits, matrix, priorityQueue);
continue;
}
auto solution = BuildSolution(graph, topNode, matrix);
delete topNode;
return solution;
}
return {&graph};
}
void BuildHeights(CVisits& visits, Matrix matrix, vector<double>& minimum, vector<double>& maximum)
{
minimum.resize(visits.m_Vertices.size(), numeric_limits<double>::max());
maximum.resize(visits.m_Vertices.size(), 0.0);
for (int row = 0; row < visits.m_Vertices.size() - 1; row++)
{
for (int col = 0; col < visits.m_Vertices.size(); col++)
{
if (row == col)
{
continue;
}
if (matrix[row][col].mDijkstraDistance < minimum[col])
{
minimum[col] = matrix[row][col].mDijkstraDistance;
}
if (matrix[row][col].mDijkstraDistance > maximum[col])
{
maximum[col] = matrix[row][col].mDijkstraDistance;
}
}
}
}
void HeightBound(Node* topNode, const CVisits& visits, const Matrix& matrix,
PriorityQueueByHeight& priorityQueue, double& globalSuperiorHeight,
double& superiorHeight, double& inferiorHeight,
const vector<double>& minimum, const vector<double>& maximum)
{
if (topNode->mIndexes.back() == visits.m_Vertices.back()->m_indexMatrix)
{
return;
}
for (int col = 0; col < visits.m_Vertices.size(); col++)
{
if (col == topNode->mIndexes.back() || topNode->mVisited[col] || col == visits.m_Vertices.size() - 1 && topNode->mIndexes.size() == 1)
{
continue;
}
inferiorHeight = topNode->mInferiorHeight - minimum[col] + matrix[topNode->mIndexes.back()][col].mDijkstraDistance;
if (globalSuperiorHeight <= inferiorHeight)
{
continue;
}
superiorHeight = topNode->mSuperiorHeight - maximum[col] + matrix[topNode->mIndexes.back()][col].mDijkstraDistance + pow(10, -6);
if (globalSuperiorHeight > superiorHeight)
{
globalSuperiorHeight = superiorHeight;
}
Node* newNode = new Node(*topNode);
newNode->mInferiorHeight = inferiorHeight;
newNode->mSuperiorHeight = superiorHeight;
newNode->mIndexes.push_back(col);
newNode->mLength = topNode->mLength + matrix[topNode->mIndexes.back()][col].mDijkstraDistance;
priorityQueue.push(newNode);
}
}
CTrack SalesmanTrackBranchAndBound2(CGraph& graph, CVisits& visits)
{
const auto matrix = BuildDistanceMatrix(graph, visits);
if (visits.m_Vertices.size() == 2)
{
return { &graph, matrix[0][1].mPath };
}
vector <double> minimum, maximum;
BuildHeights(visits, matrix, minimum, maximum);
double inferiorHeight = std::accumulate(minimum.begin(), minimum.end(), 0.0);
double superiorHeight = std::accumulate(maximum.begin(), maximum.end(), 0.0) + pow(10, -6);
double globalSuperiorHeight = superiorHeight;
PriorityQueueByHeight priorityQueue;
priorityQueue.push(new Node(0, { 0 }, visits.m_Vertices.size(), inferiorHeight, superiorHeight));
while (!priorityQueue.empty())
{
Node* topNode = priorityQueue.top();
priorityQueue.pop();
topNode->mVisited[topNode->mIndexes.back()] = true;
if (topNode->mIndexes.back() != visits.m_Vertices.back()->m_indexMatrix)
{
HeightBound(topNode, visits, matrix, priorityQueue, globalSuperiorHeight, superiorHeight, inferiorHeight, minimum, maximum);
continue;
}
bool bound = false;
for (const bool visitVertex : topNode->mVisited)
{
if (!visitVertex)
{
HeightBound(topNode, visits, matrix, priorityQueue, globalSuperiorHeight, superiorHeight, inferiorHeight, minimum, maximum);
bound = true;
break;
}
}
if(bound)
{
continue;
}
auto solution = BuildSolution(graph, topNode, matrix);
delete topNode;
return solution;
}
return { &graph };
}
// SalesmanTrackBranchAndBound3 ===================================================
CTrack SalesmanTrackBranchAndBound3(CGraph& graph, CVisits& visits)
{
return SalesmanTrackBranchAndBound2(graph, visits);
const auto matrix = BuildDistanceMatrix(graph, visits);
if (visits.m_Vertices.size() == 2)
{
return { &graph, matrix[0][1].mPath };
}
vector <double> minimum, maximum;
BuildHeights(visits, matrix, minimum, maximum);
double inferiorHeight = std::accumulate(minimum.begin(), minimum.end(), 0.0);
double superiorHeight = std::accumulate(maximum.begin(), maximum.end(), 0.0) + pow(10, -6);
double globalSuperiorHeight = superiorHeight;
PriorityQueueByHeight priorityQueue;
priorityQueue.push(new Node(0, { 0 }, visits.m_Vertices.size(), inferiorHeight, superiorHeight));
while (!priorityQueue.empty())
{
Node* topNode = priorityQueue.top();
priorityQueue.pop();
topNode->mVisited[topNode->mIndexes.back()] = true;
if (topNode->mIndexes.back() == visits.m_Vertices.back()->m_indexMatrix)
{
const auto ip = std::unique(topNode->mIndexes.begin(), topNode->mIndexes.begin() + topNode->mIndexes.size());
topNode->mIndexes.resize(distance(topNode->mIndexes.begin(), ip));
if (topNode->mIndexes.size() != visits.m_Vertices.size())
{
continue;
}
auto solution = BuildSolution(graph, topNode, matrix);
delete topNode;
return solution;
}
for (int row = 0; row < visits.m_Vertices.size() - 1; row++)
{
if (topNode->mVisited[row])
{
continue;
}
for (int col = 0; col < visits.m_Vertices.size(); col++)
{
if (row == col || topNode->mVisited[col])
{
continue;
}
if (matrix[row][col].mDijkstraDistance < minimum[col])
{
minimum[col] = matrix[row][col].mDijkstraDistance;
}
if (matrix[row][col].mDijkstraDistance > maximum[col])
{
maximum[col] = matrix[row][col].mDijkstraDistance;
}
}
}
for (int col = 0; col < visits.m_Vertices.size() - 1; col++)
{
if (topNode->mIndexes.back() == col || topNode->mVisited[col])
{
continue;
}
if (matrix[topNode->mIndexes.back()][col].mDijkstraDistance < minimum[col])
{
minimum[col] = matrix[topNode->mIndexes.back()][col].mDijkstraDistance;
}
if (matrix[topNode->mIndexes.back()][col].mDijkstraDistance > maximum[col])
{
maximum[col] = matrix[topNode->mIndexes.back()][col].mDijkstraDistance;
}
}
for (int col = 0; col < visits.m_Vertices.size(); col++)
{
if (col == topNode->mIndexes.back() || topNode->mVisited[col] || col == visits.m_Vertices.size() - 1 && topNode->mIndexes.size() == 1)
{
continue;
}
inferiorHeight = topNode->mInferiorHeight - minimum[col] + matrix[topNode->mIndexes.back()][col].mDijkstraDistance;
if (globalSuperiorHeight <= inferiorHeight)
{
continue;
}
superiorHeight = topNode->mSuperiorHeight - maximum[col] + matrix[topNode->mIndexes.back()][col].mDijkstraDistance + pow(10, -6);
if (globalSuperiorHeight > superiorHeight)
{
globalSuperiorHeight = superiorHeight;
}
Node* newNode = new Node(*topNode);
newNode->mInferiorHeight = inferiorHeight;
newNode->mSuperiorHeight = superiorHeight;
newNode->mIndexes.push_back(col);
newNode->mLength = topNode->mLength + matrix[topNode->mIndexes.back()][col].mDijkstraDistance;
priorityQueue.push(newNode);
}
}
return CTrack(&graph);
}