def test_pre_S2(): G = Graph() connectivity_edges = [0, 1, 2, 3] # directed connectivity path (one way) transition_edges = [0, 1, 2, 3] # directed transition path (one way) # Add edges to graph G.add_transition_path(transition_edges) G.add_connectivity_path(connectivity_edges) S_v_t = set([(1, 0), (2, 1), (0, 2), (1, 2), (3, 2)]) # np.testing.assert_equal(G.pre_tran_vt(S_v_t), # set([(0,0,0)])) np.testing.assert_equal( G.pre_conn_vt(S_v_t), set([(0, 1, 0), (2, 1, 0), (1, 2, 1), (3, 2, 1), (2, 1, 2), (2, 3, 2)]), ) np.testing.assert_equal( G.pre_tran_vt(S_v_t), set([ (3, 2, 0), (0, 1, 1), (0, 0, 1), (1, 0, 1), (2, 2, 0), (3, 3, 1), (1, 1, 1), ]), )
def get_linear_graph(n): G = Graph() G.add_transition_path(list(range(n))) G.add_connectivity_path(list(range(n))) G.set_node_positions({i: (0, i) for i in range(n)}) return G
def test_strategy2(): G = Graph() G.add_transition_path(list(range(0, 25))) G.add_transition_path(list(range(26, 50))) G.add_connectivity_path(list(range(0, 25))) G.add_connectivity_path(list(range(26, 50))) G.add_transition_path([0, 26]) G.set_frontiers({24: 1, 49: 1}) agent_positions = {0: 0, 1: 3, 2: 27, 3: 6, 4: 8, 5: 42, 6: 47} G.init_agents(agent_positions) cp = ClusterProblem() cp.graph = G cp.static_agents = [0] cp.master = 0 cp.max_problem_size = 1 cp.prepare_problem(remove_dead=False) cs = ClusterStructure(agent_clusters=agent_clustering(cp, 3)) cs = inflate_agent_clusters(cp, cs) kill_list = kill_largest_frontiers(cp, cs) np.testing.assert_equal(kill_list, set(range(48, 50)) | set(range(9, 25)))
def test_activationinflate1(): G = Graph() G.add_connectivity_path([0, 1]) G.add_connectivity_path([0, 2]) agent_positions = {0: 0, 1: 1, 2: 2} G.init_agents(agent_positions) cs = ClusterStructure(agent_clusters={"c0": [0], "c1": [1], "c2": [2]}) master = 0 cp = ClusterProblem() cp.graph = G cp.master = master cp.prepare_problem(remove_dead=False) cs = inflate_agent_clusters(cp, cs) np.testing.assert_equal(cs.subgraphs["c0"], set([0])) np.testing.assert_equal(cs.subgraphs["c1"], set([1])) np.testing.assert_equal(cs.subgraphs["c2"], set([2])) np.testing.assert_equal(cs.child_clusters["c0"], {("c1", 1), ("c2", 2)}) np.testing.assert_equal(cs.child_clusters["c1"], set()) np.testing.assert_equal(cs.child_clusters["c2"], set()) np.testing.assert_equal(cs.parent_clusters["c1"], ("c0", 0)) np.testing.assert_equal(cs.parent_clusters["c2"], ("c0", 0))
def test_inflate2(): G = Graph() G.add_transition_path(list(range(0, 12))) G.add_connectivity_path(list(range(0, 12))) G.add_connectivity_path([6, 8]) agent_positions = {0: 0, 1: 1, 2: 4, 3: 6, 4: 8, 5: 10} G.init_agents(agent_positions) cs = ClusterStructure(agent_clusters={ "c0": [0, 1], "c1": [2, 3], "c2": [4, 5] }) master = 0 cp = ClusterProblem() cp.graph = G cp.master = master cp.prepare_problem(remove_dead=False) cs = inflate_agent_clusters(cp, cs) np.testing.assert_equal(cs.subgraphs["c0"], set([0, 1, 2, 3])) np.testing.assert_equal(cs.subgraphs["c1"], set([4, 5, 6, 7])) np.testing.assert_equal(cs.subgraphs["c2"], set([8, 9, 10, 11])) np.testing.assert_equal(cs.child_clusters["c0"], {("c1", 4)}) np.testing.assert_equal(cs.child_clusters["c1"], {("c2", 8)}) np.testing.assert_equal(cs.child_clusters["c2"], set()) np.testing.assert_equal(cs.parent_clusters["c1"], ("c0", 3)) np.testing.assert_equal(cs.parent_clusters["c2"], ("c1", 6))
def get_small_graph(): # Define a connectivity graph G = Graph() # Add edges to graph connectivity_edges = [0, 1, 2, 3, 4, 5] transition_edges = [0, 1, 2, 3, 4, 5] G.add_transition_path(transition_edges) G.add_connectivity_path(connectivity_edges) connectivity_edges = [4, 6, 7, 8] transition_edges = [4, 6, 7, 8] G.add_transition_path(transition_edges) G.add_connectivity_path(connectivity_edges) connectivity_edges = [1, 9, 10, 11, 12, 13] transition_edges = [1, 9, 10, 11, 12, 13] G.add_transition_path(transition_edges) G.add_connectivity_path(connectivity_edges) connectivity_edges = [9, 14, 15, 12] transition_edges = [9, 14, 15, 12] G.add_transition_path(transition_edges) G.add_connectivity_path(connectivity_edges) connectivity_edges = [8, 10] G.add_connectivity_path(connectivity_edges) # Set node positions node_positions = { 0: (0, 0), 1: (0, 1.3), 2: (-0.4, 1.3), 3: (-0.4, 2.2), 4: (-0.7, 2.2), 5: (-0.7, 1), 6: (-1, 2.5), 7: (-1, 3.5), 8: (-0.7, 3.3), 9: (0, 3), 10: (-0.4, 3), 11: (-0.4, 4), 12: (0, 4), 13: (0, 5), 14: (0.4, 3), 15: (0.4, 4), } G.set_node_positions(node_positions) return G
def test_agent_clustering1(): G = Graph() G.add_transition_path(list(range(0, 50))) G.add_connectivity_path(list(range(0, 50))) agent_positions = {0: 0, 1: 1, 2: 35, 3: 30, 4: 25} G.init_agents(agent_positions) cp = ClusterProblem() cp.graph = G cp.static_agents = [0] cp.prepare_problem(remove_dead=False) agent_clusters = agent_clustering(cp, 2) np.testing.assert_equal({0, 1} in map(set, agent_clusters.values()), True) np.testing.assert_equal({2, 3, 4} in map(set, agent_clusters.values()), True)
def test_horiz2(): G = Graph() connectivity_edges = [0, 1, 2, 3] # directed connectivity path (one way) transition_edges = [0, 1, 2, 3] # directed transition path (one way) # Add edges to graph G.add_transition_path(transition_edges) G.add_connectivity_path(connectivity_edges) # Set initial position of agents agent_positions = {0: 0, 1: 1, 2: 3} # agent:position # Init agents in graphs G.init_agents(agent_positions) # Set up the connectivity problem cp = ConnectivityProblem() # Specify graph cp.graph = G # Specify time horizon cp.T = 2 # Specify sources cp.src = [2] cp.static_agents = [0, 2] if import_gurobi(): cp.solve_flow() # positions of robot 0 np.testing.assert_equal(cp.traj[0, 0], 0) np.testing.assert_equal(cp.traj[0, 1], 0) np.testing.assert_equal(cp.traj[0, 2], 0) # positions of robot 1 np.testing.assert_equal(cp.traj[1, 0], 1) np.testing.assert_equal(cp.traj[1, 1], 2) np.testing.assert_equal(cp.traj[1, 2], 1) # positions of robot 2 (fixed) np.testing.assert_equal(cp.traj[2, 0], 3) np.testing.assert_equal(cp.traj[2, 1], 3) np.testing.assert_equal(cp.traj[2, 2], 3)
def test_frontier_weighting(): n = 4 # Define a connectivity graph G = Graph() G.add_transition_path(list(range(n))) G.add_connectivity_path(list(range(n))) G.set_node_positions({i: (0, i) for i in range(n)}) frontiers = {0: 1, 1: 1, 2: 4, 3: 1} G.set_frontiers(frontiers) agent_positions = {0: 0, 1: 2, 2: 3} G.init_agents(agent_positions) # Set up the connectivity problem cp = ConnectivityProblem() cp.graph = G cp.T = 6 cp.static_agents = [] cp.master = 0 cp.src = [2] cp.snk = [1] # Solve if import_gurobi(): cp.solve_flow(master=True, frontier_reward=True, connectivity=True, cut=True) # Check solution for t in range(3): np.testing.assert_equal(cp.traj[0, t], [0, 1, 2, 2][t]) np.testing.assert_equal(cp.traj[1, t], 2) np.testing.assert_equal(cp.traj[2, t], 3) np.testing.assert_equal(cp.conn[0], set()) np.testing.assert_equal(cp.conn[1], set()) np.testing.assert_equal(cp.conn[2], set([(2, 3, "master"), (3, 2, 2)]))
def test_master_comm(): n = 4 # size of graph # Define a connectivity graph G = Graph() G.add_transition_path(list(range(n))) G.add_connectivity_path(list(range(n))) G.set_node_positions({i: (0, i) for i in range(n)}) G.set_frontiers({1: 1}) G.init_agents({0: 0, 1: 2, 2: 3}) # Set up the connectivity problem cp = ConnectivityProblem() cp.graph = G cp.T = 5 cp.static_agents = [] cp.master = 0 cp.src = [2] cp.snk = [1] # Solve with master, without frontier rewards if import_gurobi(): cp.solve_flow(frontier_reward=False, master=True, connectivity=True, cut=False) # Check solution for t in range(6): np.testing.assert_equal(cp.traj[0, t], 0 if t == 0 else 1) np.testing.assert_equal(cp.traj[1, t], 2) np.testing.assert_equal(cp.traj[2, t], 3) np.testing.assert_equal( cp.conn[t], set([(1, 2, "master"), (2, 3, "master"), (3, 2, 2)]) if t == 1 else set(), ) cp.solve_flow(frontier_reward=True, master=True, connectivity=True, cut=False) # Check solution for t in range(6): np.testing.assert_equal(cp.traj[0, t], 0 if t == 0 else 1) np.testing.assert_equal(cp.traj[1, t], 2 if t < 2 else 1) np.testing.assert_equal(cp.traj[2, t], [3, 3, 2, 1, 1, 1][t]) np.testing.assert_equal( cp.conn[t], set([(1, 2, "master"), (2, 3, "master")]) if t == 1 else set()) # Solve without master, without frontier rewards cp.solve_flow(frontier_reward=False, master=False, connectivity=True, cut=False) # Check solution for t in range(6): np.testing.assert_equal(cp.traj[0, t], 0) np.testing.assert_equal(cp.traj[1, t], 2) np.testing.assert_equal(cp.traj[2, t], 3) np.testing.assert_equal(cp.conn[t], set([(3, 2, 2)]) if t == 0 else set()) # Solve without master, with frontier rewards cp.solve_flow(frontier_reward=True, master=False, connectivity=True, cut=False) # Check solution for t in range(6): np.testing.assert_equal(cp.traj[0, t], 0 if t == 0 else 1) np.testing.assert_equal(cp.traj[1, t], 2 if t < 1 else 1) np.testing.assert_equal(cp.traj[2, t], [3, 2, 1, 1, 1, 1][t]) np.testing.assert_equal(cp.conn[t], set())
def get_huge_graph(): G = Graph() # Add edges to graph connectivity_edges = [0, 1, 2, 3, 4, 5] transition_edges = [0, 1, 2, 3, 4, 5] G.add_transition_path(transition_edges) G.add_connectivity_path(connectivity_edges) connectivity_edges = [4, 6, 7, 8] transition_edges = [4, 6, 7, 8] G.add_transition_path(transition_edges) G.add_connectivity_path(connectivity_edges) connectivity_edges = [1, 9, 10, 11, 12, 13] transition_edges = [1, 9, 10, 11, 12, 13] G.add_transition_path(transition_edges) G.add_connectivity_path(connectivity_edges) connectivity_edges = [9, 14, 15, 12] transition_edges = [9, 14, 15, 12] G.add_transition_path(transition_edges) G.add_connectivity_path(connectivity_edges) connectivity_edges = [1, 16, 17, 18, 19, 20, 21, 22, 23, 24] transition_edges = [1, 16, 17, 18, 19, 20, 21, 22, 23, 24] G.add_transition_path(transition_edges) G.add_connectivity_path(connectivity_edges) connectivity_edges = [8, 10] G.add_connectivity_path(connectivity_edges) connectivity_edges = [15, 24] G.add_connectivity_path(connectivity_edges) connectivity_edges = [11, 25, 26, 27, 28, 29, 30, 31] transition_edges = [11, 25, 26, 27, 28, 29, 30, 31] G.add_transition_path(transition_edges) G.add_connectivity_path(connectivity_edges) connectivity_edges = [29, 32, 33, 34, 35] transition_edges = [29, 32, 33, 34, 35] G.add_transition_path(transition_edges) G.add_connectivity_path(connectivity_edges) connectivity_edges = [22, 36, 37, 38, 39, 40, 41] transition_edges = [22, 36, 37, 38, 39, 40, 41] G.add_transition_path(transition_edges) G.add_connectivity_path(connectivity_edges) connectivity_edges = [39, 42, 43, 44, 45] transition_edges = [39, 42, 43, 44, 45] G.add_transition_path(transition_edges) G.add_connectivity_path(connectivity_edges) connectivity_edges = [5, 46, 47, 48, 49] transition_edges = [5, 46, 47, 48, 49] G.add_transition_path(transition_edges) G.add_connectivity_path(connectivity_edges) connectivity_edges = [15, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60] transition_edges = [15, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60] G.add_transition_path(transition_edges) G.add_connectivity_path(connectivity_edges) connectivity_edges = [55, 61, 62, 63, 64, 65, 66, 67] transition_edges = [55, 61, 62, 63, 64, 65, 66, 67] G.add_transition_path(transition_edges) G.add_connectivity_path(connectivity_edges) connectivity_edges = [46, 68, 69, 70, 71, 72, 73, 74, 75, 76, 33] transition_edges = [46, 68, 69, 70, 71, 72, 73, 74, 75, 76, 33] G.add_transition_path(transition_edges) G.add_connectivity_path(connectivity_edges) connectivity_edges = [75, 77, 78, 27] transition_edges = [75, 77, 78, 27] G.add_transition_path(transition_edges) G.add_connectivity_path(connectivity_edges) connectivity_edges = [76, 79, 80, 81, 82, 83, 84, 85, 86, 87] transition_edges = [76, 79, 80, 81, 82, 83, 84, 85, 86, 87] G.add_transition_path(transition_edges) G.add_connectivity_path(connectivity_edges) connectivity_edges = [36, 88, 89, 90, 91, 92, 93, 94, 95] transition_edges = [36, 88, 89, 90, 91, 92, 93, 94, 95] G.add_transition_path(transition_edges) G.add_connectivity_path(connectivity_edges) connectivity_edges = [24, 96, 97, 98, 99] transition_edges = [24, 96, 97, 98, 99] G.add_transition_path(transition_edges) G.add_connectivity_path(connectivity_edges) # Set node positions node_positions = { 0: (0, 0), 1: (0, 1), 2: (-2, 1), 3: (-2, 2), 4: (-3, 2), 5: (-3, 1), 6: (-3, 3), 7: (-3.5, 3.5), 8: (-2.5, 3.5), 9: (0, 3), 10: (-1.8, 3), 11: (-1.6, 4), 12: (0, 4), 13: (0, 5), 14: (1, 3), 15: (1, 4), 16: (1.5, 1), 17: (2.5, 1.3), 18: (4, 1.3), 19: (5, 1.3), 20: (5, 2), 21: (4, 3), 22: (5, 3), 23: (5, 4), 24: (3.5, 4), 25: (-1.6, 5), 26: (-1.7, 6), 27: (-2, 6.5), 28: (-2, 7.5), 29: (-1.5, 8.5), 30: (-1.5, 10), 31: (-1.5, 11), 32: (-2.5, 8.8), 33: (-3.5, 9), 34: (-4, 10), 35: (-4.5, 11), 36: (6, 3), 37: (6.5, 3.8), 38: (7.2, 4.5), 39: (7.5, 5.5), 40: (7, 6), 41: (7, 7), 42: (8.5, 6), 43: (9.5, 6.5), 44: (10, 7), 45: (11, 7), 46: (-4, 1), 47: (-4.5, 2), 48: (-5, 3), 49: (-5, 4), 50: (1, 5), 51: (1.2, 6), 52: (1.5, 7), 53: (2, 8), 54: (2, 9), 55: (3, 10), 56: (4, 10.5), 57: (5, 11), 58: (6, 11), 59: (6.5, 12), 60: (7, 13), 61: (2.5, 11), 62: (2, 12), 63: (2, 13), 64: (1, 14), 65: (0, 15), 66: (-1, 15.5), 67: (-2.5, 15), 68: (-5, 1), 69: (-6, 1), 70: (-7, 2), 71: (-7, 3), 72: (-7.5, 4), 73: (-7.5, 5), 74: (-6, 6), 75: (-5, 7), 76: (-4, 8), 77: (-4, 6), 78: (-3, 6), 79: (-5, 8), 80: (-5, 9.2), 81: (-5.5, 10), 82: (-6, 11), 83: (-7, 11), 84: (-7, 12), 85: (-6.5, 13), 86: (-6, 14), 87: (-7, 15), 88: (7, 3), 89: (8, 3), 90: (9, 4), 91: (9.5, 5), 92: (10, 5), 93: (11, 6), 94: (12, 7), 95: (13, 8), 96: (3.5, 5), 97: (4, 6), 98: (4, 7), 99: (4.5, 8), } G.set_node_positions(node_positions) # Set small nodes small_nodes = [v for v in node_positions] small_nodes.remove(0) G.set_small_node(small_nodes) return G
def get_medium_graph(): # Define a connectivity graph G = Graph() # Add edges to graph connectivity_edges = [0, 1, 2, 3, 4, 5] transition_edges = [0, 1, 2, 3, 4, 5] G.add_transition_path(transition_edges) G.add_connectivity_path(connectivity_edges) connectivity_edges = [4, 6, 7, 8] transition_edges = [4, 6, 7, 8] G.add_transition_path(transition_edges) G.add_connectivity_path(connectivity_edges) connectivity_edges = [1, 9, 10, 11, 12, 13] transition_edges = [1, 9, 10, 11, 12, 13] G.add_transition_path(transition_edges) G.add_connectivity_path(connectivity_edges) connectivity_edges = [9, 14, 15, 12] transition_edges = [9, 14, 15, 12] G.add_transition_path(transition_edges) G.add_connectivity_path(connectivity_edges) connectivity_edges = [1, 16, 17, 18, 19, 20, 21, 22, 23, 24] transition_edges = [1, 16, 17, 18, 19, 20, 21, 22, 23, 24] G.add_transition_path(transition_edges) G.add_connectivity_path(connectivity_edges) connectivity_edges = [8, 10] G.add_connectivity_path(connectivity_edges) connectivity_edges = [15, 24] G.add_connectivity_path(connectivity_edges) # Set node positions node_positions = { 0: (0, 0), 1: (0, 1), 2: (-2, 1), 3: (-2, 2), 4: (-3, 2), 5: (-3, 1), 6: (-3, 3), 7: (-3.5, 3.5), 8: (-2.5, 3.5), 9: (0, 3), 10: (-1.8, 3), 11: (-1.6, 4), 12: (0, 4), 13: (0, 5), 14: (1, 3), 15: (1, 4), 16: (1.5, 1), 17: (2.5, 1.3), 18: (4, 1.3), 19: (5, 1.3), 20: (5, 2), 21: (4, 3), 22: (5, 3), 23: (5, 4), 24: (3.5, 4), } G.set_node_positions(node_positions) return G
def test_to_frontier(): G = Graph() # Add edges to graph connectivity_edges = [0, 1, 2, 3, 4, 5] transition_edges = [0, 1, 2, 3, 4, 5] G.add_transition_path(transition_edges) G.add_connectivity_path(connectivity_edges) connectivity_edges = [4, 6, 7, 8] transition_edges = [4, 6, 7, 8] G.add_transition_path(transition_edges) G.add_connectivity_path(connectivity_edges) connectivity_edges = [1, 9, 10, 11, 12, 13] transition_edges = [1, 9, 10, 11, 12, 13] G.add_transition_path(transition_edges) G.add_connectivity_path(connectivity_edges) connectivity_edges = [9, 14, 15, 12] transition_edges = [9, 14, 15, 12] G.add_transition_path(transition_edges) G.add_connectivity_path(connectivity_edges) connectivity_edges = [1, 16, 17, 18, 19, 20, 21, 22, 23, 24] transition_edges = [1, 16, 17, 18, 19, 20, 21, 22, 23, 24] G.add_transition_path(transition_edges) G.add_connectivity_path(connectivity_edges) connectivity_edges = [8, 10] G.add_connectivity_path(connectivity_edges) connectivity_edges = [15, 24] G.add_connectivity_path(connectivity_edges) # Set node positions node_positions = { 0: (0, 0), 1: (0, 1), 2: (-2, 1), 3: (-2, 2), 4: (-3, 2), 5: (-3, 1), 6: (-3, 3), 7: (-3.5, 3.5), 8: (-2.5, 3.5), 9: (0, 3), 10: (-1.8, 3), 11: (-1.6, 4), 12: (0, 4), 13: (0, 5), 14: (1, 3), 15: (1, 4), 16: (1.5, 1), 17: (2.5, 1.3), 18: (4, 1.3), 19: (5, 1.3), 20: (5, 2), 21: (4, 3), 22: (5, 3), 23: (5, 4), 24: (3.5, 4), } G.set_node_positions(node_positions) frontiers = {6: 1, 13: 1, 22: 1} G.set_frontiers(frontiers) G.init_agents({0: 0, 1: 1, 2: 2, 3: 14}) # CLUSTERING cp = ClusterProblem() cp.graph = G cp.k = 2 # number of desired clusters cp.master = 0 cp.static_agents = [0] if import_gurobi(): cp.solve_to_frontier_problem(soft=True) end_pos = set([cp.traj[r, cp.T_sol] for r in range(4)]) num_front = len(end_pos & set(frontiers.keys())) np.testing.assert_array_less(3, num_front + 0.5) # all three frontiers