This repository has been archived by the owner on Mar 14, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEvaluator.java
326 lines (274 loc) · 9.62 KB
/
Evaluator.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
import java.util.Arrays;
public class Evaluator {
private static final int TOTAL_NUM_FEATURES = 9;
// Indices for each feature to keep track of which feature to use
private static final int INDEX_ROWS_CLEARED = Heuristic.INDEX_ROWS_CLEARED;
private static final int INDEX_COL_HEIGHT = Heuristic.INDEX_COL_HEIGHT;
private static final int INDEX_COL_HEIGHT_DIFF = Heuristic.INDEX_COL_HEIGHT_DIFF;
private static final int INDEX_MAX_COL_HEIGHT = Heuristic.INDEX_MAX_COL_HEIGHT;
private static final int INDEX_NUM_HOLES = Heuristic.INDEX_NUM_HOLES;
private static final int INDEX_LANDING_HEIGHT = Heuristic.INDEX_LANDING_HEIGHT;
private static final int INDEX_ROW_TRANSITIONS = Heuristic.INDEX_ROW_TRANSITIONS;
private static final int INDEX_COL_TRANSITIONS = Heuristic.INDEX_COL_TRANSITIONS;
private static final int INDEX_WELL_SUMS = Heuristic.INDEX_WELL_SUMS;
// Flags for features
private char[] featureFlags;
private int numFeatures;
private double[] weights;
private int weightCounter;
private TempState s;
private int[] colHeights;
public Evaluator(Heuristic h, TempState s, double[] inputWeights) {
this.featureFlags = h.getFeatureFlags();
this.numFeatures = h.getNumFeatures();
this.weightCounter = 0;
this.weights = inputWeights;
this.s = s;
}
public double evaluate() {
int[][] field = s.getField();
this.colHeights = colHeights(field);
return weightedNumRowsCleared()
+ weightedSumColHeight(colHeights)
+ weightedSumColDiff(colHeights)
+ weightedMaxColHeight(colHeights)
+ weightedNumHoles(field, colHeights)
+ weightedLandingHeight(s)
+ weightedNumRowTranstions(field)
+ weightedNumColTranstions(field)
+ weightedWellSums(field);
}
/** METHODS TO COMPUTE FEATURE SUM **/
// Returns weighted number of rows cleared by playing this move
private double weightedNumRowsCleared() {
if (featureFlags[INDEX_ROWS_CLEARED] != '1') {
return 0;
}
int rowsEliminated = s.getRowsCleared() - s.getRowsPrevCleared();
return getNextWeight() * rowsEliminated;
}
// Returns weighted sum of column heights
private double weightedSumColHeight(int[] colHeights) {
if (featureFlags[INDEX_COL_HEIGHT] != '1') {
return 0;
}
double weightedSum = 0;
// iterate through columns, count height for each col
for (int i = 0; i < colHeights.length; i++) {
weightedSum += getNextWeight() * colHeights[i];
}
return weightedSum;
}
// Returns weighted sum of absolute differences between adjacent columns
private double weightedSumColDiff(int[] colHeights) {
if (featureFlags[INDEX_COL_HEIGHT_DIFF] != '1') {
return 0;
}
double weightedSum = 0;
for (int i = 1; i < colHeights.length; i++) {
weightedSum += getNextWeight() * Math.abs(colHeights[i] - colHeights[i-1]);
}
return weightedSum;
}
// Returns weighted max col
private double weightedMaxColHeight(int[] colHeights) {
if (featureFlags[INDEX_MAX_COL_HEIGHT] != '1') {
return 0;
}
int max = 0;
for (int i = 0; i < colHeights.length; i++) {
if (colHeights[i] > max) {
max = colHeights[i];
}
}
return getNextWeight() * max;
}
// Returns weighted number of holes
private double weightedNumHoles(int[][] field, int[] colHeights) {
if (featureFlags[INDEX_NUM_HOLES] != '1') {
return 0;
}
int numHoles = 0;
for (int col = 0; col < colHeights.length; col++) {
for (int row = colHeights[col] - 1; row >= 0; row--) {
if (field[row][col] == 0) {
numHoles++;
}
}
}
return getNextWeight() * numHoles;
}
private double weightedLandingHeight (TempState s) {
if (featureFlags[INDEX_LANDING_HEIGHT] != '1') {
return 0;
}
return getNextWeight() * landingHeight(s);
}
// Landing Height:
// The height where the piece is put = the height of the column BEFORE piece is put + (the height of the piece / 2)
// Also equivalent to height of column AFTER piece is put - (the height of the piece / 2) (?)
private int landingHeight(TempState s) {
int heightPiece = s.getHeightOfPeice();
int landingHeight = s.getHeightOfCol(s.getStateSlot()) - heightPiece/2;
return landingHeight;
}
private double weightedNumRowTranstions(int[][] field) {
if (featureFlags[INDEX_ROW_TRANSITIONS] != '1') {
return 0;
}
return getNextWeight() * numRowTransitions(field);
}
// The total number of row transitions.
// A row transition occurs when an empty cell is adjacent to a filled cell
// on the same row and vice versa.
private int numRowTransitions(int[][] field) {
int count = 0;
int numRows = field.length;
int numCols = field[0].length;
for (int i = 0; i < numRows; i++) {
for (int j = 0; j < numCols - 1; j++) {
if (field[i][j] == 0 && field[i][j+1] != 0
|| field[i][j] != 0 && field[i][j+1] == 0){
count++;
}
}
}
return count;
}
private double weightedNumColTranstions(int[][] field) {
if (featureFlags[INDEX_COL_TRANSITIONS] != '1') {
return 0;
}
return getNextWeight() * numColTransitions(field);
}
// The total number of column transitions.
// A column transition occurs when an empty cell is adjacent to a filled cell
// on the same column and vice versa.
private int numColTransitions(int[][] field) {
int count = 0;
int numRows = field.length;
int numCols = field[0].length;
for (int j = 0; j < numCols; j++) {
for (int i = 0; i < numRows - 1; i++) {
if (field[i][j] == 0 && field[i+1][j] != 0
|| field[i][j] != 0 && field[i+1][j] == 0){
count++;
}
}
}
return count;
}
private double weightedWellSums(int[][] field) {
if (featureFlags[INDEX_WELL_SUMS] != '1') {
return 0;
}
return getNextWeight() * wellSums(field);
}
// A well is a succession of empty cells such that their left cells and right cells are both filled.
// Example:
// |1101100010|
// |1101101010|
// |1101101000| | denotes left/right edge border
// returns 8
private int wellSums(int[][] field) {
int count = 0;
int numRows = field.length;
int numCols = field[0].length;
for (int i = 0; i < numRows; i++) {
for (int j = 0; j < numCols; j++) {
if (field[i][j] == 0){ // if cell is empty
if (j > 0 && j < numCols - 1 && field[i][j - 1] != 0 && field[i][j + 1] != 0) {
count += exploreWell(i, j, field);
} else if (j == 0 && field[i][j+1] !=0) {
count += exploreWellLeftSide(i, field);
} else if (j == numCols - 1 && field[i][j-1] != 0){
count += exploreWellRightSide(i, field);
}
}
}
}
return count;
}
// explore up the rows within the column
private int exploreWell(int startRow, int startColumn, int[][] field) {
// if this empty column is part of an existing well. Don't bother
if (startRow != 0
&& field[startRow-1][startColumn] == 0
&& field[startRow-1][startColumn - 1] != 0
&& field[startRow-1][startColumn + 1] != 0) {
return 0;
}
int count = 0;
while(field[startRow][startColumn] == 0
&& field[startRow][startColumn-1] != 0
&& field[startRow][startColumn+1] != 0) {
count++;
startRow++;
}
if (field[startRow][startColumn] != 0) {
return 0;
}
return sumFromOneToN(count);
}
// explore up the rows within the column for wells by the left wall
private int exploreWellLeftSide(int startRow, int[][] field) {
// if this empty column is part of an existing well. Don't bother
if (startRow != 0
&& field[startRow-1][0] == 0
&& field[startRow-1][1] != 0) {
return 0;
}
int count = 0;
while(field[startRow][0] == 0
&& field[startRow][1] != 0) {
count++;
startRow++;
}
if (field[startRow][0] != 0) {
return 0;
}
return sumFromOneToN(count);
}
// explore up the rows within the column for wells by the right wall
private int exploreWellRightSide(int startRow, int[][] field) {
int numCols = field[0].length;
// if this empty column is part of an existing well. Don't bother
if (startRow != 0
&& field[startRow-1][numCols - 1] == 0
&& field[startRow-1][numCols - 2] != 0) {
return 0;
}
int count = 0;
while(field[startRow][numCols - 1] == 0
&& field[startRow][numCols - 2] != 0) {
count++;
startRow++;
}
if (field[startRow][numCols -1] != 0) {
return 0;
}
return sumFromOneToN(count);
}
/** HELPER METHODS **/
// Returns array of column heights
private int[] colHeights(int[][] field) {
int[] colHeights = new int[field[0].length];
for (int col = 0; col < field[0].length; col++) {
int row = field.length - 1;
while (row >= 0 && field[row][col] == 0) {
row--;
}
colHeights[col] = row + 1;
}
return colHeights;
}
// Given an N returns sum From One to N.
// eg: Input N. Ouput 1 + 2 + 3 + 4 + 5.
// Uses Sum of AP.
private int sumFromOneToN(int N) {
return (N*(N+1))/2;
}
private double getNextWeight() {
return weights[weightCounter++];
}
}