示例#1
0
    def test_bad_input_i_label(self):
        graph = Graph()
        e1 = gen_name()
        e2 = gen_name()
        e3 = gen_name()
        graph.add_node(e1, layer=1, position=(1.0, 2.0), label='E')
        graph.add_node(e2, layer=1, position=(1.0, 1.0), label='E')
        graph.add_node(e3, layer=1, position=(2.0, 1.0), label='E')

        graph.add_edge(e1, e2)
        graph.add_edge(e1, e3)
        graph.add_edge(e2, e3)

        i = add_interior(graph, e1, e2, e3)
        graph.nodes[i]['label'] = 'i'

        if visualize_tests:
            visualize_graph_3d(graph)
            pyplot.show()

        with self.assertRaises(AssertionError):
            P2().apply(graph, [i])

        if visualize_tests:
            visualize_graph_3d(graph)
            pyplot.show()
示例#2
0
    def test_happy_path(self):
        graph = Graph()
        e1 = gen_name()
        e2 = gen_name()
        e3 = gen_name()
        graph.add_node(e1, layer=1, position=(1.0, 2.0), label='E')
        graph.add_node(e2, layer=1, position=(1.0, 1.0), label='E')
        graph.add_node(e3, layer=1, position=(2.0, 1.0), label='E')

        graph.add_edge(e1, e2)
        graph.add_edge(e1, e3)
        graph.add_edge(e2, e3)

        i = add_interior(graph, e1, e2, e3)

        if visualize_tests:
            visualize_graph_3d(graph)
            pyplot.show()

        [i1] = P9().apply(graph, [i])

        # if correct number of nodes and edges
        self.assertEqual(len(graph.nodes()), 8)
        self.assertEqual(len(graph.edges()), 13)

        # if cross-layer interior connections
        self.assertEqual(graph.nodes[i]['label'], 'i')
        self.assertTrue(graph.has_edge(i, i1))

        # if new interior has correct label and layer
        self.assertEqual(graph.nodes[i1]['label'], 'I')
        self.assertEqual(graph.nodes[i1]['layer'], graph.nodes[i]['layer'] + 1)

        # if new interior has 3 neighbors
        i1_neighbors = get_neighbors_at(graph, i1, graph.nodes[i1]['layer'])
        self.assertEqual(len(i1_neighbors), 3)

        # if new nodes are in correct positions
        new_e1 = get_node_at(graph, 2, (1.0, 2.0))
        new_e2 = get_node_at(graph, 2, (1.0, 1.0))
        new_e3 = get_node_at(graph, 2, (2.0, 1.0))
        self.assertIsNotNone(new_e1)
        self.assertIsNotNone(new_e2)
        self.assertIsNotNone(new_e3)

        # if each vertex has correct label
        for n in i1_neighbors:
            self.assertEqual(graph.nodes[n]['label'], 'E')

        # if each vertex has correct number of neighbors
        for n in i1_neighbors:
            node_neighbors = get_neighbors_at(graph, n,
                                              graph.nodes[n]['layer'])
            self.assertEqual(len(node_neighbors), 3)

        if visualize_tests:
            visualize_graph_3d(graph)
            pyplot.show()
示例#3
0
    def apply(self,
              graph: Graph,
              prod_input: List[str],
              orientation: int = 0,
              **kwargs) -> List[str]:
        [i] = prod_input
        i_data = graph.nodes[i]
        self.__check_prod_input(graph, prod_input)

        i_data['label'] = 'i'
        i_layer = i_data['layer']
        new_layer = i_layer + 1

        i_neighbors = get_neighbors_at(graph, i, i_layer)

        # e1 doesn't mean e1 with (x1, y1)
        vx_e1 = gen_name()
        vx_e2 = gen_name()
        vx_e3 = gen_name()

        e1_pos = graph.nodes[i_neighbors[0]]['position']
        e2_pos = graph.nodes[i_neighbors[1]]['position']
        e3_pos = graph.nodes[i_neighbors[2]]['position']

        graph.add_node(vx_e1, layer=new_layer, position=e1_pos, label='E')
        graph.add_node(vx_e2, layer=new_layer, position=e2_pos, label='E')
        graph.add_node(vx_e3, layer=new_layer, position=e3_pos, label='E')

        graph.add_edge(vx_e1, vx_e2)
        graph.add_edge(vx_e2, vx_e3)
        graph.add_edge(vx_e3, vx_e1)

        sorted_segments = sort_segments_by_angle(graph, [(vx_e1, vx_e2),
                                                         (vx_e2, vx_e3),
                                                         (vx_e3, vx_e1)])
        segment_to_break = sorted_segments[orientation % 3]
        b = add_break_in_segment(graph, segment_to_break)
        b_neighbors = get_neighbors_at(graph, b, i_layer + 1)
        remaining = [x for x in [vx_e1, vx_e2, vx_e3]
                     if x not in b_neighbors][0]
        graph.add_edge(b, remaining)

        i1 = add_interior(graph, b_neighbors[0], b, remaining)
        i2 = add_interior(graph, b_neighbors[1], b, remaining)

        graph.add_edge(i1, i)
        graph.add_edge(i2, i)

        return [i1, i2]
def derive_b():
    graph = Graph()
    initial_node_name = gen_name()
    graph.add_node(initial_node_name, layer=0, position=(0.5, 0.5), label='E')

    visualize_graph_3d(graph)
    pyplot.show()

    [i1, i2] = P1().apply(graph, [initial_node_name])

    visualize_graph_3d(graph)
    pyplot.show()

    [i1_1, i1_2] = P2().apply(graph, [i1], orientation=1)
    [i2_1] = P9().apply(graph, [i2])

    visualize_graph_3d(graph)
    pyplot.show()

    P10().apply(graph, [i1_1, i1_2, i2_1])

    visualize_graph_3d(graph)
    pyplot.show()

    return graph
示例#5
0
    def test_happy_path(self):
        graph = Graph()
        initial_node = gen_name()
        graph.add_node(initial_node, layer=0, position=(0.5, 0.5), label='E')

        if visualize_tests:
            visualize_graph_3d(graph)
            pyplot.show()

        P1().apply(graph, [initial_node])

        nodes_data = graph.nodes(data=True)

        self.assertEqual(len(graph.nodes()), 7)
        self.assertEqual(len(graph.edges()), 13)

        # check the initial node
        initial_node_data = nodes_data[initial_node]
        self.assertEqual(initial_node_data['layer'], 0)
        self.assertEqual(initial_node_data['position'], (0.5, 0.5))
        self.assertEqual(initial_node_data['label'], 'e')

        # check other nodes
        vx_bl = get_node_at(graph, 1, (0, 0))
        vx_br = get_node_at(graph, 1, (1, 0))
        vx_tl = get_node_at(graph, 1, (0, 1))
        vx_tr = get_node_at(graph, 1, (1, 1))
        self.assertIsNotNone(vx_bl)
        self.assertIsNotNone(vx_br)
        self.assertIsNotNone(vx_tl)
        self.assertIsNotNone(vx_tr)
        self.assertEqual(nodes_data[vx_bl]['label'], 'E')
        self.assertEqual(nodes_data[vx_br]['label'], 'E')
        self.assertEqual(nodes_data[vx_tl]['label'], 'E')
        self.assertEqual(nodes_data[vx_tr]['label'], 'E')

        vx_i1 = get_node_at(graph, 1, (2 / 3, 1 / 3))
        vx_i2 = get_node_at(graph, 1, (1 / 3, 2 / 3))
        self.assertIsNotNone(vx_i1)
        self.assertIsNotNone(vx_i2)
        self.assertEqual(nodes_data[vx_i1]['label'], 'I')
        self.assertEqual(nodes_data[vx_i2]['label'], 'I')

        self.assertTrue(graph.has_edge(initial_node, vx_i1))
        self.assertTrue(graph.has_edge(initial_node, vx_i2))
        self.assertTrue(graph.has_edge(vx_tl, vx_tr))
        self.assertTrue(graph.has_edge(vx_tr, vx_br))
        self.assertTrue(graph.has_edge(vx_br, vx_bl))
        self.assertTrue(graph.has_edge(vx_bl, vx_tl))
        self.assertTrue(graph.has_edge(vx_bl, vx_tr))
        self.assertTrue(graph.has_edge(vx_i1, vx_bl))
        self.assertTrue(graph.has_edge(vx_i1, vx_br))
        self.assertTrue(graph.has_edge(vx_i1, vx_tr))
        self.assertTrue(graph.has_edge(vx_i2, vx_bl))
        self.assertTrue(graph.has_edge(vx_i2, vx_tl))
        self.assertTrue(graph.has_edge(vx_i2, vx_tr))

        if visualize_tests:
            visualize_graph_3d(graph)
            pyplot.show()
示例#6
0
 def create_nodes(self, graph, _layer, _label, vertex_positions):
     nodes = []
     for x, y in vertex_positions:
         e = gen_name()
         graph.add_node(e, layer=_layer, position=(x, y), label=_label)
         nodes.append(e)
     return nodes
示例#7
0
    def test_incorrect_interior(self):
        graph = Graph()
        e1, e2, e3, e4, e5 = [gen_name() for _ in range(5)]

        graph.add_node(e1, layer=1, position=(1.0, 2.0), label='I')
        graph.add_node(e2, layer=1, position=(1.0, 1.5), label='E')
        graph.add_node(e3, layer=1, position=(1.0, 1.0), label='E')
        graph.add_node(e4, layer=1, position=(2.0, 1.0), label='E')
        graph.add_node(e5, layer=1, position=(1.5, 1.5), label='E')

        graph.add_edge(e1, e2)
        graph.add_edge(e2, e3)
        graph.add_edge(e3, e4)
        graph.add_edge(e4, e5)
        graph.add_edge(e5, e1)

        if visualize_tests:
            visualize_graph_3d(graph)
            pyplot.show()

        with self.assertRaises(AssertionError):
            P4().apply(graph, [e1])

        if visualize_tests:
            visualize_graph_3d(graph)
            pyplot.show()
示例#8
0
    def test_different_position(self):
        graph = Graph()
        initial_node = gen_name()
        graph.add_node(initial_node, layer=0, position=(0, 0), label='E')

        if visualize_tests:
            visualize_graph_3d(graph)
            pyplot.show()

        P1().apply(graph, [initial_node],
                   positions=[
                       (0, 0),
                       (2, 1.5),
                       (1.5, 2),
                       (-0.5, 1.5),
                   ])

        # check other nodes
        vx_bl = get_node_at(graph, 1, (0, 0))
        vx_br = get_node_at(graph, 1, (2, 1.5))
        vx_tl = get_node_at(graph, 1, (1.5, 2))
        vx_tr = get_node_at(graph, 1, (-0.5, 1.5))
        self.assertIsNotNone(vx_bl)
        self.assertIsNotNone(vx_br)
        self.assertIsNotNone(vx_tl)
        self.assertIsNotNone(vx_tr)

        if visualize_tests:
            visualize_graph_3d(graph)
            pyplot.show()
示例#9
0
    def test_integrity(self):
        graph = Graph()
        initial_node_name = gen_name()
        graph.add_node(initial_node_name,
                       layer=0,
                       position=(0.5, 0.5),
                       label='E')

        [i1, i2] = P1().apply(graph, [initial_node_name])
        [i1_1, i1_2] = P2().apply(graph, [i1])
        [i2_1, i2_2] = P2().apply(graph, [i2])
        [i3_1, i3_2] = P2().apply(graph, [i1_1])

        if visualize_tests:
            visualize_graph_3d(graph)
            pyplot.show()

        [i4_1, i4_2] = P2().apply(graph, [i1_2])

        self.check_graph_integrity(graph, i1, 'i')
        self.check_graph_integrity(graph, i2, 'i')
        self.check_graph_integrity(graph, i1_1, 'i')
        self.check_graph_integrity(graph, i1_2, 'i')
        self.check_graph_integrity(graph, i2_1, 'I')
        self.check_graph_integrity(graph, i2_2, 'I')
        self.check_graph_integrity(graph, i3_1, 'I')
        self.check_graph_integrity(graph, i3_2, 'I')
        self.check_graph_integrity(graph, i4_1, 'I')
        self.check_graph_integrity(graph, i4_2, 'I')

        if visualize_tests:
            visualize_graph_3d(graph)
            pyplot.show()
示例#10
0
    def apply(self,
              graph: Graph,
              prod_input: List[str],
              orientation: int = 0,
              **kwargs) -> List[str]:
        [initial_node_id] = prod_input
        initial_node_data = graph.nodes[initial_node_id]

        if initial_node_data['layer'] != 0:
            raise ValueError('bad layer')

        if initial_node_data['label'] != 'E':
            raise ValueError('bad label')

        positions = self.__get_positions(kwargs['positions'] if 'positions' in
                                         kwargs else None)

        # change label
        initial_node_data['label'] = 'e'

        vx_tl = gen_name()
        vx_tr = gen_name()
        vx_bl = gen_name()
        vx_br = gen_name()
        graph.add_node(vx_bl, layer=1, position=positions[0], label='E')
        graph.add_node(vx_br, layer=1, position=positions[1], label='E')
        graph.add_node(vx_tl, layer=1, position=positions[2], label='E')
        graph.add_node(vx_tr, layer=1, position=positions[3], label='E')

        if orientation % 2 == 1:
            [vx_bl, vx_br, vx_tr, vx_tl] = [vx_br, vx_tr, vx_tl, vx_bl]

        graph.add_edge(vx_tl, vx_tr)
        graph.add_edge(vx_tr, vx_br)
        graph.add_edge(vx_br, vx_bl)
        graph.add_edge(vx_bl, vx_tl)
        graph.add_edge(vx_tr, vx_bl)

        i1 = add_interior(graph, vx_tl, vx_tr, vx_bl)
        i2 = add_interior(graph, vx_tr, vx_br, vx_bl)

        graph.add_edge(i1, initial_node_id)
        graph.add_edge(i2, initial_node_id)

        return [i1, i2]
示例#11
0
def addTriangle(graph: Graph, node, attr):
    e1 = gen_name()
    e2 = gen_name()
    e3 = gen_name()
    x = attr['position'][0]
    y = attr['position'][1]
    graph.add_node(e1,
                   layer=attr['layer'],
                   position=(x + 0.5, y + 0.5),
                   label='E')
    graph.add_node(e2, layer=attr['layer'], position=(x, y + 0.5), label='E')
    graph.add_node(e3,
                   layer=attr['layer'],
                   position=(x - 0.5, y - 0.5),
                   label='E')
    graph.add_edge(node, e1)
    graph.add_edge(node, e2)
    graph.add_edge(node, e3)
    return graph
示例#12
0
    def apply(self,
              graph: Graph,
              prod_input: List[str],
              orientation: int = 0,
              **kwargs) -> List[str]:
        # Production based on P2
        self.__check_prod_input(graph, prod_input)

        [i] = prod_input
        i_data = graph.nodes[i]
        i_data['label'] = 'i'
        i_layer = i_data['layer']
        new_layer = i_layer + 1

        i_neighbors = get_neighbors_at(graph, i, i_layer)

        # create new 'E' nodes in the next layer
        new_e1 = gen_name()
        new_e2 = gen_name()
        new_e3 = gen_name()

        e1_pos = graph.nodes[i_neighbors[0]]['position']
        e2_pos = graph.nodes[i_neighbors[1]]['position']
        e3_pos = graph.nodes[i_neighbors[2]]['position']

        graph.add_node(new_e1, layer=new_layer, position=e1_pos, label='E')
        graph.add_node(new_e2, layer=new_layer, position=e2_pos, label='E')
        graph.add_node(new_e3, layer=new_layer, position=e3_pos, label='E')

        # create edges between new 'E' nodes
        graph.add_edge(new_e1, new_e2)
        graph.add_edge(new_e2, new_e3)
        graph.add_edge(new_e3, new_e1)

        # create new 'I' node and edges between new 'I' nodes and new 'E' nodes
        i1 = add_interior(graph, new_e1, new_e2, new_e3)

        # create edges between new 'I' node and parent 'i' node
        graph.add_edge(i1, i)

        return [i1]
示例#13
0
    def test_bad_input_vertex_count(self):
        graph = Graph()
        e1 = gen_name()
        e2 = gen_name()
        e3 = gen_name()
        graph.add_node(e1, layer=1, position=(1.0, 2.0), label='I')
        graph.add_node(e2, layer=1, position=(1.0, 1.0), label='E')
        graph.add_node(e3, layer=1, position=(2.0, 1.0), label='E')

        graph.add_edge(e1, e2)
        graph.add_edge(e1, e3)
        graph.add_edge(e2, e3)

        if visualize_tests:
            visualize_graph_3d(graph)
            pyplot.show()

        with self.assertRaises(AssertionError):
            P2().apply(graph, [e1])

        if visualize_tests:
            visualize_graph_3d(graph)
            pyplot.show()
示例#14
0
    def test_wrong_label(self):
        graph = Graph()
        initial_node = gen_name()
        graph.add_node(initial_node, layer=0, position=(0.5, 0.5), label='e')

        if visualize_tests:
            visualize_graph_3d(graph)
            pyplot.show()

        with self.assertRaisesRegex(ValueError, 'bad label'):
            P1().apply(graph, [initial_node])

        self.assertEqual(len(graph.nodes()), 1)

        if visualize_tests:
            visualize_graph_3d(graph)
            pyplot.show()
示例#15
0
    def test_wrong_args(self):
        graph = Graph()
        initial_node = gen_name()
        graph.add_node(initial_node, layer=1, position=(0.5, 0.5), label='E')

        if visualize_tests:
            visualize_graph_3d(graph)
            pyplot.show()

        with self.assertRaisesRegex(ValueError, 'not enough values to unpack'):
            P1().apply(graph, [])

        self.assertEqual(len(graph.nodes()), 1)

        if visualize_tests:
            visualize_graph_3d(graph)
            pyplot.show()
示例#16
0
 def testLargerGraph(self):
     e1 = gen_name()
     graph = createCorrectGraph()
     prod_input = [
         x for x, y in graph.nodes(data=True)
         if y['label'] == 'i' or y['label'] == 'I'
     ]
     attrs = [
         y for x, y in graph.nodes(data=True)
         if y['label'] == 'i' or y['label'] == 'I'
     ]
     for node, attr in zip(prod_input, attrs):
         graph = addTriangle(graph, node, attr)
     [] = P6().apply(graph, prod_input)
     if visualize_tests:
         visualize_graph_3d(graph)
         pyplot.show()
     self.assertEqual(len(graph.nodes()), 29)
     self.assertEqual(len(graph.edges()), 37)
