Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package aima.core.environment.eightpuzzle;
- import aima.core.search.framework.HeuristicFunction;
- import aima.core.util.datastructure.XYLocation;
- /**
- * @author Ravi Mohan
- *
- */
- public class ManhattanHeuristicFunction implements HeuristicFunction {
- private EightPuzzleGoalTest goal;
- private double ep;
- public ManhattanHeuristicFunction(EightPuzzleGoalTest goal) {
- this.goal = goal;
- this.ep = 0.0;
- }
- public ManhattanHeuristicFunction(EightPuzzleGoalTest goal, double ep) {
- this.goal = goal;
- this.ep = ep;
- }
- public double h(Object state) {
- EightPuzzleBoard board = (EightPuzzleBoard) state;
- int retVal = 0;
- for (int i = 1; i < 9; i++) {
- XYLocation loc = board.getLocationOf(i);
- XYLocation locGS = goal.getLocationOf(i);
- retVal += evaluateManhattanDistanceOf(loc, locGS);
- }
- return (1+ep)*retVal;
- }
- public int evaluateManhattanDistanceOf(XYLocation loc, XYLocation locGS) {
- int retVal = 0;
- int xpos = loc.getXCoOrdinate();
- int ypos = loc.getYCoOrdinate();
- int xposGS = locGS.getXCoOrdinate();
- int yposGS = locGS.getYCoOrdinate();
- retVal = Math.abs(xpos - xposGS) + Math.abs(ypos - yposGS);
- return retVal;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement