C++ program about TSP

profile4n9r58y2u
tsp_project.rar

TSP project.pdf

CS 325 Project 4: The Travelling Salesman Problem (TSP)

1

In this project you will have fun trying out ideas to solve a very hard problem: The Traveling Salesman

Problem (TSP).

You are given a set of n cities and for each pair of cities c 1 and c2, the distances between them d(c1, c2).

Your goal is to find an ordering (called a tour) of the cities so that the distance you travel is minimized.

The distance you travel is the sum of the distances from the first city in the ordering to the second city,

plus the distance second city to the third city, and so on until you reach the last city, and then adding the

distance from the last city to the first city. For example if the cities are Seattle, Portland, Corvallis and

Boise. The total distance traveled visiting the cities in this order is:

d(tour) = d(Seattle,Portland) + d(Portland, Corvallis) + d(Corvallis,Boise) + d(Boise, Seattle)

In this project, you will only need to consider the special case where the cities are locations in a 2D grid

(given by their x and y coordinates) and the distance between two cities c1 = (x1, y1) and c2 = (x2, y2) is

given by their Euclidean distance. To avoid floating point precision problems in computing the square-

root, we will always round the distance to the nearest integer. In other words you will compute the

distance between cities c1 and c2 as:

𝑑(𝑐1,𝑐2) = nearestint(√(𝑥1 − 𝑥2) 2 + (𝑦1 − 𝑦2)

2)

For example, if the three cities are given by the coordinates c 1= (0, 0), c2 = (1, 3), c3 = (6, 0), then a tour

that visits the cities in order c1 –> c2 –> c3 – >c1 has the distance

d(tour) = d(c1, c2) + d(c2, c3) + d(c3, c1)

where

𝑑(𝑐1,𝑐2) = nearestint(√(0 − 1) 2 + (0 − 3)2)

= nearestint(√(−1)2 + (−3)2)

= nearestint(√1 + 9)

= nearestint(√10)

= nearestint(3.1622… ) = 3

CS 325 Project 4: The Travelling Salesman Problem (TSP)

2

𝑑(𝑐2,𝑐3) = nearestint(√(1 − 6) 2 + (3 − 0)2)

= nearestint(√(−5)2 + (3)2)

= nearestint(√25 + 9)

= nearestint(√34)

= nearestint(5.8309… ) = 6

𝑑(𝑐3,𝑐1) = nearestint(√(6 − 0) 2 + (0 − 0)2)

= nearestint(√(6)2 + (0)2)

= nearestint(√36 + 0)

= nearestint(√36)

= nearestint(6) = 6

So that d(tour ) = 3 + 6 + 6 = 15.

Project Specification

Your group will design and implement an algorithm (or algorithms) for finding the best tour you can.

TSP is not a problem for which you will be able to easily find optimal solutions. It is difficult. Your goal is

to find the best solution you can in a certain time frame. You may want to start w ith some local search

heuristics. There is much literature on methods to “solve” TSP please cite any sources you use. Use any

programming language you want that runs on flip2.engr.oregonstate.edu.

Your program must:

 Accept problem instances on the command line

 Name the output file as the input file’s name with .tour appended (for example input

tsp_example_1.txt will output tsp_example_1.txt.tour)

 Compile/Execute correctly and without debugging on flip2.engr.oreognstate.edu according to

specifications and any documentation you provide.

Input specifications:

 A problem instance will always be given to you as a text file.

 Each line defines a city and each line has three numbers separated by white space.

o The first number is the city identifier

o The second number is the city’s x-coordinate

o The third number is the city’s y-coordinate.

Output specifications:

 You must output your solution into another text file with n+1 lines, where n is the number of

cities.

CS 325 Project 4: The Travelling Salesman Problem (TSP)

3

 The first line is the length of the tour your program computes.

 The next n lines should contain the city identifiers in the order they are visited by your tour.

o Each city must be listed exactly once in this list.

o This is the certificate for your solution and your solutions will be checked. If they are

not valid you will not receive credit for them.

Example instances: We have provided you with three example instances. They are available on Canvas

and are provided according to the input specifications.

 tsp_example_[*].txt Input files

 tsp_example_[*].txt.tour Example outputs corresponding to these three input cases.

The optimal tour lengths for example instances 1, 2, and 3 are 108159, 2579 and 1573084,

respectively. Clearly these do not match the values in the tour files. You should use these

values to evaluate your algorithm. For full credit it is required that the ratio of

(your solution)/(optimal solution) <= 1.25.

Testing

A testing procedure tsp-verifier.py is given that we will use to verify your solutions. Usage to test

example an instance is: (NOTE: requires TSPAllVisitied.py)

python tsp-verifier.py inputfilena me solutionfilename

You should test that your outputs are correct. By “correct” we mean that the distances have been

computed correctly not that the solution is optimal.

Competition

We will hold a completion. The competition will require your program to find the best solution possible

to one or more test instances within a fixed amount of time (e.g. 3 minutes). The competition instances

will be available on Monday 5/30/16 at 8:00 am PST. You will not be told the optimal tour length for

these instances. You will post your results to the competition instances to the competition discussion

board.

Project Report

You will submit a project report containing the following:

 A description of at least three different methods/algorithms for solving the Traveling Salesman

Problem. Summarize any research you did.

 A verbal description of your algorithm(s) as completely as possible. You may select more than

one algorithm to implement.

 A discussion on why you selected the algorithm(s).

 Pseudo code

 Your “best” tours for the three example instances and the time it took to obtain these tours.

 Your best tours for the competition test instance(s).

CS 325 Project 4: The Travelling Salesman Problem (TSP)

4

Submission

 All files (including a REDME), solutions and report must be zipped and submitted to TEACH by

Friday 6/03/16 at 11:59pm PST. Only the report will be submitted to Canvas.

Check List

 Does your program correctly compute tour lengths for simple cases?

 Does your program read input files and options from the command line?

 Does your program meet the output specifications?

 Did you check that you produce solutions that verify correctly?

 Did you find solutions to the example instances?

 Did you find solutions to the competition instances?

 Does your code compile/run without issue according to your documentation?

 Have you submitted your report, your solutions to the test cases, your source and any

comments you wish to include to TEACH and Canvas?