Use the renaming thing that they wanted

This commit is contained in:
Love 2024-09-06 16:33:54 +02:00
parent 0b75e07b1d
commit decfc1c699
3 changed files with 34 additions and 2 deletions

View File

@ -141,7 +141,7 @@ Node *findbestNodeTSP(Node *const start, Point &point) {
return optimalNode;
}
void Tour::insertSmallest(Point p) {
void Tour::insertOptimalHeuristic(Point p) {
if (m_startNode == nullptr) {
m_startNode = new Node(p);
m_startNode->next = m_startNode; // Make it cirkular
@ -157,3 +157,34 @@ void Tour::insertSmallest(Point p) {
auto *newNode = new Node(p, bestNode->next);
bestNode->next = newNode;
}
void Tour::insertSmallest(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 = m_startNode;
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);
bestNode->next = newNode;
}

View File

@ -24,6 +24,7 @@ public:
double distance() const;
void insertNearest(Point p);
void insertSmallest(Point p);
void insertOptimalHeuristic(Point p);
private:
Node* m_startNode;

View File

@ -49,7 +49,7 @@ int main(int argc, char *argv[]) {
auto timeStart = std::chrono::high_resolution_clock::now();
while (input >> x >> y) {
Point p(x, y);
tour.insertSmallest(p);
tour.insertOptimalHeuristic(p);
//uncomment the 4 lines below to animate
//tour.draw(scene);
//std::chrono::milliseconds dura(50);