claukiller

h12

Oct 27th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.99 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 Heuristic12 implements HeuristicFunction {
  11.  
  12. private EightPuzzleGoalTest goal;
  13.  
  14. public Heuristic12(EightPuzzleGoalTest goal) {
  15. this.goal = goal;
  16. }
  17.  
  18. public double h(Object state) {
  19. EightPuzzleBoard board = (EightPuzzleBoard) state;
  20. int retVal = 0;
  21. for (int i = 1; i < 9; i++) {
  22. XYLocation loc = board.getLocationOf(i);
  23. XYLocation locGS = goal.getLocationOf(i);
  24. retVal += evaluateDistanceOf(loc, locGS);
  25. }
  26. return retVal;
  27.  
  28. }
  29.  
  30. public int evaluateDistanceOf(XYLocation loc, XYLocation locGS) {
  31. int retVal = 0;
  32. int xpos = loc.getXCoOrdinate();
  33. int ypos = loc.getYCoOrdinate();
  34. int xposGS = locGS.getXCoOrdinate();
  35. int yposGS = locGS.getYCoOrdinate();
  36.  
  37. if (xpos != xposGS) retVal += 1;
  38. if (ypos != yposGS) retVal +=1;
  39.  
  40. return retVal;
  41. }
  42. }
Add Comment
Please, Sign In to add comment