This commit is contained in:
Love 2024-09-05 21:53:45 +02:00
commit e28af44e55
30 changed files with 287646 additions and 0 deletions

12
.gitignore vendored Executable file
View File

@ -0,0 +1,12 @@
*~
.DS_Store
*.pro.user.*
*.pro.user
build-*/
*.app
*.exe
build

40
TSP.pro Executable file
View File

@ -0,0 +1,40 @@
QT += widgets
SOURCES = $$PWD/src/*.cpp
#SOURCES += $$PWD/lib/*.cpp
HEADERS = $$PWD/src/*.h
#HEADERS += $$PWD/lib/*.h
QMAKE_CXXFLAGS += -std=c++11
INCLUDEPATH += $$PWD/lib/
# Copies the given files to the destination directory
defineTest(copyToDestdir) {
files = $$1
for(FILE, files) {
DDIR = $$OUT_PWD
# Replace slashes in paths with backslashes for Windows
win32:FILE ~= s,/,\\,g
win32:DDIR ~= s,/,\\,g
!win32 {
QMAKE_POST_LINK += cp -r '"'$$FILE'"' '"'$$DDIR'"' $$escape_expand(\\n\\t)
}
win32 {
QMAKE_POST_LINK += xcopy '"'$$FILE'"' '"'$$DDIR'"' /e /y $$escape_expand(\\n\\t)
}
}
export(QMAKE_POST_LINK)
}
!win32 {
copyToDestdir($$files($$PWD/res/*))
}
win32 {
copyToDestdir($$PWD/res)
}
macx {
cache()
}

128
res/bier127.txt Executable file
View File

@ -0,0 +1,128 @@
573 600
365.1020 357.2109
278.2993 380.8844
270.4082 341.4286
163.8776 254.6259
317.7551 384.8299
230.9524 408.5034
447.9592 301.9728
380.8844 447.9592
242.7891 483.4694
294.0816 432.1769
254.6259 278.2993
471.6327 590.0000
337.4830 380.8844
515.0340 270.4082
333.5374 301.9728
376.9388 574.2177
294.0816 357.2109
262.5170 428.2313
384.8299 286.1905
305.9184 357.2109
175.7143 353.2653
365.1020 440.0680
369.0476 321.7007
286.1905 369.0476
511.0884 337.4830
163.8776 546.5986
254.6259 337.4830
10.0000 116.5306
329.5918 420.3401
542.6531 463.7415
203.3333 487.4150
345.3741 372.9932
384.8299 499.2517
250.6803 487.4150
404.5578 357.2109
447.9592 274.3537
313.8095 455.8503
515.0340 290.1361
305.9184 440.0680
155.9864 400.6122
195.4422 554.4898
349.3197 365.1020
305.9184 495.3061
447.9592 499.2517
266.4626 254.6259
329.5918 392.7211
534.7619 187.5510
396.6667 432.1769
361.1565 365.1020
361.1565 278.2993
349.3197 372.9932
313.8095 10.0000
365.1020 380.8844
266.4626 266.4626
372.9932 191.4966
92.8571 384.8299
294.0816 447.9592
562.3810 298.0272
345.3741 384.8299
270.4082 321.7007
372.9932 234.8980
357.2109 250.6803
262.5170 365.1020
278.2993 424.2857
412.4490 262.5170
451.9048 542.6531
313.8095 420.3401
341.4286 369.0476
372.9932 159.9320
357.2109 357.2109
558.4354 317.7551
372.9932 227.0068
262.5170 278.2993
325.6463 412.4490
396.6667 408.5034
365.1020 408.5034
372.9932 353.2653
234.8980 499.2517
305.9184 325.6463
424.2857 412.4490
400.6122 424.2857
369.0476 333.5374
270.4082 282.2449
357.2109 349.3197
357.2109 369.0476
463.7415 546.5986
246.7347 278.2993
266.4626 483.4694
266.4626 333.5374
238.8435 475.5782
317.7551 349.3197
250.6803 479.5238
21.8367 534.7619
353.2653 21.8367
313.8095 329.5918
558.4354 305.9184
562.3810 313.8095
345.3741 416.3946
392.7211 412.4490
341.4286 309.8639
294.0816 341.4286
301.9728 400.6122
420.3401 317.7551
345.3741 357.2109
361.1565 384.8299
357.2109 361.1565
294.0816 412.4490
175.7143 309.8639
408.5034 420.3401
286.1905 313.8095
404.5578 412.4490
503.1973 128.3673
353.2653 349.3197
400.6122 337.4830
357.2109 155.9864
447.9592 329.5918
238.8435 416.3946
305.9184 463.7415
246.7347 404.5578
333.5374 499.2517
337.4830 404.5578
392.7211 329.5918
290.1361 372.9932
376.9388 361.1565
274.3537 349.3197
380.8844 451.9048
309.8639 380.8844

1291
res/circuit1290.txt Executable file

File diff suppressed because it is too large Load Diff

15113
res/germany15112.txt Executable file

File diff suppressed because it is too large Load Diff

100002
res/mona-100k.txt Executable file

File diff suppressed because it is too large Load Diff

20001
res/mona-20k.txt Executable file

File diff suppressed because it is too large Load Diff

50001
res/mona-50k.txt Executable file

File diff suppressed because it is too large Load Diff

6
res/tsp0.txt Executable file
View File

@ -0,0 +1,6 @@
500 500

6
res/tsp1.txt Executable file
View File

@ -0,0 +1,6 @@
500 500
200 400

77
res/tsp10-nearest.ans Executable file
View File

@ -0,0 +1,77 @@
This file shows the tour after each point is inserted.
It may be useful for debugging.
Tour distance = 0.0
(110.0, 225.0)
Tour distance = 150.0133327407934
(110.0, 225.0)
(161.0, 280.0)
Tour distance = 787.358844018307
(110.0, 225.0)
(161.0, 280.0)
(325.0, 554.0)
Tour distance = 1094.6173929810654
(110.0, 225.0)
(161.0, 280.0)
(325.0, 554.0)
(490.0, 285.0)
Tour distance = 1139.6938059391582
(110.0, 225.0)
(161.0, 280.0)
(157.0, 443.0)
(325.0, 554.0)
(490.0, 285.0)
Tour distance = 1259.6276840705505
(110.0, 225.0)
(161.0, 280.0)
(157.0, 443.0)
(283.0, 379.0)
(325.0, 554.0)
(490.0, 285.0)
Tour distance = 1313.038203492536
(110.0, 225.0)
(161.0, 280.0)
(157.0, 443.0)
(283.0, 379.0)
(325.0, 554.0)
(397.0, 566.0)
(490.0, 285.0)
Tour distance = 1357.8298213289954
(110.0, 225.0)
(161.0, 280.0)
(157.0, 443.0)
(283.0, 379.0)
(306.0, 360.0)
(325.0, 554.0)
(397.0, 566.0)
(490.0, 285.0)
Tour distance = 1461.504290888965
(110.0, 225.0)
(161.0, 280.0)
(157.0, 443.0)
(283.0, 379.0)
(306.0, 360.0)
(325.0, 554.0)
(397.0, 566.0)
(490.0, 285.0)
(343.0, 110.0)
Tour distance = 1566.1363051360363
(110.0, 225.0)
(161.0, 280.0)
(157.0, 443.0)
(283.0, 379.0)
(306.0, 360.0)
(325.0, 554.0)
(397.0, 566.0)
(490.0, 285.0)
(552.0, 199.0)
(343.0, 110.0)

11
res/tsp10-optimal.ans Executable file
View File

@ -0,0 +1,11 @@
Distance = 1552.9612081934351
(343.0, 110.0)
(552.0, 199.0)
(490.0, 285.0)
(306.0, 360.0)
(283.0, 379.0)
(397.0, 566.0)
(325.0, 554.0)
(157.0, 443.0)
(161.0, 280.0)
(110.0, 225.0)

77
res/tsp10-smallest.ans Executable file
View File

@ -0,0 +1,77 @@
This file shows the tour after each point is inserted.
It may be useful for debugging.
Tour distance = 0.0
(110.0, 225.0)
Tour distance = 150.0133327407934
(110.0, 225.0)
(161.0, 280.0)
Tour distance = 787.358844018307
(110.0, 225.0)
(325.0, 554.0)
(161.0, 280.0)
Tour distance = 1094.6173929810654
(110.0, 225.0)
(490.0, 285.0)
(325.0, 554.0)
(161.0, 280.0)
Tour distance = 1139.6938059391582
(110.0, 225.0)
(490.0, 285.0)
(325.0, 554.0)
(157.0, 443.0)
(161.0, 280.0)
Tour distance = 1213.9433802328422
(110.0, 225.0)
(283.0, 379.0)
(490.0, 285.0)
(325.0, 554.0)
(157.0, 443.0)
(161.0, 280.0)
Tour distance = 1267.3538996548277
(110.0, 225.0)
(283.0, 379.0)
(490.0, 285.0)
(397.0, 566.0)
(325.0, 554.0)
(157.0, 443.0)
(161.0, 280.0)
Tour distance = 1268.541678478853
(110.0, 225.0)
(283.0, 379.0)
(306.0, 360.0)
(490.0, 285.0)
(397.0, 566.0)
(325.0, 554.0)
(157.0, 443.0)
(161.0, 280.0)
Tour distance = 1551.1141715191152
(110.0, 225.0)
(283.0, 379.0)
(306.0, 360.0)
(343.0, 110.0)
(490.0, 285.0)
(397.0, 566.0)
(325.0, 554.0)
(157.0, 443.0)
(161.0, 280.0)
Tour distance = 1655.7461857661865
(110.0, 225.0)
(283.0, 379.0)
(306.0, 360.0)
(343.0, 110.0)
(552.0, 199.0)
(490.0, 285.0)
(397.0, 566.0)
(325.0, 554.0)
(157.0, 443.0)
(161.0, 280.0)

11
res/tsp10.txt Executable file
View File

@ -0,0 +1,11 @@
600 600
110.0 225.0
161.0 280.0
325.0 554.0
490.0 285.0
157.0 443.0
283.0 379.0
397.0 566.0
306.0 360.0
343.0 110.0
552.0 199.0

101
res/tsp100.txt Executable file
View File

@ -0,0 +1,101 @@
600 600
273.1588 72.6027
423.7493 428.0372
509.4693 426.8362
512.0536 520.2136
112.1935 453.8568
54.4830 231.6509
266.2236 220.0501
168.0002 365.8709
180.0541 359.8832
87.0681 286.3926
315.6692 103.6946
457.7327 520.4371
221.3036 324.1944
464.6742 403.1801
549.3704 134.9750
10.0000 346.6937
152.5254 450.5733
257.7839 341.6199
354.1409 149.3780
371.5071 590.0000
430.2164 347.7012
428.4436 526.3642
384.8782 133.7609
246.6101 409.2352
53.7277 276.0534
51.3431 442.4751
138.0119 393.6293
427.1514 411.3751
589.0593 215.0038
493.3952 289.8690
438.2191 334.2714
392.4814 357.9693
58.8089 181.2302
311.7615 353.9954
165.5282 248.1620
130.9518 202.2638
261.5480 383.9406
165.5969 315.6168
595.9071 303.7975
507.6590 368.8210
56.6928 367.5756
394.4477 513.8520
188.3190 445.5601
211.9338 223.0796
513.5830 234.8127
256.7040 513.9357
195.8223 110.9538
379.3163 268.0682
419.0551 230.5192
382.4749 174.4978
523.4022 491.6742
171.4522 31.8651
168.9303 293.8379
97.2369 149.8287
24.3200 366.1980
58.4656 232.0548
147.4940 473.6869
458.7439 267.5501
276.8106 51.5341
330.1640 499.7318
516.7853 226.8680
534.9506 337.7491
186.7959 494.8372
297.4665 259.6835
339.0843 125.1764
456.5466 279.3881
301.4429 341.7011
241.7536 551.8405
504.5441 194.1188
300.6439 163.8465
559.7077 243.4871
542.6786 164.3521
99.5465 367.5969
404.9224 278.8069
195.4291 96.0121
361.0886 415.9177
233.4325 74.1015
343.2292 338.0936
484.3626 122.6139
81.8307 430.1109
40.6437 429.6571
266.8353 10.0000
86.7809 336.4725
468.0013 344.5027
171.3398 542.6068
578.7719 403.2444
539.0705 180.1515
335.8320 362.5449
416.6955 512.8015
313.8028 257.0729
322.4235 295.7811
233.9381 419.6163
260.6304 462.8601
49.2269 445.3535
208.4506 270.0458
466.2847 260.2540
575.2262 269.1700
349.3280 109.7422
543.8085 328.3337
396.3891 185.0917

1001
res/tsp1000.txt Executable file

File diff suppressed because it is too large Load Diff

6
res/tsp2.txt Executable file
View File

@ -0,0 +1,6 @@
500 500
200 400
300 100

6
res/tsp3.txt Executable file
View File

@ -0,0 +1,6 @@
500 500
200 400
300 100
100 100

6
res/tsp4.txt Executable file
View File

@ -0,0 +1,6 @@
500 500
200 400
300 100
100 100
300 200

6
res/tsp5.txt Executable file
View File

@ -0,0 +1,6 @@
600 600
0 300
600 300
300 600
300 0
0 600

9
res/tsp8.txt Executable file
View File

@ -0,0 +1,9 @@
1000 500
0 0
500 0
1000 0
500 500
700 0
800 0
900 0
800 500

85901
res/tsp85900.txt Executable file

File diff suppressed because it is too large Load Diff

13510
res/usa13509.txt Executable file

File diff suppressed because it is too large Load Diff

32
src/Node.cpp Executable file
View File

@ -0,0 +1,32 @@
/*
* TDDD86 TSP
* This file contains the implementation of the Node structure.
* See Node.h for comments about each member.
* Your code should work properly with an unmodified version of this file.
*/
#include <iomanip>
#include <iostream>
#include <sstream>
#include "Node.h"
Node::Node(Point p, Node* _next)
: point(p.x, p.y), next(_next) {}
string Node::toString() const {
stringstream out;
out << "Node{addr=" << ((void*) this);
out << ", point=" << point;
if (next != nullptr) {
out << ", next=" << ((void*) next);
} else {
out << ", next=nullptr";
}
out << "}";
return out.str();
}
ostream& operator <<(ostream& out, const Node& node) {
out << node.toString();
return out;
}

