-
Notifications
You must be signed in to change notification settings - Fork 0
/
ImprovementHeuristics.cpp
105 lines (95 loc) · 2.97 KB
/
ImprovementHeuristics.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
#include "stdafx.h"
#include "ImprovementHeuristics.h"
//2-opt General Idea
//Every iteration, the algorithm go through all possibe combination of (i, j), where 0<i<V-1, i+1<=j<V
//Then connect i-1 with j, revere i to j sequence, and connect i to j+1
//Caculate the new cost
//Each iteration update the current tour with best founding 2-opt tour
//The algorithm terminate after fixed number of iteration or after improvement is too small (<0.01)
void ImprovementHeuristics::TwoOpt(Tour& t, Graph& g) {
clock_t begin;
clock_t end;
double timeSec;
cout << "Start 2-Opt..." << endl;
begin = clock();
int V = g.numberOfNodes();
//If there are only two nodes (e.g., origin plus another node), it is no need to use TwoOpt
if (V < 3) {
return;
}
int iter = 0;
double improve = 1;
while (iter < 20 && improve > 0.01) {
improve = 0;
double bestTourLength = t.gettotalDistance();
double initial_dist = t.gettotalDistance();
int bestI = 0;
int bestJ = 0;
Tour newTour(t);
for (int i = 1; i < V - 1; i++) {
for (int j = i + 1; j < V; j++) {
newTour.TwoOptSwap(i, j, g);
if (bestTourLength > newTour.gettotalDistance()) {
improve = t.gettotalDistance() - newTour.gettotalDistance();
bestTourLength = newTour.gettotalDistance();
bestI = i;
bestJ = j;
//cout << "Swat " << i << " and " << j << " :" << t.printTour();
}
newTour.TwoOptSwap(i, j, g);
}
}
if (improve > 0)
t.TwoOptSwap(bestI, bestJ,g);
iter += 1;
improve /= initial_dist;
}
end = clock();
timeSec = (end - begin) / static_cast<double>(CLOCKS_PER_SEC);
cout << "...Finish 2-Opt in " << timeSec << "s." << endl;
}
void ImprovementHeuristics::ThreeOpt(Tour& t, Graph& g) {
//clock_t begin;
//clock_t end;
//double timeSec;
//cout << "Start 3-Opt..." << endl;
//begin = clock();
int V = g.numberOfNodes();
//If there are only two nodes (e.g., origin plus another node), it is no need to use TwoOpt
if (V < 3) {
return;
}
//If there are less than 5 nodes, it can only implement 2-opt
if (V < 5) {
TwoOpt(t, g);
return;
}
int iter = 0;
double improve = 1;
while (iter < 2 && improve > 0.01) {
improve = 0;
Tour bestTour(V);
bestTour.Copy(t);
double initial_dist = t.gettotalDistance();
for (int i = 1; i <= V - 4; i++) {
for (int j = i + 2; j <= V - 2; j++) {
for (int k = j + 2; k <= V;k++) {
Tour newTour(t);
newTour.ThreeOptSwap(i, j, k, g);
if (bestTour.gettotalDistance() > newTour.gettotalDistance()) {
improve = t.gettotalDistance() - newTour.gettotalDistance();
bestTour.Copy(newTour);
//cout << "Swat " << i << "," << j << " and " << k << ":"<< bestTour.printTour();
}
}
}
}
if (improve > 0)
t.Copy(bestTour);
iter += 1;
improve /= initial_dist;
}
//end = clock();
//timeSec = (end - begin) / static_cast<double>(CLOCKS_PER_SEC);
//cout << "...Finish 3-Opt in " << timeSec << "s." << endl;
}