def plot_searchers_and_target(g, folder_path, my_layout, target, searchers, t): """plot results of searchers position and true position of the target""" m = len(list(searchers.keys())) S = ext.get_set_searchers(list(range(m)))[0] g.vs["color"] = "white" for s_id in S: s = searchers[s_id] my_searcher_vertex = s.path_taken[t] v_idx = ext.get_python_idx(my_searcher_vertex) g.vs[v_idx]["color"] = "blue" # plot target v_target = target.stored_v_true[t] v_t_idx = ext.get_python_idx(v_target) if target.capture_time == t: g.vs[v_t_idx]["color"] = "green" else: g.vs[v_t_idx]["color"] = "red" name_file = folder_path + "/" + g["name"] + "_t" + str(t) + ".png" plot(g, name_file, layout=my_layout, figsize=(3, 3), bbox=(400, 400), margin=15, dpi=400) plt.close() return name_file
def get_vertices_and_steps(G, deadline, searchers): """Extract information from the user provided graph and information on searchers For each time step, find which vertices each searcher is allowed to be""" start = ext.get_searchers_positions(searchers) # S_ and Tau S, m = ext.get_set_searchers(start) Tau = ext.get_set_time(deadline) # get vertices V, n = ext.get_set_vertices(G) # shortest path lengths spl = ext.get_length_short_paths(G) # find V^tau(t) = {v in V} --> possible v(t) in optimal solution vertices_t = {} # find V^tau(v) = {t in Tau} --> possible t(v) in optimal solution times_v = {} start_idx = ext.get_python_idx(start) for s in S: s_idx = ext.get_python_idx(s) # initial position vertices_t[s, 0] = [start[s_idx]] # starting at t = 1 (idx = 1), it begins to move for t in Tau: vertices_t[s, t] = [] # find the nodes that are within t of distance (thus can be reached at t) for v in V: v_idx = ext.get_python_idx(v) dummy_var = spl[start_idx[s_idx]][v_idx] if dummy_var <= t: vertices_t[s, t].append(v) # find times allowed for each vertex for v in V: times_v[s, v] = [] if v == start[s_idx]: times_v[s, v] = [0] for t in Tau: if v in vertices_t[s, t]: times_v[s, v].append(t) # find dummy goal vertex and T + 1 v_g = ext.get_label_dummy_goal(V) t_g = ext.get_last_t(Tau) # append dummy goal vertices_t[s, t_g] = [v_g] # at T + 1, searcher is at dummy goal vertex times_v[s, v_g] = [ t_g ] # the only time allowed for the dummy goal vertex is T + 1 return start, vertices_t, times_v
def plot_target_belief(g, folder_name, my_layout, b_target: dict, t: int): """Plot target belief""" V, n = ext.get_set_vertices(g) rgba_color = (255, 0, 0, 1) g.vs["color"] = "white" for v in V: v_idx = ext.get_python_idx(v) b = b_target[v, t] c = b / 1 if 0.001 < c < 0.7: c = 0.7 my_color = get_color_belief(rgba_color, c) if my_color[3] < 0.001: my_color = "white" g.vs[v_idx]["color"] = my_color name_file = folder_name + "/" + g["name"] + "_tgt_t" + str(t) + ".png" plot(g, name_file, layout=my_layout, figsize=(3, 3), bbox=(400, 400), margin=15, dpi=400) return name_file
def plot_searchers_position(g, folder_name, my_layout, x_searchers: dict, t: int): """plot results of searchers position""" m = ext.get_m_from_tuple(x_searchers) V, n = ext.get_set_vertices(g) S = ext.get_set_searchers(m)[0] g.vs["color"] = "white" for s in S: for v in V: my_value = x_searchers.get((s, v, t)) if my_value == 1: v_idx = ext.get_python_idx(v) g.vs[v_idx]["color"] = "blue" name_file = folder_name + "/" + g["name"] + "_t" + str(t) + ".png" plot(g, name_file, layout=my_layout, figsize=(3, 3), bbox=(400, 400), margin=15, dpi=400) return name_file
def plot_target_belief2(g, folder_path, my_layout, b_target: dict, t: int, true_pos=None): """Plot target belief""" V, n = ext.get_set_vertices(g) rgba_color = (255, 0, 0, 1) g.vs["color"] = "white" for v in V: v_idx = ext.get_python_idx(v) b = b_target[v] if b <= 0.005: my_color = "white" else: if 0.005 < b <= 0.1: c = 0.1 elif 0.1 < b <= 0.2: c = 0.2 elif 0.2 < b <= 0.3: c = 0.3 elif 0.3 < b <= 0.4: c = 0.4 elif 0.4 < b <= 0.5: c = 0.5 elif 0.5 < b <= 0.6: c = 0.6 elif 0.6 < b <= 0.7: c = 0.7 elif 0.7 < b <= 0.8: c = 0.8 else: c = b / 1 my_color = get_color_belief(rgba_color, c) if true_pos is not None: if v == true_pos: my_color = "red" g.vs[v_idx]["color"] = my_color name_file = folder_path + "/" + g["name"] + "_tgt_t" + str(t) + ".png" plot(g, name_file, layout=my_layout, figsize=(3, 3), bbox=(400, 400), margin=15, dpi=400) plt.close() return name_file
def get_previous_vertices(g, s: int, v: int, t: int, vertices_t: dict): """Find possible previous vertices according to time """ v_idx = ext.get_python_idx(v) # do not have previous data (before start) if t == 0: return None # check for physical proximity (neighbors) AND possible vertex at t+1 else: v_t_previous = [] v_nei = g.vs[v_idx]["neighbors"] + [v_idx] for neighbor in v_nei: u = ext.get_label_name(neighbor) if u in vertices_t.get((s, t - 1)): v_t_previous.append(u) return v_t_previous
def probability_move(M, current_vertex): """get moving probabilities for current vertex""" # get current vertex id v_idx = ext.get_python_idx(current_vertex) n = len(M[v_idx]) prob_move = [] my_vertices = [] for col in range(0, n): prob_v = M[v_idx][col] # if target can move to this vertex, save to list if prob_v > 1e-4: prob_move.append(prob_v) my_vertices.append(col + 1) return my_vertices, prob_move
def plot_searchers_only(g, folder_path, my_layout, pi: list, t: int): for v in pi: v_idx = ext.get_python_idx(v) g.vs[v_idx]["color"] = "blue" name_file = folder_path + "/" + g["name"] + "_t" + str(t) + ".png" plot(g, name_file, layout=my_layout, figsize=(3, 3), bbox=(400, 400), margin=15, dpi=400) plt.close() return name_file
def init_temp_path(searchers: dict, horizon: int): """If no path was computed yet, assume all searchers will stay at the start position :param searchers: dictionary of searcher class :param horizon: planning horizon (h)""" Tau = ext.get_set_time_u_0(horizon) start = ext.get_searchers_positions(searchers) # S_ and Tau S, m = ext.get_set_searchers(start) temp_pi = dict() temp_pi['current_searcher'] = None for s in S: s_idx = ext.get_python_idx(s) v = start[s_idx] for t in Tau: temp_pi[s, t] = v return temp_pi
def get_next_vertices(g, s: int, v: int, t: int, vertices_t: dict, T_ext: list): """Find possible next vertices according to time s, v and t refers to the value of each (not index)""" v_idx = ext.get_python_idx(v) # is at last possible vertex, will be at dummy goal on T + 1 - don't worry about proximity if t == T_ext[-1]: return vertices_t.get((s, t + 1)) # check for physical proximity (neighbors) AND possible vertex at t+1 elif t < T_ext[-1]: v_t_next = [] v_nei = g.vs[v_idx]["neighbors"] + [v_idx] # get labels for neighbor in v_nei: u = ext.get_label_name(neighbor) if u in vertices_t.get((s, t + 1)): v_t_next.append(u) return v_t_next else: return None
def get_vertices_and_steps_distributed(G, deadline, searchers, temp_s_path): """Extract information from the user provided graph and information on searchers For each time step, find which vertices each searcher is allowed to be Since this is the distributed version, use info on temporary searchers path (temp_s_path)""" start = ext.get_searchers_positions(searchers) # S_ and Tau S, m = ext.get_set_searchers(start) Tau = ext.get_set_time(deadline) # get vertices V, n = ext.get_set_vertices(G) # shortest path lengths spl = ext.get_length_short_paths(G) # find V^tau(t) = {v in V} --> possible v(t) in optimal solution vertices_t = {} # find V^tau(v) = {t in Tau} --> possible t(v) in optimal solution times_v = {} for s in S: # initial position vertices_t[s, 0] = [temp_s_path[(s, 0)]] st_idx = ext.get_python_idx(vertices_t.get((s, 0))[0]) # starting at t = 1 (idx = 1), it begins to move for t in Tau: vertices_t[s, t] = [] if s == temp_s_path['current_searcher']: # if it's planning for this searcher, consider all possibilities # find the nodes that are within t of distance (thus can be reached at t) for v in V: v_idx = ext.get_python_idx(v) dummy_var = spl[st_idx][v_idx] if dummy_var <= t: vertices_t[s, t].append(v) else: # if is not the planning searcher, just use the info on the temporary path # either the start vertex of the pre-computed path v = temp_s_path[s, t] vertices_t[s, t].append(v) # find times allowed for each vertex for v in V: times_v[s, v] = [] if v == vertices_t[s, 0][0]: times_v[s, v] = [0] for t in Tau: if v in vertices_t[s, t]: times_v[s, v].append(t) # find dummy goal vertex and T + 1 v_g = ext.get_label_dummy_goal(V) t_g = ext.get_last_t(Tau) # append dummy goal vertices_t[s, t_g] = [v_g] # at T + 1, searcher is at dummy goal vertex times_v[s, v_g] = [ t_g ] # the only time allowed for the dummy goal vertex is T + 1 return start, vertices_t, times_v