示例#17
0
    def test_bad_input_vertex_label(self):
        graph = Graph()
        positions = [(1.0, 1.0), (1.0, 2.0), (1.0, 3.0), (2.0, 3.0),
                     (3.0, 3.0)]

        [e1, e12, e2, e23, e3] = self.create_nodes(graph, 1, 'E', positions)
        e31 = gen_name()
        graph.add_node(e31, layer=1, position=(2.0, 2.0), label='e')

        self.create_edges_chain(graph, [e1, e12, e2, e23, e3, e31, e1])

        i = add_interior(graph, e1, e2, e3)

        with self.assertRaises(AssertionError):
            [i1, i3, i2a, i2b] = P5().apply(graph, [i])
        self.assertEqual(len(graph.nodes()), 7)
        self.assertEqual(len(graph.edges()), 9)

        if visualize_tests:
            pyplot.title("Wrong 'e' label", fontsize=16)
            visualize_graph_3d(graph)
            pyplot.show()
示例#18
0
"""
This is an example derivation.
If you want to test something you can use it.
It's better to copy-paste this file as `test.py` in order
not to accidentally commit this file.
"""
from matplotlib import pyplot
from networkx import Graph

from agh_graphs.productions.p1 import P1
from agh_graphs.productions.p2 import P2
from agh_graphs.utils import gen_name
from agh_graphs.visualize import visualize_graph_layer, visualize_graph_3d

if __name__ == '__main__':
    graph = Graph()
    initial_node_name = gen_name()
    graph.add_node(initial_node_name, layer=0, position=(0.5, 0.5), label='E')

    [i1, i2] = P1().apply(graph, [initial_node_name])
    [i1_1, i1_2] = P2().apply(graph, [i1])
    [i2_1, i2_2] = P2().apply(graph, [i2])
    [i3_1, i3_2] = P2().apply(graph, [i1_1])

    visualize_graph_3d(graph)
    pyplot.show()

    visualize_graph_layer(graph, 2)
    pyplot.show()
示例#19
0
    def apply(self,
              graph: Graph,
              prod_input: List[str],
              orientation: int = 0,
              **kwargs) -> List[str]:
        e1, e2, e3, e12, e13 = self.__check_prod_input(graph, prod_input)
        [i] = prod_input
        i_data = graph.nodes[i]

        i_data['label'] = 'i'
        i_layer = i_data['layer']
        new_layer = i_layer + 1

        # create new layer
        new_e1 = gen_name()
        new_e2 = gen_name()
        new_e3 = gen_name()
        new_e12 = gen_name()
        new_e13 = gen_name()

        graph.add_node(new_e1,
                       layer=new_layer,
                       position=graph.nodes[e1]['position'],
                       label='E')
        graph.add_node(new_e2,
                       layer=new_layer,
                       position=graph.nodes[e2]['position'],
                       label='E')
        graph.add_node(new_e3,
                       layer=new_layer,
                       position=graph.nodes[e3]['position'],
                       label='E')
        graph.add_node(new_e12,
                       layer=new_layer,
                       position=graph.nodes[e12]['position'],
                       label='E')
        graph.add_node(new_e13,
                       layer=new_layer,
                       position=graph.nodes[e13]['position'],
                       label='E')

        graph.add_edge(new_e1, new_e12)
        graph.add_edge(new_e12, new_e2)
        graph.add_edge(new_e1, new_e13)
        graph.add_edge(new_e13, new_e3)
        graph.add_edge(new_e2, new_e3)

        sorted_segments = sort_segments_by_angle(graph, [(new_e1, new_e2),
                                                         (new_e1, new_e3)])
        segment_to_break = sorted_segments[orientation % 2]
        (v1, v2) = segment_to_break
        b = get_vertex_between(graph, v1, v2, new_layer, 'E')
        assert b is not None
        b_opposite_1 = [
            e for e in [new_e1, new_e2, new_e3] if e not in segment_to_break
        ][0]
        b_opposite_2 = [e for e in [new_e12, new_e13] if e != b][0]
        graph.add_edge(b, b_opposite_1)
        graph.add_edge(b, b_opposite_2)

        i1 = add_interior(graph, b, b_opposite_1, b_opposite_2)
        i2 = add_interior(graph, b, b_opposite_1, v2)
        i3 = add_interior(graph, b, b_opposite_2, v1)

        graph.add_edge(i1, i)
        graph.add_edge(i2, i)
        graph.add_edge(i3, i)

        return [i1, i2, i3]
示例#20
0
def createCorrectGraph():
    graph = Graph()
    e1 = gen_name()
    e2 = gen_name()
    i1 = gen_name()
    i2 = gen_name()
    I1 = gen_name()
    I2 = gen_name()
    e3 = gen_name()
    e4 = gen_name()
    e5 = gen_name()
    e6 = gen_name()
    e7 = gen_name()
    I3 = gen_name()

    graph.add_node(e1, layer=1, position=(1.0, 2.0), label='E')
    graph.add_node(e2, layer=1, position=(3.0, 2.0), label='E')

    graph.add_node(i1, layer=1, position=(2.0, 3.0), label='i')
    graph.add_node(i2, layer=1, position=(2.0, 1.0), label='i')

    graph.add_node(I1, layer=2, position=(1.5, 3.5), label='I')
    graph.add_node(I2, layer=2, position=(2.5, 3.5), label='I')

    graph.add_node(e3, layer=2, position=(1.0, 2.0), label='E')
    graph.add_node(e4, layer=2, position=(3.0, 2.0), label='E')
    graph.add_node(e5, layer=2, position=(2.0, 2.0), label='E')

    #graph.add_node(I3, layer=2, position=(1.5, 0.5), label='I') czy napewno te wspolrzedne? spr
    graph.add_node(I3, layer=2, position=(2.5, 0.5), label='I')

    graph.add_node(e6, layer=2, position=(1.0, 2.0), label='E')
    graph.add_node(e7, layer=2, position=(3.0, 2.0), label='E')

    # upper layer edges
    graph.add_edge(e1, i1)
    graph.add_edge(e1, i2)
    graph.add_edge(e2, i1)
    graph.add_edge(e2, i2)
    graph.add_edge(e1, e2)

    # interlayer connections
    graph.add_edge(I1, i1)
    graph.add_edge(I2, i1)
    graph.add_edge(I3, i2)

    # lower layer connections
    graph.add_edge(I1, e3)
    graph.add_edge(I1, e5)
    graph.add_edge(e3, e5)
    graph.add_edge(I2, e4)
    graph.add_edge(I2, e5)
    graph.add_edge(e4, e5)

    #lower layer triangle
    graph.add_edge(I3, e6)
    graph.add_edge(I3, e7)
    graph.add_edge(e6, e7)

    return graph
示例#21
0
    def testOnBiggerGraph(self):
        graph = Graph()
        initial_node_name = gen_name()
        graph.add_node(initial_node_name, layer=0, position=(0.5, 0.5), label='E')

        [i1, i2] = P1().apply(graph, [initial_node_name])
        [i1_1, i1_2] = P2().apply(graph, [i1], orientation=1)
        [i2_1] = P9().apply(graph, [i2], orientation=1)

        if visualize_tests:
            visualize_graph_3d(graph)
            pyplot.show()

        [i1_1new, i1_2new, i2_1new] = P10().apply(graph, [i1_1, i1_2, i2_1])

        self.assertEqual(len(graph.nodes()), 15)
        self.assertEqual(len(graph.edges()), 32)

        e = get_node_at(graph=graph, layer=0, pos=(0.5, 0.5))
        e1 = get_node_at(graph=graph, layer=1, pos=(0.0, 0.0))
        e2 = get_node_at(graph=graph, layer=1, pos=(1.0, 0.0))
        e3 = get_node_at(graph=graph, layer=1, pos=(1.0, 1.0))
        e4 = get_node_at(graph=graph, layer=1, pos=(0.0, 1.0))
        e5 = get_node_at(graph=graph, layer=2, pos=(0.0, 0.0))
        e6 = get_node_at(graph=graph, layer=2, pos=(1.0, 0.0))
        e7 = get_node_at(graph=graph, layer=2, pos=(1.0, 1.0))
        e8 = get_node_at(graph=graph, layer=2, pos=(0.0, 1.0))
        e9 = get_node_at(graph=graph, layer=2, pos=(0.5, 0.5))

        # check position
        self.assertIsNotNone(e)
        self.assertIsNotNone(e1)
        self.assertIsNotNone(e2)
        self.assertIsNotNone(e3)
        self.assertIsNotNone(e4)
        self.assertIsNotNone(e5)
        self.assertIsNotNone(e6)
        self.assertIsNotNone(e7)
        self.assertIsNotNone(e8)
        self.assertIsNotNone(e9)

        self.assertEqual(graph.nodes[i1_1new]['position'], graph.nodes[i1_1]['position'])
        self.assertEqual(graph.nodes[i1_2new]['position'], graph.nodes[i1_2]['position'])
        self.assertEqual(graph.nodes[i2_1new]['position'], graph.nodes[i2]['position'])  # ten co jest z prawe

        self.assertEqual(graph.nodes[i1_1new]['layer'], graph.nodes[i1_1]['layer'])
        self.assertEqual(graph.nodes[i1_2new]['layer'], graph.nodes[i1_2]['layer'])
        self.assertEqual(graph.nodes[i2_1new]['layer'], graph.nodes[i2_1]['layer'])

        i1_new = get_node_at(graph=graph, layer=1, pos=graph.nodes[i1]['position'])
        i2_new = get_node_at(graph=graph, layer=1, pos=graph.nodes[i2]['position'])

        self.assertIsNotNone(i1_new)
        self.assertIsNotNone(i2_new)

        # zero
        self.assertTrue(graph.has_edge(e, i1_new))
        self.assertTrue(graph.has_edge(e, i2_new))

        # first
        self.assertTrue(graph.has_edge(e1, e2))
        self.assertTrue(graph.has_edge(e1, e3))
        self.assertTrue(graph.has_edge(e1, e4))
        self.assertTrue(graph.has_edge(e1, i2_new))
        self.assertTrue(graph.has_edge(e1, i1_new))
        self.assertTrue(graph.has_edge(e2, e3))
        self.assertTrue(graph.has_edge(e2, i2_new))
        self.assertTrue(graph.has_edge(e3, e4))
        self.assertTrue(graph.has_edge(e3, i1_new))
        self.assertTrue(graph.has_edge(e3, i2_new))
        self.assertTrue(graph.has_edge(e4, i1_new))

        # second
        self.assertTrue(graph.has_edge(e5, e6))
        self.assertTrue(graph.has_edge(e5, e9))
        self.assertTrue(graph.has_edge(e5, e8))
        self.assertTrue(graph.has_edge(e6, e7))
        self.assertTrue(graph.has_edge(e7, e9))
        self.assertTrue(graph.has_edge(e7, e8))

        self.assertTrue(graph.has_edge(i1_new, i1_1new))
        self.assertTrue(graph.has_edge(i1_new, i1_2new))
        self.assertTrue(graph.has_edge(i2_new, i2_1new))

        self.assertTrue(graph.has_edge(e5, i1_1new))
        self.assertTrue(graph.has_edge(e8, i1_1new))
        self.assertTrue(graph.has_edge(e9, i1_1new))

        self.assertTrue(graph.has_edge(e7, i1_2new))
        self.assertTrue(graph.has_edge(e8, i1_2new))
        self.assertTrue(graph.has_edge(e9, i1_2new))

        self.assertTrue(graph.has_edge(e5, i2_1new))
        self.assertTrue(graph.has_edge(e6, i2_1new))
        self.assertTrue(graph.has_edge(e7, i2_1new))

        # check labels
        self.assertEqual(graph.nodes[e]['label'], 'e')

        self.assertEqual(graph.nodes[e1]['label'], 'E')
        self.assertEqual(graph.nodes[e2]['label'], 'E')
        self.assertEqual(graph.nodes[e3]['label'], 'E')
        self.assertEqual(graph.nodes[e4]['label'], 'E')
        self.assertEqual(graph.nodes[e5]['label'], 'E')
        self.assertEqual(graph.nodes[e6]['label'], 'E')
        self.assertEqual(graph.nodes[e7]['label'], 'E')
        self.assertEqual(graph.nodes[e8]['label'], 'E')
        self.assertEqual(graph.nodes[e9]['label'], 'E')

        self.assertEqual(graph.nodes[i1_1new]['label'], 'I')
        self.assertEqual(graph.nodes[i1_2new]['label'], 'I')
        self.assertEqual(graph.nodes[i2_1new]['label'], 'I')
        self.assertEqual(graph.nodes[i1_new]['label'], 'i')
        self.assertEqual(graph.nodes[i2_new]['label'], 'i')
示例#22
0
    def test_happy_path(self):
        graph = Graph()
        e1 = gen_name()
        e2 = gen_name()
        e3 = gen_name()
        graph.add_node(e1, layer=0, position=(1.0, 2.0), label='E')
        graph.add_node(e2, layer=0, position=(1.0, 1.0), label='E')
        graph.add_node(e3, layer=0, position=(2.0, 1.0), label='E')

        graph.add_edge(e1, e2)
        graph.add_edge(e1, e3)
        graph.add_edge(e2, e3)

        i = add_interior(graph, e1, e2, e3)

        if visualize_tests:
            visualize_graph_3d(graph)
            pyplot.show()

        [i1, i2] = P2().apply(graph, [i])

        self.assertIsNotNone(get_node_at(graph, 1, (1.0, 2.0)))
        self.assertIsNotNone(get_node_at(graph, 1, (1.0, 1.0)))
        self.assertIsNotNone(get_node_at(graph, 1, (2.0, 1.0)))
        self.assertIsNotNone(get_node_at(graph, 1, (1.5, 1.0)))

        (i1_x, i1_y) = graph.nodes[i1]['position']
        (i2_x, i2_y) = graph.nodes[i2]['position']
        self.assertTrue(isclose(i1_x, 1.166666, rel_tol=eps))
        self.assertTrue(isclose(i1_y, 1.333333, rel_tol=eps))
        self.assertTrue(isclose(i2_x, 1.5, rel_tol=eps))
        self.assertTrue(isclose(i2_y, 1.333333, rel_tol=eps))

        self.assertEqual(len(graph.nodes()), 10)
        self.assertEqual(len(graph.edges()), 19)

        self.assertEqual(graph.nodes[i]['label'], 'i')
        self.assertTrue(graph.has_edge(i, i1))
        self.assertTrue(graph.has_edge(i, i2))

        self.assertEqual(graph.nodes[i1]['label'], 'I')
        self.assertEqual(graph.nodes[i2]['label'], 'I')
        self.assertEqual(graph.nodes[i1]['layer'], graph.nodes[i]['layer'] + 1)
        self.assertEqual(graph.nodes[i2]['layer'], graph.nodes[i]['layer'] + 1)

        i1_neighbors = get_neighbors_at(graph, i1, graph.nodes[i1]['layer'])
        self.assertEqual(len(i1_neighbors), 3)
        i2_neighbors = get_neighbors_at(graph, i2, graph.nodes[i2]['layer'])
        self.assertEqual(len(i2_neighbors), 3)

        common_neighbors = [x for x in i1_neighbors if x in i2_neighbors]

        for n in i1_neighbors:
            if n not in common_neighbors:
                self.assertEqual(graph.nodes[n]['label'], 'E')
                self.assertEqual(
                    len(get_neighbors_at(graph, n, graph.nodes[i1]['layer'])),
                    3)

        for n in i2_neighbors:
            if n not in common_neighbors:
                self.assertEqual(graph.nodes[n]['label'], 'E')
                self.assertEqual(
                    len(get_neighbors_at(graph, n, graph.nodes[i2]['layer'])),
                    3)

        for c_neighbor in common_neighbors:
            self.assertEqual(graph.nodes[c_neighbor]['label'], 'E')
            self.assertEqual(
                len(
                    get_neighbors_at(graph, c_neighbor,
                                     graph.nodes[i1]['layer'])), 5)

        if visualize_tests:
            visualize_graph_3d(graph)
            pyplot.show()
