Define a function to generate the distance matrix

def generate_distance_matrix(all_areas, all_trips): # Get the number of areas N = len(all_areas) # Initialize the distance matrix Dis = [[0 for i in range(N)] for j in range(N)] # Loop through all pairs of areas for ai in range(N): for aj in range(N): # Get all trips between the two areas trips = [trip for trip in all_trips if trip.start_area == all_areas[ai] and trip.end_area == all_areas[aj]] # If there are no recorded trips, infer the distance using bidirectional search if len(trips) == 0: candidate_paths = bidirectional_search(all_areas[ai], all_areas[aj], Dis) Dis[ai][aj] = min(candidate_paths) else: # Calculate the mean distance of all recorded trips distances = [trip.distance for trip in trips] Dis[ai][aj] = sum(distances) / len(distances) # Return the distance matrix return Dis

Python Distance Matrix Generation Algorithm: Efficiently Calculate Distances Between Areas

原文地址: https://www.cveoy.top/t/topic/m1js 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录