Compare commits
2 Commits
ca5d4a938e
...
3a4c7a6dc9
Author | SHA1 | Date | |
---|---|---|---|
3a4c7a6dc9 | |||
a7dee833f3 |
43
src/Tour.cpp
43
src/Tour.cpp
@ -52,7 +52,7 @@ void Tour::draw(QGraphicsScene *scene) const {
|
|||||||
return;
|
return;
|
||||||
|
|
||||||
Node *node = m_startNode;
|
Node *node = m_startNode;
|
||||||
do{
|
do {
|
||||||
node->point.draw(scene);
|
node->point.draw(scene);
|
||||||
node->point.drawTo(node->next->point, scene);
|
node->point.drawTo(node->next->point, scene);
|
||||||
node = node->next;
|
node = node->next;
|
||||||
@ -116,6 +116,31 @@ void Tour::insertNearest(Point p) {
|
|||||||
nearest->next = newNode;
|
nearest->next = newNode;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Node *findbestNodeTSP(Node *const start, Point &point) {
|
||||||
|
auto calcHeuristic = [&point](Node *const insertAt) -> double {
|
||||||
|
double distanceWithNewPoint = insertAt->point.distanceTo(point) +
|
||||||
|
point.distanceTo(insertAt->next->point);
|
||||||
|
double originalDistance = insertAt->point.distanceTo(insertAt->next->point);
|
||||||
|
|
||||||
|
return distanceWithNewPoint - originalDistance;
|
||||||
|
};
|
||||||
|
|
||||||
|
double optimalH = Q_INFINITY;
|
||||||
|
Node *optimalNode;
|
||||||
|
|
||||||
|
Node *node = start;
|
||||||
|
do {
|
||||||
|
double h = calcHeuristic(node);
|
||||||
|
if (h < optimalH) {
|
||||||
|
optimalH = h;
|
||||||
|
optimalNode = node;
|
||||||
|
}
|
||||||
|
node = node->next;
|
||||||
|
} while (node != start);
|
||||||
|
|
||||||
|
return optimalNode;
|
||||||
|
}
|
||||||
|
|
||||||
void Tour::insertSmallest(Point p) {
|
void Tour::insertSmallest(Point p) {
|
||||||
if (m_startNode == nullptr) {
|
if (m_startNode == nullptr) {
|
||||||
m_startNode = new Node(p);
|
m_startNode = new Node(p);
|
||||||
@ -128,21 +153,7 @@ void Tour::insertSmallest(Point p) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
Node *bestNode = m_startNode;
|
Node *bestNode = findbestNodeTSP(m_startNode, p);
|
||||||
double minIncrease = Q_INFINITY;
|
|
||||||
|
|
||||||
Node *node = m_startNode;
|
|
||||||
do {
|
|
||||||
double increase = node->point.distanceTo(p) +
|
|
||||||
p.distanceTo(node->next->point) -
|
|
||||||
node->point.distanceTo(node->next->point);
|
|
||||||
if (increase < minIncrease) {
|
|
||||||
minIncrease = increase;
|
|
||||||
bestNode = node;
|
|
||||||
}
|
|
||||||
node = node->next;
|
|
||||||
} while (node != m_startNode);
|
|
||||||
|
|
||||||
auto *newNode = new Node(p, bestNode->next);
|
auto *newNode = new Node(p, bestNode->next);
|
||||||
bestNode->next = newNode;
|
bestNode->next = newNode;
|
||||||
}
|
}
|
||||||
|
15
src/tsp.cpp
15
src/tsp.cpp
@ -18,7 +18,7 @@
|
|||||||
int main(int argc, char *argv[]) {
|
int main(int argc, char *argv[]) {
|
||||||
QApplication a(argc, argv);
|
QApplication a(argc, argv);
|
||||||
|
|
||||||
string filename = "tsp10.txt";
|
string filename = "tsp1000.txt";
|
||||||
#ifdef __APPLE__
|
#ifdef __APPLE__
|
||||||
// On macOS the resources are copeied to build/<release-target>/
|
// On macOS the resources are copeied to build/<release-target>/
|
||||||
// whilst, the running binary is under build/<release-target>/TSP.app/Contents/MacOS/
|
// whilst, the running binary is under build/<release-target>/TSP.app/Contents/MacOS/
|
||||||
@ -46,22 +46,25 @@ int main(int argc, char *argv[]) {
|
|||||||
Tour tour;
|
Tour tour;
|
||||||
double x;
|
double x;
|
||||||
double y;
|
double y;
|
||||||
|
auto timeStart = std::chrono::high_resolution_clock::now();
|
||||||
while (input >> x >> y) {
|
while (input >> x >> y) {
|
||||||
Point p(x, y);
|
Point p(x, y);
|
||||||
tour.insertNearest(p);
|
tour.insertSmallest(p);
|
||||||
//uncomment the 4 lines below to animate
|
//uncomment the 4 lines below to animate
|
||||||
tour.draw(scene);
|
//tour.draw(scene);
|
||||||
std::chrono::milliseconds dura(50);
|
//std::chrono::milliseconds dura(50);
|
||||||
std::this_thread::sleep_for(dura);
|
//std::this_thread::sleep_for(dura);
|
||||||
a.processEvents();
|
//a.processEvents();
|
||||||
}
|
}
|
||||||
input.close();
|
input.close();
|
||||||
|
auto took = std::chrono::high_resolution_clock::now() - timeStart;
|
||||||
|
|
||||||
// print tour to standard output
|
// print tour to standard output
|
||||||
cout << "Tour distance: " << std::fixed << std::setprecision(4)
|
cout << "Tour distance: " << std::fixed << std::setprecision(4)
|
||||||
<< std::showpoint << tour.distance() << endl;
|
<< std::showpoint << tour.distance() << endl;
|
||||||
cout << "Number of points: " << tour.size() << endl;
|
cout << "Number of points: " << tour.size() << endl;
|
||||||
tour.show();
|
tour.show();
|
||||||
|
cout << "Calculation took: " << took.count()*1E-6 << "ms" << endl;
|
||||||
|
|
||||||
// draw tour
|
// draw tour
|
||||||
tour.draw(scene);
|
tour.draw(scene);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user