Compare commits

..

No commits in common. "E4" and "master" have entirely different histories.
E4 ... master

3 changed files with 2 additions and 45 deletions

View File

@ -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,48 +116,6 @@ 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);

View File

@ -24,7 +24,6 @@ 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;

View File

@ -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.insertOptimalHeuristic(p); tour.insertNearest(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);