コード例 #1
0
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)))
コード例 #2
0
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))
コード例 #3
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))
コード例 #4
0
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)
コード例 #5
0
def test_agent_clustering2():
    G = Graph()
    G.add_transition_path(list(range(0, 25)))
    G.add_transition_path(list(range(26, 50)))
    G.add_transition_path([0, 26])

    agent_positions = {0: 0, 1: 3, 2: 27, 3: 20, 4: 24, 5: 42, 6: 47}
    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, 3)

    np.testing.assert_equal({0, 1, 2} in map(set, agent_clusters.values()), True)
    np.testing.assert_equal({3, 4} in map(set, agent_clusters.values()), True)
    np.testing.assert_equal({5, 6} in map(set, agent_clusters.values()), True)
コード例 #6
0
from cops.clustering import ClusterProblem
from cops.animate import animate_cluster

from graph_examples import get_medium_graph

G = get_medium_graph()

frontiers = {6: 1, 13: 1, 22: 1}
G.set_frontiers(frontiers)

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

master = 0
static_agents = [0]

# CLUSTERING
cp = ClusterProblem()
cp.graph = G
cp.num_clusters = 2  # number of clusters to create
cp.master = master
cp.static_agents = static_agents
tofront = cp.solve_to_frontier_problem(soft=True)

animate_cluster(G, cp.traj, cp.conn, tofront.cs.subgraphs)
コード例 #7
0
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
コード例 #8
0
    G.set_frontiers(frontiers)

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

    # Process1-TRAVERSE TO FRONTIERS-----------------------------------------
    # CLUSTERING
    print()
    print(Style.BRIGHT + Fore.BLUE +
          "Solving to frontier problem on {} known nodes".format(len(g1)) +
          Style.RESET_ALL)
    cp1 = ClusterProblem()
    cp1.graph = g1
    cp1.master = master
    cp1.static_agents = [r for r in static_agents]
    # cp1.big_agents = eagents
    cp1.eagents = eagents
    cp1.graph.init_agents(agent_positions)
    tofront_data = cp1.solve_to_frontier_problem(verbose=True,
                                                 soft=True,
                                                 dead=True)
    agent_positions = {r: cp1.traj[(r, cp1.T_sol)] for r in cp1.graph.agents}

    problem_list.append(cp1)

    # Process2-EXPLORE FRONTIERS---------------------------------------------
    ep = ExplorationProblem()