示例#23
0
    def test_happy_path(self):
        graph = Graph()
        e1 = gen_name()
        e2 = gen_name()
        e3 = gen_name()
        e4 = gen_name()
        e1_1 = gen_name()
        e1_2 = gen_name()
        e1_3 = gen_name()
        e2_1 = gen_name()
        e2_2 = gen_name()
        e2_4 = gen_name()
        e3_5 = gen_name()

        graph.add_node(e1, layer=1, position=(1.0, 1.0), label='E')
        graph.add_node(e2, layer=1, position=(2.0, 2.0), label='E')
        graph.add_node(e3, layer=1, position=(1.0, 2.0), label='E')
        graph.add_node(e4, layer=1, position=(2.0, 1.0), label='E')
        graph.add_node(e1_1, layer=2, position=(1.0, 1.0), label='E')
        graph.add_node(e1_2, layer=2, position=(2.0, 2.0), label='E')
        graph.add_node(e1_3, layer=2, position=(1.0, 2.0), label='E')
        graph.add_node(e2_1, layer=2, position=(1.0, 1.0), label='E')
        graph.add_node(e2_2, layer=2, position=(2.0, 2.0), label='E')
        graph.add_node(e2_4, layer=2, position=(2.0, 1.0), label='E')
        graph.add_node(e3_5, layer=2, position=(2.0, 3.0), label='E')

        graph.add_edge(e1, e2)
        graph.add_edge(e1, e3)
        graph.add_edge(e1, e4)
        graph.add_edge(e2, e3)
        graph.add_edge(e2, e4)
        graph.add_edge(e1_1, e1_2)
        graph.add_edge(e1_1, e1_3)
        graph.add_edge(e1_2, e1_3)
        graph.add_edge(e2_1, e2_2)
        graph.add_edge(e2_1, e2_4)
        graph.add_edge(e2_2, e2_4)
        graph.add_edge(e3_5, e1_2)
        graph.add_edge(e3_5, e1_3)

        i1 = add_interior(graph, e1, e2, e3)
        i2 = add_interior(graph, e1, e2, e4)
        i1_1 = add_interior(graph, e1_1, e1_2, e1_3)
        i2_1 = add_interior(graph, e2_1, e2_2, e2_4)
        i3_1 = add_interior(graph, e3_5, e1_2, e1_3)
        graph.nodes[i1]['label'] = 'i'
        graph.nodes[i2]['label'] = 'i'

        graph.add_edge(i1, i1_1)
        graph.add_edge(i2, i2_1)
        graph.add_edge(i1, i3_1)

        if visualize_tests:
            visualize_graph_3d(graph)
            pyplot.show()

        P12().apply(graph, [i1, i2, i1_1, i2_1])

        # if correct number of nodes and edges
        self.assertEqual(len(graph.nodes()), 14)
        self.assertEqual(len(graph.edges()), 30)

        # if interiors has correct labels, layers and are connected
        self.assertEqual(graph.nodes[i1]['label'], 'i')
        self.assertEqual(graph.nodes[i2]['label'], 'i')
        self.assertEqual(graph.nodes[i1_1]['label'], 'I')
        self.assertEqual(graph.nodes[i2_1]['label'], 'I')
        self.assertEqual(graph.nodes[i1]['layer'], 1)
        self.assertEqual(graph.nodes[i2]['layer'], 1)
        self.assertEqual(graph.nodes[i1_1]['layer'], 2)
        self.assertEqual(graph.nodes[i2_1]['layer'], 2)
        self.assertTrue(graph.has_edge(i1, i1_1))
        self.assertTrue(graph.has_edge(i2, i2_1))

        # if each interior has 3 neighbors
        i1_neighbors = get_neighbors_at(graph, i1, graph.nodes[i1]['layer'])
        self.assertEqual(len(i1_neighbors), 3)
        i2_neighbors = get_neighbors_at(graph, i2, graph.nodes[i2]['layer'])
        self.assertEqual(len(i2_neighbors), 3)
        i1_1_neighbors = get_neighbors_at(graph, i1_1,
                                          graph.nodes[i1_1]['layer'])
        self.assertEqual(len(i1_1_neighbors), 3)
        i2_1_neighbors = get_neighbors_at(graph, i2_1,
                                          graph.nodes[i2_1]['layer'])
        self.assertEqual(len(i2_1_neighbors), 3)

        # if nodes in lower layer exists and are correctly connected
        new_e1 = get_node_at(graph, 2, (1.0, 1.0))
        new_e2 = get_node_at(graph, 2, (2.0, 2.0))
        new_e3 = get_node_at(graph, 2, (1.0, 2.0))
        new_e4 = get_node_at(graph, 2, (2.0, 1.0))
        self.assertIsNotNone(new_e1)
        self.assertIsNotNone(new_e2)
        self.assertIsNotNone(new_e3)
        self.assertIsNotNone(new_e4)
        self.assertTrue(graph.has_edge(new_e1, new_e2))
        self.assertTrue(graph.has_edge(new_e1, new_e3))
        self.assertTrue(graph.has_edge(new_e1, new_e4))
        self.assertTrue(graph.has_edge(new_e2, new_e3))
        self.assertTrue(graph.has_edge(new_e2, new_e4))

        # if lower interiors connect with all 4 vertices
        all_neighbors = i1_1_neighbors + i2_1_neighbors
        all_neighbors = list(dict.fromkeys(all_neighbors))  # remove duplicates
        self.assertEqual(len(all_neighbors), 4)

        # if each vertex has correct label
        for n in all_neighbors:
            self.assertEqual(graph.nodes[n]['label'], 'E')

        # if each vertex has correct number of neighbors (based on neighbour interiors count)
        for n in all_neighbors:
            node_neighbors = get_neighbors_at(graph, n,
                                              graph.nodes[n]['layer'])
            i_neighbors = [
                x for x in node_neighbors if graph.nodes[x]['label'] == 'I'
            ]
            if len(i_neighbors) == 1:
                self.assertEqual(len(node_neighbors), 3)
            elif len(i_neighbors) == 2:
                self.assertEqual(len(node_neighbors), 5)
            else:
                self.assertEqual(len(node_neighbors), 7)

        if visualize_tests:
            visualize_graph_3d(graph)
            pyplot.show()
示例#24
0
    def test_risky_triangle_break(self):
        graph = Graph()
        e1, e2, e3, e4, e5 = [gen_name() for _ in range(5)]
        e6 = gen_name()

        graph.add_node(e1, layer=1, position=(1.0, 2.0), label='E')
        graph.add_node(e2, layer=1, position=(1.0, 1.5), label='E')
        graph.add_node(e3, layer=1, position=(1.0, 1.0), label='E')
        graph.add_node(e4, layer=1, position=(2.0, 1.0), label='E')
        graph.add_node(e5, layer=1, position=(1.5, 1.5), label='E')
        graph.add_node(e6, layer=1, position=(0.0, 1.5), label='E')

        graph.add_edge(e1, e2)
        graph.add_edge(e2, e3)
        graph.add_edge(e3, e4)
        graph.add_edge(e4, e5)
        graph.add_edge(e5, e1)

        graph.add_edge(e1, e6)
        graph.add_edge(e6, e3)

        i = add_interior(graph, e1, e3, e4)

        if visualize_tests:
            visualize_graph_3d(graph)
            pyplot.show()

        [i1, i2, i3] = P4().apply(graph, [i])

        self.assertEqual(len(graph.nodes()), 15)
        self.assertEqual(len(graph.edges()), 29)

        self.assertEqual(graph.nodes[i]['label'], 'i')
        self.assertTrue(graph.has_edge(i, i1))
        self.assertTrue(graph.has_edge(i, i2))
        self.assertTrue(graph.has_edge(i, i3))

        self.assertEqual(graph.nodes[i1]['label'], 'I')
        self.assertEqual(graph.nodes[i2]['label'], 'I')
        self.assertEqual(graph.nodes[i3]['label'], 'I')
        self.assertEqual(graph.nodes[i1]['layer'], graph.nodes[i]['layer'] + 1)
        self.assertEqual(graph.nodes[i2]['layer'], graph.nodes[i]['layer'] + 1)
        self.assertEqual(graph.nodes[i3]['layer'], graph.nodes[i]['layer'] + 1)

        i1_neighbors = get_neighbors_at(graph, i1, graph.nodes[i1]['layer'])
        self.assertEqual(len(i1_neighbors), 3)
        i2_neighbors = get_neighbors_at(graph, i2, graph.nodes[i2]['layer'])
        self.assertEqual(len(i2_neighbors), 3)
        i3_neighbors = get_neighbors_at(graph, i3, graph.nodes[i3]['layer'])
        self.assertEqual(len(i3_neighbors), 3)

        i1_i2_n = [x for x in i1_neighbors if x in i2_neighbors]
        i1_i3_n = [x for x in i1_neighbors if x in i3_neighbors]
        i2_i3_n = [x for x in i2_neighbors if x in i3_neighbors]

        # Test i1-only neighbors
        for n in [
                x for x in i1_neighbors
                if x not in i1_i2_n and x not in i1_i3_n
        ]:
            self.assertEqual(graph.nodes[n]['label'], 'E')
            self.assertEqual(
                3, len(get_neighbors_at(graph, n, graph.nodes[i1]['layer'])))

        # Test i2-only neighbors
        for n in [
                x for x in i2_neighbors
                if x not in i1_i2_n and x not in i2_i3_n
        ]:
            self.assertEqual(graph.nodes[n]['label'], 'E')
            self.assertEqual(
                3, len(get_neighbors_at(graph, n, graph.nodes[i2]['layer'])))

        # Test i3-only neighbors
        for n in [
                x for x in i3_neighbors
                if x not in i1_i3_n and x not in i2_i3_n
        ]:
            self.assertEqual(graph.nodes[n]['label'], 'E')
            self.assertEqual(
                3, len(get_neighbors_at(graph, n, graph.nodes[i3]['layer'])))

        # Test nodes connected to 2 interiors
        for n in [x for x in i1_i2_n if x not in i1_i3_n and x not in i2_i3_n]:
            self.assertEqual(graph.nodes[n]['label'], 'E')
            self.assertEqual(
                5, len(get_neighbors_at(graph, n, graph.nodes[i1]['layer'])))

        for n in [x for x in i1_i3_n if x not in i1_i2_n and x not in i2_i3_n]:
            self.assertEqual(graph.nodes[n]['label'], 'E')
            self.assertEqual(
                5, len(get_neighbors_at(graph, n, graph.nodes[i1]['layer'])))

        for n in [x for x in i2_i3_n if x not in i1_i2_n and x not in i1_i3_n]:
            self.assertEqual(graph.nodes[n]['label'], 'E')
            self.assertEqual(
                5, len(get_neighbors_at(graph, n, graph.nodes[i1]['layer'])))

        # Test nodes connected to 3 interiors
        for n in [x for x in i2_i3_n if x in i1_i2_n and x in i1_i3_n]:
            self.assertEqual(graph.nodes[n]['label'], 'E')
            self.assertEqual(
                7, len(get_neighbors_at(graph, n, graph.nodes[i1]['layer'])))

        if visualize_tests:
            visualize_graph_3d(graph)
            pyplot.show()
示例#25
0
    def test_in_bigger_graph(self):
        graph = Graph()

        # Base nodes
        e1, e2, e3, e4, e5 = [gen_name() for _ in range(5)]

        # Additional nodes
        e6, e7, e8, e9 = [gen_name() for _ in range(4)]

        graph.add_node(e1, layer=1, position=(1.0, 2.0), label='E')
        graph.add_node(e2, layer=1, position=(1.0, 1.5), label='E')
        graph.add_node(e3, layer=1, position=(1.0, 1.0), label='E')
        graph.add_node(e4, layer=1, position=(2.0, 1.0), label='E')
        graph.add_node(e5, layer=1, position=(1.5, 1.5), label='E')

        graph.add_node(e6, layer=1, position=(2.0, 2.0), label='E')
        graph.add_node(e7, layer=1, position=(1.0, 0.0), label='E')
        graph.add_node(e8, layer=1, position=(2.0, 0.0), label='E')
        graph.add_node(e9, layer=1, position=(1.5, -1.0), label='E')

        graph.add_edge(e1, e2)
        graph.add_edge(e2, e3)
        graph.add_edge(e3, e4)
        graph.add_edge(e4, e5)
        graph.add_edge(e5, e1)

        graph.add_edge(e1, e6)
        graph.add_edge(e6, e5)

        graph.add_edge(e7, e3)
        graph.add_edge(e7, e8)
        graph.add_edge(e7, e9)

        graph.add_edge(e8, e4)
        graph.add_edge(e8, e9)

        I = add_interior(graph, e1, e3, e4)
        I1 = add_interior(graph, e1, e5, e6)
        I2 = add_interior(graph, e7, e8, e4)
        I3 = add_interior(graph, e7, e8, e9)

        if visualize_tests:
            visualize_graph_3d(graph)
            pyplot.show()

        [i1, i2, i3] = P4().apply(graph, [I])

        self.assertEqual(len(graph.nodes()), 21)
        self.assertEqual(len(graph.edges()), 43)

        self.assertEqual(graph.nodes[I]['label'], 'i')
        self.assertTrue(graph.has_edge(I, i1))
        self.assertTrue(graph.has_edge(I, i2))
        self.assertTrue(graph.has_edge(I, i3))

        self.assertEqual(graph.nodes[i1]['label'], 'I')
        self.assertEqual(graph.nodes[i2]['label'], 'I')
        self.assertEqual(graph.nodes[i3]['label'], 'I')
        self.assertEqual(graph.nodes[i1]['layer'], graph.nodes[I]['layer'] + 1)
        self.assertEqual(graph.nodes[i2]['layer'], graph.nodes[I]['layer'] + 1)
        self.assertEqual(graph.nodes[i3]['layer'], graph.nodes[I]['layer'] + 1)

        i1_neighbors = get_neighbors_at(graph, i1, graph.nodes[i1]['layer'])
        self.assertEqual(len(i1_neighbors), 3)
        i2_neighbors = get_neighbors_at(graph, i2, graph.nodes[i2]['layer'])
        self.assertEqual(len(i2_neighbors), 3)
        i3_neighbors = get_neighbors_at(graph, i3, graph.nodes[i3]['layer'])
        self.assertEqual(len(i3_neighbors), 3)

        i1_i2_n = [x for x in i1_neighbors if x in i2_neighbors]
        i1_i3_n = [x for x in i1_neighbors if x in i3_neighbors]
        i2_i3_n = [x for x in i2_neighbors if x in i3_neighbors]

        # Test i1-only neighbors
        for n in [
                x for x in i1_neighbors
                if x not in i1_i2_n and x not in i1_i3_n
        ]:
            self.assertEqual(graph.nodes[n]['label'], 'E')
            self.assertEqual(
                3, len(get_neighbors_at(graph, n, graph.nodes[i1]['layer'])))

        # Test i2-only neighbors
        for n in [
                x for x in i2_neighbors
                if x not in i1_i2_n and x not in i2_i3_n
        ]:
            self.assertEqual(graph.nodes[n]['label'], 'E')
            self.assertEqual(
                3, len(get_neighbors_at(graph, n, graph.nodes[i2]['layer'])))

        # Test i3-only neighbors
        for n in [
                x for x in i3_neighbors
                if x not in i1_i3_n and x not in i2_i3_n
        ]:
            self.assertEqual(graph.nodes[n]['label'], 'E')
            self.assertEqual(
                3, len(get_neighbors_at(graph, n, graph.nodes[i3]['layer'])))

        # Test nodes connected to 2 interiors
        for n in [x for x in i1_i2_n if x not in i1_i3_n and x not in i2_i3_n]:
            self.assertEqual(graph.nodes[n]['label'], 'E')
            self.assertEqual(
                5, len(get_neighbors_at(graph, n, graph.nodes[i1]['layer'])))

        for n in [x for x in i1_i3_n if x not in i1_i2_n and x not in i2_i3_n]:
            self.assertEqual(graph.nodes[n]['label'], 'E')
            self.assertEqual(
                5, len(get_neighbors_at(graph, n, graph.nodes[i1]['layer'])))

        for n in [x for x in i2_i3_n if x not in i1_i2_n and x not in i1_i3_n]:
            self.assertEqual(graph.nodes[n]['label'], 'E')
            self.assertEqual(
                5, len(get_neighbors_at(graph, n, graph.nodes[i1]['layer'])))

        # Test nodes connected to 3 interiors
        for n in [x for x in i2_i3_n if x in i1_i2_n and x in i1_i3_n]:
            self.assertEqual(graph.nodes[n]['label'], 'E')
            self.assertEqual(
                7, len(get_neighbors_at(graph, n, graph.nodes[i1]['layer'])))

        if visualize_tests:
            visualize_graph_3d(graph)
            pyplot.show()
示例#26
0
def createCorrectGraph():
    graph = Graph()
    e1 = gen_name()
    e2 = gen_name()
    i1 = gen_name()
    i2 = gen_name()
    I1 = gen_name()
    I2 = gen_name()
    e3 = gen_name()
    e4 = gen_name()
    e5 = gen_name()
    I3 = gen_name()
    I4 = gen_name()
    e6 = gen_name()
    e7 = gen_name()
    e8 = gen_name()
    graph.add_node(e1, layer=1, position=(1.0, 2.0), label='E')
    graph.add_node(e2, layer=1, position=(3.0, 2.0), label='E')
    graph.add_node(i2, layer=1, position=(2.0, 1.0), label='i')
    graph.add_node(i1, layer=1, position=(2.0, 3.0), label='i')
    graph.add_node(I1, layer=2, position=(1.5, 3.5), label='I')
    graph.add_node(I2, layer=2, position=(2.5, 3.5), label='I')
    graph.add_node(e3, layer=2, position=(1.0, 2.0), label='E')
    graph.add_node(e4, layer=2, position=(3.0, 2.0), label='E')
    graph.add_node(e5, layer=2, position=(2.0, 2.0), label='E')
    graph.add_node(I3, layer=2, position=(1.5, 0.5), label='I')
    graph.add_node(I4, layer=2, position=(2.5, 0.5), label='I')
    graph.add_node(e6, layer=2, position=(1.0, 2.0), label='E')
    graph.add_node(e7, layer=2, position=(3.0, 2.0), label='E')
    graph.add_node(e8, layer=2, position=(2.0, 2.0), label='E')
    # upper layer edges
    graph.add_edge(e1, i1)
    graph.add_edge(e1, i2)
    graph.add_edge(e2, i1)
    graph.add_edge(e2, i2)
    graph.add_edge(e1, e2)
    # interlayer connections
    graph.add_edge(i1, I1)
    graph.add_edge(i1, I2)
    graph.add_edge(i2, I3)
    graph.add_edge(i2, I4)
    # lower layer connections
    graph.add_edge(I1, e3)
    graph.add_edge(I1, e5)
    graph.add_edge(e3, e5)
    graph.add_edge(I2, e4)
    graph.add_edge(I2, e5)
    graph.add_edge(e4, e5)
    graph.add_edge(I3, e6)
    graph.add_edge(I3, e8)
    graph.add_edge(e6, e8)
    graph.add_edge(I4, e7)
    graph.add_edge(I4, e8)
    graph.add_edge(e7, e8)
    return graph
示例#27
0
        [i1_] = P9().apply(graph, [i1])
        self.visualize_if_enabled(graph)

        [i2_] = P9().apply(graph, [i2])
        self.visualize_if_enabled(graph)

        [] = P12().apply(graph, [i1, i2, i1_, i2_])
        self.visualize_if_enabled(graph)

        if self.visualize:
            visualize_graph_layer(graph, 0)
            pyplot.show()

            visualize_graph_layer(graph, 1)
            pyplot.show()

            visualize_graph_layer(graph, 2)
            pyplot.show()

    def visualize_if_enabled(self, graph):
        if self.visualize:
            visualize_graph_3d(graph)
            pyplot.show()


if __name__ == '__main__':
    graph = Graph()
    graph.add_node(gen_name(), layer=0, position=(0.5, 0.5), label='E')
    p1_positions = [(0, 0), (1, 0), (0, 1), (1, 1)]
    DerivationA(visualize=True).run(graph, p1_positions)
def derive_e():
    g = Graph()
    initial_node_name = gen_name()
    g.add_node(initial_node_name, layer=0, position=(0.5, 0.5), label='E')

    # Layer 1
    [a1, a2] = P1().apply(g, [initial_node_name])

    # Layer 2
    [b3, b2] = P2().apply(g, [a1], orientation=1)
    [b4, b1] = P2().apply(g, [a2], orientation=1)
    P6().apply(g, [a1, a2, b1, b2, b3, b4])

    # Layer 3
    [c3, c2] = P2().apply(g, [b1], orientation=1)
    [i11, c1] = P2().apply(g, [b2])
    [i13, i12] = P2().apply(g, [b3], orientation=1)
    [i14, c4] = P2().apply(g, [b4])
    P12().apply(g, [b3, b4, i13, i14])
    P12().apply(g, [b1, b4, c3, c4])
    P12().apply(g, [b2, b1, c1, c2])
    P13().apply(g, [b2, b3, i11, i12])

    # Layer 4
    [new_i11] = P9().apply(g, [i11])
    [new_i12] = P9().apply(g, [i12])
    [new_i13] = P9().apply(g, [i13])
    [new_i14] = P9().apply(g, [i14])
    [i22, d1] = P2().apply(g, [c1], orientation=1)
    [d3, d2] = P2().apply(g, [c2], orientation=1)
    [d4, d5] = P2().apply(g, [c3], orientation=2)
    [i25, d6] = P2().apply(g, [c4], orientation=2)
    P12().apply(g, [i12, i13, new_i12, new_i13])
    P12().apply(g, [i13, i14, new_i13, new_i14])
    P12().apply(g, [i14, c4, new_i14, i25])
    P12().apply(g, [i11, c1, new_i11, i22])
    P12().apply(g, [c2, c3, d3, d4])
    P6().apply(g, [c1, c2, d1, d2, d3, i22])
    P6().apply(g, [c3, c4, d4, d5, d6, i25])
    P13().apply(g, [i11, i12, new_i11, new_i12])
    i11 = new_i11
    i12 = new_i12
    i13 = new_i13
    i14 = new_i14

    # Layer 5
    [new_i11] = P9().apply(g, [i11])
    [new_i12] = P9().apply(g, [i12])
    [new_i13] = P9().apply(g, [i13])
    [new_i14] = P9().apply(g, [i14])
    [new_i22] = P9().apply(g, [i22])
    [new_i25] = P9().apply(g, [i25])
    [i21, e1] = P2().apply(g, [d1])
    [e3, e2] = P2().apply(g, [d2], orientation=1)
    [i23, e4] = P2().apply(g, [d3])
    [i24, e5] = P2().apply(g, [d4])
    [e7, e6] = P2().apply(g, [d5], orientation=1)
    [i26, e8] = P2().apply(g, [d6])
    P12().apply(g, [i12, i13, new_i12, new_i13])
    P12().apply(g, [i13, i14, new_i13, new_i14])
    P12().apply(g, [i11, i22, new_i11, new_i22])
    P12().apply(g, [i14, i25, new_i14, new_i25])
    P12().apply(g, [i22, d1, new_i22, i21])
    P12().apply(g, [i25, d6, new_i25, i26])
    P12().apply(g, [d2, d3, e3, e4])
    P12().apply(g, [d1, d2, e1, e2])
    P12().apply(g, [d4, d5, e5, e6])
    P12().apply(g, [d5, d6, e7, e8])
    P6().apply(g, [d3, d4, e4, e5, i23, i24])
    P13().apply(g, [d3, i22, new_i22, i23])
    P13().apply(g, [d4, i25, i24, new_i25])
    P13().apply(g, [i11, i12, new_i11, new_i12])
    i11 = new_i11
    i12 = new_i12
    i13 = new_i13
    i14 = new_i14
    i22 = new_i22
    i25 = new_i25

    # Layer 6
    [new_i11] = P9().apply(g, [i11])
    [new_i12] = P9().apply(g, [i12])
    [new_i13] = P9().apply(g, [i13])
    [new_i14] = P9().apply(g, [i14])
    [new_i21] = P9().apply(g, [i21])
    [new_i22] = P9().apply(g, [i22])
    [new_i23] = P9().apply(g, [i23])
    [new_i24] = P9().apply(g, [i24])
    [new_i25] = P9().apply(g, [i25])
    [new_i26] = P9().apply(g, [i26])
    P12().apply(g, [i12, i13, new_i12, new_i13])
    P12().apply(g, [i13, i14, new_i13, new_i14])
    P12().apply(g, [i11, i22, new_i11, new_i22])
    P12().apply(g, [i14, i25, new_i14, new_i25])
    P12().apply(g, [i21, i22, new_i21, new_i22])
    P12().apply(g, [i25, i26, new_i25, new_i26])
    [i31, i41] = P2().apply(g, [e1], orientation=1)
    [i42, f1] = P2().apply(g, [e2], orientation=1)
    [i43, f2] = P2().apply(g, [e3], orientation=2)
    [i32, i44] = P2().apply(g, [e4], orientation=2)
    [i33, i45] = P2().apply(g, [e5], orientation=1)
    [i46, f3] = P2().apply(g, [e6], orientation=1)
    [i47, f4] = P2().apply(g, [e7], orientation=2)
    [i34, i48] = P2().apply(g, [e8], orientation=2)
    P6().apply(g, [e1, e2, f1, i41, i42, i31])
    P6().apply(g, [e3, e4, f2, i43, i44, i32])
    P6().apply(g, [e5, e6, f3, i45, i46, i33])
    P6().apply(g, [e7, e8, f4, i47, i48, i34])
    P12().apply(g, [e2, e3, i42, i43])
    P12().apply(g, [e4, e5, i44, i45])
    P12().apply(g, [e6, e7, i46, i47])
    P12().apply(g, [i21, e1, new_i21, i31])
    P12().apply(g, [i23, e4, new_i23, i32])
    P12().apply(g, [i24, e5, new_i24, i33])
    P12().apply(g, [i26, e8, new_i26, i34])
    P13().apply(g, [i22, i23, new_i22, new_i23])
    P13().apply(g, [i23, i24, new_i23, new_i24])
    P13().apply(g, [i24, i25, new_i24, new_i25])
    P13().apply(g, [i11, i12, new_i11, new_i12])
    i11 = new_i11
    i12 = new_i12
    i13 = new_i13
    i14 = new_i14
    i21 = new_i21
    i22 = new_i22
    i23 = new_i23
    i24 = new_i24
    i25 = new_i25
    i26 = new_i26

    # Layer 7
    [new_i11] = P9().apply(g, [i11])
    [new_i12] = P9().apply(g, [i12])
    [new_i13] = P9().apply(g, [i13])
    [new_i14] = P9().apply(g, [i14])
    [new_i21] = P9().apply(g, [i21])
    [new_i22] = P9().apply(g, [i22])
    [new_i23] = P9().apply(g, [i23])
    [new_i24] = P9().apply(g, [i24])
    [new_i25] = P9().apply(g, [i25])
    [new_i26] = P9().apply(g, [i26])
    [new_i31] = P9().apply(g, [i31])
    [new_i32] = P9().apply(g, [i32])
    [new_i33] = P9().apply(g, [i33])
    [new_i34] = P9().apply(g, [i34])
    [new_i41] = P9().apply(g, [i41])
    [new_i42] = P9().apply(g, [i42])
    [new_i43] = P9().apply(g, [i43])
    [new_i44] = P9().apply(g, [i44])
    [new_i45] = P9().apply(g, [i45])
    [new_i46] = P9().apply(g, [i46])
    [new_i47] = P9().apply(g, [i47])
    [new_i48] = P9().apply(g, [i48])
    P12().apply(g, [i12, i13, new_i12, new_i13])
    P12().apply(g, [i13, i14, new_i13, new_i14])
    P12().apply(g, [i11, i22, new_i11, new_i22])
    P12().apply(g, [i14, i25, new_i14, new_i25])
    P12().apply(g, [i21, i22, new_i21, new_i22])
    P12().apply(g, [i25, i26, new_i25, new_i26])
    P12().apply(g, [i21, i31, new_i21, new_i31])
    P12().apply(g, [i23, i32, new_i23, new_i32])
    P12().apply(g, [i24, i33, new_i24, new_i33])
    P12().apply(g, [i26, i34, new_i26, new_i34])
    P12().apply(g, [i31, i41, new_i31, new_i41])
    P12().apply(g, [i31, i42, new_i31, new_i42])
    P12().apply(g, [i32, i43, new_i32, new_i43])
    P12().apply(g, [i32, i44, new_i32, new_i44])
    P12().apply(g, [i33, i45, new_i33, new_i45])
    P12().apply(g, [i33, i46, new_i33, new_i46])
    P12().apply(g, [i34, i47, new_i34, new_i47])
    P12().apply(g, [i34, i48, new_i34, new_i48])
    P12().apply(g, [i42, i43, new_i42, new_i43])
    P12().apply(g, [i44, i45, new_i44, new_i45])
    P12().apply(g, [i46, i47, new_i46, new_i47])
    [i52, i51] = P2().apply(g, [f1], orientation=1)
    [i54, i53] = P2().apply(g, [f2], orientation=1)
    [i56, i55] = P2().apply(g, [f3], orientation=1)
    [i58, i57] = P2().apply(g, [f4], orientation=1)
    P12().apply(g, [i41, f1, new_i41, i51])
    P12().apply(g, [i43, f2, new_i43, i53])
    P12().apply(g, [i45, f3, new_i45, i55])
    P12().apply(g, [i47, f4, new_i47, i57])
    P13().apply(g, [i22, i23, new_i22, new_i23])
    P13().apply(g, [i23, i24, new_i23, new_i24])
    P13().apply(g, [i24, i25, new_i24, new_i25])
    P13().apply(g, [i11, i12, new_i11, new_i12])
    P13().apply(g, [i42, f1, new_i42, i52])
    P13().apply(g, [i44, f2, new_i44, i54])
    P13().apply(g, [i46, f3, new_i46, i56])
    P13().apply(g, [i48, f4, new_i48, i58])

    return g
示例#29
0
    def apply(self, graph: Graph, prod_input: List[str], orientation: int = 0, **kwargs) -> List[str]:
        eps = kwargs.get('epsilon', 1e-6)
        self.__check_prod_input(graph, prod_input, eps)

        [i] = prod_input
        i_data = graph.nodes[i]
        i_data['label'] = 'i'
        i_layer = i_data['layer']
        new_layer = i_layer + 1

        # get 'E' nodes from the left side of production
        [e1, e2, e3] = self.get_corner_nodes(graph, i, i_layer, orientation)

        e12 = self.get_node_between(graph, e1, e2, i_layer, eps)
        e23 = self.get_node_between(graph, e2, e3, i_layer, eps)
        e31 = self.get_node_between(graph, e3, e1, i_layer, eps)

        # create new 'E' nodes in the next layer
        new_e1 = gen_name()
        new_e2 = gen_name()
        new_e3 = gen_name()

        new_e12 = gen_name()
        new_e23 = gen_name()
        new_e31 = gen_name()

        graph.add_node(new_e1, layer=new_layer, position=graph.nodes[e1]['position'], label='E')
        graph.add_node(new_e2, layer=new_layer, position=graph.nodes[e2]['position'], label='E')
        graph.add_node(new_e3, layer=new_layer, position=graph.nodes[e3]['position'], label='E')

        graph.add_node(new_e12, layer=new_layer, position=graph.nodes[e12]['position'], label='E')
        graph.add_node(new_e23, layer=new_layer, position=graph.nodes[e23]['position'], label='E')
        graph.add_node(new_e31, layer=new_layer, position=graph.nodes[e31]['position'], label='E')

        # create edges between new 'E' nodes
        graph.add_edge(new_e1, new_e12)
        graph.add_edge(new_e12, new_e2)

        graph.add_edge(new_e2, new_e23)
        graph.add_edge(new_e23, new_e3)

        graph.add_edge(new_e3, new_e31)
        graph.add_edge(new_e31, new_e1)

        graph.add_edge(new_e23, new_e31)
        graph.add_edge(new_e12, new_e31)
        graph.add_edge(new_e2, new_e31)

        # create new 'I' nodes and edges between new 'I' nodes and new 'E' nodes
        i1 = add_interior(graph, new_e1, new_e12, new_e31)
        i3 = add_interior(graph, new_e3, new_e23, new_e31)
        i2a = add_interior(graph, new_e2, new_e12, new_e31)
        i2b = add_interior(graph, new_e2, new_e23, new_e31)

        # create edges between new 'I' nodes and parent 'i' node
        graph.add_edge(i1, i)
        graph.add_edge(i3, i)
        graph.add_edge(i2a, i)
        graph.add_edge(i2b, i)

        return [i1, i3, i2a, i2b]
示例#30
0
    def test_in_bigger_graph(self):
        graph = Graph()
        positions = [(0, 0), (0, 1), (1, 0), (0, 0.5), (0.5, 0.5)]

        vx_e1 = gen_name()
        vx_e2 = gen_name()
        vx_e3 = gen_name()
        vx_e12 = gen_name()
        vx_e23 = gen_name()
        graph.add_node(vx_e1, layer=0, position=positions[0], label='E')
        graph.add_node(vx_e2, layer=0, position=positions[1], label='E')
        graph.add_node(vx_e3, layer=0, position=positions[2], label='E')
        graph.add_node(vx_e12, layer=0, position=positions[3], label='E')
        graph.add_node(vx_e23, layer=0, position=positions[4], label='E')

        graph.add_edge(vx_e1, vx_e12)
        graph.add_edge(vx_e12, vx_e2)
        graph.add_edge(vx_e2, vx_e23)
        graph.add_edge(vx_e23, vx_e3)
        graph.add_edge(vx_e3, vx_e1)

        vx_e1122 = gen_name()
        graph.add_node(vx_e1122, layer=0, position=(-0.5, 0.5), label='E')
        graph.add_edge(vx_e1, vx_e1122)
        graph.add_edge(vx_e12, vx_e1122)
        graph.add_edge(vx_e2, vx_e1122)

        vx_e2233 = gen_name()
        graph.add_node(vx_e2233, layer=0, position=(1, 1), label='E')
        graph.add_edge(vx_e2, vx_e2233)
        graph.add_edge(vx_e23, vx_e2233)
        graph.add_edge(vx_e3, vx_e2233)

        vx_e13 = gen_name()
        graph.add_node(vx_e13, layer=0, position=(0.5, -0.5), label='E')
        graph.add_edge(vx_e1, vx_e13)
        graph.add_edge(vx_e3, vx_e13)

        I = add_interior(graph, vx_e1, vx_e2, vx_e3)
        I1 = add_interior(graph, vx_e1122, vx_e1, vx_e12)
        I2 = add_interior(graph, vx_e1122, vx_e12, vx_e2)
        I3 = add_interior(graph, vx_e2233, vx_e2, vx_e23)
        I4 = add_interior(graph, vx_e2233, vx_e23, vx_e3)
        I5 = add_interior(graph, vx_e1, vx_e3, vx_e13)

        if visualize_tests:
            visualize_graph_3d(graph)
            pyplot.show()

        [i1, i2, i3] = P4().apply(graph, [I])

        self.assertEqual(len(graph.nodes()), 22)
        self.assertEqual(len(graph.edges()), 50)

        self.assertEqual(graph.nodes[I]['label'], 'i')
        self.assertTrue(graph.has_edge(I, i1))
        self.assertTrue(graph.has_edge(I, i2))
        self.assertTrue(graph.has_edge(I, i3))

        self.assertEqual(graph.nodes[i1]['label'], 'I')
        self.assertEqual(graph.nodes[i2]['label'], 'I')
        self.assertEqual(graph.nodes[i3]['label'], 'I')
        self.assertEqual(graph.nodes[i1]['layer'], graph.nodes[I]['layer'] + 1)
        self.assertEqual(graph.nodes[i2]['layer'], graph.nodes[I]['layer'] + 1)
        self.assertEqual(graph.nodes[i3]['layer'], graph.nodes[I]['layer'] + 1)

        i1_neighbors = get_neighbors_at(graph, i1, graph.nodes[i1]['layer'])
        self.assertEqual(len(i1_neighbors), 3)
        i2_neighbors = get_neighbors_at(graph, i2, graph.nodes[i2]['layer'])
        self.assertEqual(len(i2_neighbors), 3)
        i3_neighbors = get_neighbors_at(graph, i3, graph.nodes[i3]['layer'])
        self.assertEqual(len(i3_neighbors), 3)

        i1_i2_n = [x for x in i1_neighbors if x in i2_neighbors]
        i1_i3_n = [x for x in i1_neighbors if x in i3_neighbors]
        i2_i3_n = [x for x in i2_neighbors if x in i3_neighbors]

        # Test i1-only neighbors
        for n in [
                x for x in i1_neighbors
                if x not in i1_i2_n and x not in i1_i3_n
        ]:
            self.assertEqual(graph.nodes[n]['label'], 'E')
            self.assertEqual(
                3, len(get_neighbors_at(graph, n, graph.nodes[i1]['layer'])))

        # Test i2-only neighbors
        for n in [
                x for x in i2_neighbors
                if x not in i1_i2_n and x not in i2_i3_n
        ]:
            self.assertEqual(graph.nodes[n]['label'], 'E')
            self.assertEqual(
                3, len(get_neighbors_at(graph, n, graph.nodes[i2]['layer'])))

        # Test i3-only neighbors
        for n in [
                x for x in i3_neighbors
                if x not in i1_i3_n and x not in i2_i3_n
        ]:
            self.assertEqual(graph.nodes[n]['label'], 'E')
            self.assertEqual(
                3, len(get_neighbors_at(graph, n, graph.nodes[i3]['layer'])))

        # Test nodes connected to 2 interiors
        for n in [x for x in i1_i2_n if x not in i1_i3_n and x not in i2_i3_n]:
            self.assertEqual(graph.nodes[n]['label'], 'E')
            self.assertEqual(
                5, len(get_neighbors_at(graph, n, graph.nodes[i1]['layer'])))

        for n in [x for x in i1_i3_n if x not in i1_i2_n and x not in i2_i3_n]:
            self.assertEqual(graph.nodes[n]['label'], 'E')
            self.assertEqual(
                5, len(get_neighbors_at(graph, n, graph.nodes[i1]['layer'])))

        for n in [x for x in i2_i3_n if x not in i1_i2_n and x not in i1_i3_n]:
            self.assertEqual(graph.nodes[n]['label'], 'E')
            self.assertEqual(
                5, len(get_neighbors_at(graph, n, graph.nodes[i1]['layer'])))

        # Test nodes connected to 3 interiors
        for n in [x for x in i2_i3_n if x in i1_i2_n and x in i1_i3_n]:
            self.assertEqual(graph.nodes[n]['label'], 'E')
            self.assertEqual(
                7, len(get_neighbors_at(graph, n, graph.nodes[i1]['layer'])))

        if visualize_tests:
            visualize_graph_3d(graph)
            pyplot.show()