Assignment 2: Travelling Salesperson

Objectives

Introduction

The Travelling Salesperson Problem is the problem of, given a list of locations and the distances between every pair of them, determining the shortest tour that starts and ends at the same city and visits each other city exactly once. It is a very common problem in logistics: consider, for example, a delivery driver who needs to make stops at a list of 50 addresses and wants to minimize fuel use along the way.

Assignment

Write a program called TSP that produces tours of cities by applying various heuristic algorithms. The hope is that the result of those heuristics is something reasonably close to the shortest possible tour, which is very time consuming to find. The name of the file containing the cities and their locations, and which algorithms to use to calculate the routes will be given as command-line arguments. For each algorithm, the program should output the algorithm used, the total distance of the resulting route, and the order of the cities in the route produced by the algorithm (see below for the exact format).

The name of the file containing the cities and their geographic coordinates will be the first command-line argument. That file will contain the following (and nothing else): 1) on the first line, the number of cities in a format readable by atoi, which will be no greater than 10000; 2) on the second line, a list of distinct three-character labels, one for each city, separated by single spaces (no label will contain a space or other whitespace character); 3) one line per city containing the latitude and longitude of the city in degrees in a format readable by atof and separated by a single space, where the latitude is between -90.0 and 90.0 (inclusive) and the longitude is between -180.0 (inclusive) and 180.0 (exclusive), and no two cities with have both the same latitude and the same longitude.

Which algorithms should be used to construct the tours (if any) will be given as the subsequent command-line arguments. They can contain any number of the following, in any order, and possibly with repeats. In all cases, distances should be calculated with the location_distance function in the location library that is provided as header and implementation files in /c/cs223/hw2/Required that you can compile and link with your code.

Output

When the input is valid, the output must be written to standard output and must consist of one line per algorithm on the command line, where each line contains: 1) the algorithm (including adjacent or any for the -exchange algorithm); 2) the total distance in kilometers (as computed by summing the values returned by location_distance for each leg), rounded to the nearest hundredth of a kilometer and displayed with two digits after the decimal point even if the last one or both are zero, and right-justified so the decimal points line up in the same column as in the example shown below; and 3) the route, starting with the first city in the input and ending with that city again.

Output for multiple algorithms must be given in the order the algorithms appeared on the command line. Each line of output must be followed by a single newline character; see the example for other spacing and punctuation. Algorithms can be repeated and in such cases the output for the repeats must appear in order according to their place on the command line. If no algorithms are given on the command line but the command-line arguments and input file are otherwise valid then there must be no output.

In addition, for certain invalid inputs (either invalid command-line arguments or invalid status or contents of the input file), exactly one of the following error messages should be output to standard error followed by a single newline (and it does not matter what is written to standard output):

When more than one error message is appropriate, output the one that appears first in the list above. For other invalid input, your program must not crash or hang, but the output can be anything or nothing.

You may assume that the distances returned by a single call to location_distance will not exceed the longest possible distance on the largest rocky planet explored in person or robotically by humankind.

Grading and Testing

Your program must not produce any errors when run with valgrind, regardless of whether the input is valid or not.

Some consideration will be given to time efficiency, with particuarly efficient implementations earning a few points more than typical implementations. Grossly inefficient but otherwise correct implementations may fail to complete in the time bounds enforced by the grading scripts and will incur more substantial penalties.

Distances of routes between airports (a convenient source of three-character labels for cities) can be checked at gcmap.com. Beware that the distances computed by gcmap may disagree with the expected values calculated from the input files because the databases may choose different points on airport property to represent the airports, and because of slight differences in the model of the surface of the Earth used.

Example

$ ./TSP avelo_east.in -given -farthest -exchange adjacent -exchange any
-given            :      4490.78 HVN RSW PBI TPA FLL MCO HVN
-farthest         :      4089.15 HVN MCO TPA PBI FLL RSW HVN
-exchange adjacent:      3907.75 HVN PBI FLL RSW TPA MCO HVN
-exchange any     :      3907.75 HVN PBI FLL RSW TPA MCO HVN
Where the file avelo_east.in contains
6
HVN RSW PBI TPA FLL MCO
41.263889 -72.886944
26.536111 -81.755278
26.683056 -80.095556
27.975556 -82.533333
26.072500 -80.152778
28.429444 -81.308889

Submissions

Submit your makefile, log file, and source code necessary to build the executable TSP using your makefile. Do not submit the provided location library. Your makefile should assume that the files for the location library have been copied into the current directory; they may not still exist in their original locations when the final grading scripts run.