# include # include # include # include # include # include # include using namespace std; # include "tsp_dantzig42.hpp" int main ( ); void tsp_display ( string prefix, int n, double *x2 ); double tsp_tour_cost ( int n, double *x, int *order ); void timestamp ( ); //****************************************************************************80 int main ( ) //****************************************************************************80 // // Purpose: // // tsp_dantzig42_test() tests tsp_dantzig42(). // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 24 July 2026 // // Author: // // John Burkardt // { double cost; int *dantzig42_order; double *dantzig42_xy; int i; int i2; int n; string prefix = "dantzig42"; double *x2; timestamp ( ); cout << "\n"; cout << "tsp_dantzig42_test():\n"; cout << " C++ version\n"; cout << " Test tsp_dantzig42(), which returns the Dantzig 42 city\n"; cout << " traveling salesperson problem challenge data.\n"; n = 42; dantzig42_order = tsp_dantzig42_order ( ); dantzig42_xy = tsp_dantzig42_xy ( ); cout << "\n"; cout << " City coordinates:\n"; cout << "\n"; for ( i = 0; i < n; i++ ) { cout << " " << setw(2) << i << " " << setw(8) << dantzig42_xy[2*i] << " " << setw(8) << dantzig42_xy[2*i+1] << "\n"; } // // Compute cost of shortest round trip. // cost = tsp_tour_cost ( n, dantzig42_xy, dantzig42_order ); cout << "\n"; cout << " Cost of round trip is " << cost << "\n"; // // Copy the city locations in TSP order. // x2 = new double[(n+1)*2]; for ( i2 = 0; i2 <= n; i2++ ) { if ( i2 < n ) { i = dantzig42_order[i2]; } else { i = dantzig42_order[0]; } x2[2*i2] = dantzig42_xy[2*i]; x2[2*i2+1] = dantzig42_xy[2*i+1]; } // // Display the locations. // tsp_display ( prefix, n + 1, x2 ); // // Free memory. // delete [] dantzig42_order; delete [] dantzig42_xy; delete [] x2; // // Terminate. // cout << "\n"; cout << "tsp_dantzig42_test():\n"; cout << " Normal end of execution.\n"; timestamp ( ); return 0; } //****************************************************************************80 void tsp_display ( string prefix, int n, double *x2 ) //****************************************************************************80 // // Purpose: // // tsp_display() plots cities and a tour connecting them. // // Discussion: // // If a "round trip" is desired, then the x2 array must repeat the // first city at the end. // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 13 June 2026 // // Author: // // John Burkardt // // Input: // // string prefix: a string that defines the name of // the problem. It is also used to create file names needed for // input to gnuplot. // // int n: the number of cities. // // double x[n*2]: the x and y coordinates of the cities. // { string command_filename; ofstream command; ofstream data; string data_filename; int i; // // Create the data file. // data_filename = prefix + "_data.txt"; data.open ( data_filename.c_str ( ) ); for ( i = 0; i < n; i++ ) { data << x2[2*i] << " " << x2[2*i+1] << "\n"; } data.close ( ); cout << " Created data file '" << data_filename << "'\n"; // // Create the command file. // command_filename = prefix + "_commands.txt"; command.open ( command_filename.c_str ( ) ); command << "# " << command_filename << "\n"; command << "#\n"; command << "# Usage:\n"; command << "# gnuplot < " << command_filename << "\n"; command << "#\n"; command << "set term png\n"; command << "set output '" << prefix << ".png'\n"; command << "set xlabel '<--- X --->'\n"; command << "set ylabel '<--- Y --->'\n"; command << "set title '" << prefix << "'\n"; command << "set grid\n"; command << "unset key\n"; command << "set style data lines\n"; command << "plot '" << data_filename << "' using 1:2 lw 3 linecolor rgb 'blue', \\\n"; command << " '" << data_filename << "' using 1:2 with points pointtype 7 pointsize 3 linecolor rgb 'green'\n"; command << "quit\n"; command.close ( ); cout << " Created command file '" << command_filename << "'\n"; return; } //****************************************************************************80 double tsp_tour_cost ( int n, double *x, int *order ) //****************************************************************************80 // // Purpose: // // tsp_tour_cost() evaluates the cost of a round trip. // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 18 June 2026 // // Author: // // John Burkardt // // Input: // // int n: the number of cities. // // double x[n,2]: the city locations. // // int order[n]: the order in which the cities are visited. // // Output: // // double cost: the cost of the route. // { double cost; int from; int to; cost = 0.0; from = n - 1; for ( to = 0; to < n; to++ ) { cost = cost + sqrt ( pow ( x[order[from]*2] - x[order[to]*2], 2 ) + pow ( x[order[from]*2+1] - x[order[to]*2+1], 2 ) ); from = to; } return cost; } //****************************************************************************80 void timestamp ( ) //****************************************************************************80 // // Purpose: // // timestamp() prints the current YMDHMS date as a time stamp. // // Example: // // 31 May 2001 09:45:54 AM // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 19 March 2018 // // Author: // // John Burkardt // { # define TIME_SIZE 40 static char time_buffer[TIME_SIZE]; const struct std::tm *tm_ptr; std::time_t now; now = std::time ( NULL ); tm_ptr = std::localtime ( &now ); std::strftime ( time_buffer, TIME_SIZE, "%d %B %Y %I:%M:%S %p", tm_ptr ); std::cout << time_buffer << "\n"; return; # undef TIME_SIZE }