Advertisement
Guest User

Untitled

a guest
Apr 24th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.62 KB | None | 0 0
  1. package org.educationalProject.surfacePathfinder.test;
  2.  
  3. import java.io.IOException;
  4. import java.util.HashMap;
  5. import java.util.List;
  6. import java.util.Vector;
  7.  
  8. import org.educationalProject.surfacePathfinder.*;
  9. import org.educationalProject.surfacePathfinder.path.AStarPathFind;
  10. import org.educationalProject.surfacePathfinder.path.DijkstraPathFind;
  11. import org.educationalProject.surfacePathfinder.timing.*;
  12. import org.educationalProject.surfacePathfinder.visualization.ColorizedMapVisualizer;
  13. import org.educationalProject.surfacePathfinder.visualization.PathVisualizer;
  14. import org.educationalProject.surfacePathfinder.visualization.SwingWindow;
  15. import org.jgrapht.alg.shortestpath.AStarShortestPath;
  16. import org.jgrapht.alg.shortestpath.DijkstraShortestPath;
  17. import org.jgrapht.graph.DefaultWeightedEdge;
  18. import org.jgrapht.graph.SimpleWeightedGraph;
  19.  
  20. import io.github.jdiemke.triangulation.NotEnoughPointsException;
  21. import io.github.jdiemke.triangulation.Triangle2D;
  22. import io.github.jdiemke.triangulation.Vector2D;
  23.  
  24. public class SerejaTest {
  25.  
  26. //addition punishment in our distances
  27. static final double ALTITUDE_MULTIPLIER = 16;
  28. //is used to determene whether the edge is "bad" and should not be included
  29. static final double COS_THRESHOLD = 0.7;
  30.  
  31. public void test() {
  32. try{
  33. double resultingTime;
  34. // We will used this clock to measure time through all the phases
  35. NanoClock clock = new NanoClock();
  36.  
  37. // Obj file reading
  38. clock.tic();
  39. Vector<Vector2D> points = ObjFileParser.getPoints("/home/merlin/DigDes/map.obj");
  40. resultingTime = clock.tocd();
  41. System.out.println("Reading is finished, phase duration is: " + resultingTime);
  42.  
  43. // Points triangulating
  44. clock.tic();
  45. List<Triangle2D> triangles = Triangulator.triangulate(points);
  46. resultingTime = clock.tocd();
  47. System.out.println("Triangulation is finished, phase duration is: " + resultingTime);
  48.  
  49. // Triangles -> graph convertion. Some edges are deleted if they have high altitude delta
  50. clock.tic();
  51. SimpleWeightedGraph<Point,DefaultWeightedEdge> graph = TrianglesToGraphConverter.convert(triangles, COS_THRESHOLD, ALTITUDE_MULTIPLIER);
  52. resultingTime = clock.tocd();
  53. System.out.println("Graph building is finished, phase duration is: " + resultingTime);
  54.  
  55.  
  56. //Finding the shortest path
  57. clock.tic();
  58. AStarShortestPath<Point,DefaultWeightedEdge> astar =
  59. new AStarShortestPath<Point,DefaultWeightedEdge>(
  60. graph,
  61. new EuclidianEuristic<Point>()
  62. );
  63. resultingTime = clock.tocd();
  64. System.out.println("Euristic building is finished, phase duration is: " + resultingTime);
  65.  
  66. double jgraphtResult;
  67. double serejaResult;
  68. double middle = 0.0;
  69. double jgraphtMiddle = 0.0;
  70. double serejaMiddle = 0.0;
  71. int errors = 0;
  72. int incorrect = 0;
  73. double jgraphtLength = 0.0;
  74. double serejaLength = 0.0;
  75. for (int i = 0; i < 1000; i++) {
  76.  
  77. try {
  78. clock.tic();
  79.  
  80. Point a = (Point) points.get((int) (Math.random() * points.size()));
  81. Point b = (Point) points.get((int) (Math.random() * points.size()));
  82. List<Point> nodes = astar.getPath(a, b).getVertexList();
  83. jgraphtResult = clock.tocd();
  84. //System.out.println("\nJgrapht A*: " + jgraphtResult);
  85. jgraphtMiddle += jgraphtResult;
  86.  
  87. clock.tic();
  88. AStarPathFind aStarPathFind = new AStarPathFind();
  89. List<Point> aStarNodes = aStarPathFind.getShortestPath(graph, a, b);
  90. serejaResult = clock.tocd();
  91. // System.out.println("Sereja A*: " + serejaResult);
  92. serejaMiddle += serejaResult;
  93.  
  94. for (int j = 0; j < nodes.size() - 1; j++) {
  95. DefaultWeightedEdge e = graph.getEdge(nodes.get(j), nodes.get(j + 1));
  96. jgraphtLength += (double) graph.getEdgeWeight(e);
  97. }
  98.  
  99. for (int j = 0; j < aStarNodes.size() - 1; j++) {
  100. DefaultWeightedEdge e = graph.getEdge(aStarNodes.get(j), aStarNodes.get(j + 1));
  101. serejaLength += (double) graph.getEdgeWeight(e);
  102. }
  103. if (serejaLength - jgraphtLength < 0.5)
  104. incorrect++;
  105.  
  106. } catch (NullPointerException e){
  107. errors++;
  108. continue;
  109. }
  110. }
  111.  
  112. //System.out.println("Error in seconds: " + middle / 1000.0);
  113. System.out.println("Correct length: " + incorrect + " / 1000");
  114. System.out.println("Errors in path: " + errors);
  115. System.out.println("Sereja middle: " + serejaMiddle / 1000.0);
  116. System.out.println("Jgrapht middle: " + jgraphtMiddle / 1000.0);
  117. //System.out.println("Jgrapht length: " + jgraphtLength);
  118. //System.out.println("Sereja length: " + serejaLength);
  119. /* //Visualizing
  120. ColorizedMapVisualizer vis1 = new ColorizedMapVisualizer();
  121. vis1.setData(triangles, nodes, graph);
  122. SwingWindow.start(vis1, 800, 600, "Jgrapht`s A* map");
  123.  
  124. //Visualizing
  125. ColorizedMapVisualizer vis2 = new ColorizedMapVisualizer();
  126. vis2.setData(triangles, aStarNodes, graph);
  127. SwingWindow.start(vis2, 800, 600, "Sereja`s A* map");
  128.  
  129. PathVisualizer vis3 = new PathVisualizer();
  130. vis3.setData(nodes, points);
  131. SwingWindow.start(vis3, 800, vis3.calculateWindowHeight(800), "Jgrapht`s A*");
  132.  
  133. PathVisualizer vis4 = new PathVisualizer();
  134. vis4.setData(aStarNodes, points);
  135. SwingWindow.start(vis4, 800, vis4.calculateWindowHeight(800), " Sereja`s A*");*/
  136.  
  137. System.out.println("end");
  138. }catch(IOException e){
  139. System.out.println("Problem with file reading accured!");
  140. }catch(TicTocException e){
  141. System.out.println("There is a problem with time measuring code. toc is executed before tic.");
  142. } catch (NotEnoughPointsException e) {
  143. System.out.println("Not enough points to triangulate");
  144. }
  145. }
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement