# include # include # include # include using namespace std; # include "tsp_nearest.hpp" //****************************************************************************80 void tsp_nearest ( int n, double *x, int *order_best, double &cost_best ) //****************************************************************************80 // // Purpose: // // tsp_nearest() seeks an optimal traveling salesperson path by the nearest neighbor method. // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 14 July 2026 // // Author: // // John Burkardt // // Input: // // int n: the number of cities. // // double *x[n*2]: the coordinates of the cities. // // Output: // // int order_best[n]: the order in which the N cities should be visited. // // double &cost_best: the length of the round trip. // { double cost; int i; int *order; int start; // // Initialize: // the best cost so far is Infinity; // the best tour so far is 1, 2, ..., n. // cost_best = HUGE_VAL; for ( i = 0; i < n; i++ ) { order_best[i] = i; } // // Try starting at city START and moving to nearest neighbor. // for ( start = 0; start < n; start++ ) { order = tsp_tour_nearest ( n, x, start ); cost = tsp_tour_cost ( n, x, order ); if ( cost < cost_best ) { cout << "Order best:"; for ( i = 0; i < n; i++ ) { order_best[i] = order[i]; cout << " " << order_best[i]; } cost_best = cost; cout << " Route starting at city " << start << " has cost = " << cost_best << "\n"; } delete [] order; } 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: // // 19 July 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 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 int *tsp_tour_nearest ( int n, double *x, int start ) //****************************************************************************80 // // Purpose: // // tsp_tour_nearest() finds a nearest neighbor route for a given start. // // Licensing: // // This code is distributed under the MIT license. // // Modified: // // 14 July 2026 // // Author: // // John Burkardt // // Input: // // int n: the number of cities. // // double x[n,2]: the city coordinates. // // int start: the starting city. // // Output: // // int order[n]: the route that starts at START. // { double *d; double dmin; int from; int i; int j; int k; int *order; int to; // // Make a copy of the distance table that we can modify. // d = new double [n*n]; for ( i = 0; i < n; i++ ) { for ( j = 0; j < n; j++ ) { d[i+j*n] = sqrt ( pow ( x[i*2] - x[j*2], 2 ) + pow ( x[i*2+1] - x[j*2+1], 2 ) ); } } for ( i = 0; i < n; i++ ) { d[i+i*n] = HUGE_VAL; } // // At FROM, find nearest unvisited city TO. // order = new int[n]; for ( j = 0; j < n; j++ ) { if ( j == 0 ) { to = start; } else { to = 0; dmin = d[from+to*n]; for ( k = 1; k < n; k++ ) { if ( d[from+k*n] < dmin ) { to = k; dmin = d[from+to*n]; } } } // // Add TO to path, and move there. // order[j] = to; from = to; // // Reset D so we can't revisit city TO. // for ( i = 0; i < n; i++ ) { d[i+to*n] = HUGE_VAL; } } delete [] d; return order; }