Compare commits
No commits in common. "E4" and "master" have entirely different histories.
42
src/Tour.cpp
42
src/Tour.cpp
@ -116,48 +116,6 @@ void Tour::insertNearest(Point p) {
|
||||
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) {
|
||||
if (m_startNode == nullptr) {
|
||||
m_startNode = new Node(p);
|
||||
|
@ -24,7 +24,6 @@ public:
|
||||
double distance() const;
|
||||
void insertNearest(Point p);
|
||||
void insertSmallest(Point p);
|
||||
void insertOptimalHeuristic(Point p);
|
||||
|
||||
private:
|
||||
Node* m_startNode;
|
||||
|
@ -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.insertOptimalHeuristic(p);
|
||||
tour.insertNearest(p);
|
||||
//uncomment the 4 lines below to animate
|
||||
//tour.draw(scene);
|
||||
//std::chrono::milliseconds dura(50);
|
||||
|
Loading…
Reference in New Issue
Block a user