47
src/Node.h Executable file
View File

@ -0,0 +1,47 @@
/*
* TDDD86 TSP
* This file contains the declaration of the Node structure.
* See Node.cpp for implementation of each member.
* Your code should work properly with an unmodified version of this file.
*/
#ifndef NODE_H
#define NODE_H
#include <iostream>
#include "Point.h"
using namespace std;
/*
* Each Node structure represents a single node in a circular linked list
* used in a TSP tour.
*/
struct Node {
Point point; // this nodes point
Node* next; // pointer to next node in the list (nullptr if none)
/*
* Constructs a new node storing the given point and next pointer.
*/
Node(Point p, Node* _next = nullptr);
/*
* Returns a string representation of the Node for debugging, such as
* "Node{addr=0x7e83f4, point=(2.5, 3.5), next=0x43b2a0}".
*
* Note that the toString output includes the node's memory address, as well
* as the memory address where its next pointer points.
*
* Keep in mind that toString won't be called if you try to print a Node*.
* You must print the node itself.
*/
string toString() const;
};
/*
* Outputs the given node to the given output stream (e.g. cout) in the same
* format as returned by its toString member function.
*/
ostream& operator <<(ostream& out, const Node& node);
#endif // END NODE_H

50
src/Point.cpp Executable file
View File

@ -0,0 +1,50 @@
/*
* TDDD86 TSP
* This file contains the implementation of the Point structure.
* See Point.h for comments about each member.
* Your code should work properly with an unmodified version of this file.
*/
#include <iomanip>
#include <iostream>
#include <sstream>
#include <cmath>
#include <QGraphicsEllipseItem>
#include <QGraphicsLineItem>
#include "Point.h"
Point::Point(double _x, double _y)
: x(_x), y(_y) {}
double Point::distanceTo(Point that) const
{
double dx = x - that.x;
double dy = y - that.y;
return std::sqrt(dx*dx + dy*dy);
}
void Point::draw(QGraphicsScene *scene) const
{
QGraphicsEllipseItem *item = new QGraphicsEllipseItem(x, y, 1, 1);
item->setBrush(QBrush(QColor(255, 0, 0)));
scene->addItem(item);
}
void Point::drawTo(Point that, QGraphicsScene *scene) const
{
QGraphicsLineItem *item = new QGraphicsLineItem(x, y, that.x, that.y);
scene->addItem(item);
}
string Point::toString() const
{
stringstream string;
string << "(" << std::fixed << std::setprecision(1) << std::showpoint
<< x << ", " << y << ")";
return string.str();
}
ostream& operator <<(ostream& out, const Point& p) {
out << p.toString();
return out;
}

53
src/Point.h Executable file
View File

@ -0,0 +1,53 @@
/*
* TDDD86 TSP
* This file contains the declaration of the Point structure.
* See Point.cpp for implementation of each member.
* Your code should work properly with an unmodified version of this file.
*/
#ifndef POINT_H
#define POINT_H
#include <iostream>
#include <string>
#include <QGraphicsScene>
using namespace std;
/*
* Each Point structure represents a single point in the Euclidean plane.
*/
struct Point {
const double x; // x position of point
const double y; // y position of point
Point(double _x, double _y);
/*
* Returns Euclidean distance between this point and that.
*/
double distanceTo(Point that) const;
/*
* Draws the point onto the given QGraphicsScene.
*/
void draw(QGraphicsScene* scene) const;
/*
* Draws the line from this point to that onto the given QGraphicsScene.
*/
void drawTo(Point that, QGraphicsScene* scene) const;
/*
* Returns a string representation of the point, such as
* (2.5, 3.5).
*/
string toString() const;
};
/*
* Outputs the given point to the given output stream (e.g. cout) in the same
* format as returned by its toString member function.
*/
ostream& operator<<(ostream& out, const Point& p);
#endif // POINT_H

50
src/Tour.cpp Executable file
View File

@ -0,0 +1,50 @@
// This is the .cpp file you will edit and turn in.
// We have provided a skeleton for you,
// but you must finish it as described in the spec.
// Also remove these comments here and add your own.
// TODO: remove this comment header
#include <iostream>
#include "Tour.h"
#include "Node.h"
#include "Point.h"
Tour::Tour()
{
// TODO: write this member
}
Tour::~Tour()
{
// TODO: write this member
}
void Tour::show()
{
// TODO: write this member
}
void Tour::draw(QGraphicsScene *scene)
{
// TODO: write this member
}
int Tour::size()
{
// TODO: write this member
}
double Tour::distance()
{
// TODO: write this member
}
void Tour::insertNearest(Point p)
{
// TODO: write this member
}
void Tour::insertSmallest(Point p)
{
// TODO: write this member
}

