Advertisement
Guest User

Untitled

a guest
Feb 8th, 2016
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.11 KB | None | 0 0
  1. import java.awt.Color;
  2. import java.awt.Point;
  3. import java.awt.image.BufferedImage;
  4. import java.io.File;
  5. import java.util.ArrayList;
  6. import java.util.HashMap;
  7. import java.util.List;
  8. import java.util.PriorityQueue;
  9.  
  10. import javax.imageio.ImageIO;
  11.  
  12. public class MyPlanner {
  13. public static MyState uniform_cost_search(MyState startState, MyState goalState) throws Exception{
  14. PriorityQueue<MyState> frontier = new PriorityQueue<MyState>(); // lowest cost comes out first
  15. HashMap<Point, MyState> beenthere = new HashMap<Point, MyState>();
  16. startState.cost = 0.0;
  17. startState.parent = null;
  18. beenthere.put(new Point (startState.x, startState.y), startState);
  19. frontier.add(startState);
  20. int iter = 0;
  21. while(frontier.size() > 0) {
  22. iter++;
  23. MyState s = frontier.poll();
  24. if(iter % 5000 < 1000)
  25. image.setRGB(s.x, s.y, 0xff00ff00);
  26. if(s.isEqual(goalState))
  27. return s;
  28.  
  29. MyState north = new MyState(0.0, s, s.x, s.y+1);
  30. MyState south = new MyState(0.0, s, s.x, s.y-1);
  31. MyState east = new MyState(0.0, s, s.x+1, s.y);
  32. MyState west = new MyState(0.0, s, s.x-1, s.y);
  33.  
  34. List<MyState> actions = new ArrayList<MyState>();
  35.  
  36. actions.add(north);
  37. actions.add(south);
  38. actions.add(east);
  39. actions.add(west);
  40.  
  41. MyState oldchild = null;
  42. for(MyState a : actions){
  43. if((a.x >= 0 && a.x < 500) && (a.y >= 0 && a.y < 500)){
  44. double acost = action_cost(a);
  45. Point childPoint = new Point(a.x, a.y);
  46. if(beenthere.containsKey(childPoint)){
  47. oldchild = beenthere.get(childPoint);
  48. if(s.cost + acost < oldchild.cost){
  49. oldchild.cost = s.cost + acost;
  50. oldchild.parent = s;
  51. }
  52. }
  53. else {
  54. a.cost = s.cost + acost;
  55. a.parent = s;
  56. frontier.add(a);
  57. Point childPt = new Point(a.x, a.y);
  58. beenthere.put(childPt, a);
  59. }
  60. }
  61. }
  62. }
  63. throw new Exception("There is no path to the goal");
  64. }
  65.  
  66. private static double action_cost(MyState a) {
  67. // TODO Auto-generated method stub
  68. Color c = new Color(image.getRGB(a.x, a.y));
  69. double green = c.getGreen();
  70. return green;
  71. }
  72.  
  73. static BufferedImage image = null;
  74. public static final String INPUT_FILE_PATH = "terrain.png";
  75. public static final String OUTPUT_FILE_PATH = "path.png";
  76. public static void main(String[] args) throws Exception{
  77.  
  78. image = ImageIO.read(new File(INPUT_FILE_PATH));
  79.  
  80. MyState start = new MyState(0.0, null, 100, 100);
  81. MyState goal = new MyState(0.0, null, 400, 400);
  82. MyState answer = uniform_cost_search(start, goal);
  83. System.out.print(answer.cost);
  84.  
  85. while(answer.parent != null){
  86. image.setRGB(answer.x, answer.y, 0xffff0000);
  87. answer = answer.parent;
  88. }
  89.  
  90. ImageIO.write(image, "png", new File(OUTPUT_FILE_PATH));
  91. }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement