Assignment 3: 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 calculates routes between cities by various methods. The methods to use to calculate the routes and the names (or other labels) of the cities will be given as command-line arguments. For each method, the output should be the method used, the total distance, and the route.

The methods given on the command line will be any number of the following, in any order, and possibly with repeats:

Your solution must support at least one of the last two methods.

The names (or other labels) of the cities will be given as command-line arguments after the methods – the first command-line argument that does not begin with a hyphen and does not immediately follow -direction should be considered the first city name and everything after that should also be considered a city name . Each name will be given as a single argument, so New Haven would be a single command-line argument; you would have to quote New Haven on the command line so that it would be passed in as "New Haven" and not two separate arguments "New" and "Haven". The names on the command-line will be unique (that is, there will be no duplicates) and there will be at least two and no more than 1024 of them.

The locations of the cities will be given in the library cities (found in /c/cs223/hw3) which will define two global variables:

Do not make other assumptions about the contents of cities.c, as the definitions used for grading may differ from those used for testing. The declarations in cities.h will not change, although there may be new declarations added for grading as necessary.

Output

When the input is valid, the output must be written to standard output and must consist of one line per method giving the method (including east or west for the -direction method), the total distance in kilometers to two places after the decimal point (as computed by location_distance ), and the route, starting with the first city and ending with that city again. Output for multiple methods must be given in the order the methods 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. Methods 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 methods are given on the command line then there must be no output.

In addition, for certain invalid inputs, exactly one of the following error messages should be output to standard error followed by a single newline (and then nothing should be written to standard output):

When more than one error message is appropriate, output the one that applies first when considering the command-line arguments from first to last, with "too few cities" considered after the last method and before the first city.

For other invalid input, your program must not crash or hang. Your program will not be tested on invalid cities databases (and think about the extent to which you could test the validity of that data).

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.

Testing

It may be helpful to know for testing that one degree of longitude is about 111.32km at the equator and about 85.39km at 40 degrees latitude. The cities library used for testing contains locations of many airports, and distances of routes between airports can be checked at gcmap.com.

Example

$ ./TSP -direction east -direction west -nearest -insert -optimal BWI HER JNB DAC CNS
-direction east: 46753.53 BWI HER JNB DAC CNS BWI
-direction west: 46753.53 BWI CNS DAC JNB HER BWI
-nearest       : 47149.47 BWI HER DAC CNS JNB BWI
-insert        : 46753.53 BWI HER JNB DAC CNS BWI
-optimal       : 46753.53 BWI HER JNB DAC CNS BWI

Submissions

Submit your makefile, log file, and source code necessary to build the executable TSP using your makefile. Do not submit the provided libraries cities and location. Your makefile should assume that they have been copied into the current directory.