29
src/Tour.h Executable file
View File

@ -0,0 +1,29 @@
// This is the .h file you will edit and turn in.
// We have provided a skeleton for you,
// but you must finish it as described in the spec.
// Also remove these comments here and add your own, as well as on the members.
// TODO: remove this comment header
#ifndef TOUR_H
#define TOUR_H
#include "Node.h"
#include "Point.h"
class Tour {
public:
Tour();
~Tour();
void show();
void draw(QGraphicsScene* scene);
int size();
double distance();
void insertNearest(Point p);
void insertSmallest(Point p);
private:
};
#endif // TOUR_H

63
src/tsp.cpp Executable file
View File

@ -0,0 +1,63 @@
/*
* TDDD86 TSP
* This client program uses your Tour class and contains the 'main'
* function to open the input file and set up the program's primitive GUI.
*/
#include <QApplication>
#include <QGraphicsView>
#include <QGraphicsScene>
#include <chrono>
#include <thread>
#include <fstream>
#include <iostream>
#include <iomanip>
#include "Point.h"
#include "Tour.h"
int main(int argc, char *argv[]) {
QApplication a(argc, argv);
string filename = "tsp10.txt";
ifstream input;
input.open(filename);
// get dimensions
int width;
int height;
input >> width;
input >> height;
// setup graphical window
QGraphicsView *view = new QGraphicsView();
QGraphicsScene *scene = new QGraphicsScene();
view->setScene(scene);
view->scale(1, -1); //screen y-axis is inverted
view->setSceneRect(0, 0, width, height);
view->show();
// run insertion heuristic
Tour tour;
double x;
double y;
while (input >> x >> y) {
Point p(x, y);
tour.insertNearest(p);
//uncomment the 4 lines below to animate
//tour.draw(scene);
//std::chrono::milliseconds dura(50);
//std::this_thread::sleep_for(dura);
//a.processEvents();
}
input.close();
// print tour to standard output
cout << "Tour distance: " << std::fixed << std::setprecision(4)
<< std::showpoint << tour.distance() << endl;
cout << "Number of points: " << tour.size() << endl;
tour.show();
// draw tour
tour.draw(scene);
return a.exec(); // start Qt event loop
}