Compare commits
2 Commits
Author | SHA1 | Date | |
---|---|---|---|
decfc1c699 | |||
0b75e07b1d |
44
src/Tour.cpp
44
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,48 @@ 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::insertOptimalHeuristic(Point p) {
|
||||||
|
if (m_startNode == nullptr) {
|
||||||
|
m_startNode = new Node(p);
|
||||||
|
m_startNode->next = m_startNode; // Make it cirkular
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (m_startNode->next == nullptr) {
|
||||||
|
auto newNode = new Node(p, m_startNode);
|
||||||
|
m_startNode->next = newNode;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
Node *bestNode = findbestNodeTSP(m_startNode, p);
|
||||||
|
auto *newNode = new Node(p, bestNode->next);
|
||||||
|
bestNode->next = newNode;
|
||||||
|
}
|
||||||
|
|
||||||
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);
|
||||||
|
@ -24,6 +24,7 @@ public:
|
|||||||
double distance() const;
|
double distance() const;
|
||||||
void insertNearest(Point p);
|
void insertNearest(Point p);
|
||||||
void insertSmallest(Point p);
|
void insertSmallest(Point p);
|
||||||
|
void insertOptimalHeuristic(Point p);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Node* m_startNode;
|
Node* m_startNode;
|
||||||
|
@ -49,7 +49,7 @@ int main(int argc, char *argv[]) {
|
|||||||
auto timeStart = std::chrono::high_resolution_clock::now();
|
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.insertOptimalHeuristic(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);
|
||||||
|
Loading…
Reference in New Issue
Block a user