Submit Tour.java and your readme_tsp.txt file, and answer the questions. You may also submit an extra.zip file containing your extra credit implementation and any additional files it requires.
Please take a few minutes to fill out our anonymous end-of-semester evaluation. While the University's evaluation gives limited (but helpful) feedback to your fellow students through Penn Course Review, to the Dean's office, and to our chair, this survey is a way to quickly give more directed feedback to us and to your TAs.
Given N cities, the goal of a traveling salesman is to visit each of them exactly once (and arrive back home) while keeping the total distance traveled as short as possible. Stated more abstractly, the goal is to find a path connecting N points in a plane that passes through each point exactly once. Implement three greedy heuristics to find good (but not optimal) solutions to the traveling salesman problem (TSP). In doing this assignment, You should (i) learn about the notorious traveling salesman problem, (ii) get more practice with linked lists, and (iii) learn to adapt them to your problem.
1,000 points optimal tour
Perspective. The importance of the TSP does not arise from an overwhelming demand of salespeople to minimize their travel distance, but rather from a wealth of other applications such as vehicle routing, circuit board drilling, VLSI design, robot control, X-ray crystallography, machine scheduling, and computational biology.
Greedy heuristics. The traveling salesman problem is a notoriously difficult combinatorial optimization problem, In principle, one can enumerate all possible tours and pick the shortest one; in practice, the number of tours is so staggeringly large (roughly N factorial) that this approach is useless. For large N, no one knows an efficient method that can find the shortest possible tour for any given set of points. However, many methods have been studied that seem to work well in practice, even though they are not guaranteed to produce the best possible tour. Such methods are called heuristics. Your main task is to implement the in-order, nearest neighbor and smallest detour insertion heuristics for building a tour incrementally. Start with a one-point tour (from the first point back to itself), and iterate the following process until there are no points left.
Each Point object can return a string representation of itself, draw itself to standard draw, draw a line segment from itself to another point using standard draw, and calculate the Euclidean distance between itself and another point.public class Point (2D point data type) --------------------------------------------------------------------------------------- Point(double x, double y) // create the point (x, y) String toString() // return string representation void draw() // draw point using standard draw void drawTo(Point that) // draw line segment between the two points double distanceTo(Point that) // return Euclidean distance between the two points
public interface TourInterface ------------------------------------------------------------------------------------------------------ void show() // print the tour to standard output void draw(Point p) // draw the tour to standard draw // any edge starting or ending at p should be in a distinct color int size() // number of points on tour double distance() // return the total distance of the tour void insertInOrder(Point p) // insert p at the end of the tour void insertNearest(Point p) // insert p using nearest neighbor heuristic void insertSmallest(Point p) // insert p using smallest increase heuristic
Your task is to create a Tour data type that represents the sequence of points visited in a TSP tour. It must implement the TourInterface interface.
If your method is working properly, you will get the following output for the 4 city problem above.// main method for testing public static void main(String[] args) { // define 4 points forming a square Point a = new Point(100.0, 100.0); Point b = new Point(500.0, 100.0); Point c = new Point(500.0, 500.0); Point d = new Point(100.0, 500.0); // Set up a Tour with those four points // The constructor should link a->b->c->d->a Tour squareTour = new Tour(a, b, c, d); // Output the Tour squareTour.show(); }
(100.0, 100.0) (500.0, 100.0) (500.0, 500.0) (100.0, 500.0)
You will need to include the statements
in your main() before you call the Tour draw() method. The four point example above should produce a square.StdDraw.setXscale(0, 600); StdDraw.setYscale(0, 600);
You should see the following output when running insertInOrder() on tsp10.txt and tsp1000.txt (you should comment out the animation lines or speed the animation up a lot in OrderInsertion before attempting tsp1000.txt:
> java OrderInsertion tsp10.txt (110.0, 225.0) (161.0, 280.0) (325.0, 554.0) (490.0, 285.0) (157.0, 443.0) (283.0, 379.0) (397.0, 566.0) (306.0, 360.0) (343.0, 110.0) (552.0, 199.0) Tour distance = 2586.6759 Number of points = 10 |
> java OrderInsertion tsp1000.txt (185.0411, 457.8824) ... (334.9344, 524.7824) (396.0952, 150.0943) (462.1732, 486.3889) (193.5595, 527.8311) (331.8716, 533.5717) (748.401, 462.4281) (94.0231, 544.9736) (254.982, 302.2548) Tour distance = 327693.7622 Number of points = 1000 |
||
As a check, tsp10-smallest.ans contains the result tour, which has distance 1655.7462. In this case, the smallest insertion heuristic actually does worse than the nearest insertion heuristic (although this is not typical).
775 768 185.0411 457.8824 247.5023 299.4322 701.3532 369.7156 563.2718 442.3282 144.5569 576.4812 535.9311 478.4692 383.8523 458.4757 329.9402 740.9576 ... 254.9820 302.2548
> java NearestInsertion tsp1000.txt (185.0411, 457.8824) (198.3921, 464.6812) (195.8296, 456.6559) (216.8989, 455.126) (213.3513, 468.0186) (241.4387, 467.413) (259.0682, 473.7961) (221.5852, 442.8863) ... (264.57, 410.328) Tour distance = 27868.7106 Number of points = 1000 |
> java SmallestInsertion tsp1000.txt (185.0411, 457.8824) (195.8296, 456.6559) (193.0671, 450.2405) (200.7237, 426.3461) (200.5698, 422.6481) (217.4682, 434.3839) (223.1549, 439.8027) (221.5852, 442.8863) ... (186.8032, 449.9557) Tour distance = 17265.6282 Number of points = 1000 |
||
Estimate the running times of your programs as a function of the number of points N.
Implement a better heuristic in a class TourEC.java. For example, observe that any tour with paths that cross can be transformed into a shorter one with no crossing paths: add that improvement to your program. Here are some other ideas. The speed of your program may vary depending on your computer, but in general it should be at most five minutes for tsp1000.txt. You are also not required to use the Tour data type or TourInterface interface for you extra credit solution.
Any code beyond TourEC.java necessary to run your extra credit should be included in extra.zip. You do not have to use Point.java or any of your code from the main part of the assignment. (You may also modify Point.java in any way you wish for the extra credit.) However, if you do use any of that code, you should include it in your extra.zip file or your TA may not be able to compile your program.
Be warned that this is a relatively difficult extra credit, although it gives an opportunity to learn a great deal about an extremely important problem. Answer the relevant questions in the readme_tsp.txt file. We will award a special prize to the student who finds the shortest tour around the 1,000-point set.