-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBoard.java
More file actions
383 lines (343 loc) · 16.1 KB
/
Board.java
File metadata and controls
383 lines (343 loc) · 16.1 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
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
package com.company;
import java.util.*;
public class Board {
public State state; // the current state of the Board instance
public int maxNodes = 15000; // the maximum amount of nodes that can be checked in a search (set high by default)
public int h = 1; // int indicating the heuristic function used by searches
public int numberExpanded = 0;
public int numberMoves = 0;
boolean failed = false;
// constructs a Board instance
Board(State s) {
setState(s);
}
// solves the puzzle using local beam search
public Node solveBeam(int k){
failed = false;
setHeuristicFunction(1);
int numExpanded = 0;
Node node = new Node(getState(), null, null, h);
PriorityQueue<Node> frontier = new PriorityQueue<>(11, new NodeComparator());
frontier.add(node);
HashSet<Node> nodesToExpand = new HashSet<>();
HashMap<State, Node> reached = new HashMap<>();
reached.put(node.getState(), node);
// check if starting node is solution
if(node.getHeuristicVal() == 0) {
traceNodePath(node,true);
return node;
}
else{
for (Node child : expand(node)) {
frontier.add(child);
}
}
while(!frontier.isEmpty()){
// poll the first k elements (best h-value) from the frontier and add them to expand list
for(int i = 0; i<k; i++){
Node x = frontier.poll();
if (x == null) {
break;
}
nodesToExpand.add(x);
}
// clear frontier to forget all but best k nodes
frontier.clear();
// check if any of the k-best nodes polled from the frontier are the goal state and expand them otherwise
for(Node n: nodesToExpand){
numExpanded++;
// check if solution has been found
if(n.getHeuristicVal()==0){
traceNodePath(n,true);
System.out.println("Number of nodes expanded: "+numExpanded+"\nk value = "+k);
return n;
}
else {
reached.put(n.getState(), n);
frontier.addAll(expand(n));
}
// check if node limit has been found and return
if (reached.size() >= maxNodes) {
traceNodePath(n, false);
System.out.println("\nThe maximum number of nodes allowed in the search has been exceeded. Current Node has been returned.");
System.out.println("Number of nodes expanded: "+numExpanded+"\nk value = "+k);
failed= true;
return n;
}
}
nodesToExpand.clear();
}
// if the beam search reaches end of tree or hits the max nodes limit without finding the goal, print msg and return the last node
traceNodePath(node, false);
System.out.println("Number of nodes expanded: "+numExpanded+"\nk value = "+k);
System.out.println("\nNo Solution could be found and the most recent node has been returned.");
failed= true;
return node;
}
// overloaded solveA_star using the current heuristic in this board instance
public Node solveA_star(){
return solveA_star(this.h);
}
// solves the puzzle using A* search
public Node solveA_star(int heuristic){
int numExpanded = 0;
// set the current heuristic to the specified value if the specified value is legal
if(heuristic<1 || heuristic>2) throw new IllegalArgumentException();
else setHeuristicFunction(heuristic);
// initialize frontier and reached with the starting node
Node node = new Node(getState(), null, null, heuristic);
PriorityQueue<Node> frontier = new PriorityQueue<>(11, new NodeComparator());
frontier.add(node);
HashMap<State, Integer> reached = new HashMap<>();
reached.put(getState(), node.getPathCost());
// while the frontier has elements
while(!frontier.isEmpty()) {
// get minimum node in frontier
node = frontier.poll();
//System.out.println(node.getGValue());
// expand each child node one by one
for (Node child:expand(node)) {
numExpanded++;
State s = child.state;
// if h(n) is 0, then we are at a goal state
if (child.getHeuristicVal()==0) {
traceNodePath(child, true);
System.out.println("Number of nodes expanded: "+numExpanded+"\nHeuristic used: h"+h);
this.numberExpanded = numExpanded;
return child;
}
// check if maxNodes has been hit
if (reached.size() >= maxNodes) {
traceNodePath(node, false);
System.out.println("\nThe maximum number of nodes allowed in the search has been exceeded and the most recent node has been returned.");
System.out.println("Number of nodes expanded: "+numExpanded+"\nHeuristic used: h"+h);
this.numberExpanded = numExpanded;
return child;
}
// if s hasn't been reached or its path cost is minimal, add it to reached and frontier
if(!reached.containsKey(s) || child.getPathCost() < reached.get(s)) {
reached.put(s, child.getPathCost());
frontier.add(child);
}
}
}
System.out.println("\nNo Solution could be found and the most recent node has been returned.");
return node;
}
// traces the input nodes path (used to return solution path)
public void traceNodePath(Node n, boolean solved){
System.out.print("NOW TRACING NODE PATH:\n");
int numMoves = 0;
StringBuilder sb = new StringBuilder();
if(solved)sb.append("\nSOLUTION:\n"+n.getState().toString());
else{
sb.append("\nFINAL STATE:\n"+n.getState().toString());
return;
}
while(n.parent!=null) {
numMoves++;
sb.append("\n" + n.action);
n = n.parent;
sb.append("\n" + n.getState().toString());
}
this.numberMoves = numMoves;
sb.append("STARTING STATE SHOWN ABOVE. MOVES MADE ARE SHOWN IN BETWEEN STATES.\n");
System.out.println(sb.toString() + "Done in " + numMoves + " moves.");
}
// expands a node using all of its legal moves
public ArrayList<Node> expand(Node n){
State s = new State(n.state);
ArrayList<Node> children = new ArrayList<>();
ArrayList<Direction> actions = getLegalMoves(s);
// for each move(or Direction) in the legal moves, add a node with that action to the list
for (Direction d: actions) {
State sNext = checkMove(s, d);
Node child = new Node(sNext, n, d, h);
children.add(child);
}
return children;
}
// randomizes the state by starting at the goal state and making n random moves
public void randomizeState(int n){
state = new State("b12 345 678");
Random rand = new Random();
rand.setSeed(777);
Direction direction;
for(int i = 0; i < n; i++) {
ArrayList<Direction> legalMoves = getLegalMoves(getState());
direction = legalMoves.get(rand.nextInt(legalMoves.size()));
move(getState(), direction);
// printState();
}
}
// locates the blank tile and moves it in the input direction
public State move(State s, Direction d){
State result = s;
int index = findBlankIndex(result);
// store tile at the slot b is moving and swap them
int tileHolder = result.getTiles()[index + d.getTransitionIndex()];
result.getTiles()[index] = tileHolder;
result.getTiles()[index + d.getTransitionIndex()] = 0;
return result;
}
// locates the blank tile and returns the state that would result(w/o actually moving it)
public State checkMove(State s, Direction d){
State result = new State(s);
int index = findBlankIndex(result);
// store tile at the slot b is moving and swap them
int tileHolder = result.getTiles()[index + d.getTransitionIndex()];
result.getTiles()[index] = tileHolder;
result.getTiles()[index + d.getTransitionIndex()] = 0;
return result;
}
// checks legal moves and returns a list of legal moves
public ArrayList<Direction> getLegalMoves(State s){
ArrayList<Direction> legalMoves = new ArrayList<>();
int b = findBlankIndex(s);
for (Direction d: Direction.values()) {
int nextPosition = b + d.getTransitionIndex();
if (d == Direction.UP && nextPosition >= 0){
legalMoves.add(Direction.UP);
}
else if (d == Direction.DOWN && nextPosition <= 8){
legalMoves.add(Direction.DOWN);
}
else if (d == Direction.RIGHT && b != 2 && b!= 5 && b!=8){
legalMoves.add(Direction.RIGHT);
}
else if (d == Direction.LEFT && b!=0 && b!=3 && b!=6){
legalMoves.add(Direction.LEFT);
}
}
return legalMoves;
}
// finds index of b (the blank tile)
public int findBlankIndex(State s){
int index = 0;
for (int i : s.getTiles()) {
if (i == 0)
return index;
else index++;
}
return -1;
}
// sets the maximum number of nodes to be considered in a search
public void maxNodes(int n){
this.maxNodes = n;
}
// sets the board's state
public void setState(State s){
this.state = s;
}
// returns the current state of the board
public State getState(){
return this.state;
}
// sets the board's current heuristic function( h1(n)=1 and h2(n)=2 )
public void setHeuristicFunction(int h){
this.h = h;
}
// prints the current state of the board
public void printState(){
if(h==1) System.out.println("h1(n) = "+ getState().getHeuristicValue());
else if(h==2) System.out.print("h2(n) = "+getState().getHeuristicValue() + "\n");
System.out.print(getState().toString());
}
// collects data on the search methods and returns it
public void testSearchMethods(){
maxNodes(1000000);
IntSummaryStatistics statistics1 = new IntSummaryStatistics();
IntSummaryStatistics statistics2 = new IntSummaryStatistics();
IntSummaryStatistics statistics3 = new IntSummaryStatistics();
IntSummaryStatistics statistics4 = new IntSummaryStatistics();
int cntFailed = 0;
for(int i = 0; i < 100; i++){
randomizeState(i+1);
solveA_star(1);
if(numberExpanded >= maxNodes || numberExpanded > maxNodes - 4) cntFailed++;
else if(numberMoves <= 5) statistics1.accept(numberExpanded);
else if(numberMoves >5 && numberMoves <= 10) statistics2.accept(numberExpanded);
else if (numberMoves >10 && numberMoves <= 15) statistics3.accept(numberExpanded);
else statistics4.accept(numberExpanded);
}
System.out.println("\nA* with h1 failed " + cntFailed + " times out of 100 tries, n from 1-100");
System.out.println("Average nodes expanded for numMoves <= 5: "+statistics1.getAverage());
System.out.println("Average nodes expanded for 5 < numMoves <= 10: "+statistics2.getAverage());
System.out.println("Average nodes expanded for 10 < numMoves <= 15: "+statistics3.getAverage());
System.out.println("Average nodes expanded for 15 < numMoves: "+statistics4.getAverage());
IntSummaryStatistics statistics5 = new IntSummaryStatistics();
IntSummaryStatistics statistics6 = new IntSummaryStatistics();
IntSummaryStatistics statistics7 = new IntSummaryStatistics();
IntSummaryStatistics statistics8 = new IntSummaryStatistics();
cntFailed = 0;
for(int i = 0; i < 100; i++){
randomizeState(i+1);
solveA_star(2);
if(numberExpanded >= maxNodes || numberExpanded > maxNodes - 4) cntFailed++;
else if(numberMoves <= 5) statistics5.accept(numberExpanded);
else if(numberMoves >5 && numberMoves <= 10) statistics6.accept(numberExpanded);
else if (numberMoves >10 && numberMoves <= 15) statistics7.accept(numberExpanded);
else statistics8.accept(numberExpanded);
}
System.out.println("\nA* with h2 failed " + cntFailed + " times out of 100 tries, n from 1-100");
System.out.println("Average nodes expanded for numMoves <= 5: "+statistics5.getAverage());
System.out.println("Average nodes expanded for 5 < numMoves <= 10: "+statistics6.getAverage());
System.out.println("Average nodes expanded for 10 < numMoves <= 15: "+statistics7.getAverage());
System.out.println("Average nodes expanded for 15 < numMoves: "+statistics8.getAverage());
IntSummaryStatistics statistics9 = new IntSummaryStatistics();
IntSummaryStatistics statistics10 = new IntSummaryStatistics();
IntSummaryStatistics statistics11 = new IntSummaryStatistics();
IntSummaryStatistics statistics12 = new IntSummaryStatistics();
cntFailed = 0;
for(int i = 0; i < 100; i++){
randomizeState(i+1);
solveBeam(10);
if(numberExpanded >= maxNodes || failed) cntFailed++;
else if(numberMoves <= 5) statistics9.accept(numberExpanded);
else if(numberMoves >5 && numberMoves <= 10) statistics10.accept(numberExpanded);
else if (numberMoves >10 && numberMoves <= 15) statistics11.accept(numberExpanded);
else statistics12.accept(numberExpanded);
}
System.out.println("\nBeam Search with k=10 failed " + cntFailed + " times out of 100 tries, n from 1-100");
System.out.println("Average nodes expanded for numMoves <= 5: "+statistics9.getAverage());
System.out.println("Average nodes expanded for 5 < numMoves <= 10: "+statistics10.getAverage());
System.out.println("Average nodes expanded for 10 < numMoves <= 15: "+statistics11.getAverage());
System.out.println("Average nodes expanded for 15 < numMoves: "+statistics12.getAverage());
}
// tests NodeComparator (debugging purposes)
public void testComparator(){
PriorityQueue<Node> frontier = new PriorityQueue<>(11, new NodeComparator());
Node node = new Node(new State("1b2 345 678"), null, null, h);
frontier.add(node);
frontier.addAll(expand(node));
System.out.println(frontier.poll().getPathCost());
System.out.println(frontier.poll().getPathCost());
System.out.println(frontier.poll().getPathCost());
System.out.println(frontier.poll().getPathCost());
}
// comparator for the priority queue in A* search
private class NodeComparator implements Comparator<Node>{
// overrides compare() method of Comparator for minimal h-value
public int compare(Node s1, Node s2) {
if (s1.getPathCost() < s2.getPathCost())
return -1;
else if (s1.getPathCost() > s2.getPathCost())
return 1;
return 0;
}
}
// simple enum for cardinal directions
public enum Direction{
UP(-3),
DOWN(+3),
LEFT(-1),
RIGHT(1);
private int transitionIndex;
Direction(int i){
this.transitionIndex = i;
}
private int getTransitionIndex(){
return transitionIndex;
}
}
}