def BFS(root, end): oce_od_elementa = collections.defaultdict(tuple) graf = Graph() vrsta = [] seen = set() # dodam root vrsta.append(list_to_tuple(root)) seen.add(str(root)) # kopija = naredi_matriko(root) #kopija start napolni(graf, root) stevilo_pregledanih_vozlisc = 1 while vrsta: vozlisce = vrsta.pop(0) for neighbour in graf.get(vozlisce): if str(neighbour) not in seen: stevilo_pregledanih_vozlisc += 1 oce_od_elementa[neighbour] = vozlisce napolni(graf, neighbour) vrsta.append(neighbour) seen.add(str(neighbour)) if tuple_to_list(neighbour) == end: print("Stevilo pregledanih vozlisc:", stevilo_pregledanih_vozlisc) return find_path(graf, neighbour, oce_od_elementa, root)
def A_star(root): graf = Graph() queue = [list_to_tuple(root)] oce_od_elementa = collections.defaultdict(tuple) g_score = collections.defaultdict(int) g_score[list_to_tuple(root)] = 0 f_score = collections.defaultdict(int) #f_score[list_to_tuple(root)] = eucledian_distance(root, NxP_end) f_score[list_to_tuple(root)] = wrong_place(root, NxP_end) #worng place napolni(graf, root) #napolni vse moznosti iz root-a stevilo_pregledanih_vozlisc = 1 while queue: current = min_value_dict(queue, f_score) #get lowest score in f_score dict if current == list_to_tuple(NxP_end): print('Stevilo pregledanih vozlisc:', stevilo_pregledanih_vozlisc) return find_path( graf, current, oce_od_elementa, NxP_start ) # current # find_path(graf, current, oce_od_elementa) queue.remove(current) for neighbour in graf.get(current): #dodam soseda v g_score z max value g_score[neighbour] = sys.maxsize stevilo_pregledanih_vozlisc += 1 zacasen_g_score = g_score[list_to_tuple(current)] + distance( current, neighbour) if zacasen_g_score < g_score[neighbour]: if neighbour not in oce_od_elementa: oce_od_elementa[neighbour] = current g_score[neighbour] = zacasen_g_score #f_score[neighbour] = g_score[neighbour] + eucledian_distance(current, NxP_end) f_score[neighbour] = g_score[neighbour] + wrong_place( current, NxP_end) #dodam v graf vse podvozisse vozlisc neigbour napolni(graf, neighbour) if neighbour not in queue: queue.append(neighbour) return "Fail"
def dvosmerno_bfs(root, end): global st_obiskanih_vozlisc oce_od_elementa = collections.defaultdict(tuple) oce_od_elementa_nzj = collections.defaultdict(tuple) g = Graph() ng = Graph() vrsta = [] vrsta_nzj = [] seen = set() seen_nzj = set() # dodam root vrsta.append(list_to_tuple(root)) vrsta_nzj.append(list_to_tuple(end)) seen.add(str(root)) seen_nzj.add(str(end)) # kopija = naredi_matriko(root) #kopija start resitve = (list_to_tuple(end), ) konci = (list_to_tuple(root), ) napolni(g, root) napolni(ng, end) stevilo_pregledanih_vozlisc = 1 while vrsta: vozlisce = vrsta.pop(0) vozlisce_nzj = vrsta_nzj.pop(0) for neighbour in ng.get(vozlisce_nzj): if str(neighbour) not in seen_nzj: stevilo_pregledanih_vozlisc += 1 oce_od_elementa_nzj[neighbour] = vozlisce_nzj napolni(ng, neighbour) vrsta_nzj.append(neighbour) seen_nzj.add(str(neighbour)) resitve += (neighbour, ) if neighbour in konci: st_obiskanih_vozlisc = stevilo_pregledanih_vozlisc return find_path(g, neighbour, oce_od_elementa, root) + find_path_reverse( ng, neighbour, oce_od_elementa_nzj, end) for neighbour in g.get(vozlisce): if str(neighbour) not in seen: stevilo_pregledanih_vozlisc += 1 oce_od_elementa[neighbour] = vozlisce napolni(g, neighbour) vrsta.append(neighbour) seen.add(str(neighbour)) konci += (neighbour, ) if neighbour in resitve: st_obiskanih_vozlisc = stevilo_pregledanih_vozlisc return find_path(g, neighbour, oce_od_elementa, root) + find_path_reverse( ng, neighbour, oce_od_elementa_nzj, end)
def setUp(self): self.graph = Graph()
class TestGraph(unittest.TestCase): """ This class contains the test methods that influence the creation of the members in a GrAF. """ def setUp(self): self.graph = Graph() def test_create_annotation_space(self): # Test values as_id = 'as_id' aspace = self.graph.annotation_spaces.create(as_id) assert(aspace.as_id == as_id) assert(list(self.graph.annotation_spaces) == [aspace]) def test_add_annotation_space(self): # Test values as_id = 'as_id' aspace = AnnotationSpace(as_id) self.graph.annotation_spaces.add(aspace) assert(self.graph.annotation_spaces[as_id] == aspace) def test_add_edge(self): # Test values fnode = Node('node_1') # From Node tnode = Node('node_2') # To Node edge = Edge('id_test', fnode, tnode) self.graph.nodes.add(fnode) self.graph.nodes.add(tnode) self.graph.edges.add(edge) assert(list(self.graph.edges)[0] == edge) def test_create_edge(self): # Test values fnode = Node('node_1') # From Node tnode = Node('node_2') # To Node self.graph.create_edge(fnode, tnode, id='3') assert(1 == len(self.graph.edges)) assert(self.graph.edges['3'].from_node == fnode) assert(self.graph.edges['3'].to_node == tnode) def test_add_feature(self): name = 'feature' value = 'value' self.graph.features[name] = value assert(self.graph.features[name] == value) def test_add_node(self): node = Node('test_node') self.graph.nodes.add(node) assert(list(self.graph.nodes) == [node]) def test_add_region(self): # Test values # The Region needs at least 2 anchors #anchor = Anchor(t) # Tokenizer anchors = ['anchor1', 'anchor2'] id = '1' region = Region(id, *anchors) self.graph.regions.add(region) assert(list(self.graph.regions) == [region]) def test_get_edge_by_id(self): fnode = Node('node_1') # From Node tnode = Node('node_2') # To Node edge = Edge('id_test', fnode, tnode) self.graph.nodes.add(fnode) self.graph.nodes.add(tnode) self.graph.edges.add(edge) assert(self.graph.edges['id_test'] == edge) def test_get_edge_by_nodes(self): fnode = Node('node_1') # From Node tnode = Node('node_2') # To Node edge = Edge('id_test', fnode, tnode) self.graph.nodes.add(fnode) self.graph.nodes.add(tnode) self.graph.edges.add(edge) assert(self.graph.find_edge(fnode, tnode) ==edge) assert(self.graph.find_edge(fnode.id, tnode.id) ==edge) def test_get_node(self): node = Node('test_node') self.graph.nodes.add(node) assert(self.graph.nodes['test_node'] ==node) def test_get_region(self): node = Node('test_node') self.graph.nodes.add(node) assert(self.graph.nodes['test_node'] ==node) def test_get_annotation_space(self): aspace = AnnotationSpace('as_id') self.graph.annotation_spaces.add(aspace) assert(self.graph.annotation_spaces['as_id'] ==aspace) def test_get_region_from_id(self): region = Region('1', 'anchor1', 'anchor2') self.graph.regions.add(region) assert(self.graph.regions['1'] ==region) def test_get_region_from_anchors(self): region = Region('1', 'anchor1', 'anchor2') self.graph.regions.add(region) assert(self.graph.get_region('anchor1', 'anchor2') == region) def test_get_root(self): node = Node('test_node') self.graph.nodes.add(node) self.graph.root = node assert(self.graph.root == node) def test_iter_roots(self): node = Node('test_node') self.graph.nodes.add(node) self.graph.root = node assert(list(self.graph.iter_roots()) == [node]) def test_parents_and_children(self): n1 = Node('n1') n2 = Node('n2') n3 = Node('n3') n4 = Node('n4') self.graph.nodes.add(n1) self.graph.nodes.add(n2) self.graph.nodes.add(n3) self.graph.nodes.add(n4) self.graph.create_edge(n1, n2) self.graph.create_edge(n2, n1) self.graph.create_edge(n1, n3) self.graph.create_edge(n3, n4) assert(list(n1.iter_children()) == [n2, n3]) assert(list(n2.iter_children()) == [n1]) assert(list(n3.iter_children()) == [n4]) assert(list(n4.iter_children()) == []) assert(list(n1.iter_parents()) == [n2]) assert(list(n2.iter_parents()) == [n1]) assert(list(n3.iter_parents()) == [n1]) assert(list(n4.iter_parents()) == [n3])
# # Copyright (C) 2009-2012 Poio Project # Author: António Lopes <*****@*****.**> # URL: <http://media.cidles.eu/poio/> # For license information, see LICENSE.TXT import sys from graf import Node, Graph, Edge, FeatureStructure, Annotation, GrafRenderer # Create three nodes node_one = Node('node_one') node_two = Node('node_two') node_three = Node('node_three') # Create the Annotation Graph and set the values graph = Graph() #Adding the nodes graph.nodes.add(node_one) graph.nodes.add(node_two) graph.nodes.add(node_three) # Create the edge edge = Edge(node_one, node_three) # Add an the edge #graph.add_edge(edge) # Add Features feature_strct = FeatureStructure() # Structure
class TestGraph(unittest.TestCase): """ This class contain the test methods if the class Graph. """ def setUp(self): self.graph = Graph() def test_create_annotation_space(self): # Test values name = 'name' type = 'type' aspace = self.graph.annotation_spaces.create(name, type) self.assertIsInstance(aspace, AnnotationSpace) self.assertEqual(aspace.name, name) self.assertEqual(aspace.type, type) self.assertListEqual(list(self.graph.annotation_spaces), [aspace]) def test_add_annotation_space(self): # Test values name = 'name' type = 'type' aspace = AnnotationSpace(name, type) self.graph.annotation_spaces.add(aspace) self.assertEqual(self.graph.annotation_spaces[name], aspace) def test_add_edge(self): # Test values fnode = Node('node_1') # From Node tnode = Node('node_2') # To Node edge = Edge('id_test', fnode, tnode) self.graph.nodes.add(fnode) self.graph.nodes.add(tnode) self.graph.edges.add(edge) self.assertListEqual(list(self.graph.edges), [edge]) def test_create_edge(self): # Test values fnode = Node('node_1') # From Node tnode = Node('node_2') # To Node self.graph.create_edge(fnode, tnode, id='3') self.assertEqual(1, len(self.graph.edges)) self.assertEqual(self.graph.edges['3'].from_node, fnode) self.assertEqual(self.graph.edges['3'].to_node, tnode) def test_add_feature(self): name = 'feature' value = 'value' self.graph.features[name] = value self.assertEqual(self.graph.features[name], value) def test_add_node(self): node = Node('test_node') self.graph.nodes.add(node) self.assertListEqual(list(self.graph.nodes), [node]) def test_add_region(self): # Test values # The Region needs at least 2 anchors #anchor = Anchor(t) # Tokenizer anchors = ['anchor1', 'anchor2'] id = '1' region = Region(id, *anchors) self.graph.regions.add(region) self.assertListEqual(list(self.graph.regions), [region]) def test_get_edge_by_id(self): fnode = Node('node_1') # From Node tnode = Node('node_2') # To Node edge = Edge('id_test', fnode, tnode) self.graph.nodes.add(fnode) self.graph.nodes.add(tnode) self.graph.edges.add(edge) self.assertEqual(self.graph.edges['id_test'], edge) def test_get_edge_by_nodes(self): fnode = Node('node_1') # From Node tnode = Node('node_2') # To Node edge = Edge('id_test', fnode, tnode) self.graph.nodes.add(fnode) self.graph.nodes.add(tnode) self.graph.edges.add(edge) self.assertEqual(self.graph.find_edge(fnode, tnode), edge) self.assertEqual(self.graph.find_edge(fnode.id, tnode.id), edge) def test_get_node(self): node = Node('test_node') self.graph.nodes.add(node) self.assertEqual(self.graph.nodes['test_node'], node) def test_get_region(self): node = Node('test_node') self.graph.nodes.add(node) self.assertEqual(self.graph.nodes['test_node'], node) def test_get_annotation_space(self): aspace = AnnotationSpace('name', 'type') self.graph.annotation_spaces.add(aspace) self.assertEqual(self.graph.annotation_spaces['name'], aspace) def test_get_region_from_id(self): region = Region('1', 'anchor1', 'anchor2') self.graph.regions.add(region) self.assertEqual(self.graph.regions['1'], region) def test_get_region_from_anchors(self): region = Region('1', 'anchor1', 'anchor2') self.graph.regions.add(region) self.assertEqual(self.graph.get_region('anchor1', 'anchor2'), region) def test_set_root_not_in_graph(self): node = Node('test_node') self.assertRaises(ValueError, lambda: setattr(self.graph, 'root', node)) def test_get_root(self): node = Node('test_node') self.graph.nodes.add(node) self.graph.root = node self.assertEqual(self.graph.root, node) def test_iter_roots(self): node = Node('test_node') self.graph.nodes.add(node) self.graph.root = node self.assertListEqual(list(self.graph.iter_roots()), [node]) def test_parents_and_children(self): n1 = Node('n1') n2 = Node('n2') n3 = Node('n3') n4 = Node('n4') self.graph.nodes.add(n1) self.graph.nodes.add(n2) self.graph.nodes.add(n3) self.graph.nodes.add(n4) self.graph.create_edge(n1, n2) self.graph.create_edge(n2, n1) self.graph.create_edge(n1, n3) self.graph.create_edge(n3, n4) self.assertListEqual(list(n1.iter_children()), [n2, n3]) self.assertListEqual(list(n2.iter_children()), [n1]) self.assertListEqual(list(n3.iter_children()), [n4]) self.assertListEqual(list(n4.iter_children()), []) self.assertListEqual(list(n1.iter_parents()), [n2]) self.assertListEqual(list(n2.iter_parents()), [n1]) self.assertListEqual(list(n3.iter_parents()), [n1]) self.assertListEqual(list(n4.iter_parents()), [n3])
# # Copyright (C) 2009-2012 Poio Project # Author: António Lopes <*****@*****.**> # URL: <http://media.cidles.eu/poio/> # For license information, see LICENSE.TXT import sys from graf import Node, Graph, Edge, FeatureStructure, Annotation, GrafRenderer # Create three nodes node_one = Node('node_one') node_two = Node('node_two') node_three = Node('node_three') # Create the Annotation Graph and set the values graph = Graph() #Adding the nodes graph.nodes.add(node_one) graph.nodes.add(node_two) graph.nodes.add(node_three) # Create an edge edge = graph.create_edge(node_one, node_three) # Add Features feature_strct = FeatureStructure() # Structure # Adding two features # There are two ways to it. # Use the simple first
class TestGraph: """ This class contains the test methods that influence the creation of the members in a GrAF. """ def setUp(self): self.graph = Graph() def test_create_annotation_space(self): # Test values as_id = 'as_id' aspace = self.graph.annotation_spaces.create(as_id) assert(aspace.as_id == as_id) assert(list(self.graph.annotation_spaces) == [aspace]) def test_add_annotation_space(self): # Test values as_id = 'as_id' aspace = AnnotationSpace(as_id) self.graph.annotation_spaces.add(aspace) assert(self.graph.annotation_spaces[as_id] == aspace) def test_add_edge(self): # Test values fnode = Node('node_1') # From Node tnode = Node('node_2') # To Node edge = Edge('id_test', fnode, tnode) self.graph.nodes.add(fnode) self.graph.nodes.add(tnode) self.graph.edges.add(edge) assert(list(self.graph.edges)[0] == edge) def test_create_edge(self): # Test values fnode = Node('node_1') # From Node tnode = Node('node_2') # To Node self.graph.create_edge(fnode, tnode, id='3') assert(1 == len(self.graph.edges)) assert(self.graph.edges['3'].from_node == fnode) assert(self.graph.edges['3'].to_node == tnode) def test_add_feature(self): name = 'feature' value = 'value' self.graph.features[name] = value assert(self.graph.features[name] == value) def test_add_node(self): node = Node('test_node') self.graph.nodes.add(node) assert(list(self.graph.nodes) == [node]) def test_add_region(self): # Test values # The Region needs at least 2 anchors #anchor = Anchor(t) # Tokenizer anchors = ['anchor1', 'anchor2'] id = '1' region = Region(id, *anchors) self.graph.regions.add(region) assert(list(self.graph.regions) == [region]) def test_get_edge_by_id(self): fnode = Node('node_1') # From Node tnode = Node('node_2') # To Node edge = Edge('id_test', fnode, tnode) self.graph.nodes.add(fnode) self.graph.nodes.add(tnode) self.graph.edges.add(edge) assert(self.graph.edges['id_test'] == edge) def test_get_edge_by_nodes(self): fnode = Node('node_1') # From Node tnode = Node('node_2') # To Node edge = Edge('id_test', fnode, tnode) self.graph.nodes.add(fnode) self.graph.nodes.add(tnode) self.graph.edges.add(edge) assert(self.graph.find_edge(fnode, tnode) ==edge) assert(self.graph.find_edge(fnode.id, tnode.id) ==edge) def test_get_node(self): node = Node('test_node') self.graph.nodes.add(node) assert(self.graph.nodes['test_node'] ==node) def test_get_region(self): node = Node('test_node') self.graph.nodes.add(node) assert(self.graph.nodes['test_node'] ==node) def test_get_annotation_space(self): aspace = AnnotationSpace('as_id') self.graph.annotation_spaces.add(aspace) assert(self.graph.annotation_spaces['as_id'] ==aspace) def test_get_region_from_id(self): region = Region('1', 'anchor1', 'anchor2') self.graph.regions.add(region) assert(self.graph.regions['1'] ==region) def test_get_region_from_anchors(self): region = Region('1', 'anchor1', 'anchor2') self.graph.regions.add(region) assert(self.graph.get_region('anchor1', 'anchor2') == region) def test_get_root(self): node = Node('test_node') self.graph.nodes.add(node) self.graph.root = node assert(self.graph.root == node) def test_iter_roots(self): node = Node('test_node') self.graph.nodes.add(node) self.graph.root = node assert(list(self.graph.iter_roots()) == [node]) def test_parents_and_children(self): n1 = Node('n1') n2 = Node('n2') n3 = Node('n3') n4 = Node('n4') self.graph.nodes.add(n1) self.graph.nodes.add(n2) self.graph.nodes.add(n3) self.graph.nodes.add(n4) self.graph.create_edge(n1, n2) self.graph.create_edge(n2, n1) self.graph.create_edge(n1, n3) self.graph.create_edge(n3, n4) assert(list(n1.iter_children()) == [n2, n3]) assert(list(n2.iter_children()) == [n1]) assert(list(n3.iter_children()) == [n4]) assert(list(n4.iter_children()) == []) assert(list(n1.iter_parents()) == [n2]) assert(list(n2.iter_parents()) == [n1]) assert(list(n3.iter_parents()) == [n1]) assert(list(n4.iter_parents()) == [n3]) # TODO: Test makes wrong assumption. The problem is not that # Annotations might get added twice, but that one file might # be parsed twice. # def test_verify_annotation_existence(self): # """ Verification if the same annotation is parsed # more then one time. The same annotation can only # exist and allowed to be added one time. # """ # node = Node('test_node') # annotation_1 = Annotation('annotation_value', None, 'id-1') # # Same id # annotation_2 = Annotation('annotation_value', None, 'id-1') # # Add the first node # node.annotations.add(annotation_1) # # Try to add again the same annotation # node.annotations.add(annotation_2) # self.graph.nodes.add(node) # expected_result = 1 # element = self.graph.get_element('test_node') # assert(len(element.annotations) == expected_result) # TODO: Test makes wrong assumption. The problem is not that # Annotations might get added twice, but that one file might # be parsed twice. # def test_verify_edge_existence(self): # """ Verification if the same edge is parsed # more then one time. The same edge can only # exist and allowed to be added one time. # """ # # Test values # fnode = Node('node_1') # From Node # tnode = Node('node_2') # To Node # edge_1 = Edge('id_test', fnode, tnode) # # Same id # edge_2 = Edge('id_test', fnode, tnode) # self.graph.nodes.add(fnode) # self.graph.nodes.add(tnode) # self.graph.edges.add(edge_1) # # Try to add again the edge annotation # self.graph.edges.add(edge_2) # assert(len(self.graph.edges) == 1)