Advertisement
Elandiro

G&A 4

May 23rd, 2013
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.23 KB | None | 0 0
  1. package gna;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. import java.util.PriorityQueue;
  6.  
  7. /**
  8. * A tour constructed using the minimal spanning tree heuristic.
  9. */
  10. public class MinimalSpanningTreeTour extends Tour {
  11.  
  12. private Point root = getWorld().getPoints().get(0);
  13. private List<MSTEdge> MST = new ArrayList<MSTEdge>();
  14. private List<Point> visitSequence = new ArrayList<Point>();
  15.  
  16. public class MSTEdge {
  17. public final Point p1, p2;
  18.  
  19. public MSTEdge(Point p1, Point p2) {
  20. this.p1 = p1;
  21. this.p2 = p2;
  22. }
  23. }
  24.  
  25. public MinimalSpanningTreeTour(World world) {
  26. super(world);
  27.  
  28. // compute route here
  29.  
  30. PriorityQueue<MSTEdge> edges = new PriorityQueue<MSTEdge>(1,
  31. new edgeComparator());
  32. for (Point p : getWorld().getPoints()) {
  33. if (p != root) {
  34. MSTEdge edge = new MSTEdge(root, p);
  35. edges.offer(edge);
  36. }
  37. }
  38. MSTEdge current = edges.peek();
  39. MST.add(edges.poll());
  40.  
  41. while (MST.size() <= getWorld().getNbPoints() - 2) {
  42. getOptimalEdges(edges, current);
  43. current = edges.peek();
  44. MST.add(edges.poll());
  45. }
  46. }
  47.  
  48. private void getOptimalEdges(PriorityQueue<MSTEdge> edges, MSTEdge current) {
  49. ArrayList<MSTEdge> removeList = new ArrayList<>();
  50. ArrayList<MSTEdge> addList = new ArrayList<>();
  51. for (MSTEdge e : edges) {
  52. if (current.p2.distanceTo(e.p2) < e.p1.distanceTo(e.p2)) {
  53. Point p2 = e.p2;
  54. removeList.add(e);
  55. MSTEdge edge = new MSTEdge(current.p2, p2);
  56. addList.add(edge);
  57. }
  58. }
  59. for (MSTEdge e : removeList) {
  60. edges.remove(e);
  61. }
  62. for (MSTEdge e : addList) {
  63. edges.offer(e);
  64. }
  65. }
  66.  
  67. @Override
  68. public double getTotalDistance() {
  69. double d = 0;
  70. findVisitSequence();
  71. for (int i = 0; i < visitSequence.size(); i++) {
  72. if (i != visitSequence.size() - 1) {
  73. d += visitSequence.get(i).distanceTo(visitSequence.get(i + 1));
  74. } else {
  75. d += visitSequence.get(i).distanceTo(visitSequence.get(0));
  76. }
  77. }
  78. return d;
  79. }
  80.  
  81. /**
  82. * Return the root of the MST used to construct the visit sequence.
  83. *
  84. * This method returns null if and only if
  85. * <code>getWorld().getPoints()</code> is empty.
  86. */
  87. public Point getMSTRoot() {
  88. if (getWorld().getPoints().isEmpty()) {
  89. return null;
  90. } else {
  91. return root;
  92. }
  93. }
  94.  
  95. /**
  96. * Return the edges on the MST used to construct the visit sequence.
  97. *
  98. * The result of this method is never null.
  99. */
  100. public List<MSTEdge> getMST() {
  101. return MST;
  102. }
  103.  
  104. @Override
  105. /**
  106. * The visit sequence is a PRE-ORDER traversal of the MST
  107. * starting at a root (e.g. the first point of the world).
  108. *
  109. * Traverse the children of each node in increasing order of distance.
  110. *
  111. * Return the empty list if world is empty.
  112. */
  113. public List<Point> getVisitSequence() {
  114. return visitSequence;
  115. }
  116.  
  117. private void getChildren(Point current) {
  118. int position = visitSequence.indexOf(current) + 1;
  119. for (MSTEdge e : MST) {
  120. if (e.p1 == current) {
  121. visitSequence.add(position++, e.p2);
  122. }
  123. }
  124. }
  125.  
  126. private void findVisitSequence() {
  127. Point current = root;
  128. int counter = 0;
  129. visitSequence.add(counter++, current);
  130. while (visitSequence.size() <= MST.size()) {
  131. getChildren(current);
  132. current = visitSequence.get(counter++);
  133. }
  134. }
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement