-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathManhattenDistanceHeuristic.java
70 lines (60 loc) · 2.46 KB
/
ManhattenDistanceHeuristic.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import java.util.HashMap;
public class ManhattenDistanceHeuristic extends Heuristic {
HashMap<Byte, Integer[]> initTileCoords = new HashMap<Byte, Integer[]>();
HashMap<Byte, Integer[]> goalTileCoords = new HashMap<Byte, Integer[]>();
//Setup hash maps for quick lookup
public ManhattenDistanceHeuristic(PuzzleState init, PuzzleState goal) {
byte[][] initBoard = init.board.board;
byte[][] goalBoard = goal.board.board;
for (int i = 0; i < initBoard.length; i++) {
for (int j = 0; j < initBoard.length; j++) {
initTileCoords.put(initBoard[i][j], new Integer[]{new Integer(i), new Integer(j)});
goalTileCoords.put(goalBoard[i][j], new Integer[]{new Integer(i), new Integer(j)});
}
}
}
public ManhattenDistanceHeuristic(PuzzleState goal) {
byte[][] goalBoard = goal.board.board;
for (int i = 0; i < goalBoard.length; i++) {
for (int j = 0; j < goalBoard.length; j++) {
goalTileCoords.put(goalBoard[i][j], new Integer[]{new Integer(i), new Integer(j)});
}
}
}
/**
* Calculates and returns Manhattan distance for the given state's board.
*/
public int getHXvalue(PuzzleState state, boolean toGoal) {
HashMap<Byte,Integer[]> locations = toGoal ? goalTileCoords : initTileCoords;
byte[][] board = ((PuzzleState) state).board.board;
int dist = 0;
for (int x = 0; x < board.length; x++) {
for (int y = 0; y < board.length; y++) {
byte val = board[x][y];
if( val == 0 )continue;
Integer[] coords = locations.get(val);
dist += (Math.abs(x-coords[0])+Math.abs(y-coords[1]));
}
}
return dist;
}
/**
* Calculates the Manhattan distance in constant time by observing the difference the Manhattan distances of the
* current tile position and where it will land in the empty space.
*/
@Override
public int getHXvalueImproved(PuzzleState state, PuzzleMove move, int oldEvaluationValue, boolean isGoal) {
HashMap<Byte,Integer[]> locations = isGoal ? goalTileCoords : initTileCoords;
int moveX = move.x;
int moveY = move.y;
int emptyX = state.board.emptyX;
int emptyY = state.board.emptyY;
byte val = state.board.get(moveX, moveY);
Integer[] coords = locations.get(val);
int preMoveDist = (Math.abs(moveX-coords[0])+Math.abs(moveY-coords[1]));
int postMoveDist = (Math.abs(emptyX-coords[0])+Math.abs(emptyY-coords[1]));
int difference = Math.abs(preMoveDist-postMoveDist);
int toReturn = preMoveDist > postMoveDist ? oldEvaluationValue-difference : oldEvaluationValue+difference;
return toReturn;
}
}