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
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
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)
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]
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]
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"}
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]
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
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}"
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]
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)
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)
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) }
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}]" )