Ejemplo n.º 1
0
main(int argc, char *argv[]) {
    int  n;                   // 点の数
    struct point  p[MAX_N];   // 各点の座標を表す配列
    int tour[MAX_N];   // 巡回路を表現する配列

    if(argc != 2) {
        fprintf(stderr,"Usage: %s <tsp_filename>\n",argv[0]);
        exit(EXIT_FAILURE);
    }

    // 点の数と各点の座標を1番目のコマンドライン引数で指定されたファイルから読み込む
    read_tsp_data(argv[1],p, &n);
    // 最近近傍法による巡回路構築
    nn(p,n,tour);
    // 巡回路をテキストファイルとして出力
    write_tour_data("tour1.dat",n,tour);
    // 巡回路長を画面に出力
    printf("%lf\n",tour_length(p,n,tour));
    // 2opt による改善
    TwoOpt(p,n,tour);
    // 巡回路をテキストファイルとして出力
    write_tour_data("tour2.dat",n,tour);
    // 巡回路長を画面に出力
    printf("%lf\n",tour_length(p,n,tour));

    exit(EXIT_SUCCESS);
}
Ejemplo n.º 2
0
// Description: Optimize TSP problem's solution with 2-opt algorithm
void TSPSolver::StartTwoOpt()
{
	int size = tour.size();

	for ( int i = 0; i < size - 3; i++ ) {
		for ( int j = i + 3; j < size - 1; j++ ) {
			TwoOpt(i, i + 1, j, j + 1);		// use 2-opt algorithm with selected nodes
		}
	}

	SetTourDistance(CalcTourDistance());	// recalculating tour distance
}