-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAstar.java
168 lines (161 loc) · 4.85 KB
/
Astar.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
import java.util.*;
public class Astar {
private Hashtable<String, Integer> misplacedExistSet;
private Hashtable<String, Integer> manhattanExistSet;
private MisplacedHeuristic misplacedAlg;
private ManhattanHeuristic manhattanAlg;
private PriorityQueue<Board> misplacedFrontier;
private PriorityQueue<Board> manhattanFrontier;
private String goal = "012345678";
private int nodesExplored;
private Board goalState = null;
public Board getGoal() {
return goalState;
}
public int astarMisplaced(Board board) {
misplacedExistSet = new Hashtable<String, Integer>();
misplacedAlg = new MisplacedHeuristic();
misplacedFrontier = new PriorityQueue<Board>(20, misplacedAlg);
nodesExplored = 1;
misplacedExistSet.put(board.getAll(), 1);
board.putCost(0);
misplacedFrontier.add(board);
while ((misplacedFrontier.size() > 0) && (!misplacedFrontier.peek().getAll().equals(goal))) {
processMisplacedChildren(misplacedFrontier.poll());
}
goalState = misplacedFrontier.poll();
System.out.println("End Cost: " + goalState.getCost());
return nodesExplored;
}
public int astarManhattan(Board board) {
manhattanExistSet = new Hashtable<String, Integer>();
manhattanAlg = new ManhattanHeuristic();
manhattanFrontier = new PriorityQueue<Board>(20, manhattanAlg);
nodesExplored = 1;
manhattanExistSet.put(board.getAll(), 1);
board.putCost(0);
manhattanFrontier.add(board);
while ((manhattanFrontier.size() > 0) && (!manhattanFrontier.peek().getAll().equals(goal))) {
processManhattanChildren(manhattanFrontier.poll());
}
goalState = manhattanFrontier.poll();
System.out.println("End Cost: " + goalState.getCost());
return nodesExplored;
}
private void processMisplacedChildren(Board board) {
int cost = board.getCost();
Board testLeft = testMove(board, "left");
Board testRight = testMove(board, "right");
Board testUp = testMove(board, "up");
Board testDown = testMove(board, "down");
if (testLeft != null) {
if (misplacedExistSet.get(testLeft.getAll()) == null) {
misplacedExistSet.put(testLeft.getAll(), 1);
testLeft.putCost(cost + 1);
testLeft.setParent(board);
misplacedFrontier.add(testLeft);
nodesExplored++;
}
}
if (testRight != null) {
if (misplacedExistSet.get(testRight.getAll()) == null) {
misplacedExistSet.put(testRight.getAll(), 1);
testRight.putCost(cost + 1);
testRight.setParent(board);
misplacedFrontier.add(testRight);
nodesExplored++;
}
}
if (testUp != null) {
if (misplacedExistSet.get(testUp.getAll()) == null) {
misplacedExistSet.put(testUp.getAll(), 1);
testUp.putCost(cost + 1);
testUp.setParent(board);
misplacedFrontier.add(testUp);
nodesExplored++;
}
}
if (testDown != null) {
if (misplacedExistSet.get(testDown.getAll()) == null) {
misplacedExistSet.put(testDown.getAll(), 1);
testDown.putCost(cost + 1);
testDown.setParent(board);
misplacedFrontier.add(testDown);
nodesExplored++;
}
}
}
private void processManhattanChildren(Board board) {
int cost = board.getCost();
Board testLeft = testMove(board, "left");
Board testRight = testMove(board, "right");
Board testUp = testMove(board, "up");
Board testDown = testMove(board, "down");
if (testLeft != null) {
if (manhattanExistSet.get(testLeft.getAll()) == null) {
manhattanExistSet.put(testLeft.getAll(), 1);
testLeft.putCost(cost + 1);
testLeft.setParent(board);
manhattanFrontier.add(testLeft);
nodesExplored++;
}
}
if (testRight != null) {
if (manhattanExistSet.get(testRight.getAll()) == null) {
manhattanExistSet.put(testRight.getAll(), 1);
testRight.putCost(cost + 1);
testRight.setParent(board);
manhattanFrontier.add(testRight);
nodesExplored++;
}
}
if (testUp != null) {
if (manhattanExistSet.get(testUp.getAll()) == null) {
manhattanExistSet.put(testUp.getAll(), 1);
testUp.putCost(cost + 1);
testUp.setParent(board);
manhattanFrontier.add(testUp);
nodesExplored++;
}
}
if (testDown != null) {
if (manhattanExistSet.get(testDown.getAll()) == null) {
manhattanExistSet.put(testDown.getAll(), 1);
testDown.putCost(cost + 1);
testDown.setParent(board);
manhattanFrontier.add(testDown);
nodesExplored++;
}
}
}
private Board testMove(Board board, String direction) {
Board tempBoard = new Board(board.getAll());
if (direction.equals("left")) {
if (tempBoard.shiftLeft()) {
return tempBoard;
} else {
return null;
}
} else if (direction.equals("right")) {
if (tempBoard.shiftRight()) {
return tempBoard;
} else {
return null;
}
} else if (direction.equals("up")) {
if (tempBoard.shiftUp()) {
return tempBoard;
} else {
return null;
}
} else if (direction.equals("down")) {
if (tempBoard.shiftDown()) {
return tempBoard;
} else {
return null;
}
} else {
return null;
}
}
}