def create_graph(self): graph = Graph(directed=False, multiedged=False) for i, level in enumerate(self._field): for j, cell, in enumerate(level): start = i, j ends = [i for i in self.get_environment(i, j)] for valid_end in (end for end in ends if self.is_valid(*end)): graph.add_edge(start, valid_end) return graph
def make_graph(self): """ Строит граф шестиугольного поля и возвращает его. """ graph = Graph(directed=False, multiedged=False) middle = len(self.field) // 2 for i, level in enumerate(self.field): for j, cell, in enumerate(level): start = i, j ends = {(i + 1, j), (i, j + 1), (i + 1, j + 1) if i < middle else (i + 1, j - 1)} for valid_end in (end for end in ends if self.is_valid(*end)): graph.add_edge(start, valid_end) return graph
from graph_tools import Graph g = Graph(directed=True) ok(g) ok(g.is_directed()) ok(not g.is_undirected()) g.add_vertices(1, 2, 3, 4) ok(g.has_vertex(1)) ok(g.has_vertex(2)) ok(g.has_vertex(3)) ok(g.has_vertex(4)) ok(not g.has_vertex(5)) ok(not g.has_vertex(0)) g.add_edge(1, 2) g.add_edge(1, 2) g.add_edge(2, 3) g.add_edge(3, 4) ok(g.has_edge(1, 2)) ok(g.has_edge(2, 3)) ok(not g.has_edge(2, 1)) ok(not g.has_edge(1, 3)) ok(not g.has_edge(4, 1)) eq(len(g.vertices()), 4) eq(len(g.successors(1)), 1) eq(len(g.predecessors(1)), 0) eq(len(g.neighbors(1)), 1) eq(len(g.neighbors(2)), 2)
#!/usr/bin/env python3 from test_more import ok, eq from graph_tools import Graph g = Graph(directed=True) eq(len(g.get_multiedge_ids(1, 2)), 0) g.add_edge(1, 2) g.add_edge(2, 1) ok(g.has_edge(1, 2)) ok(g.has_edge(2, 1)) eq(len(g.get_multiedge_ids(1, 2)), 1) eq(len(g.get_multiedge_ids(2, 1)), 1) eq(len(g.neighbors(1)), 1) eq(len(g.neighbors(2)), 1) g.add_edge(1, 2) ok(g.has_edge(1, 2)) ok(g.has_edge(2, 1)) eq(len(g.get_multiedge_ids(1, 2)), 2) eq(len(g.get_multiedge_ids(2, 1)), 1) eq(len(g.neighbors(1)), 1) eq(len(g.neighbors(2)), 1)
def test_make_graph(self): expected = Graph(directed=False) # Top-Down expected.add_edge((0, 0), (1, 0)) expected.add_edge((1, 0), (2, 0)) expected.add_edge((0, 1), (1, 1)) expected.add_edge((1, 1), (2, 1)) # Left-Right expected.add_edge((0, 0), (0, 1)) expected.add_edge((1, 0), (1, 1)) expected.add_edge((1, 1), (1, 2)) expected.add_edge((2, 0), (2, 1)) # Diagonal expected.add_edge((0, 0), (1, 1)) expected.add_edge((0, 1), (1, 2)) expected.add_edge((2, 0), (1, 1)) expected.add_edge((2, 1), (1, 2)) actual = self.simple_game.make_graph() self.assertSetEqual(set(expected.vertices()), set(actual.vertices())) self.assertSetEqual(set(frozenset(edge) for edge in expected.edges()), set(frozenset(edge) for edge in actual.edges()))
#!/usr/bin/env python3 from test_more import ok, eq from graph_tools import Graph g = Graph(directed=True, multiedged=True) g.add_edge(1, 2) g.add_edge(2, 3) g.add_edge(3, 1) g.set_vertex_attribute(1, 'foo', 123) g.set_edge_attribute_by_id(1, 2, 0, 'bar', 456) T = g.copy_graph() ok(T.directed()) ok(T.multiedged()) ok(T.has_edge(1, 2)) ok(not T.has_edge(2, 1)) eq(T.get_vertex_attribute(1, 'foo'), 123) eq(T.get_edge_attribute_by_id(1, 2, 0, 'bar'), 456) g = Graph(directed=False, multiedged=True) g.add_edge(1, 2) g.add_edge(2, 3) g.add_edge(3, 1) g.set_vertex_attribute(1, 'foo', 123) g.set_edge_attribute_by_id(1, 2, 0, 'bar', 456) T = g.copy_graph() ok(T.undirected()) ok(T.multiedged()) ok(T.has_edge(1, 2)) ok(T.has_edge(2, 1)) eq(T.get_vertex_attribute(1, 'foo'), 123)
class SolverTest(unittest.TestCase): def setUp(self): self.test_solver = solver.Solver() self.field1 = TriangleField([[1], [1, 0, 2], [3, 0, 3, 0, 2]]) self.field2 = TriangleField([[1], [1, 0, 2], [1, 0, 3, 0, 2], [3, 0, 0, 0, 4, 0, 4]]) self.field3 = TriangleField([[1], [1, 0, 2]]) self.graph = Graph(directed=False) self.graph.add_edge('a', 'b') self.graph.add_edge('a', 'c') self.graph.add_edge('c', 'b') self.graph.add_edge('b', 'd') def test_update_neighbors(self): self.method_tester( expected={ 'a': 'b', 'b': 'a', 'c': 'c', 'd': 'd' }, parent_node=Node(self.graph.edges()[0], {v: v for v in self.graph.vertices()}, 1), main_path=list('abcd')) self.method_tester(expected={ 'b': 'c', 'c': 'b', 'd': 'd' }, parent_node=Node(self.graph.edges()[1], { 'a': 'b', 'b': 'a', 'c': 'c', 'd': 'd' }, 1), main_path=list('bcd')) self.method_tester(expected={ 'b': 0, 'd': 'd' }, parent_node=Node(self.graph.edges()[2], { 'b': 'a', 'c': 'c', 'd': 'd' }, 1), main_path=list('bd')) def method_tester(self, expected, parent_node, main_path): actual = self.test_solver.update_main_path( self.test_solver.update_neighbors(parent_node), main_path) self.assertDictEqual(expected, actual) def test_solve(self): expected = [[(0, 0), (1, 1)], [(1, 0), (1, 1)], [(1, 2), (2, 3)], [(2, 0), (2, 1)], [(2, 1), (2, 2)], [(2, 3), (2, 4)]] actual = list(self.test_solver.solve(self.field1)) assert len(actual) == 64 self.assertListEqual(expected, actual[0]) expected = [] actual = list(self.test_solver.solve(self.field2)) self.assertListEqual(expected, actual) actual = list(self.test_solver.solve(self.field3)) self.assertListEqual(expected, actual)
#!/usr/bin/env python3 from test_more import ok, eq from graph_tools import Graph g = Graph(directed=False) g.add_vertices(1, 2, 3) g.add_edge(1, 2) g.add_edge(2, 3) ok(g.betweenness(1) == 0) ok(g.betweenness(2) == 2) ok(g.betweenness(3) == 0) """ 2 / \ 1 -- 3 --4 """ g = Graph(directed=False) g.add_vertices(1, 2, 3, 4) g.add_edge(1, 2) g.add_edge(2, 3) g.add_edge(1, 3) g.add_edge(3, 4) ok(g.betweenness(1) == 0) ok(g.betweenness(2) == 0) ok(g.betweenness(3) == 4) ok(g.betweenness(4) == 0) """ 2 // \
class SolverTest(unittest.TestCase): def setUp(self): self.instance_one_solution = HexLink([ [1, 0], # 1 0 [0, 2, 1], # 0 2 1 [0, 2] # 0 2 ]) self.instance_many_solutions = HexLink([ [1, 2], # 1 2 [0, 0, 0], # 0 0 0 [1, 2], # 1 2 ]) self.instance_no_solutions = HexLink([ [1, 2], # 1 2 [0, 0, 0], # 0 0 0 [2, 1] # 2 1 ]) self.graph = Graph(directed=False) self.graph.add_edge("p", "q") # e_1 self.graph.add_edge("p", "r") # e_2 self.graph.add_edge("r", "q") # e_3 self.graph.add_edge("q", "s") # e_4 def test_update_mate(self): self.update_mate_tester( expected={ "p": "q", "q": "p", "r": "r", "s": "s" }, parent_node=Node(self.graph.edges()[0], {v: v for v in self.graph.vertices()}, 1), domain=list("pqrs")) self.update_mate_tester(expected={ "q": "r", "r": "q", "s": "s" }, parent_node=Node(self.graph.edges()[1], { "p": "q", "q": "p", "r": "r", "s": "s" }, 1), domain=list("qrs")) self.update_mate_tester(expected={ "q": 0, "s": "s" }, parent_node=Node(self.graph.edges()[2], { "q": "p", "r": "r", "s": "s" }, 1), domain=list("qs")) def update_mate_tester(self, expected, parent_node, domain): actual = update_domain(update_mate(parent_node), domain) self.assertDictEqual(expected, actual) def test_solve_one_solution(self): expected = [[(0, 0), (0, 1)], [(0, 1), (1, 2)], [(1, 0), (1, 1)], [(1, 0), (2, 0)], [(2, 0), (2, 1)]] actual = list(solve(self.instance_one_solution)) self.assertTrue(len(actual) == 1) self.assertListEqual(expected, actual[0]) def test_solve_many_solutions(self): expected = [[[(0, 0), (1, 0)], [(0, 1), (1, 1)], [(1, 0), (2, 0)], [(1, 1), (1, 2)], [(1, 2), (2, 1)]], [[(0, 0), (1, 0)], [(0, 1), (1, 2)], [(1, 0), (2, 0)], [(1, 1), (1, 2)], [(1, 1), (2, 1)]], [[(0, 0), (1, 0)], [(0, 1), (1, 2)], [(1, 0), (1, 1)], [(1, 1), (2, 0)], [(1, 2), (2, 1)]], [[(0, 0), (1, 1)], [(0, 1), (1, 2)], [(1, 0), (1, 1)], [(1, 0), (2, 0)], [(1, 2), (2, 1)]]] actual = list(solve(self.instance_many_solutions)) self.assertListEqual(expected, actual) def test_solve_no_solutions(self): expected = [] actual = list(solve(self.instance_no_solutions)) self.assertListEqual(expected, actual)