-
Notifications
You must be signed in to change notification settings - Fork 0
/
PathFinderTester.java
344 lines (297 loc) · 12.9 KB
/
PathFinderTester.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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
import java.io.*;
import java.util.*;
import joptsimple.OptionParser;
import joptsimple.OptionSet;
import map.*;
import pathFinder.*;
/**
* @author Jeffrey Chan, Youhan Xia, Phuc Chu
* RMIT Algorithms & Analysis, 2019 semester 1
* <p>
* Main class for testing the maze generators and solvers.
*/
class PathFinderTester {
/**
* Name of class, used in error messages.
*/
protected static final String progName = "PathFindingTester";
/**
* Standard outstream.
*/
protected static final PrintStream outStream = System.out;
/**
* Print help/usage message.
*/
public static void usage(String progName) {
System.err.println(progName + ": [-v -t: -w: -o:] <parameter fileName>");
System.err.println("options are: ");
System.err.println("-v ");
System.err.println("-t <terrain parameter filename> ");
System.err.println("-w <waypoint parameter filename> ");
System.err.println("-o <path output filename> ");
System.err.println("-v will activate map and path visualisation.");
System.exit(1);
} // end of usage
/**
* Main function of tester.
*
* @param args Two arguments which are input filename and "y/n" indicating whether to visualize the maze.
*/
public static void main(String[] args) {
//
// parse command line options
//
OptionParser parser = new OptionParser("o:vt:w:");
OptionSet options = parser.parse(args);
String outputFilename = null;
boolean isVisu = false;
String terrainFilename = null;
String waypointFilename = null;
// -o <inputFilename> specifies the file that stores the shortest path results (optional)
if (options.has("o")) {
if (options.hasArgument("o")) {
outputFilename = (String) options.valueOf("o");
} else {
System.err.println("Missing filename argument for -o option.");
usage(progName);
}
}
// -v to visualise graph
if (options.has("v")) {
isVisu = true;
}
// -t <terrain filename> specifies the (optional) terrain parameter filename
if (options.has("t")) {
if (options.hasArgument("t")) {
terrainFilename = (String) options.valueOf("t");
} else {
System.err.println("Missing filename argument for -t option.");
usage(progName);
}
}
// -w <terrain filename> specifies the (optional) terrain parameter filename
if (options.has("w")) {
if (options.hasArgument("w")) {
waypointFilename = (String) options.valueOf("w");
} else {
System.err.println("Missing filename argument for -w option.");
usage(progName);
}
}
// non option arguments
List<?> tempArgs = options.nonOptionArguments();
List<String> remainArgs = new ArrayList<String>();
for (Object object : tempArgs) {
remainArgs.add((String) object);
}
// check number of non-option command line arguments
if (remainArgs.size() != 1) {
System.err.println("Incorrect number of arguments.");
usage(progName);
}
// parameter filename
String paraFilename = remainArgs.get(0);
// number of rows and columns in map
int rowNum = 0;
int colNum = 0;
// origin and destination lists
List<Coordinate> originCells = new ArrayList<Coordinate>();
List<Coordinate> destCells = new ArrayList<Coordinate>();
// impassable, terrain and waypoint containters
Set<Coordinate> impassableCells = new HashSet<Coordinate>();
Map<Coordinate, Integer> terrainCells = new HashMap<Coordinate, Integer>();
List<Coordinate> waypointCells = new ArrayList<Coordinate>();
//
// Parse parameter files
//
// First parse main parameter file
try {
BufferedReader reader = new BufferedReader(new FileReader(paraFilename));
String line;
// read in row and column number
if ((line = reader.readLine()) != null) {
String[] tokens = line.trim().split("\\s+");
if (tokens.length != 2) {
System.err.println(
"There should be two numbers representing the number of rows and columns.");
} else {
rowNum = Integer.parseInt(tokens[0]);
colNum = Integer.parseInt(tokens[1]);
// check Input
if (rowNum <= 0 || colNum <= 0) {
throw new IllegalArgumentException("Map dimensions cannot be 0 or less.");
}
}
}
// read in origin coordinates
if ((line = reader.readLine()) != null) {
String[] tokens = line.trim().split("\\s+");
if (tokens.length < 2 || tokens.length % 2 != 0) {
System.err.println(
"Origin coordinates should be in pairs.");
} else {
for (int i = 0; i < tokens.length; i += 2) {
int r = Integer.parseInt(tokens[i]);
int c = Integer.parseInt(tokens[i + 1]);
if (r < 0 || r >= rowNum || c < 0 || c >= colNum) {
throw new IllegalArgumentException(
"Origin coordinates cannot be less than 0 or greater than the number of rows or columns in map.");
}
originCells.add(new Coordinate(r, c));
}
}
}
// read in destination coordinates
if ((line = reader.readLine()) != null) {
String[] tokens = line.trim().split("\\s+");
if (tokens.length < 2 || tokens.length % 2 != 0) {
System.err.println(
"Destination coordinates should be in pairs.");
} else {
for (int i = 0; i < tokens.length; i += 2) {
int r = Integer.parseInt(tokens[i]);
int c = Integer.parseInt(tokens[i + 1]);
if (r < 0 || r >= rowNum || c < 0 || c >= colNum) {
throw new IllegalArgumentException(
"Destination coordinates cannot be less than 0 or greater than the number of rows or columns in map.");
}
destCells.add(new Coordinate(r, c));
}
}
}
// read in impassible coordinates
while ((line = reader.readLine()) != null) {
String[] tokens = line.trim().split("\\s+");
if (tokens.length != 2) {
System.err.println(
"Impassable coordinates should be in pairs");
} else {
impassableCells.add(new Coordinate(Integer.parseInt(tokens[0]), Integer.parseInt(tokens[1])));
}
}
} catch (FileNotFoundException e) {
System.err.println("Parameter file doesn't exist.");
usage(progName);
} catch (IOException e) {
System.err.println("IO error.");
usage(progName);
} catch (IllegalArgumentException e) {
System.err.println(e);
usage(progName);
}
// check if need to parse terrain parameter file
if (terrainFilename != null) {
// parse terrain file
try {
BufferedReader reader = new BufferedReader(new FileReader(terrainFilename));
String line;
while ((line = reader.readLine()) != null) {
String[] tokens = line.trim().split("\\s+");
if (tokens.length != 3) {
System.err.println(
"Terrain should be two coordinates and cost");
} else {
int cost = Integer.parseInt(tokens[2]);
if (cost < 1) {
System.err.println(
"Terrain cost must be 1 or more");
} else {
Coordinate coord = new Coordinate(Integer.parseInt(tokens[0]), Integer.parseInt(tokens[1]));
terrainCells.put(coord, new Integer(cost));
}
}
}
} catch (FileNotFoundException e) {
System.err.println("Parameter file doesn't exist.");
usage(progName);
} catch (IOException e) {
System.err.println("IO error.");
usage(progName);
}
}
// check if need to parse waypoint parameter file
if (waypointFilename != null) {
try {
BufferedReader reader = new BufferedReader(new FileReader(waypointFilename));
String line;
while ((line = reader.readLine()) != null) {
String[] tokens = line.trim().split("\\s+");
if (tokens.length != 2) {
System.err.println(
"Waypoints should be two coordinates");
} else {
waypointCells.add(new Coordinate(Integer.parseInt(tokens[0]), Integer.parseInt(tokens[1])));
}
}
reader.close();
} catch (FileNotFoundException e) {
System.err.println("Parameter file doesn't exist.");
usage(progName);
} catch (IOException e) {
System.err.println("IO error: " + e);
usage(progName);
}
}
//
// Construct map
//
PathMap map = new PathMap();
// load map
map.initMap(rowNum, colNum, originCells, destCells, impassableCells, terrainCells, waypointCells);
map.isVisu = isVisu;
// display it
map.draw();
//
// Find path
//
// setup path finding algorithm
PathFinder pathFinder = new DijkstraPathFinder(map);
outStream.println(pathFinder.getClass().getSimpleName() + " is finding a path.");
// find path
List<Coordinate> path = pathFinder.findPath();
// check if a path has been found
if (path.size() == 0) {
outStream.println("No path found.");
} else {
outStream.println("A path has been found.");
// print out path
Iterator<Coordinate> it = path.iterator();
if (it.hasNext()) {
Coordinate coord = it.next();
outStream.print("(" + coord.getRow() + "," + coord.getColumn() + ")");
}
while (it.hasNext()) {
Coordinate coord = it.next();
outStream.print(" -> (" + coord.getRow() + "," + coord.getColumn() + ")");
}
outStream.println("");
// This is optional, more for your own curiousity (not tested)
outStream.println("Number of coordinates visited = " + pathFinder.coordinatesExplored());
// display the path on screen
map.drawPath(path);
// see if we need to output to file also
if (outputFilename != null) {
try {
PrintWriter writer = new PrintWriter(new FileWriter(outputFilename));
it = path.iterator();
if (it.hasNext()) {
Coordinate coord = it.next();
writer.print("(" + coord.getRow() + "," + coord.getColumn() + ")");
}
while (it.hasNext()) {
Coordinate coord = it.next();
writer.print(" (" + coord.getRow() + "," + coord.getColumn() + ")");
}
writer.println("");
writer.close();
} catch (FileNotFoundException e) {
System.err.println("Parameter file doesn't exist.");
usage(progName);
} catch (IOException e) {
System.err.println("IO Error: " + e);
usage(progName);
}
}
}
} //end of main.
}