-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
309 lines (238 loc) · 6.05 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
// Beczi Eliezer
// beim1697
// 511
// Grafikus konyvtar: SFML
// Kompilalas: g++ main.cpp -O3 -lsfml-graphics -lsfml-window -lsfml-system
// Futtatas: ./a.out
// Zoom in: I
// Zoom out: O
// Move view: W, A, S, D
#include <cmath>
#include <vector>
#include <limits>
#include <fstream>
#include <iostream>
#include <unordered_map>
#include <SFML/Graphics.hpp>
using std::cout;
using std::endl;
using std::pair;
using std::vector;
using std::string;
using std::ifstream;
using std::make_pair;
using std::unordered_map;
using std::numeric_limits;
#define K 16
#define FPS 1
#define OFFSET 2
#define OPTIMIZE 1
#define SHOW_GREEDY 0
#define IMPROVEMENT_LIMIT 2
#define INPUT_FILE "tsp1.txt"
typedef pair<double, vector<int>> tour;
typedef unordered_map<int, pair<int, int>> city;
typedef unordered_map<int, sf::Color> cityColor;
tour myTour;
city cities;
cityColor colors;
sf::RenderWindow window(sf::VideoMode::getDesktopMode(), "TSP", sf::Style::Fullscreen);
sf::View view = window.getDefaultView();
void beOlvas(const string& fileName = INPUT_FILE) {
ifstream inputFile(fileName);
int id;
int x;
int y;
while(inputFile >> id && inputFile >> x && inputFile >> y) {
cities[id] = make_pair(x, y);
colors[id] = sf::Color(rand() % 255, rand() % 255, rand() % 255);
}
}
inline void kiIr() {
cout << myTour.first << endl;
for(const auto& v : myTour.second) {
cout << v << ' ';
}
cout << endl;
}
inline int dist(const int& a, const int& b) {
return (pow(cities[b].first - cities[a].first, 2) + pow(cities[b].second - cities[a].second, 2));
}
inline void getTourLength(tour& myTour) {
int hossz = (myTour.second).size();
myTour.first = static_cast<double>(0);
for(int i = 0; i < hossz - 1; ++i) {
myTour.first += sqrt(dist((myTour.second)[i], (myTour.second)[i + 1]));
}
}
tour twoOptSwap(const int& i, const int& k) {
tour newTour;
int tourSize = (myTour.second).size();
for(int a = 0; a < i; ++a) {
(newTour.second).push_back((myTour.second)[a]);
}
for(int a = k; a >= i; --a) {
(newTour.second).push_back((myTour.second)[a]);
}
for(int a = k + 1; a < tourSize; ++a) {
(newTour.second).push_back((myTour.second)[a]);
}
getTourLength(newTour);
return newTour;
}
void showCities() {
for(const auto& m : cities) {
sf::CircleShape circle;
circle.setRadius(7);
circle.setPointCount(100);
circle.setFillColor(colors[m.first]);
circle.setPosition(m.second.first * K, m.second.second * K);
circle.setOrigin(circle.getRadius(), circle.getRadius());
window.draw(circle);
}
}
void showAllEdges() {
int numOfCities = cities.size();
for(int i = 0; i < numOfCities - 1; ++i) {
for(int j = i + 1; j < numOfCities; ++j) {
int x1 = cities[i].first * K;
int y1 = cities[i].second * K;
int x2 = cities[j].first * K;
int y2 = cities[j].second * K;
sf::Vertex line[] = {
sf::Vertex(sf::Vector2f(x1, y1), colors[i]),
sf::Vertex(sf::Vector2f(x2, y2), colors[i + 1])
};
window.draw(line, 2, sf::Lines);
}
}
}
void showEdges(const tour& myTour) {
int numOfCities = (myTour.second).size() - 1;
for(int i = 0; i < numOfCities; ++i) {
int x1 = (cities[(myTour.second)[i]]).first * K;
int y1 = (cities[(myTour.second)[i]]).second * K;
int x2 = (cities[(myTour.second)[i + 1]]).first * K;
int y2 = (cities[(myTour.second)[i + 1]]).second * K;
sf::Vertex line[] = {
sf::Vertex(sf::Vector2f(x1, y1), colors[i]),
sf::Vertex(sf::Vector2f(x2, y2), colors[i + 1])
};
window.draw(line, 2, sf::Lines);
}
}
void draw(const tour& myTour, const int& numOfIterations) {
int kontor = 0;
while(kontor < numOfIterations) {
sf::Event event;
while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed) {
window.close();
}
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::I)) {
view.zoom(0.99f);
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::O)) {
view.zoom(1.003f);
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::A)) {
view.move(-OFFSET, 0);
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::D)) {
view.move(OFFSET, 0);
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::W)) {
view.move(0, -OFFSET);
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::S)) {
view.move(0, OFFSET);
}
window.setView(view);
window.clear();
showCities();
showEdges(myTour);
// showAllEdges();
window.display();
++kontor;
}
}
tour nearestNeighbour(const int& start) {
int numOfCities = cities.size();
bool visited[numOfCities + 1]{false};
tour newTour;
(newTour.second).push_back(start);
int kontor = 1;
visited[start] = true;
while(kontor < numOfCities) {
int index = -1;
int x = (newTour.second).back();
int edge = numeric_limits<int>::max();
for(int y = 1; y <= numOfCities; ++y) {
if(visited[y]) {
continue;
}
int aux = dist(x, y);
if(aux < edge) {
index = y;
edge = aux;
}
}
(newTour.second).push_back(index);
visited[index] = true;
++kontor;
if(SHOW_GREEDY) {
draw(newTour, FPS);
}
}
(newTour.second).push_back(start);
getTourLength(newTour);
return newTour;
}
void run() {
int numOfCities = cities.size();
myTour = nearestNeighbour(1);
for(int i = 2; i < numOfCities; ++i) {
tour tmp = nearestNeighbour(i);
if(tmp.first < myTour.first) {
myTour = tmp;
}
}
}
void twoOpt() {
int improvement = 0;
int tourSize = cities.size();
do {
++improvement;
// cout << myTour.first << endl;
for(int i = 1; i < tourSize - 1; ++i) {
for(int k = i + 1; k < tourSize; ++k) {
if(OPTIMIZE) {
int aux1 = dist((myTour.second)[i - 1], (myTour.second)[i]) + dist((myTour.second)[k], (myTour.second)[k + 1]);
int aux2 = dist((myTour.second)[i - 1], (myTour.second)[k]) + dist((myTour.second)[i], (myTour.second)[k + 1]);
if(aux1 <= aux2) {
continue;
}
}
draw(myTour, FPS);
tour newTour = twoOptSwap(i, k);
if(newTour.first < myTour.first) {
myTour = newTour;
improvement = 0;
}
}
}
} while(improvement < IMPROVEMENT_LIMIT);
}
int main(int argc, char const *argv[])
{
srand(time(NULL));
beOlvas();
run();
twoOpt();
kiIr();
while(window.isOpen()) {
draw(myTour, 1);
}
return 0;
}