コード例 #1
0
    def _build_position_node_map(self, ncols: int, nrows: int):
        pos_node_map = {}
        for col in range(0, ncols):
            for row in range(0, nrows):
                pos = Vector2(row, col)
                pos_node_map[pos] = Node(str(pos), pos)

        return pos_node_map
コード例 #2
0
ファイル: tests_graph.py プロジェクト: tylertjburns/coopgraph
    def test_add_node_with_connnections__base(self):
        a = Node(name='A', pos=Vector2(0, 0))
        b = Node(name='B', pos=Vector2(3, 3))
        c = Node(name='C', pos=Vector2(2, 0))
        d = Node(name='D', pos=Vector2(2, 1))
        e = Node(name='E', pos=Vector2(3, 4))
        f = Node(name='F', pos=Vector2(5, 5))

        g = {a: [d], b: [c], c: [b, d, e], d: [a, b, c], e: [c], f: []}

        graph = Graph(g)

        h = Node('H', Vector2(100, 100))

        graph.add_node_with_connnections(h, {
            a: EdgeDirection.FROM,
            b: EdgeDirection.TO,
            c: EdgeDirection.TWOWAY
        })

        assert h in graph.nodes

        assert graph.edge_between(a, h) is not None
        assert graph.edge_between(h, a) is None
        assert graph.edge_between(b, h) is None
        assert graph.edge_between(h, b) is not None
        assert graph.edge_between(c, h) is not None
        assert graph.edge_between(h, c) is not None
コード例 #3
0
ファイル: tests_graph.py プロジェクト: tylertjburns/coopgraph
    def init_a_test_graph(self):
        a = Node(name='A', pos=Vector2(0, 0))
        b = Node(name='B', pos=Vector2(3, 3))
        c = Node(name='C', pos=Vector2(2, 0))
        d = Node(name='D', pos=Vector2(2, 1))
        e = Node(name='E', pos=Vector2(3, 4))
        f = Node(name='F', pos=Vector2(5, 5))

        g = {a: [d], b: [c], c: [b, d, e], d: [a, c], e: [c, f], f: []}

        return Graph(g)
コード例 #4
0
ファイル: tests_graph.py プロジェクト: tylertjburns/coopgraph
    def test_find_isolated_vertices(self):
        a = Node(name='A', pos=Vector2(0, 0))
        b = Node(name='B', pos=Vector2(3, 3))
        c = Node(name='C', pos=Vector2(2, 0))
        d = Node(name='D', pos=Vector2(2, 1))
        e = Node(name='E', pos=Vector2(3, 4))
        f = Node(name='F', pos=Vector2(5, 5))

        g = {a: [d], b: [c], c: [b, d, e], d: [a, c], e: [c], f: []}

        graph = Graph(g)
        assert graph.find_isolated_vertices() == [f]
コード例 #5
0
ファイル: tests_graph.py プロジェクト: tylertjburns/coopgraph
    def test_nodes_at__multiple(self):
        a = Node(name='A', pos=Vector2(0, 0))
        b = Node(name='B', pos=Vector2(3, 3))
        c = Node(name='C', pos=Vector2(2, 0))
        d = Node(name='D', pos=Vector2(2, 1))
        e = Node(name='E', pos=Vector2(3, 4))
        f = Node(name='F', pos=Vector2(5, 5))
        h = Node(name='H', pos=Vector2(0, 0))

        g = {a: [d], b: [c], c: [b, d, e], d: [a, c], e: [c], f: [], h: []}

        graph = Graph(g)

        nodes = graph.nodes_at_point(Vector2(0, 0))

        assert nodes == [a, h]
コード例 #6
0
ファイル: tests_graph.py プロジェクト: tylertjburns/coopgraph
    def test_disable_edges(self):
        a = Node(name='A', pos=Vector2(0, 0))
        b = Node(name='B', pos=Vector2(3, 3))
        c = Node(name='C', pos=Vector2(2, 0))
        d = Node(name='D', pos=Vector2(2, 1))
        e = Node(name='E', pos=Vector2(3, 4))
        f = Node(name='F', pos=Vector2(5, 5))

        g = {a: [d], b: [c], c: [b, d, e], d: [a, b, c], e: [c, f], f: []}

        graph = Graph(g)
        graph.disable_edges(graph.edge_between(d, c), "BLOCK")
        assert graph.edge_between(d, c).disablers() == {"BLOCK"}
コード例 #7
0
ファイル: tests_graph.py プロジェクト: tylertjburns/coopgraph
    def test_nodes(self):
        a = Node(name='A', pos=Vector2(0, 0))
        b = Node(name='B', pos=Vector2(3, 3))
        c = Node(name='C', pos=Vector2(2, 0))
        d = Node(name='D', pos=Vector2(2, 1))
        e = Node(name='E', pos=Vector2(3, 4))
        f = Node(name='F', pos=Vector2(5, 5))

        g = {a: [d], b: [c], c: [b, d, e], d: [a, c], e: [c, f], f: []}

        graph = Graph(g)

        assert len(graph.nodes) == 6
        assert graph.nodes == [a, b, c, d, e, f]
コード例 #8
0
ファイル: tests_graph.py プロジェクト: tylertjburns/coopgraph
    def test_astar_no_valid_path(self):
        a = Node(name='A', pos=Vector2(0, 0))
        b = Node(name='B', pos=Vector2(3, 3))
        c = Node(name='C', pos=Vector2(2, 0))
        d = Node(name='D', pos=Vector2(2, 1))
        e = Node(name='E', pos=Vector2(3, 4))
        f = Node(name='F', pos=Vector2(5, 5))

        g = {a: [d], b: [c], c: [b, d, e], d: [a, b, c], e: [c], f: []}

        graph = Graph(g)
        path = graph.astar(a, f)

        assert path.path is None
