/** * Find the path from start to goal using breadth first search * * @param start The starting location * @param goal The goal location * @param nodeSearched A hook for visualization. See assignment instructions for how to use it. * @return The list of intersections that form the shortest (unweighted) path from start to goal * (including both start and goal). */ public List<GeographicPoint> bfs( GeographicPoint start, GeographicPoint goal, Consumer<GeographicPoint> nodeSearched) { // TODO: Implement this method in WEEK 2 // Hook for visualization. See writeup. // nodeSearched.accept(next.getLocation()); Queue<GeographicPoint> queue = new LinkedList<GeographicPoint>(); Set<GeographicPoint> visited = new HashSet<GeographicPoint>(); Map<GeographicPoint, GeographicPoint> parentMap = new HashMap<GeographicPoint, GeographicPoint>(); queue.add(start); visited.add(start); while (!queue.isEmpty()) { GeographicPoint curr = queue.poll(); nodeSearched.accept(curr); if (curr.equals(goal)) { // reaches the goal return RetrievePath(parentMap, start, goal); // retrieve the path } MapVertex node = Vertices.get(curr); // the MapVertex associated with the location curr Set<MapEdge> edges = node.getEdges(); // the edges connected to the node for (MapEdge edge : edges) { MapVertex next = edge.getEnd(); // neighbor of the location curr GeographicPoint nextLoc = next.getLocation(); if (!visited.contains(nextLoc)) { visited.add(nextLoc); parentMap.put(nextLoc, curr); // set curr as the parent of next in tha path map queue.add(nextLoc); } } } /////// if the goal is not achievable return null; }
/** @return the distance b/w two points given their latitude and longitude */ private double calculateDistance(MapVertex source, MapVertex destination) { double EarthRadius = 6371000.0; GeographicPoint sourceLocation = source.getLocation(); GeographicPoint destinationLocation = destination.getLocation(); double sourceLatitude = Math.toRadians(sourceLocation.x); double sourceLongitude = Math.toRadians(sourceLocation.y); double destinationLatitude = Math.toRadians(destinationLocation.x); double destinationLongitude = Math.toRadians(destinationLocation.y); double latDiff = sourceLatitude - destinationLatitude; double lonDiff = sourceLongitude - destinationLongitude; double a = Math.sin(latDiff / 2) * Math.sin(latDiff / 2) + Math.cos(sourceLatitude) * Math.cos(destinationLatitude) * Math.sin(lonDiff / 2) * Math.sin(lonDiff / 2); double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a)); return EarthRadius * c; }
/** * Find the path from start to goal using A-Star search * * @param start The starting location * @param goal The goal location * @param nodeSearched A hook for visualization. See assignment instructions for how to use it. * @return The list of intersections that form the shortest path from start to goal (including * both start and goal). */ public List<GeographicPoint> aStarSearch( GeographicPoint start, GeographicPoint goal, Consumer<GeographicPoint> nodeSearched) { // TODO: Implement this method in WEEK 3 // Hook for visualization. See writeup. // nodeSearched.accept(next.getLocation()); PriorityQueue<MapVertex> pq = new PriorityQueue<MapVertex>(MapVertex.AStarComparator); Set<MapVertex> visited = new HashSet<MapVertex>(); Map<GeographicPoint, GeographicPoint> parentMap = new HashMap<GeographicPoint, GeographicPoint>(); setDistancesFromStart(start); setDistancesToEnd(start, goal); pq.add(Vertices.get(start)); // put the start node into the PQ while (!pq.isEmpty()) { MapVertex curr = pq.poll(); nodeSearched.accept(curr.getLocation()); // for visualization if (!visited.contains(curr)) { visited.add(curr); if (curr.getLocation().equals(goal)) return RetrievePath(parentMap, start, goal); ; // retrieve the shortest path Set<MapEdge> edges = curr.getEdges(); for (MapEdge edge : edges) { MapVertex next = edge.getEnd(); if (!visited.contains(next)) { double currLenFromStart = curr.getDistFromStart() + edge.getRoadLength(); if (currLenFromStart < next.getDistFromStart()) { next.setDistFromStart(currLenFromStart); // update shortest distance to the start parentMap.put( next.getLocation(), curr.getLocation()); // update the parent-child relation pq.add(next); } } } } } return null; }