-
Notifications
You must be signed in to change notification settings - Fork 0
/
MapSolver.java
99 lines (89 loc) · 3.92 KB
/
MapSolver.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
//Dylan Wulf
//Artificial Intelligence Project 1
//Feb 12, 2017
import java.io.FileReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.LinkedList;
public class MapSolver {
public static void main(String[] args) {
//Check for enough arguments
if (args.length < 2) {
System.err.println("Not enough arguments.");
System.err.println("Usage: java MapSolver path/to/file.ext search-strategy");
System.err.println("Exiting.");
System.exit(1);
}
//Read in the file and save it as a string
//Check for thrown exceptions such as file not found and ioexception
String mapStr = "";
try {
mapStr = readMap(args[0]);
}
catch (FileNotFoundException e) {
System.err.println("Could not find file " + args[0] + ". Exiting.");
System.exit(1);
}
catch (IOException e) {
System.err.print("Something went wrong when reading the file. ");
System.err.println("Please make sure you have permission to read the file.");
System.err.println("Exiting.");
System.exit(1);
}
//Get search strategy object using argument and map
SearchStrategy strategy = getSearchStrategy(args[1], mapStr);
if (strategy == null) {
System.err.println("Unknown search strategy.");
System.err.println("Possible strategies: BFS, DFS, A*-straight-line, A*-taxi-distance");
System.err.println("Exiting.");
System.exit(1);
}
//Search for a solution and record time taken
long startTime = 0;
long endTime = 0;
startTime = System.nanoTime();
//Initiate the actual search
MapProblemNode goalNode = (MapProblemNode) strategy.search();
endTime = System.nanoTime();
long timeTaken = endTime - startTime;
if (goalNode == null) {
System.out.println("No solution found.");
}
else {
int startX = MapProblemNode.getStartLocation()[0];
int startY = MapProblemNode.getStartLocation()[1];
System.out.println("Solution:");
for (String step : goalNode.getPath())
System.out.println(step);
System.out.println("Path cost: " + goalNode.getPathCost());
}
System.out.println("Time taken: " + timeTaken + " nanoseconds");
System.out.println("Nodes created: " + MapProblemNode.getCreatedNodes());
}
//Takes in the search strategy argument and the string representation of the map
//and creates a search strategy object. If the command line argument does not match
//any known strategy, returns null.
private static SearchStrategy getSearchStrategy(String strategyName, String mapStr) {
if (strategyName.equalsIgnoreCase("BFS"))
return new BFSearch(new MapProblemNode(mapStr, null));
else if (strategyName.equalsIgnoreCase("DFS"))
return new DFSearch(new MapProblemNode(mapStr, null));
else if (strategyName.equalsIgnoreCase("A*-straight-line"))
return new AStarSearch(new MapProblemNode(mapStr, new MapHeuristicStraightLine()));
else if (strategyName.equalsIgnoreCase("A*-taxi-distance"))
return new AStarSearch(new MapProblemNode(mapStr, new MapHeuristicTaxiDistance()));
else
return null;
}
//Reads the file specified in the command line arguments into a String.
private static String readMap(String filePath) throws IOException, FileNotFoundException{
File mapFile = new File(filePath);
long fileSize = mapFile.length(); //get file size
FileReader fr = new FileReader(mapFile);
char[] map = new char[(int)fileSize]; //make array that's the same size as the file
fr.read(map);
fr.close();
return new String(map);
}
}