-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblemMap.java
More file actions
100 lines (80 loc) · 3.68 KB
/
ProblemMap.java
File metadata and controls
100 lines (80 loc) · 3.68 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
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
public class ProblemMap extends Problem {
Map<String, Map<String, Double>> map;
Map<String, Double> sld;
public Object goalState;
public ProblemMap(String mapfilename) throws Exception {
map = new HashMap<String, Map<String, Double>>();
//read map from file of source-destination-cost triples (tab separated)
BufferedReader reader = new BufferedReader( new FileReader (mapfilename));
String line;
while( ( line = reader.readLine() ) != null ) {
String[] strA = line.split("\t");
String from_city = strA[0],
to_city = strA[1];
Double cost = Double.parseDouble(strA[2]);
if(!map.containsKey(from_city))
map.put(from_city, new HashMap<String, Double>());
map.get(from_city).put(to_city,cost);
//putting the reverse edge as well
if(!map.containsKey(to_city))
map.put(to_city, new HashMap<String, Double>());
map.get(to_city).put(from_city,cost);
}
reader.close();
}
public ProblemMap(String mapfilename, String heuristicfilename) throws Exception {
this(mapfilename);
sld = new HashMap<String, Double>();
BufferedReader reader = new BufferedReader( new FileReader (heuristicfilename));
String line;
while( ( line = reader.readLine() ) != null ) {
String[] strA = line.split("\t");
String city = strA[0];
double h = Double.parseDouble(strA[1]);
sld.put(city, h);
}
reader.close();
}
boolean goal_test(Object state) {
return state.equals(goalState);
}
Set<Object> getSuccessors(Object state) {
Set<Object> result = new HashSet<Object>();
for(Object successor_state : map.get(state).keySet())
result.add(successor_state);
return result;
}
double step_cost(Object fromState, Object toState) {
return map.get(fromState).get(toState);
}
public double h(Object state) {
return sld.get(state);
}
public static void main(String[] args) throws Exception {
ProblemMap problem = new ProblemMap("romania.txt","romaniaSLD.txt");
problem.initialState = "Timisoara";
problem.goalState = "Bucharest";
Search search = new Search(problem);
System.out.println("TreeSearch------------------------");
System.out.println("BreadthFirstTreeSearch:\t\t" + search.BreadthFirstTreeSearch());
System.out.println("UniformCostTreeSearch:\t\t" + search.UniformCostTreeSearch());
System.out.println("DepthFirstTreeSearch:\t\t" + search.DepthFirstTreeSearch());
System.out.println("GreedyBestFirstTreeSearch:\t" + search.GreedyBestFirstTreeSearch());
System.out.println("AstarTreeSearch:\t\t" + search.AstarTreeSearch());
System.out.println("\n\nGraphSearch----------------------");
System.out.println("BreadthFirstGraphSearch:\t" + search.BreadthFirstGraphSearch());
System.out.println("UniformCostGraphSearch:\t\t" + search.UniformCostGraphSearch());
System.out.println("DepthFirstGraphSearch:\t\t" + search.DepthFirstGraphSearch());
System.out.println("GreedyBestGraphSearch:\t\t" + search.GreedyBestFirstGraphSearch());
System.out.println("AstarGraphSearch:\t\t" + search.AstarGraphSearch());
System.out.println("\n\nIterativeDeepening----------------------");
System.out.println("IterativeDeepeningTreeSearch:\t" + search.IterativeDeepeningTreeSearch());
System.out.println("IterativeDeepeningGraphSearch:\t" + search.IterativeDeepeningGraphSearch());
}
}