/** * tsp.c * Builds tours of cities in input according to one of three possible * heuristics. Names of cities are given on the command line along * with the heuristics to use and the name of a file containing the * locations (latitude, longitude) of the cities. For example, * * ./TSP ne_6.dat -given -nearest -insert HVN ALB MHT BDL ORH PVD * * where ne_6.dat could contain * * HVN,41.26388889,-72.88694444 * ALB,42.74916667,-73.80194444 * MHT,42.93277778,-71.43583333 * BDL,41.93916667,-72.68333333 * ORH,42.26722222,-71.87555556 * PVD,41.72388889,-71.42833333 * * @author CPSC 223 Staff and you * @version 2025.01.31.0 */ #define _GNU_SOURCE // SEE THIS FILE FOR DECLARATIONS AND DOCUMENTATION OF SUGGESTED FUNCTIONS #include "tsp.h" #include #include #include #include #include #include #include "location.h" int main(int argc, char **argv) { // the index on the command line of the origin city // TODO: this is hard-coded as if there is always one method // after the filename; fix that to account for more than one size_t origin = 3; // the number of cities on the command line size_t num_cities = argc - origin; // TODO: maybe add some more error checking here // initialize names and indices of cities in the tour // TODO: read coordinates from the file and make this work for any // cities on the command line; you will need to do something // different to allocate and initialize the array since this assumes // there are 6 (HVN ALB MHT BDL ORH PVD), but you can use this to test // the algorithms without having to worry about reading input city cities[] = { {"HVN", {41.26388889, -72.88694444}, 0}, {"ALB", {42.74916667, -73.80194444}, 1}, {"MHT", {42.93277778, -71.43583333}, 2}, {"BDL", {41.93916667, -72.68333333}, 3}, {"ORH", {42.26722222, -71.87555556}, 4}, {"PVD", {41.72388889, -71.42833333}, 5}, }; // iterate over methods requested on command line for (size_t a = 2; a < origin; a++) { if (strcmp(argv[a], "-insert") == 0) { route_insert(num_cities, cities); } else if (strcmp(argv[a], "-nearest") == 0) { route_nearest(num_cities, cities); } else if (strcmp(argv[a], "-given") == 0) { } normalize_start(num_cities, cities); normalize_direction(num_cities, cities); double total = calculate_total(num_cities, cities); printf("%-10s: %12.2f ", argv[a], total); print_tour(num_cities, cities); } // TODO: there may be other opportunities for error checking! return 0; } void route_insert(size_t n, city tour[]) { } void find_closest_pair(size_t n, city tour[], size_t *best_orig, size_t *best_dest) { } size_t find_closest_to_tour(size_t n, city tour[], size_t tour_len) { return 0; } size_t find_insertion_point(size_t n, city tour[], size_t subtour_len, size_t next) { return 0; } void route_nearest(size_t n, city tour[]) { } size_t find_closest_city(city tour[], size_t c, size_t from, size_t to) { return 0; } double calculate_total(size_t n, city tour[]) { double tot = 0.0; return tot; } void swap(city arr[], size_t i, size_t j) { if (i != j) { city temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } } void normalize_start(size_t n, city tour[]) { } void normalize_direction(size_t n, city tour[]) { } int read_file(FILE *in, size_t n, city cities[]) { return 0; } void print_tour(size_t n, city cities[]) { if (n == 0) { return; } fprintf(stdout, "%s", cities[0].name); for (size_t i = 1; i < n; i++) { fprintf(stdout, " %s", cities[i].name); } fprintf(stdout, " %s\n", cities[0].name); }