-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSearch.java
More file actions
125 lines (108 loc) · 4.57 KB
/
Copy pathSearch.java
File metadata and controls
125 lines (108 loc) · 4.57 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
import org.jgrapht.Graph;
import org.jgrapht.GraphPath;
import org.jgrapht.alg.interfaces.AStarAdmissibleHeuristic;
import org.jgrapht.alg.interfaces.ShortestPathAlgorithm;
import org.jgrapht.alg.shortestpath.ALTAdmissibleHeuristic;
import org.jgrapht.alg.shortestpath.AStarShortestPath;
import org.jgrapht.alg.shortestpath.DijkstraShortestPath;
import org.jgrapht.graph.DefaultUndirectedWeightedGraph;
import org.jgrapht.graph.DefaultWeightedEdge;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.*;
public class Search {
static int test = 0;
public static void main(String[] args) throws Exception {
DefaultUndirectedWeightedGraph<String, DefaultWeightedEdge> g;
g = InitializeGraph();
/*
// 0 to 5 D is faster:
// 0,2 0,3 0,4
for (int i = 0; i < 100; i++) {
long start = System.nanoTime();
test = i;
AStar("0", "10", g);
long end = System.nanoTime();
long time = (end - start);// / 1000000;
System.out.println("A: " + time);
}
start = System.nanoTime();
Dijkstra("0", "10", g);
end = System.nanoTime();
time = (end - start);// / 1000000;
System.out.println("D: " + time);
*/
long[][] DTimes = new long[100][100];
long start = System.nanoTime();
for (int i = 0; i < 100; i++)
for (int j = i+1; j < 100; j++) {
Dijkstra(Integer.toString(i), Integer.toString(j), g);
//DTimes[i][j] = time;
}
long end = System.nanoTime();
long time = (end - start) / 1000000;
System.out.println("Dijkstra time: " + time + "ms");
start = System.nanoTime();
HashSet<String> set = new HashSet<String>();
set.add("34");
set.add("25");
set.add("54");
set.add("60");
AStarShortestPath path;
ALTAdmissibleHeuristic Hero = new ALTAdmissibleHeuristic(g, set);
path = new AStarShortestPath(g, Hero);
for (int i = 0; i < 100; i++)
for (int j = i+1; j < 100; j++)
AStar(Integer.toString(i), Integer.toString(j), g, Hero);
end = System.nanoTime();
time = (end - start) / 1000000;
System.out.println("A* time: " + time + "ms");
}
public static void Dijkstra(String v1, String v2, DefaultUndirectedWeightedGraph<String, DefaultWeightedEdge> g) {
DijkstraShortestPath dijkstraShortestPath = new DijkstraShortestPath(g);
GraphPath shortestPath = dijkstraShortestPath.getPath(v1,v2);
List<String> sToVPath = shortestPath.getEdgeList();
double sToVWeight = shortestPath.getWeight();
//System.out.println(sToVPath.toString() + "\nWeight: " + sToVWeight);
}
public static void AStar(String v1, String v2, DefaultUndirectedWeightedGraph<String, DefaultWeightedEdge> g, ALTAdmissibleHeuristic Hero)
{
AStarShortestPath path;
path = new AStarShortestPath(g, Hero);
GraphPath shortestPath = path.getPath(v1, v2);
List<String> sToVPath = shortestPath.getEdgeList();
}
public static DefaultUndirectedWeightedGraph<String, DefaultWeightedEdge> InitializeGraph() throws IOException
{
DefaultUndirectedWeightedGraph<String, DefaultWeightedEdge> g =
new DefaultUndirectedWeightedGraph<String, DefaultWeightedEdge>(DefaultWeightedEdge.class);
// TODO: Generalize the vertices added
for (int i = 0; i < 100; i++)
{
g.addVertex(Integer.toString(i));
}
File file = new File("p1_graph.txt");
BufferedReader in = new BufferedReader(new FileReader(file));
String line;
// Get to the point in the .txt file where the graph is defined
// Count number of vertices to initialize matrix
int count = 0;
while ((line = in.readLine()) != null)
{
if (line.contains("# From, To, Distance")) break;
if ((line.contains("#")) || (line.equals(""))) continue;
count++;
}
// Translate the graph into an adjacency matrix
while ((line = in.readLine()) != null)
{
if (line.equals("")) break;
String[] tokens = line.split(",");
DefaultWeightedEdge temp = g.addEdge(tokens[0], tokens[1]);
g.setEdgeWeight(temp, Integer.parseInt(tokens[2]));
}
return g;
}
}