-
Notifications
You must be signed in to change notification settings - Fork 0
/
heuristic_solver.cpp
160 lines (127 loc) · 4.99 KB
/
heuristic_solver.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
#include "heuristic_solver.hpp"
#include "genetic_algorithm.hpp"
#include "mean_position_heuristic.hpp"
Order HeuristicSolver::largeGraphHeuristic(PaceGraph &graph) {
std::vector<int> positionOrder(graph.size_free);
for (int i = 0; i < graph.size_free; ++i) {
positionOrder[i] = i;
}
MeanPositionParameter meanPositionParameter;
meanPositionParameter.meanType = average;
MeanPositionSolver meanPositionSolver1(
[this](int it) {
return it == 0 && this->time_percentage_past() < 0.2;
},
meanPositionParameter);
Order o1 = meanPositionSolver1.solve(graph);
meanPositionParameter.meanType = median;
MeanPositionSolver meanPositionSolver2(
[this](int it) {
return it == 0 && this->has_time_left() &&
this->time_percentage_past() < 0.5;
},
meanPositionParameter);
Order o2 = meanPositionSolver2.solve(graph);
long cost1 = o1.count_crossings(graph);
long cost2 = o2.count_crossings(graph);
Order bestOrder = cost1 < cost2 ? o1 : o2;
bool foundImprovement = true;
long largestFallback = 20000;
int largestMoveDistance = 2000;
while (foundImprovement) {
foundImprovement = false;
if (!has_time_left()) {
break;
}
std::shuffle(positionOrder.begin(), positionOrder.end(),
std::mt19937(std::random_device()()));
for (int i = 0; i < graph.size_free; i++) {
if (!has_time_left()) {
break;
}
int v = positionOrder[i];
int posOfV = bestOrder.get_position(v);
int bestCostChange = 0;
int bestPos = posOfV;
int currentCostChange = 0;
bool foundBestPos = true;
long currentFallback = 0;
for (int pos = posOfV - 1;
pos >= std::max(0, posOfV - largestMoveDistance); pos--) {
if (!has_time_left()) {
foundBestPos = false;
break;
}
int u = bestOrder.get_vertex(pos);
auto [u_v, v_u] = graph.calculatingCrossingNumber(u, v);
int crossingDiff = v_u - u_v;
currentCostChange += crossingDiff;
if (currentCostChange >= largestFallback) {
break;
}
if (currentFallback < currentCostChange) {
currentFallback = currentCostChange;
}
if (currentCostChange <= 0) {
if (2 * currentFallback > largestFallback) {
largestFallback = 2 * currentFallback;
std::cerr << "Increase Fallback: " << largestFallback
<< std::endl;
}
}
if (currentCostChange < bestCostChange) {
foundImprovement = true;
bestCostChange = currentCostChange;
bestPos = pos;
}
}
currentCostChange = 0;
currentFallback = 0;
for (int pos = posOfV + 1;
pos < std::min(graph.size_free, posOfV + largestMoveDistance);
pos++) {
if (!has_time_left()) {
foundBestPos = false;
break;
}
int u = bestOrder.get_vertex(pos);
auto [u_v, v_u] = graph.calculatingCrossingNumber(u, v);
int crossingDiff = -v_u + u_v;
currentCostChange += crossingDiff;
if (currentCostChange >= largestFallback) {
break;
}
if (currentFallback < currentCostChange) {
currentFallback = currentCostChange;
}
if (currentCostChange <= 0) {
if (2 * currentFallback > largestFallback) {
largestFallback = 2 * currentFallback;
std::cerr << "Increase Fallback: " << largestFallback
<< std::endl;
}
}
if (currentCostChange < bestCostChange) {
bestCostChange = currentCostChange;
bestPos = pos;
foundImprovement = true;
}
}
if (bestPos != posOfV && foundBestPos) {
bestOrder.move_vertex(v, bestPos);
}
}
}
return bestOrder;
}
Order HeuristicSolver::run(PaceGraph &graph) {
bool canInitCrossingMatrix = graph.init_crossing_matrix_if_necessary();
if (canInitCrossingMatrix) {
GeneticHeuristicParameter geneticHeuristicParameter;
GeneticHeuristic geneticHeuristic(
[this](int it) { return this->has_time_left(); },
geneticHeuristicParameter);
return geneticHeuristic.solve(graph);
}
return largeGraphHeuristic(graph);
}