-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathAStarSolver.java
More file actions
238 lines (205 loc) · 6.46 KB
/
Copy pathAStarSolver.java
File metadata and controls
238 lines (205 loc) · 6.46 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
import java.util.AbstractCollection;
import java.util.Comparator;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.PriorityQueue;
public class AStarSolver extends Solver
{
/*
* Constructor
* m: The maze to solve
*/
public AStarSolver(Maze m, Boolean manhattan)
{
this.maze = m;
this.result = "";
this.manhattan = manhattan;
this.frontier = new PriorityQueue<Node<Maze>>(new Comparator<Node<Maze>>()
{
public int compare(Node<Maze> s1, Node<Maze> s2)
{
Double sf1 = s1.getContent().getCurrState().getF();
Double sf2 = s2.getContent().getCurrState().getF();
Double sh1 = s1.getContent().getCurrState().getH();
Double sh2 = s2.getContent().getCurrState().getH();
if(sf1 > sf2)
return 1;
else if(sf1 == sf2)
{
if(sh1 > sh2)
return 1;
else if(sh1 == sh2)
return 0;
else
return -1;
}
else
return -1;
}
});
this.closedSquares = new PriorityQueue<Square>(new Comparator<Square>()
{
public int compare(Square s1, Square s2)
{
Double sf1 = s1.getF();
Double sf2 = s2.getF();
Double sh1 = s1.getH();
Double sh2 = s2.getH();
if(sf1 > sf2)
return 1;
else if(sf1 == sf2)
{
if(sh1 > sh2)
return 1;
else if(sh1 == sh2)
return 0;
else
return -1;
}
else
return -1;
}
});
}
public String solve()
{
this.maze.initMaze(); //Re-init maze
Boolean endfound = false;
this.nodesCounter = 0;
this.pathLength = 0;
//Compute F value of Starting square
if(manhattan)
this.maze.getStart().calcManhattanH();
else
this.maze.getStart().calcEuclidH();
this.maze.getStart().calcF();
//Init data structures
this.frontier.clear(); //Clear frontier Queue
((PriorityQueue<Node<Maze>>) this.frontier).offer(new Node<Maze>(this.maze)); //Adding the first node (Start node) (G is at 0, Start to Start = 0)
this.closedSquares.clear(); //Clear closedSquares
//Measure run time
long startTime = System.currentTimeMillis();
while(!endfound)
{
if(this.frontier.isEmpty())
break;
else
{
Node<Maze> current = ((PriorityQueue<Node<Maze>>) this.frontier).remove();
this.maze = (Maze) current.getContent();
Square currState = this.maze.getCurrState();
if(currState.getCol() == this.maze.getEnd().getCol() && currState.getLine() == this.maze.getEnd().getLine())
{
Node<Maze> temp = new Node<Maze>(this.maze);
temp.setFather(current);
((PriorityQueue<Node<Maze>>) this.frontier).offer(temp);
endfound = true;
}
else
{
LinkedList<Node<Maze>> nexts = this.getNextSquares();
if(!this.closedSquares.contains(currState))
{
this.closedSquares.add(currState);
currState.setAttribute("*");
}
Iterator<Node<Maze>> x = nexts.iterator();
while(x.hasNext())
{
Node<Maze> neighbor = x.next();
if(this.closedSquares.contains(neighbor.getContent().getCurrState()))
continue;
else
{
if(!this.frontier.contains(neighbor))
{
neighbor.setFather(current);
((PriorityQueue<Node<Maze>>) this.frontier).offer(neighbor);
this.nodesCounter++;
}
}
}
}
}
}
long endTime = System.currentTimeMillis();
long time = endTime - startTime;
if(this.manhattan)
this.result = " ___ __ ___ __ __ __ \r\n" +
" / | __/|_ / |/ /___ _____ / /_ ____ _/ /_/ /_____ _____ \r\n" +
" / /| || / ______ / /|_/ / __ `/ __ \\/ __ \\/ __ `/ __/ __/ __ `/ __ \\\r\n" +
" / ___ /_ __| /_____/ / / / / /_/ / / / / / / / /_/ / /_/ /_/ /_/ / / / /\r\n" +
"/_/ |_||/ /_/ /_/\\__,_/_/ /_/_/ /_/\\__,_/\\__/\\__/\\__,_/_/ /_/ \n";
else
this.result = " ___ ______ ___ __\r\n" +
" / | __/|_ / ____/_ _______/ (_)___/ /\r\n" +
" / /| || / ______ / __/ / / / / ___/ / / __ / \r\n" +
" / ___ /_ __| /_____/ / /___/ /_/ / /__/ / / /_/ / \r\n" +
"/_/ |_||/ /_____/\\__,_/\\___/_/_/\\__,_/ \n";
if(endfound)
{
this.maze.resetGrid();
Node<Maze> revertedTree = ((PriorityQueue<Node<Maze>>) this.frontier).remove();
revertedTree = revertedTree.getFather();
this.result += "Path: " + this.maze.getEnd().toString() + "(End) <- ";
this.pathLength++;
while(revertedTree.hasFather())
{
Maze temp = revertedTree.getContent();
Square state = temp.getCurrState();
if(!state.equals(this.maze.getEnd()))
{
this.result += state.toString() + " <- ";
this.maze.getGrid()[state.getLine()][state.getCol()].setAttribute("*");
this.pathLength++;
}
revertedTree = revertedTree.getFather();
}
this.result += this.maze.getStart().toString() + "(Start) \n" + "Path length: " + this.pathLength + "\nNumber of nodes created: " + this.nodesCounter + "\nExecution time: " + time/1000d + " seconds\n";
this.result += this.maze.printMaze();
}
else
{
this.result += "Failed : Unable to go further and/or end is unreachable.";
}
return this.result;
}
/*
* Get the next ("walkables") squares from the given square
* c: Square from where to get the nexts squares
*/
public LinkedList<Node<Maze>> getNextSquares()
{
LinkedList<Node<Maze>> res = new LinkedList<Node<Maze>>();
//Get 4 next squares
LinkedList<Maze> nexts = this.maze.getCurrState().getNexts();
int gCurrent = this.maze.getCurrState().getG();
for(int i = 0; i < nexts.size(); i++)
{
Square tempSq = nexts.get(i).getCurrState();
if(!this.closedSquares.contains(tempSq))
{
if(this.manhattan)
nexts.get(i).getCurrState().calcManhattanH();
else
nexts.get(i).getCurrState().calcEuclidH();
nexts.get(i).getCurrState().incG(gCurrent);
nexts.get(i).getCurrState().calcF();
Node<Maze> tempNode = new Node<Maze>(nexts.get(i));
res.add(tempNode);
}
}
return res;
}
public String getResult()
{
if(this.result == "")
return "No resolution computed, use the solve method first";
else
return this.result;
}
public AbstractCollection<Node<Maze>> getFrontier()
{
return this.frontier;
}
}