-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAssignment
More file actions
290 lines (222 loc) · 6.97 KB
/
Assignment
File metadata and controls
290 lines (222 loc) · 6.97 KB
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
package queens8;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class Queens8 {
public static int numQUEENS = 8;
private int[][] board;
private int[] queenPositions;
public static void main(String[] args) {
boolean climb = true;
int climbCount = 0;
// 5 restarts
while (climb) {
Queens8 board = new Queens8(
new int[numQUEENS][numQUEENS], new int[8]);
// randomly place queens
board.placeQueens();
System.out.println("Trial #: " + (climbCount+1));
System.out.println("Original board:");
board.printBoard();
System.out.println("# pairs of queens attacking each other: "
+ board.h() + "\n");
// score to be compared against
int localMin = board.h();
boolean best = false;
// array to store best queen positions by row (array index is column)
int[] bestQueenPositions = new int[8];
// iterate through each column
for (int j = 0; j < board.board.length; j++) {
System.out.println("Iterating through COLUMN " + j + ":");
best = false;
// iterate through each row
for (int i = 0; i < board.board.length; i++) {
// skip score calculated by original board
if (i != board.queenPositions[j]) {
// move queen
board.moveQueen(i, j);
board.printBoard();
System.out.println();
// calculate score, if best seen then store queen position
if (board.h() < localMin) {
best = true;
localMin = board.h();
bestQueenPositions[j] = i;
}
// reset to original queen position
board.resetQueen(i, j);
}
}
// change 2 back to 1
board.resetBoard(j);
if (best) {
// if a best score was found, place queen in this position
board.placeBestQueen(j, bestQueenPositions[j]);
System.out.println("Best board found this iteration: ");
board.printBoard();
System.out
.println("# pairs of queens attacking each other: "
+ board.h() + "\n");
} else {
System.out.println("No better board found.");
board.printBoard();
System.out
.println("# pairs of queens attacking each other: "
+ board.h() + "\n");
}
}
// if score = 0, hill climbing has solved problem
if (board.h() == 0)
climb = false;
climbCount++;
// only 5 restarts
if (climbCount == 6) {
climb = false;
}
System.out.println("Done in " + (climbCount-1) + " restarts.");
}
}
public Queens8(int[][] board, int[] positions) {
this.board = board;
this.queenPositions = positions;
}
private int[] generateQueens() {
List<Integer> randomPos = new ArrayList<Integer>();
Random r = new Random();
for (int i = 0; i < numQUEENS; i++) {
randomPos.add(r.nextInt(8));
}
int[] randomPositions = new int[numQUEENS];
for (int i = 0; i < randomPos.size(); i++) {
randomPositions[i] = randomPos.get(i);
}
return randomPositions;
}
public void placeQueens() {
queenPositions = generateQueens();
for (int i = 0; i < board.length; i++) {
board[queenPositions[i]][i] = 1;
}
}
public int h() {
int totalPairs = 0;
// checking rows
for (int i = 0; i < board.length; i++) {
ArrayList<Boolean> pairs = new ArrayList<Boolean>();
for (int j = 0; j < board[i].length; j++) {
if (board[i][j] == 1) {
pairs.add(true);
}
}
if (pairs.size() != 0)
totalPairs = totalPairs + (pairs.size() - 1);
}
// check diagonal from top left
int rows = board.length;
int cols = board.length;
int maxSum = rows + cols - 2;
for (int sum = 0; sum <= maxSum; sum++) {
ArrayList<Boolean> pairs = new ArrayList<Boolean>();
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (i + j - sum == 0) {
if (board[i][j] == 1) {
pairs.add(true);
}
}
}
}
if (pairs.size() != 0)
totalPairs = totalPairs + (pairs.size() - 1);
}
// check mirrored diagonal. couldn't figure out algorithm so solved brute force.
int pairs = checkMirrorDiagonal();
return totalPairs + pairs;
}
private int checkMirrorDiagonal() {
int[] b1 = { board[7][0] };
int[] b2 = { board[7][1], board[6][0] };
int[] b3 = { board[7][2], board[6][1], board[5][0] };
int[] b4 = { board[7][3], board[6][2], board[5][1], board[4][0] };
int[] b5 = { board[7][4], board[6][3], board[5][2], board[4][1],
board[3][0] };
int[] b6 = { board[7][5], board[6][4], board[5][3], board[4][2],
board[3][1], board[2][0] };
int[] b7 = { board[7][6], board[6][5], board[5][4], board[4][3],
board[3][2], board[2][1], board[1][0] };
int[] b8 = { board[7][7], board[6][6], board[5][5], board[4][4],
board[3][3], board[2][2], board[1][1], board[0][0] };
int[] b9 = { board[6][7], board[5][6], board[4][5], board[3][4],
board[2][3], board[1][2], board[0][1] };
int[] b10 = { board[5][7], board[4][6], board[3][5], board[2][4],
board[1][3], board[0][2] };
int[] b11 = { board[4][7], board[3][6], board[2][5], board[1][4],
board[0][3] };
int[] b12 = { board[3][7], board[2][6], board[1][5], board[0][4] };
int[] b13 = { board[2][7], board[1][6], board[0][5] };
int[] b14 = { board[1][7], board[0][6] };
int[] b15 = { board[0][7] };
int totalPairs = 0;
totalPairs += checkMirrorHorizontal(b1);
totalPairs += checkMirrorHorizontal(b2);
totalPairs += checkMirrorHorizontal(b3);
totalPairs += checkMirrorHorizontal(b4);
totalPairs += checkMirrorHorizontal(b5);
totalPairs += checkMirrorHorizontal(b6);
totalPairs += checkMirrorHorizontal(b7);
totalPairs += checkMirrorHorizontal(b8);
totalPairs += checkMirrorHorizontal(b9);
totalPairs += checkMirrorHorizontal(b10);
totalPairs += checkMirrorHorizontal(b11);
totalPairs += checkMirrorHorizontal(b12);
totalPairs += checkMirrorHorizontal(b13);
totalPairs += checkMirrorHorizontal(b14);
totalPairs += checkMirrorHorizontal(b15);
return totalPairs;
}
public void moveQueen(int row, int col) {
// original queen will become a 2 and act as a marker
board[queenPositions[col]][col] = 2;
board[row][col] = 1;
}
private int checkMirrorHorizontal(int[] b) {
int totalPairs = 0;
ArrayList<Boolean> pairs = new ArrayList<Boolean>();
for (int i = 0; i < b.length; i++) {
if (b[i] == 1)
pairs.add(true);
}
if (pairs.size() != 0)
totalPairs = (pairs.size() - 1);
return totalPairs;
}
public void resetQueen(int row, int col) {
if (board[row][col] == 1)
board[row][col] = 0;
}
public void resetBoard(int col) {
for (int i = 0; i < board.length; i++) {
if (board[i][col] == 2)
board[i][col] = 1;
}
}
public void placeBestQueen(int col, int queenPos) {
for (int i = 0; i < board.length; i++) {
if (board[i][col] == 1)
board[i][col] = 2;
}
board[queenPos][col] = 1;
for (int i = 0; i < board.length; i++) {
if (board[i][col] == 2)
board[i][col] = 0;
}
}
public void printBoard() {
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board[i].length; j++) {
System.out.print(board[i][j] + " ");
}
System.out.println();
}
}
}