def run(source, destination, show, prediction, seed): (graph, ways, data) = read_xml(config.osm_path, config.elev_path) source = sys.argv[1].lower() dest = sys.argv[2].lower() try: source = int(source) assert source in graph except: if source in ways: source = ways[source][0] else: print('Source must be a valid node ID or a street name') return try: dest = int(dest) assert dest in graph except: if dest in ways: dest = ways[dest][0] else: print('Destination must be a valid node ID or a street name') return costfunc = None heuristic = None if prediction == 'toblers': costfunc = astar.toblers heuristic = astar.toblers_heuristic if prediction in ('linear', 'nearest'): walks = models.read_walk_data(config.walk_data_path, graph, data) training_walks, test_walks = models.partition_walks(walks, seed=seed) train = models.build_dist_elev_examples(training_walks, data) test = models.build_dist_elev_examples(test_walks, data) if prediction == 'linear': model = models.linear_model(train) costfunc = lambda a, b: model([astar.euclidean(a, b), b.z_m - a.z_m]) heuristic = costfunc elif prediction == 'nearest': model = models.nearest_neighbor_model(train) costfunc = lambda a, b: model([astar.euclidean(a, b), b.z_m - a.z_m]) heuristic = lambda a, b: 0 result = astar.astar(costfunc, heuristic, graph, data, source, dest) if result is None: print('No path to destination found') pathstr = ', '.join((str(nd) for nd in result[0])) print('\npath: {}\n'.format(pathstr)) print('time: {:.2f} minutes\n'.format(result[1])) #walk_data = models.read_walk_data(config.walk_data_path, graph, data) #print(walk_data) #if seed == None: # seed = random.random() #models.compare_models(walk_data, data, seed) if show: graphics.display(graph, data, result[0], result[1])
def path_dist(path, id_to_data): pathdata = [id_to_data[nid] for nid in path] xydistsum = sum([astar.euclidean(l, r) for l, r in zip(pathdata, pathdata[1:])]) return xydistsum