# include # include # include # include # include # include using namespace std; # include "tsp_brute.hpp" int main ( ); void timestamp ( ); void tsp_display ( string prefix, int n, double *x2 ); //****************************************************************************80 int main ( ) //****************************************************************************80 // // Purpose: // // tsp_brute_test() tests tsp_brute(). // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 18 June 2026 // // Author: // // John Burkardt // { # define N 12 double cost; int i; int i2; int n = N; int *order; string prefix = "att12"; double x[N*2] = { 6734, 1453, 2233, 10, 5530, 1424, 401, 841, 3082, 1644, 7608, 4458, 7573, 3716, 7265, 1268, 6898, 1885, 1112, 2049, 5468, 2606, 5989, 2873 }; double *x2; timestamp ( ); cout << "\n"; cout << "tsp_brute_test()\n"; cout << " C++ version\n"; cout << " tsp_brute() solves a traveling salesman problem (TSP)\n"; cout << " using brute force.\n"; // // Get the solution. // order = new int[n]; tsp_brute ( n, x, cost, order ); cout << " Length of optimal path is " << cost << "\n"; // // Display final path. // x2 = new double[(n+1)*2]; for ( i2 = 0; i2 < n + 1; i2++ ) { if ( i2 < n ) { i = order[i2]; } else { i = order[0]; } x2[2*i2] = x[2*i]; x2[2*i2+1] = x[2*i+1]; } // // Send the data to the plotter. // tsp_display ( prefix, n + 1, x2 ); delete [] x2; // // Terminate. // cout << "\n"; cout << "tsp_brute_test():\n"; cout << " Normal end of execution.\n"; timestamp ( ); return 0; } //****************************************************************************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 } //****************************************************************************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; }