int main(int argc, char *argv[]) { QApplication a(argc, argv); // define 4 points forming a square Point p(100.0, 100.0); Point q(500.0, 100.0); Point r(500.0, 500.0); Point s(100.0, 500.0); // Set up a Tour with those four points // The constructor should link p->q->r->s->p Tour squareTour(p, q, r, s); squareTour.show(); string filename = "tsp10.txt"; ifstream input; input.open(filename); // get dimensions int width; int height; input >> width; input >> height; // setup graphical window QGraphicsView *view = new QGraphicsView(); QGraphicsScene *scene = new QGraphicsScene(); view->setScene(scene); view->scale(1, -1); //screen y-axis is inverted view->setSceneRect(0, 0, width, height); view->show(); // run insertion heuristic Tour tour; double x; double y; while (input >> x >> y) { Point p(x, y); //tour.insertNearest(p); tour.insertSmallest(p); //uncomment the 4 lines below to animate tour.draw(scene); std::chrono::milliseconds dura(50); std::this_thread::sleep_for(dura); a.processEvents(); } input.close(); // print tour to standard output cout << "Tour distance: " << std::fixed << std::setprecision(4) << std::showpoint << tour.distance() << endl; cout << "Number of points: " << tour.size() << endl; tour.show(); // draw tour tour.draw(scene); return a.exec(); // start Qt event loop }
void twoOpt() { if (done_twoOpt) return; cur_energy = cur.getDist(); int start, finish, temp; finish = -1; start = 0; while (finish < start) { start = cur.getDist(); for (int r = 0; r < NUM_COLORS - 1; r++) { for (int c = r + 1; c < NUM_COLORS; c++) { if (!cur.swapIsBeneficial(r, c)) continue; cur.twoOptSwap(r, c); cur_energy = cur.getUpdatedDistWhenTwoOptSwap(cur_energy, r, c); if (ANIMATE) { cur.draw(); update(); } } } finish = cur.getDist(); if (finish < start) best_tour.updateColors(cur.colors); } cout << "Final distance after 2-opt: " << best_tour.getDist() << endl; done_twoOpt = true; }
int main(int argc, char * argv[]) { srand ( time(NULL) ); CSDLManagerLite::getInstance()->initializeSDL(WIDTH, HEIGHT, TITLE); cout << "Initial distance: " << cur.getDist() << endl; cout << "Colors: " << NUM_COLORS << endl; int a, b; double cur_energy, new_energy; while (!CSDLInputManagerLite::getInstance() -> isExit()) { CSDLManagerLite::getInstance()->delay(1000); twoOpt(); best_tour.draw(); update(); } CSDLManagerLite::getInstance()->clean(); }