CSCI 136 |
1,000 points optimal tour
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.class Point (2D point data type) --------------------------------------------------------------------------------------- __init__(float x, float y) # create the point (x, y) string toString() # return string representation draw() # draw point using standard draw drawTo(Point that) # draw line segment between the two points float distanceTo(Point that) # return Euclidean distance between the two points
Your Tour data type must implement the following API:class Node : def __init__(self): self.p = None # This will hold the data, in this case a 2D point self.next = None # This will be the pointer to the next node
Each Tour object should be able to print its constituent points to standard output (the screen), draw its points to standard draw, count its number of points, compute its total distance, and insert a new point using any of the three heuristics. The constructor creates an empty tour (that is, a linked list with no nodes).class Tour ---------------------------------------------------------------------------------------------- __init__() # create an empty tour show() # print the tour to standard output draw() # draw the tour to standard draw int size() # number of points on tour float distance() # return the total distance of the tour insertInOrder(Point p) # insert p using in order heuristic insertNearest(Point p) # insert p using nearest neighbor heuristic insertSmallest(Point p) # insert p using smallest increase heuristic
After implementing Tour.py, you can use the client programs InOrderInsertion.py, NearestInsertion.py, and SmallestInsertion.py to read in the points from file run the associated heuristic, print the resulting tour and its distance to standard output (the screen); and draw the resulting tour to standard draw. Your code will implement the methods to carry this out, as described above, and should be able to do this using only the methods in the Point class, but you will need to import StdDraw in your Tour.py code if you wish to test the drawing part in your test main.% type tsp1000.txt 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
% python InOrderInsertion.py tsp1000.txt Tour distance = 327693.76217093336 (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) |
% python NearestInsertion.py tsp1000.txt Tour distance = 27868.710634854797 (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) |
% python SmallestInsertion.py tsp1000.txt Tour distance = 17265.628155352584 (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) |
|
Grade Item | Points Possible | Points Earned |
---|---|---|
Program Compiles and Runs | 2 | |
Comments on All Classes, Methods and Header | 4 | |
Implemented __init__() Correctly | 2 | |
Implemented insertInOrder(p) Correctly | 4 | |
Implemented show() Correctly | 2 | |
Implemented draw() Correctly | 2 | |
Implemented size() Correctly | 2 | |
Implemented distance() Correctly | 2 | |
Implemented insertNearest(p) Correctly | 4 | |
Implemented insertSmallest(p) Correctly | 4 | |
Write Up of Results across Several Test Files | 2 | |
Total | 30 |
Page last updated: December 28, 2020