-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDynamicShortestPath.java
267 lines (231 loc) · 9.3 KB
/
DynamicShortestPath.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
/*
DynamicShortestPath.java
Author: Shreyas Jayanna
Version 1
Date: 05/15/2015
*/
// Import statements
import java.util.*;
/**
* Class DynamicShortestPath. This class finds shortest paths between a source and destination
*/
public class DynamicShortestPath {
RoadGraph graph;
Set<MyNode> visitedQueue;
ArrayList<MyNode> unvisitedQueue;
HashMap<String,Double> nodeDistanceMap;
HashMap<String,String> nodePreviousMap;
HashMap<MyEdge,MyEdge> edgePreviousMap;
Connections connections;
/**
* Default Constructor
*/
public DynamicShortestPath() {
this.graph = new RoadGraph();
this.visitedQueue = new HashSet<>();
this.unvisitedQueue = new ArrayList<MyNode>();
this.nodeDistanceMap = new HashMap<>();
this.nodePreviousMap = new HashMap<>();
this.connections = new Connections();
this.edgePreviousMap = new HashMap<>();
}
/**
* Constructor
* @param graph The graph of road network
* @param connections The connections in the graph
*/
public DynamicShortestPath(RoadGraph graph, Connections connections) {
this();
this.graph = new RoadGraph(graph);
this.connections = new Connections(connections);
}
/**
* This method sets the roadgraph object
* @param graph The road graph
*/
public void setGraph(RoadGraph graph) {
this.graph = graph;
}
/**
* This method returns the road graph
* @return The road graph object
*/
public RoadGraph getGraph() {
return this.graph;
}
/**
* This method finds shortest path between two nodes
* @param from The source node
* @param target The destination node
* @param currentEdge The current edge from which the vehicles will be rerouted
* @return The new path in a Stack
*/
public Stack<MyEdge> findShortestPath(MyNode from, MyNode target, MyEdge currentEdge) {
/*
Distance refers to Travel Time
Pick edge list from the from node,
update the distance to every to node of each edge in a hashtable <node(string),double(distance)>
Update the previous node for each node in a hashtable <node(string),prevNode(string)>
*/
//System.out.println();
//System.out.println("In DSP - findpaths, from " + from.getId() + " target " + target.getId() + " current edge " + currentEdge.getId());
Stack<MyEdge> path = null;
boolean targetReached = false;
MyNode node = from;
node.setDistance(0.0);
this.nodeDistanceMap.put(node.getId(), 0.0);
this.unvisitedQueue.add(node);
MyEdge prevEdge = null;
boolean firstEdgeConsidered = false;
boolean firstEdgeFlag = true;
while(unvisitedQueue.size() > 0) {
if(!firstEdgeConsidered) {
prevEdge = currentEdge;
firstEdgeConsidered = true;
}
Collections.sort(unvisitedQueue, new Comparator<MyNode>() {
@Override
public int compare(MyNode o1, MyNode o2) {
if(o1.distance < o2.distance)
return -1;
return 1;
}
});
// print unvisited queue contents
/*
System.out.print("Unvisited queue [");
for(MyNode myNode : unvisitedQueue) {
System.out.print(" " + myNode.getId());
}
System.out.println(" ]");
*/
MyNode minNode = unvisitedQueue.remove(0);
//System.out.println("min node from unvisited queue " + minNode.getId());
visitedQueue.add(minNode);
if(minNode == target) {
targetReached = true;
// System.out.println("Target " + target.getId() + " reached");
break;
}
this.calculateShortestPaths(minNode, prevEdge, firstEdgeFlag);
firstEdgeFlag = false;
}
ArrayList<String> edges = new ArrayList<>();
// If the target is reached, it means a path exists from source to destination node
if(targetReached) {
path = new Stack<>();
MyNode prev = null;
while(target != from) {
prev = this.graph.nodes.get(this.nodePreviousMap.get(target.getId()));
MyEdge connectingEdge = this.getConnectingEdge(prev, target, prevEdge);
edges.add(connectingEdge.getId());
path.push(connectingEdge);
prevEdge = connectingEdge;
target = prev;
//System.out.println("Connecting edge : " + connectingEdge);
}
path.push(currentEdge);
edges.add(currentEdge.getId());
//Collections.reverse(edges);
}
// System.out.println((path == null ? "path is null" : "path size " + path.size()));
return path;
}
/**
* This method calculates the shortest path between the source and a node in the graph
* @param from From node
* @param prevEdge Previous edge
* @param firstEdge Frist edge
*/
public void calculateShortestPaths(MyNode from, MyEdge prevEdge, boolean firstEdge) {
ArrayList<MyNode> neighbors = new ArrayList<>(from.getNeighborNodes());
ArrayList<MyEdge> edges = new ArrayList<>(from.getEdgeList());
for(MyNode to : neighbors) {
double dist = getDistance(from) + distance(from,to);
if(getDistance(to) > dist) {
MyEdge possibleEdge = null;
for(MyEdge edge : edges) {
if(edge.getFrom().getId().equals(from.getId()) && edge.getTo().getId().equals(to.getId())) {
possibleEdge = edge;
break;
}
}
boolean edgeFound = false;
if(!firstEdge) {
MyNode fromPrevNode = this.graph.getNodes().get(this.nodePreviousMap.get(from.getId()));
if(fromPrevNode != null) {
List<MyEdge> fromPrevEdges = fromPrevNode.getEdgeList();
for (MyEdge anEdge : fromPrevEdges) {
if (anEdge.getFrom().getId().equals(fromPrevNode.getId()) &&
anEdge.getTo().getId().equals(from.getId())) {
edgeFound = true;
prevEdge = anEdge;
break;
}
}
}
if(!edgeFound)
continue;
}
if(possibleEdge != null) {
if(this.connections.edgeConnections.containsKey(prevEdge.getId())) {
if (this.connections.getConnection(prevEdge.getId()).contains(possibleEdge.getId())) {
to.setDistance(dist);
this.nodeDistanceMap.put(to.getId(), dist);
this.nodePreviousMap.put(to.getId(), from.getId());
unvisitedQueue.add(to);
this.edgePreviousMap.put(possibleEdge, prevEdge);
// System.out.println("Edge in path " + possibleEdge.getId());
}
}
}
}
}
}
/**
* This method returns the distance between two nodes
* @param from From node
* @param to To node
* @return The distance between from and to node as a double value
*/
private double distance(MyNode from, MyNode to) {
ArrayList<MyEdge> edges = new ArrayList<>(from.getEdgeList());
for(MyEdge edge : edges) {
// If an edge exists between from and to node, return the distance
if(edge.getFrom().getId().equals(from.getId()) && edge.getTo().getId().equals(to.getId())) {
return edge.getTravelTime();
}
}
// If no edge exists between from and to node, return infinity
return Double.POSITIVE_INFINITY;
}
/**
* This method returns the distance of a node from source
* @param node Node
* @return distance of node from source as a double value
*/
public double getDistance(MyNode node) {
if(this.nodeDistanceMap.containsKey(node.getId()))
return this.nodeDistanceMap.get(node.getId());
return Double.POSITIVE_INFINITY;
}
/**
* This method retruns the connecting edge between two nodes
* @param from From node
* @param to To node
* @param prevEdge Previous edge
* @return Edge connecting from and to nodes, null if no such edge exists
*/
public MyEdge getConnectingEdge(MyNode from, MyNode to, MyEdge prevEdge) {
ArrayList<MyEdge> edges;
if(from.getEdgeList().size() > 0) {
edges = new ArrayList<>(from.getEdgeList());
for (MyEdge edge : edges) {
if (edge.getFrom().getId().equals(from.getId()) && edge.getTo().getId().equals(to.getId())) {
return edge;
}
}
}
return null;
}
}