Guest User

Untitled

a guest
Jan 22nd, 2018
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.03 KB | None | 0 0
  1. package motion;
  2.  
  3. import java.awt.*;
  4. import java.awt.event.*;
  5. import java.awt.image.*;
  6.  
  7. import javax.swing.*;
  8. import java.awt.Color;
  9. import java.util.ArrayList;
  10.  
  11. import searchalgorithm.*;
  12. import searchproblem.*;
  13.  
  14. public class MotionTest extends Component
  15. {
  16.  
  17. private static final long serialVersionUID = 1L;
  18.  
  19. static final String TERRAIN_PATH = "src/motion/th.png";
  20. static final int SURFACE_STEP=10;
  21. static final boolean SURFACE_LEVELS=true;
  22. static final boolean ANIMATE=true;
  23.  
  24. private static ArrayList<Object> optimal;
  25.  
  26. static BufferedImage img;
  27. static int startx, starty, goalx,goaly;
  28.  
  29. public void paint(Graphics g) {
  30. g.drawImage(img, 0, 0, null);
  31. g.setColor( Color.WHITE);
  32. g.drawOval(startx-5, starty-5, 10, 10);
  33. g.drawOval(goalx-5, goaly-5, 10, 10);
  34. g.drawOval(goalx-3, goaly-3, 6, 6);
  35.  
  36. }
  37.  
  38. public Dimension getPreferredSize() {
  39. if (img == null) {
  40. return new Dimension(100,100);
  41. } else {
  42. return new Dimension(img.getWidth(null), img.getHeight(null));
  43. }
  44. }
  45.  
  46. public static void main(String[] args) {
  47.  
  48. System.gc();
  49. BitmapTerrain t = new BitmapTerrain(TERRAIN_PATH);
  50. img = t.getTerrain();
  51.  
  52. // AUXILIARY CLASS TO SHOW THE EVOLUTION OF THE FRONTIER
  53. if( ANIMATE ) {
  54. AnimatedSearch.init(TERRAIN_PATH);
  55. }
  56.  
  57. // DRAWS SURFACE LEVELS
  58. if( SURFACE_LEVELS ) {
  59. for( int x=0; x < t.getHorizontalSize() ; x++)
  60. for( int y=0 ; y < t.getVerticalSize() ; y++ )
  61. if( t.getHeight(x, y) % SURFACE_STEP < 1)
  62. img.setRGB(x, y, Color.PINK.getRGB() ^ img.getRGB(x, y));
  63. }
  64.  
  65. // Creates and shows the window with the terrain
  66. JFrame f = new JFrame("Rover's path in Mars!");
  67. f.addWindowListener(new WindowAdapter(){
  68. public void windowClosing(WindowEvent e) {
  69. System.exit(0);
  70. }
  71. });
  72.  
  73. f.add(new MotionTest());
  74. f.pack();
  75. f.setVisible(true);
  76. f.paint(f.getGraphics());
  77.  
  78. // Some data points for testing the algorithms
  79. int pointx[] = {611, 495, 818, 772, 996, 805, 113, 698, 562, 415, 527, 28, 887, 955, 772,
  80. 927, 712, 434, 611, 721, 75, 966, 346, 872, 679, 623, 173, 264, 476, 458};
  81.  
  82. int pointy[] = {190, 659, 10, 638, 741, 115, 714, 453, 707, 777, 124, 617, 880, 271, 13,
  83. 866, 615, 418, 836, 156, 132, 132, 64, 236, 766, 305, 540, 677, 506, 342};
  84.  
  85. int i=0;
  86. int j=6;
  87. int startx =pointx[i];
  88. int starty =pointy[i];
  89. int goalx = pointx[j];
  90. int goaly = pointy[j];
  91.  
  92. // Avoids side-effects of writing in the image
  93. t = new BitmapTerrain(TERRAIN_PATH);
  94.  
  95. RoverState init = new RoverState(startx,starty,t);
  96. RoverState goal = new RoverState(goalx,goaly,t);
  97.  
  98. // Solves the problem using AStarSearch
  99. InformedSearchProblem prob = new RoverProblem(init,goal);
  100. // SearchAlgorithm u = new AStarSearch(prob);
  101. SearchAlgorithm u = new UniformCostSearch(prob);
  102.  
  103.  
  104. // Determines the solution and writes the metrics
  105. Node n = u.searchSolution();
  106. System.out.println(u.getMetrics());
  107.  
  108. // Draws the solution in the screen
  109. if( n != null) {
  110. System.out.print( ((int) (n.getPathCost()*100)) + " ");
  111. System.out.println(n.getPath());
  112. RoverState current = (RoverState) init.clone();
  113. img.setRGB(current.getCoordX(), current.getCoordY(), 0xFF << 16);
  114. for( Object action: n.getPath()) {
  115. current.applyOperator(action);
  116. img.setRGB(current.getCoordX(), current.getCoordY(), 0xFF << 16 );
  117. }
  118. }
  119. f.paint(f.getGraphics());
  120.  
  121. SearchAlgorithm a = new AStarSearch(prob);
  122. AnimatedSearch.changeColor();
  123. Node node = a.searchSolution();
  124. System.out.println(a.getMetrics());
  125.  
  126. if( node != null) {
  127. System.out.print( ((int) (node.getPathCost()*100)) + " ");
  128. System.out.println(node.getPath());
  129. RoverState current = (RoverState) init.clone();
  130. img.setRGB(current.getCoordX(), current.getCoordY(), 0xFF << 16);
  131. for( Object action: node.getPath()) {
  132. current.applyOperator(action);
  133. img.setRGB(current.getCoordX(), current.getCoordY(), 0xFF << 16 );
  134. }
  135. }
  136. f.paint(f.getGraphics());
  137. System.gc();
  138. }
  139.  
  140. }
Add Comment
Please, Sign In to add comment