コード例 #9
0
ファイル: tests_graph.py プロジェクト: tylertjburns/coopgraph
    def test_closest_node__base(self):
        a = Node(name='A', pos=Vector2(0, 0))
        b = Node(name='B', pos=Vector2(3, 3))
        c = Node(name='C', pos=Vector2(2, 0))
        d = Node(name='D', pos=Vector2(2, 1))
        e = Node(name='E', pos=Vector2(3, 4))
        f = Node(name='F', pos=Vector2(5, 5))

        g = {a: [d], b: [c], c: [b, d, e], d: [a, b, c], e: [c], f: []}

        graph = Graph(g)

        test = Vector2(0, 1)
        closest = graph.closest_nodes(test)[0]
        assert closest == a
        f"{closest}"
コード例 #10
0
ファイル: tests_graph.py プロジェクト: tylertjburns/coopgraph
    def test__astar_with_disablers(self):
        a = Node(name='A', pos=Vector2(0, 0))
        b = Node(name='B', pos=Vector2(3, 3))
        c = Node(name='C', pos=Vector2(2, 0))
        d = Node(name='D', pos=Vector2(2, 1))
        e = Node(name='E', pos=Vector2(3, 4))
        f = Node(name='F', pos=Vector2(5, 5))

        g = {a: [d], b: [c], c: [b, d, e], d: [a, b, c], e: [c, f], f: []}

        graph = Graph(g)
        graph.disable_edges(graph.edge_between(d, c), "BLOCK")

        path = graph.astar(a, e)

        assert path.path == [a, d, b, c, e]
コード例 #11
0
ファイル: tests_graph.py プロジェクト: tylertjburns/coopgraph
    def test__path_length(self):
        a = Node(name='A', pos=Vector2(0, 0))
        b = Node(name='B', pos=Vector2(3, 3))
        c = Node(name='C', pos=Vector2(2, 0))
        d = Node(name='D', pos=Vector2(2, 1))
        e = Node(name='E', pos=Vector2(3, 4))
        f = Node(name='F', pos=Vector2(5, 5))

        g = {a: [d], b: [c], c: [b, d, e], d: [a, c], e: [c, f], f: []}

        graph = Graph(g)
        path = [a, d, c, e]
        length = graph.path_length(path)

        assert length == d.pos.distance_from(a.pos) + c.pos.distance_from(
            d.pos) + e.pos.distance_from(c.pos)
コード例 #12
0
ファイル: tests_graph.py プロジェクト: tylertjburns/coopgraph
    def test_copy__base(self):
        a = Node(name='A', pos=Vector2(0, 0))
        b = Node(name='B', pos=Vector2(3, 3))
        c = Node(name='C', pos=Vector2(2, 0))
        d = Node(name='D', pos=Vector2(2, 1))
        e = Node(name='E', pos=Vector2(3, 4))
        f = Node(name='F', pos=Vector2(5, 5))

        g = {a: [d], b: [c], c: [b, d, e], d: [a, b, c], e: [c], f: []}

        graph = Graph(g)

        copy = graph.copy()

        assert len(copy.nodes) == len(graph.nodes)
        assert len(copy.edges) == len(graph.edges)
        assert (edge.disablers() == graph.edge_between(edge.start,
                                                       edge.end).disablers()
                for edge in copy.edges)
コード例 #13
0
ファイル: tests_graph.py プロジェクト: tylertjburns/coopgraph
    def test_edges_to_node(self):
        a = Node(name='A', pos=Vector2(0, 0))
        b = Node(name='B', pos=Vector2(3, 3))
        c = Node(name='C', pos=Vector2(2, 0))
        d = Node(name='D', pos=Vector2(2, 1))
        e = Node(name='E', pos=Vector2(3, 4))
        f = Node(name='F', pos=Vector2(5, 5))

        g = {a: [d], b: [c], c: [b, d, e], d: [a, c], e: [c, f], f: []}

        graph = Graph(g)

        assert set(graph.edges_to_node(c)) == {
            graph.edge_between(b, c),
            graph.edge_between(d, c),
            graph.edge_between(e, c),
            graph.edge_between(c, b),
            graph.edge_between(c, d),
            graph.edge_between(c, e)
        }
コード例 #14
0
ファイル: tests_graph.py プロジェクト: tylertjburns/coopgraph
    def test__edge_by_id(self):
        a = Node(name='A', pos=Vector2(0, 0))
        b = Node(name='B', pos=Vector2(3, 3))
        c = Node(name='C', pos=Vector2(2, 0))
        d = Node(name='D', pos=Vector2(2, 1))
        e = Node(name='E', pos=Vector2(3, 4))
        f = Node(name='F', pos=Vector2(5, 5))

        g = {a: [d], b: [c], c: [b, d, e], d: [a, b, c], e: [c], f: []}

        graph = Graph(g)

        edge = graph.edge_between(a, d)

        self.assertIsNotNone(edge,
                             "Edge was returned none when expected value")

        edge_ret = graph.edges_by_id([edge.id])[0]

        self.assertEqual(
            edge, edge_ret,
            f"returned edge {edge_ret} [{edge_ret.id}] does not match {edge} [{edge.id}]"
        )