forked from saschazar21/rushhour
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAdvancedHeuristic.java
220 lines (157 loc) · 5.2 KB
/
AdvancedHeuristic.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
package Heuristics;
import java.util.ArrayList;
import AStar.Puzzle;
import AStar.State;
public class AdvancedHeuristic implements Heuristic {
private Puzzle puzzle;
private State state;
private int numCars;
ArrayList<Integer> visited;
public AdvancedHeuristic(Puzzle puzzle) {
this.puzzle = puzzle;
this.numCars = this.puzzle.getNumCars();
this.visited = new ArrayList<Integer>();
}
public int getValue(State state) {
this.state = state;
this.visited.clear();
if (state.isGoal()) {
return 0;
}
return this.getMinimumRequiredMoves(state);
}
private int getMinimumRequiredMoves(State state) {
visited.add(0);
int value = 1;
for (Integer car : getInitialBlockingCars()) {
int needsSpaceFront = needsSpace(0, car, 0, true);
int needsSpaceBack = needsSpace(0, car, 0, false);
value += getBlockingValue(car, needsSpaceFront, needsSpaceBack);
}
return value;
}
private ArrayList<Integer> getInitialBlockingCars() {
ArrayList<Integer> blocking = new ArrayList<Integer>();
boolean carOrient = puzzle.getCarOrient(0);
int carSize = puzzle.getCarSize(0);
int carPos = state.getVariablePosition(0);
int carFixed = puzzle.getFixedPosition(0);
for (int i = 1; i < this.numCars; i++) {
if (carOrient == puzzle.getCarOrient(i)) {
continue;
}
int iFixed = puzzle.getFixedPosition(i);
if (iFixed < carPos + carSize) {
continue;
}
int iPos = state.getVariablePosition(i);
int iPosFront = iPos + puzzle.getCarSize(i);
if (carFixed >= iPos && carFixed < iPosFront) {
blocking.add(i);
}
}
return blocking;
}
private int getBlockingValue(int car, int needsSpaceFront, int needsSpaceBack) {
visited.add(car);
int value = 1;
for (int next = 0; next < numCars; next++) {
if (next == car) {
continue;
}
if (visited.contains(next)) {
continue;
}
if (!isIntersecting(car, next)) {
continue;
}
int valueFwd = 0, valueBwd = 0;
boolean fwdMoveable = canMove(car, next, needsSpaceFront, true);
boolean bwdMoveable = canMove(car, next, needsSpaceBack, false);
int needsSpaceFwd = needsSpace(car, next, needsSpaceFront, true);
int needsSpaceBwd = needsSpace(car, next, needsSpaceBack, false);
if (!fwdMoveable) {
valueFwd = getBlockingValue(next, needsSpaceFwd, needsSpaceBwd);
} else if (isWallBlocking(car, needsSpaceFront, true)) {
valueFwd = Integer.MAX_VALUE;
}
if (!bwdMoveable) {
valueBwd = getBlockingValue(next, needsSpaceFwd, needsSpaceBwd);
} else if (isWallBlocking(car, needsSpaceBack, false)) {
valueBwd = Integer.MAX_VALUE;
}
value += Math.min(valueFwd, valueBwd);
}
return value;
}
private boolean canMove(int car, int next, int needsSpace, boolean direction) {
boolean isBehind = isBehind(car, next);
if (isBehind && direction || !isBehind && !direction) {
return true;
}
int hasSpace = hasSpace(car, next, direction);
return hasSpace >= needsSpace;
}
private int needsSpace(int car, int next, int needsSpace, boolean direction) {
if (puzzle.getCarOrient(car) == puzzle.getCarOrient(next)) {
int hasSpace = hasSpace(car, next, direction);
return needsSpace - hasSpace;
}
int carFixed = puzzle.getFixedPosition(car);
int nextPos = state.getVariablePosition(next);
if (direction) {
return Math.abs(carFixed - nextPos) + 1;
}
int nextPosFront = nextPos + puzzle.getCarSize(next);
return Math.abs(carFixed - nextPosFront);
}
private int hasSpace(int car, int next, boolean direction) {
int carPos = state.getVariablePosition(car);
int carPosFront = carPos + puzzle.getCarSize(car);
int nextFixed = puzzle.getFixedPosition(next);
if (puzzle.getCarOrient(car) != puzzle.getCarOrient(next)) {
if (direction) {
return Math.abs(carPosFront - nextFixed);
}
return Math.abs(carPos - nextFixed) + 1;
}
int nextPos = state.getVariablePosition(next);
if (direction) {
return Math.abs(carPosFront - nextPos);
}
int nextPosFront = nextPos + puzzle.getCarSize(next);
return Math.abs(carPos - nextPosFront);
}
private boolean isWallBlocking(int car, int needsSpace, boolean direction) {
int carPos = state.getVariablePosition(car);
int carPosFront = carPos + puzzle.getCarSize(car);
if (direction && (carPosFront + needsSpace > puzzle.getGridSize())) {
return true;
}
if (!direction && (carPos - needsSpace < 0)) {
return true;
}
return false;
}
private boolean isIntersecting(int car, int next) {
int carFixed = puzzle.getFixedPosition(car);
int nextFixed = puzzle.getFixedPosition(next);
if (puzzle.getCarOrient(car) == puzzle.getCarOrient(next)) {
return carFixed == nextFixed;
}
int nextPos = state.getVariablePosition(next);
int nextPosFront = nextPos + puzzle.getCarSize(next);
return carFixed >= nextPos && carFixed < nextPosFront;
}
private boolean isBehind(int car, int next) {
int carSize = puzzle.getCarSize(car);
int carPos = state.getVariablePosition(car);
int nextPos = state.getVariablePosition(next);
int nextSize = puzzle.getCarSize(next);
if (this.puzzle.getCarOrient(car) == this.puzzle.getCarOrient(next)) {
return nextPos + nextSize <= carPos;
}
int nextFixed = puzzle.getFixedPosition(next);
return nextFixed < carPos + carSize;
}
}