def find_nodes(longitude, latitude, grid_of_nodes, node_info, n): if out_of_bounds(longitude, latitude, node_info): print "OUT OF BOUNDS: (Long, Lat) = (" + str(longitude) + ", " + ( str(latitude) + ")") return None # Node closest to coords and its distance best_node = None best_distance = 1000000 i = int(float(longitude - node_info[3]) * (n / float(node_info[2] - node_info[3]))) j = int(float(latitude - node_info[1]) * (n / float(node_info[0] - node_info[1]))) # You have to check the surrounding area (if a coordinate is really close # to the edge of the region[i][j] it could be in a different region # [i - 1][j] for example if i != 0: i -= 1 if j != 0: j -= 1 for n in range(3): if i + n >= len(grid_of_nodes): break for m in range(3): if j + m >= len(grid_of_nodes[0]): break grid_region = grid_of_nodes[i + n][j + m] for node in grid_region.nodes: currDistance = distance(latitude, longitude, node.lat, node.long) if currDistance < best_distance and ( len(node.forkward_links) > 0): best_node = node best_distance = currDistance return best_node
def find_nodes(longitude, latitude, grid_of_nodes, node_info, n): if out_of_bounds(longitude, latitude, node_info): print "OUT OF BOUNDS: (Long, Lat) = (" + (str(longitude) + ", " + str(latitude) + ")") return None # node closest to coords and its distance best_node = None best_dist = 1000000 i = int( float(longitude - node_info[3]) * (n / float(node_info[2] - node_info[3]))) j = int( float(latitude - node_info[1]) * (n / float(node_info[0] - node_info[1]))) # You have to check the surrounding area (if a coordinate is really close # to the edge of the region[i][j] it could be in a different region # [i - 1][j] for example if i != 0: i -= 1 if j != 0: j -= 1 for n in range(3): if i + n >= len(grid_of_nodes): break for m in range(3): if j + m >= len(grid_of_nodes[0]): break grid_region = grid_of_nodes[i + n][j + m] for node in grid_region.nodes: curr_dist = distance(latitude, longitude, node.lat, node.long) if curr_dist < best_dist and (len(node.forward_links) > 0): best_node = node best_dist = curr_dist return best_node
def PickupRequest(self, TAXINOTNEEDED, arrayFromCSV, timeRequested): global searchAlgorithm, searchRadius #Occasionally a customer's data is bad, and we opt to throw those events out if float(arrayFromCSV[10]) == 0 or float(arrayFromCSV[11]) == 0 or float(arrayFromCSV[12]) == 0 or float(arrayFromCSV[13]) == 0: print "BAD CUSTOMER longitude/latitude" return #If there are no taxis available, we assign the customers to the waiting array so when a taxi is freed up we can pick them up if len(self.taxisAvailable) == 0: self.customersWaiting.append((timeRequested, arrayFromCSV)) self.taxiNeeded += 1 return custLong = float(arrayFromCSV[10]) custLat = float(arrayFromCSV[11]) taxiList = [] #We sort the taxis by their distance by making tuples (distance, taxi) for taxi in self.taxisAvailable: taxiList.append((distance(taxi.lat, taxi.long, custLat, custLong), taxi)) taxiList.sort(key = lambda taxi: taxi[0]) #Using either the A* algorithm ('A') or the winding factor ('W') finds the closest taxi. bestTaxi = None best_distance = 10000000 if searchAlgorithm == 'A': for taxi in taxiList: #We sorted them all by Euclidean distance - if the current best distance is smaller than the current euclidean distance (the shortest distance) then we don't need to check anymore as none can be closer if taxi[0] > best_distance or taxi[0] > searchRadius: break currDistance = AStar(taxi[1].long, taxi[1].lat, custLong, custLat, self.grid, self.node_info, self.gridSize) if currDistance < best_distance: bestTaxi = taxi[1] best_distance = currDistance if searchAlgorithm == 'W': for taxi in taxiList: #We sorted them all by Euclidean distance - if the current best distance is smaller than the current euclidean distance (the shortest distance) then we don't need to check anymore as none can be closer if taxi[0] > best_distance or taxi[0] > searchRadius: break currDistance = estimateActual(taxi[1].lat, taxi[1].long, custLat, custLong) if currDistance < best_distance: bestTaxi = taxi[1] best_distance = currDistance #If all were outside the search radius, then no taxis will have picked them up and thus no customers would have been found if bestTaxi is None: self.customersWaiting.append((timeRequested, arrayFromCSV)) self.taxiNeeded += 1 return #This is what we are hoping to minimize, so we keep track of it all self.distanceTravelled += best_distance #For now, convert to time by assuming they are going twenty miles per hour bestTime = (best_distance / float(5280)) / 20 * 3600 #20 mph and 3600 seconds per hour #We must remove the taxi from available taxis but add them to the ones in use self.taxisInUse.add(bestTaxi) self.taxisAvailable.discard(bestTaxi) self.generatePickup(bestTaxi, arrayFromCSV, changeTime(timeRequested, bestTime))
def PickupRequest(self, TAXINOTNEEDED, arrayFromCSV, timeRequested): global searchAlgorithm, searchRadius #Occasionally a customer's data is bad, and we opt to throw those events out if float(arrayFromCSV[10]) == 0 or float( arrayFromCSV[11]) == 0 or float( arrayFromCSV[12]) == 0 or float(arrayFromCSV[13]) == 0: print "BAD CUSTOMER longitude/latitude" return #If there are no taxis available, we assign the customers to the waiting array so when a taxi is freed up we can pick them up if len(self.taxisAvailable) == 0: self.customersWaiting.append((timeRequested, arrayFromCSV)) self.taxiNeeded += 1 return custLong = float(arrayFromCSV[10]) custLat = float(arrayFromCSV[11]) taxiList = [] #We sort the taxis by their distance by making tuples (distance, taxi) for taxi in self.taxisAvailable: taxiList.append((distance(taxi.lat, taxi.long, custLat, custLong), taxi)) taxiList.sort(key=lambda taxi: taxi[0]) #Using either the A* algorithm ('A') or the winding factor ('W') finds the closest taxi. bestTaxi = None best_distance = 10000000 if searchAlgorithm == 'A': for taxi in taxiList: #We sorted them all by Euclidean distance - if the current best distance is smaller than the current euclidean distance (the shortest distance) then we don't need to check anymore as none can be closer if taxi[0] > best_distance or taxi[0] > searchRadius: break currDistance = AStar(taxi[1].long, taxi[1].lat, custLong, custLat, self.grid, self.node_info, self.gridSize) if currDistance < best_distance: bestTaxi = taxi[1] best_distance = currDistance if searchAlgorithm == 'W': for taxi in taxiList: #We sorted them all by Euclidean distance - if the current best distance is smaller than the current euclidean distance (the shortest distance) then we don't need to check anymore as none can be closer if taxi[0] > best_distance or taxi[0] > searchRadius: break currDistance = estimateActual(taxi[1].lat, taxi[1].long, custLat, custLong) if currDistance < best_distance: bestTaxi = taxi[1] best_distance = currDistance #If all were outside the search radius, then no taxis will have picked them up and thus no customers would have been found if bestTaxi is None: self.customersWaiting.append((timeRequested, arrayFromCSV)) self.taxiNeeded += 1 return #This is what we are hoping to minimize, so we keep track of it all self.distanceTravelled += best_distance #For now, convert to time by assuming they are going twenty miles per hour bestTime = (best_distance / float(5280)) / 20 * 3600 #20 mph and 3600 seconds per hour #We must remove the taxi from available taxis but add them to the ones in use self.taxisInUse.add(bestTaxi) self.taxisAvailable.discard(bestTaxi) self.generatePickup(bestTaxi, arrayFromCSV, changeTime(timeRequested, bestTime))
def heuristic(node, end_node, max_speed): return distance(node.lat, node.long, end_node.lat, end_node.long) / ( max_speed)
def heuristic(node, end_node, max_speed): return distance(node.lat, node.long, end_node.lat, end_node.long) / max_speed