-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCampusMap.java
More file actions
334 lines (271 loc) · 14.8 KB
/
CampusMap.java
File metadata and controls
334 lines (271 loc) · 14.8 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
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
import java.util.*;
public class CampusMap {
private Map<String, Location> locations;
public CampusMap() {
locations = new HashMap<>();
}
public void addLocation(String name, double latitude, double longitude) {
Location location = new Location(name, latitude, longitude);
locations.put(name, location);
}
public List<String> getLocationNames() {
List<String> locationNames = new ArrayList<>();
for (Location location : locations.values()) {
locationNames.add(location.getName());
}
return locationNames;
}
public void addDistance(String source, String destination, double distance) {
Location sourceLocation = locations.get(source);
Location destinationLocation = locations.get(destination);
if (sourceLocation != null && destinationLocation != null) {
sourceLocation.addNeighbor(destinationLocation, distance);
destinationLocation.addNeighbor(sourceLocation, distance);
}
}
public Location getLocation(String name) {
return locations.get(name);
}
public List<Location> findShortestPath(String source, String destination) {
Location sourceLocation = locations.get(source);
Location destinationLocation = locations.get(destination);
if (sourceLocation == null || destinationLocation == null) {
return null; // One or both locations do not exist
}
Map<Location, Double> distances = new HashMap<>();
Map<Location, Location> previousLocations = new HashMap<>();
Set<Location> visited = new HashSet<>();
// Initialize distances with infinity and previousLocations with null
for (Location location : locations.values()) {
distances.put(location, Double.POSITIVE_INFINITY);
previousLocations.put(location, null);
}
distances.put(sourceLocation, 0.0); // Distance from source to itself is 0
PriorityQueue<Location> queue = new PriorityQueue<>(Comparator.comparingDouble(distances::get));
queue.offer(sourceLocation);
while (!queue.isEmpty()) {
Location currentLocation = queue.poll();
if (currentLocation == destinationLocation) {
break; // Reached the destination
}
if (visited.contains(currentLocation)) {
continue; // Already visited this location
}
visited.add(currentLocation);
for (Map.Entry<Location, Double> entry : currentLocation.getNeighbors().entrySet()) {
Location neighbor = entry.getKey();
double distance = entry.getValue();
double newDistance = distances.get(currentLocation) + distance;
if (newDistance < distances.get(neighbor)) {
distances.put(neighbor, newDistance);
previousLocations.put(neighbor, currentLocation);
queue.offer(neighbor);
}
}
}
// Reconstruct the shortest path
List<Location> shortestPath = new ArrayList<>();
Location current = destinationLocation;
while (current != null) {
shortestPath.add(0, current);
current = previousLocations.get(current);
}
return shortestPath.isEmpty() ? null : shortestPath;
}
public List<List<Location>> filterRoutesByDistance(List<List<Location>> routes) {
routes.sort(Comparator.comparingDouble(this::calculateTotalDistance));
return routes;
}
public List<List<Location>> filterRoutesByArrivalTime(List<List<Location>> routes) {
routes.sort(Comparator.comparingDouble(this::calculateEstimatedArrivalTime));
return routes;
}
public List<List<Location>> searchRoutesByLandmark(List<List<Location>> routes, String landmark) {
List<List<Location>> filteredRoutes = new ArrayList<>();
if (routes != null) {
for (List<Location> route : routes) {
if (route != null && !route.isEmpty()) {
for (Location location : route) {
if (location != null && location.getName() != null && location.getName().equals(landmark)) {
filteredRoutes.add(route);
break;
}
}
}
}
}
return filteredRoutes;
}
double calculateTotalDistance(List<Location> route) {
double totalDistance = 0.0;
for (int i = 0; i < route.size() - 1; i++) {
Location sourceLocation = route.get(i);
Location destinationLocation = route.get(i + 1);
if (sourceLocation != null && destinationLocation != null) {
double distance = sourceLocation.getDistanceTo(destinationLocation);
totalDistance += distance;
}
}
return totalDistance;
}
double calculateEstimatedArrivalTime(List<Location> route) {
double totalTravelTime = 0.0;
for (int i = 0; i < route.size() - 1; i++) {
Location sourceLocation = route.get(i);
Location destinationLocation = route.get(i + 1);
if (sourceLocation != null && destinationLocation != null) {
double distance = sourceLocation.getDistanceTo(destinationLocation);
double averageTravelTime = calculateAverageTravelTime(distance);
double adjustedTravelTime = applyTrafficPatternAdjustments(averageTravelTime);
totalTravelTime += adjustedTravelTime;
}
}
return totalTravelTime;
}
private double calculateAverageTravelTime(double distance) {
// Assuming an average speed of 50 meters per minute
double averageSpeed = 500.0;
return distance / averageSpeed;
}
private double applyTrafficPatternAdjustments(double travelTime) {
// Apply traffic pattern adjustments based on specific conditions
if (travelTime >= 5 && travelTime < 10) {
// Moderate traffic, increase travel time by 10%
return travelTime * 1.1;
} else if (travelTime >= 10) {
// Heavy traffic, increase travel time by 20%
return travelTime * 1.2;
} else {
// No traffic adjustments needed
return travelTime;
}
}
public static class Location {
private final String name;
private final double latitude;
private final double longitude;
private final Map<Location, Double> neighbors;
public Location(String name, double latitude, double longitude) {
this.name = name;
this.latitude = latitude;
this.longitude = longitude;
this.neighbors = new HashMap<>();
}
public void addNeighbor(Location neighbor, double distance) {
neighbors.put(neighbor, distance);
}
public double getDistanceTo(Location neighbor) {
return neighbors.getOrDefault(neighbor, Double.POSITIVE_INFINITY);
}
public String getName() {
return name;
}
public double getLatitude() {
return latitude;
}
public double getLongitude() {
return longitude;
}
public Map<Location, Double> getNeighbors() {
return neighbors;
}
}
public static void main(String[] args) {
CampusMap campusMap = new CampusMap();
// Add locations and distances
campusMap.addLocation("Great Hall", 5.650593742091174, -0.19616334087129655);
campusMap.addLocation("Public Affairs Directorate", 5.650717164959013, -0.19814852617387896);
campusMap.addLocation("Viannis Bistro", 5.650957390663127, -0.19026283176032083);
campusMap.addLocation("Sociology Department", 5.650455585725553, -0.18976394085109477);
campusMap.addLocation("School of Languages", 5.6491637029058195, -0.1895547285513495);
campusMap.addLocation("Cash Office", 5.650422210867779, -0.19429252267837213);
campusMap.addLocation("Career and Counseling Centre", 5.649498852019727, -0.19030038266889965);
campusMap.addLocation("Legon Hall Annex A", 5.647985190833957, -0.18841787439056107);
campusMap.addLocation("Legon Hall Annex B", 5.647077666301523, -0.18842860322605126);
campusMap.addLocation("Legon Hall Annex C", 5.647534, -0.189123);
campusMap.addLocation("BrightHills Research", 5.644746122440687, -0.18778084978432888);
campusMap.addLocation("WELLDONE RESEARCH CONSULT", 5.645577578484664, -0.18912061314990058);
campusMap.addLocation("University of Ghana Guest Centre", 5.644822, -0.189128);
campusMap.addLocation("University of Ghana Basic School", 5.643079203024593, -0.1879940854128992);
campusMap.addLocation("Centre for Aging Studies", 5.64341041867396, -0.18622878936265513);
campusMap.addLocation("Night Market", 5.6418962813844455, -0.189128);
campusMap.addDistance("Great Hall", "Night Market Via University of Ghana Guest Centre", 2200); // Example distance in meters
campusMap.addDistance("Great Hall", "Cash Office", 600); // Example distance in meters
campusMap.addDistance("Cash Office", "Career and Counseling centre", 500); // Example distance in meters
campusMap.addDistance("Great Hall", "Public Affairs Directorate", 350); // Example distance in meters
campusMap.addDistance("Public Affairs Directorate", "Viannis Bistro", 650); // Example distance in meters
campusMap.addDistance("Viannis Bistro", "Sociology Department", 90); // Example distance in meters
campusMap.addDistance("Sociology Department", "School of Languages", 230); // Example distance in meters campusMap.addDistance("Career and Counseling centre", "Legon Hall Annex A", 500); // Example distance in meters
campusMap.addDistance("Legon Hall Annex A", "Legon Hall Annex C", 300); // Example distance in meters
campusMap.addDistance("Legon Hall Annex C", "Legon Hall Annex B", 350); // Example distance in meters
campusMap.addDistance("Legon Hall Annex C", "WELLDONE RESEARCH CONSULT", 210); // Example distance in meters
campusMap.addDistance("Legon Hall Annex B", "BrightHills Research", 260); // Example distance in meters
campusMap.addDistance("School of Languages", "University of Ghana Guest Centre", 500); // Example distance in meters
campusMap.addDistance("WELLDONE RESEARCH CONSULT", "University of Ghana Guest Centre", 90); // Example distance in meters
campusMap.addDistance("University of Ghana Guest Centre", "University of Ghana Basic School", 210); // Example distance in meters
campusMap.addDistance("BrightHills Research", "Centre for Aging Studies", 350); // Example distance in meters
campusMap.addDistance("University of Ghana Basic School", "Centre for Aging Studies", 220); // Example distance in meters
campusMap.addDistance("Centre for Aging Studies", "Night Market", 170); // Example distance in meters
campusMap.addDistance("Great Hall", "Public Affairs Directorate", 350); // Example distance in meters
// Find shortest path
String source = "Great Hall";
String destination = "Night Market";
List<Location> shortestPath = campusMap.findShortestPath(source, destination);
if (shortestPath != null) {
System.out.println("Shortest Path from " + source + " to " + destination + ". Starting from:");
for (Location location : shortestPath) {
System.out.println(location.getName());
}
double totalDistance = campusMap.calculateTotalDistance(shortestPath);
double estimatedArrivalTime = campusMap.calculateEstimatedArrivalTime(shortestPath);
System.out.println("Total Distance: " + totalDistance + " meters");
System.out.println("Estimated Arrival Time: " + estimatedArrivalTime + " minutes");
} else {
System.out.println("No path found between " + source + " and " + destination);
}
//Sorting and filtering
List<List<Location>> routes = new ArrayList<>();
// Route 1: Shortest path from Great Hall to Night Market via UGGC
List<Location> route1 = new ArrayList<>();
route1.add(campusMap.getLocation("Great Hall"));
route1.add(campusMap.getLocation("Cash Office"));
route1.add(campusMap.getLocation("Career and Counseling Centre"));
route1.add(campusMap.getLocation("Legon Hall Annex C"));
route1.add(campusMap.getLocation("WELLDONE RESEARCH CONSULT"));
route1.add(campusMap.getLocation("University of Ghana Guest Centre"));
route1.add(campusMap.getLocation("University of Ghana Basic School"));
route1.add(campusMap.getLocation("Centre for Aging Studies"));
route1.add(campusMap.getLocation("Night Market"));
routes.add(route1);
// Route 2: Alternative path from Great Hall to Night Market via Legon Hall Annexes
List<Location> route2 = new ArrayList<>();
route2.add(campusMap.getLocation("Great Hall"));
route2.add(campusMap.getLocation("Cash Office"));
route2.add(campusMap.getLocation("Career and Counseling Centre"));
route2.add(campusMap.getLocation("Legon Hall Annex A"));
route2.add(campusMap.getLocation("Legon Hall Annex B"));
route2.add(campusMap.getLocation("BrightHills Research"));
route2.add(campusMap.getLocation("Centre for Aging Studies"));
route2.add(campusMap.getLocation("Night Market"));
routes.add(route2);
// Route 3: Another alternative path from Great Hall to Night Market
List<Location> route3 = new ArrayList<>();
route3.add(campusMap.getLocation("Great Hall"));
route3.add(campusMap.getLocation("Public Affairs Directorate"));
route3.add(campusMap.getLocation("Viannis Bistro"));
route3.add(campusMap.getLocation("Sociology Department"));
route3.add(campusMap.getLocation("School of Languages"));
route3.add(campusMap.getLocation("University of Ghana Gust Centre"));
route3.add(campusMap.getLocation("University of Ghana Basic School"));
route3.add(campusMap.getLocation("Centre for Aging Studies"));
route3.add(campusMap.getLocation("Night Market"));
routes.add(route3);
// Filter routes by distance
List<List<Location>> routesSortedByDistance = campusMap.filterRoutesByDistance(routes);
// Filter routes by arrival time
List<List<Location>> routesSortedByArrivalTime = campusMap.filterRoutesByArrivalTime(routes);
// Search routes by landmark
String landmark = "Cash Office";
List<List<Location>> routesWithLandmark = campusMap.searchRoutesByLandmark(routes, landmark);
}
}