def generate_corridors_sample_path( instance, start_inds, dest_inds, factor, balance=[1, 3], quantile=0.2, n_sample=4, n_onpath=5, n_dilate=100 ): out_inds = CorridorUtils.get_reduced_patches( instance, start_inds, dest_inds, factor, balance=[1, 3], quantile=0.1 ) # compute distances from start point minus_start = [ np.linalg.norm(out_inds[:, i] - start_inds / factor) for i in range(out_inds.shape[1]) ] sorted_patches = np.argsort(minus_start) all_corridors = list() for _ in range(n_sample): drawn_points = np.random.choice( np.arange(out_inds.shape[1]), n_onpath, replace=False ) drawn_path = out_inds[:, sorted_patches[np. sort(drawn_points)]] * factor path = [ [start_inds.tolist()] + np.swapaxes(drawn_path, 1, 0).tolist() + [dest_inds.tolist()] ] all_corridors.append( get_distance_surface( instance.shape[1:], path, n_dilate=n_dilate ) ) return all_corridors
def get_reduced_patches( instance, start_inds, dest_inds, factor, balance=[1, 1], quantile=0.1 ): summed = np.sum(instance, axis=0) red = rescale(summed, factor) x_len, y_len = red.shape path_start_end = [ [ (start_inds / factor).astype(int).tolist(), (dest_inds / factor).astype(int).tolist() ] ] dist_corr = 1 - normalize( get_distance_surface( red.shape, path_start_end, n_dilate=min([x_len, y_len]) ) ) surface_comb = balance[0] * dist_corr + balance[1] * red quantile_surface = np.quantile(surface_comb, quantile) patches = surface_comb < quantile_surface inds_x, inds_y = np.where(patches) return np.array([inds_x, inds_y]) # *factor
paths = [path] # graph.get_pareto( # np.arange(0, 1.1, 0.1), source_v, target_v, compare=[2, 3] # ) time_infos.append(graph.time_logs.copy()) if cfg.VERBOSE: del graph.time_logs['edge_list_times'] del graph.time_logs['add_edges_times'] print(graph.time_logs) if dist > 0: # do specified numer of dilations corridor = get_distance_surface( graph.pos2node.shape, paths, mode="dilation", n_dilate=dist ) print("5) compute distance surface") # remove the edges of vertices in the corridor (to overwrite) graph.remove_vertices(corridor, delete_padding=cfg.PYLON_DIST_MAX) print("6) remove edges") time_pipeline = round(time.time() - tic, 3) print("FINISHED PIPELINE:", time_pipeline) # SAVE timing test time_test_csv( ID, cfg.CSV_TIMES, SCALE_PARAM, cfg.GTNX, GRAPH_TYPE, graph, path_costs, cost_sum, str(PIPELINE), time_pipeline, NOTES ) # save information for plotting
cfg.start_inds, cfg.dest_inds, factor_or_n_edges=factor, sample_method="simple") # main path computation path_gt, path_costs_gt, cost_sum_wg = graph.single_sp( **vars(cfg)) edge_numbers.append(graph.n_edges) if factor == 1 or factor == 0: actual_pipe.append((1, 0)) break corridor = get_distance_surface( graph.hard_constraints.shape, [path_gt], mode="dilation", n_dilate=10 # dist ) # estimated edges are pixels times neighbors # divided by resolution squared estimated_edges_10 = len(np.where(corridor > 0)[0]) * len( graph.shifts) / ((PIPE[pipe_step + 1])**2) now_dist = (mult_factor * graph.n_edges) / estimated_edges_10 # print("reduce corridor:", dist) corridor = get_distance_surface( graph.hard_constraints.shape, [path_gt], mode="dilation", n_dilate=int(np.ceil(now_dist))) # print( # "estimated with distance ", int(np.ceil(now_dist)), # len(np.where(corridor > 0)[0]) * len(graph.shifts) /
if USE_KSP: graph.get_shortest_path_tree(source_v, target_v) ksp = graph.k_shortest_paths(source_v, target_v, 3, overlap=0.2) paths = [k[0] for k in ksp] flat_list = [item for sublist in paths for item in sublist] del output_paths[-1] output_paths.append((flat_list, path_costs)) plot_k_sp(ksp, graph.instance * (corridor > 0).astype(int), out_path=OUT_PATH + str(factor)) else: paths = [path] # do specified numer of dilations corridor = get_distance_surface(graph.hard_constraints.shape, paths, mode="dilation", n_dilate=dist // factor) print("5) compute distance surface") # remove the edges of vertices in the corridor (to overwrite) graph.remove_vertices(corridor, delete_padding=cfg.PYLON_DIST_MAX) print("6) remove edges") instance_corr = upscale_corr(instance_corr, corridor > 0, factor) # BEST IN WINDOW # path_window, path_window_cost, cost_sum_window = graph.best_in_window( # 30, 35, 60, 70, source_v, target_v # ) # print("cost actually", cost_sum, "cost_new", cost_sum_window) # COMPUTE KSP
# COMPUTE STATISTICS for iteri in range(nr_iters): actual_pipe = [] graph = graphclass(belgium_inst, belgium_inst_corr, verbose=False) # set shift necessary in case of random graph automatic # probability estimation by edge bound graph.set_shift(cfg.start_inds, cfg.dest_inds, **vars(cfg)) if random and USE_PRIOR: corridor = get_distance_surface( belgium_inst_corr.shape, [[cfg.start_inds, cfg.dest_inds]], mode="dilation", n_dilate=200) else: corridor = np.ones(belgium_inst_corr.shape) * 0.5 if USE_PRIOR: DOWNSAMPLE_METHOD = "watershed" else: DOWNSAMPLE_METHOD = "simple" edge_numbers = list() tic = time.time() for pipe_step, factor in enumerate(PIPE):