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)]))
Example #2
0
    def subproblem(self, c, clusterproblem):
        # return basic connectivityproblem
        #  (graph, agents, eagents, big_agents, reward_dict)
        # induced by cluster c

        agents = {
            r: clusterproblem.graph.agents[r]
            for r in self.agent_clusters[c]
        }

        static_agents = [
            r for r in agents.keys() if r in clusterproblem.static_agents
        ]

        # add childcluster stuff
        addn_nodes = set()
        for C in self.child_clusters[c]:
            agents[self.submasters[C[0]]] = C[1]
            static_agents.append(self.submasters[C[0]])
            addn_nodes.add(C[1])

        G = deepcopy(clusterproblem.graph)
        del_nodes = set(
            clusterproblem.graph.nodes) - self.subgraphs[c] - addn_nodes
        G.remove_nodes_from(del_nodes)
        G.init_agents(agents)

        # basic rewards based on centrality
        reward_dict = betweenness_centrality(nx.DiGraph(G))
        norm = max(reward_dict.values())
        if norm == 0:
            norm = 1
        reward_dict = {
            v: clusterproblem.max_centrality_reward * val / norm
            for v, val in reward_dict.items()
        }

        # initialize subproblem
        return ConnectivityProblem(
            graph=G,
            static_agents=static_agents,
            eagents=[r for r in agents if r in clusterproblem.eagents],
            big_agents=[r for r in agents if r in clusterproblem.big_agents],
            reward_dict=reward_dict,
        )
Example #3
0
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)
Example #4
0
from cops.problem import ConnectivityProblem
from cops.animate import animate

from graph_examples import get_linear_graph

n = 4

G = get_linear_graph(n)

frontiers = {i: 1 for i in range(3)}
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
cp.solve_flow(master=True, frontier_reward=True, connectivity=True, cut=True, solver='gurobi')

# Animate solution
animate(G, cp.traj, cp.conn)
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())
Example #6
0
G = get_medium_graph()

# Set small nodes
small_nodes = [1]
G.set_small_node(small_nodes)

frontiers = {2: 1, 14: 1}
G.set_frontiers(frontiers)

# Set initial position of agents
agent_positions = {0: 0, 1: 1, 2: 1}  # agent:position
G.init_agents(agent_positions)

# Set up the connectivity problem
cp = ConnectivityProblem()
cp.graph = G  # graph
cp.T = 7  # time
cp.master = [0]  # master_agent
cp.static_agents = [0]  # static agents
cp.big_agents = [0, 1, 2]

# Define sources and sinks as subsets of agents
cp.src = []
cp.snk = []

# Solve
cp.solve_flow(master=True, connectivity=True)

# Animate solution
animate(G, cp.traj, cp.conn)
Example #7
0
from cops.animate import animate

from examples.graph_examples import get_small_graph

G = get_small_graph()

# Set small nodes
small_nodes = [G.nodes]
G.set_small_node(small_nodes)

frontiers = {}
G.set_frontiers(frontiers)

# Set initial position of agents
agent_positions = {0: 0, 1: 3, 2: 11, 3: 6}  # agent:position
G.init_agents(agent_positions)

# Set up the connectivity problem
cp = ConnectivityProblem()
cp.graph = G  # graph
cp.T = 5  # time
cp.master = [0]  # master_agent
cp.static_agents = []  # static agents
cp.big_agents = [0, 1, 2, 3]

# Solve
cp.solve_flow(master=True, connectivity=True, frontier_reward = False)

# Animate solution
animate(G, cp.traj, cp.conn, save_static_figures = False)
Example #8
0
static_agents = [0]

# MAIN-LOOP----------------------------------------------------------------------
while not G.is_known():

    # find frontiers
    frontiers = {v: 1 for v in G.nodes if G.is_frontier(v)}
    G.set_frontiers(frontiers)

    # create sub-graph
    g = deepcopy(G)
    unknown = [v for v in G.nodes if not G.nodes[v]["known"]]
    g.remove_nodes_from(unknown)

    # Process1-TRAVERSE TO FRONTIERS-------------------------------------------------
    cp1 = ConnectivityProblem()
    cp1.graph = g  # graph
    cp1.master = master  # master_agent
    cp1.static_agents = static_agents  # static agents
    cp1.graph.agents = agent_positions
    cp1.src = []
    cp1.snk = []
    cp1.diameter_solve_flow(master=True, connectivity=False)
    agent_positions = {r: cp1.traj[(r, cp1.T_sol)] for r in cp1.graph.agents}
    problem_list.append(cp1)

    # Process2-EXPLORE FRONTIERS-----------------------------------------------------
    ep = ExplorationProblem()
    ep.graph = G  # full graph
    ep.T = 8  # exploration time
    ep.static_agents = static_agents  # static agents