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.
Study the Point.java API. Its main() method reads in data from standard input in the TSP input format and draws the points to standard draw.
Point.java represents a point in the plane, as described by the following API:
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
Remember that to implement an interface, you have to write all the above methods with the exact same method signature and return types.public interface TourInterface ------------------------------------------------------------------------------------------------------ public String toString() // create a String representation of the Tour. 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
It is easiest done via the following steps
Our strong suggestion is to create a Node inner class and keep an instance variable (field) called Node head. For an example of this look at LinkedStackOfStrings.java which can be found on booksite at link.
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 System.out.println(squareTour); }
(100.0, 100.0) (500.0, 100.0) (500.0, 500.0) (100.0, 500.0)
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. You will also need to change a conditional in the main function that does not print tours of size greater than 20. )
> 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 |
||