Advertisement
claukiller

manhattanheuristiccambiao

Oct 27th, 2017
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.19 KB | None | 0 0
  1. package aima.core.environment.eightpuzzle;
  2.  
  3. import aima.core.search.framework.HeuristicFunction;
  4. import aima.core.util.datastructure.XYLocation;
  5.  
  6. /**
  7. * @author Ravi Mohan
  8. *
  9. */
  10. public class ManhattanHeuristicFunction implements HeuristicFunction {
  11.  
  12. private EightPuzzleGoalTest goal;
  13. private double ep;
  14.  
  15. public ManhattanHeuristicFunction(EightPuzzleGoalTest goal) {
  16. this.goal = goal;
  17. this.ep = 0.0;
  18. }
  19.  
  20. public ManhattanHeuristicFunction(EightPuzzleGoalTest goal, double ep) {
  21. this.goal = goal;
  22. this.ep = ep;
  23. }
  24.  
  25.  
  26. public double h(Object state) {
  27. EightPuzzleBoard board = (EightPuzzleBoard) state;
  28. int retVal = 0;
  29. for (int i = 1; i < 9; i++) {
  30. XYLocation loc = board.getLocationOf(i);
  31. XYLocation locGS = goal.getLocationOf(i);
  32. retVal += evaluateManhattanDistanceOf(loc, locGS);
  33. }
  34. return (1+ep)*retVal;
  35.  
  36. }
  37.  
  38. public int evaluateManhattanDistanceOf(XYLocation loc, XYLocation locGS) {
  39. int retVal = 0;
  40. int xpos = loc.getXCoOrdinate();
  41. int ypos = loc.getYCoOrdinate();
  42. int xposGS = locGS.getXCoOrdinate();
  43. int yposGS = locGS.getYCoOrdinate();
  44.  
  45. retVal = Math.abs(xpos - xposGS) + Math.abs(ypos - yposGS);
  46.  
  47. return retVal;
  48. }
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement