def test_distance_to(self): node1 = graph.Node("1") node2 = graph.Node("2") node1.add_neighbor(node2, 5) self.assertEqual(node1.get_distance_to(node2), 5) self.assertEqual(node2.get_distance_to(node1), float("inf"))
def construct_connection_graph(self, chromo_haplotype): """construct_connection_graph [create a graph structure based on primary and secondary phase set] node: phase set edge: shared record between phaseset Args: chromo_haplotype (ChromosomoHaplotype): [description] """ for phase_set in self.chromo_phase_set.values(): self.graph_struct.insert_node(graph.Node(phase_set.starting_pos, 0)) # primary node for phase_set in chromo_haplotype.chromo_phase_set.values(): self.graph_struct.insert_node(graph.Node(phase_set.starting_pos, 1)) # secondary Node phase_set: PhaseSet for phase_set in self.chromo_phase_set.values(): met_phase_set = set() for record_pos in phase_set.records_idx: if record_pos not in chromo_haplotype.chromo_record2phaseset_map.keys( ): # not in secondary phase set continue ps_secondary = chromo_haplotype.chromo_record2phaseset_map[ record_pos] if ps_secondary in met_phase_set: # connection already found continue self.graph_struct.add_edge_with_ps(phase_set.starting_pos, ps_secondary) met_phase_set.clear() self.graph_struct.load_connected_components()
def __init__(self, tensor, node_desc, graph_creator, activation_size=None): self.tensor = tensor global object_id self.object_id = object_id object_id += 1 self.node_desc = node_desc i = 0 for i in range(len(graph_creator.summary)): if str(graph_creator.summary[i]['layer_name']) == node_desc: break if i < len(graph_creator.summary) and node_desc == str(graph_creator.summary[i]['layer_name']): summary_elem = graph_creator.summary.pop(i) forward_compute_time = summary_elem['forward_time'] backward_compute_time = summary_elem['backward_time'] if isinstance(summary_elem['output_shape'][0], list): activation_sizes = [4.0 * functools.reduce(lambda x, y: x * y, elem) for elem in summary_elem['output_shape']] else: activation_sizes = 4.0 * functools.reduce(lambda x, y: x * y, summary_elem['output_shape']) parameter_size = 4.0 * float(summary_elem['nb_params']) self._node = graph.Node("node%d" % object_id, node_desc=node_desc, forward_compute_time=forward_compute_time, backward_compute_time=backward_compute_time, activation_size=activation_sizes, parameter_size=parameter_size) elif activation_size is not None: self._node = graph.Node("node%d" % object_id, node_desc=node_desc, activation_size=activation_size) else: self._node = graph.Node("node%d" % object_id, node_desc=node_desc) self.graph_creator = graph_creator
def test_add_inbound_duplicate(self): """Tests that adding the same inbound edge twice will not dupe.""" begin_node = graph.Node(self.UNIQUE_KEY_1) end_node = graph.Node(self.UNIQUE_KEY_2) end_node.add_inbound(begin_node) end_node.add_inbound(begin_node) self.assertEqual(end_node.inbound, {begin_node})
def test_basic(self): g = graph.DGraph() a = graph.Node('a') b = graph.Node('b') self.assertFalse(g.contains(a)) # neither node is in the graph with self.assertRaises(Exception): g.addedge(a, b) g.addnode(a) self.assertTrue(g.contains(a)) # can't add a node twice with self.assertRaises(graph.GraphException): g.addnode(a) # still can't add the edge because b isn't there with self.assertRaises(Exception): g.addedge(a, b) g.addnode(b) g.addedge(a, b) # adding an edge twice is ok g.addedge(a, b)
def test_zigzag(self): gr = graph.UGraph() a = graph.Node('a') b = graph.Node('b') c = graph.Node('c') d = graph.Node('d') e = graph.Node('e') f = graph.Node('f') g = graph.Node('g') h = graph.Node('h') i = graph.Node('i') j = graph.Node('j') gr.addnodes(a, b, c, d, e, f, g, h, i, j) gr.addedge(a, b) gr.addedge(a, c) gr.addedge(b, d) gr.addedge(b, e) gr.addedge(c, f) gr.addedge(d, g) gr.addedge(d, h) gr.addedge(f, i) gr.addedge(f, j) r = graph.bfs_zigzag(gr, a) self.assertEqual('abcdefghij', r)
def build_problem1_graph(): csv_file = open(constants.DATASET_FILE, mode="r") lines = csv_file.readlines() csv_file.close() split_lines = [l.strip('\n').strip(' ').split(',') for l in lines] # l[0] is the origin # l[1] is the destination # l[3] is the straight line distance node_id = 0 for l in split_lines: l[0] = l[0].lower().strip(' ') l[1] = l[1].lower().strip(' ') if l[0] not in shared.GRAPH_NODES: graph_node = graph.Node(node_id, l[0]) node_id += 1 shared.GRAPH_NODES[l[0]] = graph_node if l[1] not in shared.GRAPH_NODES: graph_node = graph.Node(node_id, l[1]) node_id += 1 shared.GRAPH_NODES[l[1]] = graph_node for l in split_lines: l[0] = l[0].lower().strip(' ') l[1] = l[1].lower().strip(' ') node1 = shared.GRAPH_NODES[l[0]] node2 = shared.GRAPH_NODES[l[1]] node1.add_edge(node2, float(l[2])) node2.add_edge(node1, float(l[2]))
def test_addNode(self): g = graph.UGraph() n1 = graph.Node('a') g.addnode(n1) self.assertEqual(1, len(g)) with self.assertRaises(graph.GraphException): g.addnode(n1) n2 = graph.Node('a') with self.assertRaises(graph.GraphException): g.addnode(n2) self.assertEqual(1, len(g)) n3 = graph.Node('b') g.addnode(n3) self.assertEqual(2, len(g)) self.assertTrue(g.contains(n1)) self.assertTrue(g.contains(n2)) self.assertTrue(g.contains(n3)) n4 = graph.Node('notMe') self.assertFalse(g.contains(n4))
def test_cmp(self): a = graph.Node('aaa') b = graph.Node('bbb') c = graph.Node('ccc') self.assertTrue(b == b) self.assertTrue(a < b) self.assertTrue(b < c) self.assertTrue(c > a)
def _build(self, pattern=[], fragments_stack=[]): if not pattern: return for c in pattern: fragment = Fragment() if c == expression.REGEX_NOTATION.CONCATENATION: fragment_latter = fragments_stack.pop() fragment_former = fragments_stack.pop() for edge in fragment_former.out_edges: edge.set_end(fragment_latter.start_node) fragment.start_node = fragment_former.start_node fragment.out_edges = fragment_latter.out_edges elif c == expression.REGEX_NOTATION.OR: fragment_branch_1 = fragments_stack.pop() fragment_branch_2 = fragments_stack.pop() node = graph.Node() graph.Edge().set_start(node).set_end( fragment_branch_1.start_node) graph.Edge().set_start(node).set_end( fragment_branch_2.start_node) fragment.start_node = node fragment.out_edges = fragment_branch_1.out_edges + fragment_branch_2.out_edges elif c == expression.REGEX_NOTATION.REPEAT_MORE_THAN_ONE: fragment_last = fragments_stack.pop() node = graph.Node() for edge in fragment_last.out_edges: edge.set_end(node) graph.Edge().set_start(node).set_end(fragment_last.start_node) edge = graph.Edge() edge.set_start(node) fragment.start_node = fragment_last.start_node fragment.out_edges.append(edge) elif c == expression.REGEX_NOTATION.REPEAT_OR_ZERO: fragment_last = fragments_stack.pop() node = graph.Node() for edge in fragment_last.out_edges: edge.set_end(node) graph.Edge().set_start(node).set_end(fragment_last.start_node) edge = graph.Edge() edge.set_start(node) fragment.start_node = node fragment.out_edges.append(edge) elif c == expression.REGEX_NOTATION.ZERO_OR_ONE: fragment_last = fragments_stack.pop() node = graph.Node() graph.Edge().set_start(node).set_end(fragment_last.start_node) edge = graph.Edge() edge.set_start(node) fragment.start_node = node fragment.out_edges = fragment_last.out_edges fragment.out_edges.append(edge) else: node = graph.Node() edge = graph.Edge(expression.MatchFunction(c)) edge.set_start(node) fragment.start_node = node fragment.out_edges.append(edge) fragments_stack.append(fragment)
def test_path_iter(self): node1 = graph.Node("1") node2 = graph.Node("2") node3 = graph.Node("3") node3.shortest_source = node2 node2.shortest_source = node1 self.assertEqual([node for node in node3], [node1, node2, node3])
def unreverse_edges(g): if g.root.label == 'multi-sentence': roots = [edge.tails[0] for edge in g.root.outedges] else: roots = [g.root] for node in g.dfs(): edges = [] for edge in node.outedges: reverse = False m = re.match(r"(.*)-of$", edge.label) if m: reverse = True newlabel = m.group(1) """elif edge.label in ["purpose"] and not re.match(r".*-\d+^", node.label): reverse = True newlabel = edge.label + '-of'""" if reverse: (root, ) = edge.tails root.outedges.append(graph.Edge(newlabel, [node])) roots.append(root) else: edges.append(edge) node.outedges = edges dummy = graph.Graph( graph.Node('dummy', [graph.Edge('dummy', [root]) for root in roots])) components = dummy.scc() cindex = {} for ci, component in enumerate(components): for node in component: cindex[node] = ci reachable = set() for node in dummy.dfs(): if node is dummy: reachable.add(cindex[node]) continue for edge in node.outedges: for tail in edge.tails: if cindex[node] != cindex[tail]: reachable.add(cindex[tail]) roots = [ component.pop() for ci, component in enumerate(components) if ci not in reachable ] if len(roots) > 1: edges = [] for ri, root in enumerate(roots): edges.append(graph.Edge('snt{0}'.format(ri + 1), [root])) g.root = graph.Node('multi-sentence', edges) else: g.root = roots[0] g.update()
def star_graph(n): """generate star graph n""" g = graph.Graph() ni = graph.Node() for i in xrange(n): n = graph.Node() e = graph.Edge(ni, n) g.add_edge(e) return g
def test_neighbor(self): node1 = graph.Node("1") node2 = graph.Node("2") node1.add_neighbor(node2, 5.3) self.assertIn(node2, node1._neighbors) self.assertTrue(node1.is_neighbor(node2)) self.assertNotIn(node1, node2._neighbors) self.assertFalse(node2.is_neighbor(node1)) self.assertEqual(node1.get_distance_to(node2), 5.3) self.assertEqual(node2.get_distance_to(node1), float("inf"))
def test_equals(self): m = graph.Node('aoeu') n = graph.Node('aoeu') self.assertEqual(n, n) self.assertEqual(m, n) q = graph.Node('ueoa') self.assertNotEqual(q, m)
def test_graph_build(): n1 = graph.Node() n2 = graph.Node() e = graph.Edge() e.set_start(n1) e.set_end(n2) assert(e.start_node == n1) assert(e.end_node == n2) assert(e in n1.out_edges) assert(e not in n1.in_edges) assert(e in n2.in_edges) assert(e not in n2.out_edges)
def test_update_distance(self): node1 = graph.Node("1") node2 = graph.Node("2") node3 = graph.Node("3") node1.add_neighbor(node2, 3) node2.update_distance(node1, 5) self.assertEqual(node2.distance, 5) node2.update_distance(node3, 4) self.assertEqual(node2.distance, 5) node3.add_neighbor(node2, 3) node2.update_distance(node3, 4) self.assertEqual(node2.distance, 4)
def test_addEdge(self): g = graph.UGraph() a = graph.Node('a') b = graph.Node('b') with self.assertRaises(Exception): g.addedge(a, b) g.addnode(a) with self.assertRaises(Exception): g.addedge(a, b) g.addnode(b) g.addedge(a, b)
def test_bfs(self): gr = graph.UGraph() a = graph.Node('a') b = graph.Node('b') c = graph.Node('c') d = graph.Node('d') e = graph.Node('e') f = graph.Node('f') g = graph.Node('g') h = graph.Node('h') gr.addnodes(a, b, c, d, e, f, g, h) gr.addedge(a, b) gr.addedge(a, g) gr.addedge(a, d) gr.addedge(b, e) gr.addedge(b, f) gr.addedge(c, f) gr.addedge(c, h) gr.addedge(d, f) gr.addedge(e, g) r = graph.bfs(gr, a) self.assertEqual('abdgefch', r)
def setUp(self): self.nodes = [graph.Node('sid'), graph.Node('meagan'), graph.Node('casper'), graph.Node('john')] self.not_present = ['jack', 'jill', 'shantanu'] self.g = graph.Graph() for node in self.nodes: self.g.add_node(node) self.edges = [] self.edges.append((self.nodes[0], self.nodes[1])) self.edges.append((self.nodes[1], self.nodes[2])) for n1,n2 in self.edges: self.g.add_edge(n1, n2)
def test_sorting(self): # make sure that a node's neighbors are # presented in sorted order. gr = graph.UGraph() a = graph.Node('a') b = graph.Node('b') c = graph.Node('c') d = graph.Node('d') e = graph.Node('e') f = graph.Node('f') g = graph.Node('g') h = graph.Node('h') gr.addnodes(a, b, c, d, e, f, g, h) gr.addedge(a, h) gr.addedge(a, g) gr.addedge(a, f) gr.addedge(a, e) gr.addedge(a, d) gr.addedge(a, c) gr.addedge(a, b) k = "" for n in gr.neighbors(a): k += str(n) self.assertEqual('bcdefgh', k)
def plan(domain, problem, useheuristic=True): """ Find a solution to a planning problem in the given domain The parameters domain and problem are exactly what is returned from pddl.parse_domain and pddl.parse_problem. If useheuristic is true, a planning heuristic (developed in task 4) should be used, otherwise use pathfinding.default_heuristic. This allows you to compare the effect of your heuristic vs. the default one easily. The return value of this function should be a 4-tuple, with the exact same elements as returned by pathfinding.astar: - A plan, which is a sequence of graph.Edge objects that have to be traversed to reach a goal state from the start. Each Edge object represents an action, and the edge's name should be the name of the action, consisting of the name of the operator the action was derived from, followed by the parenthesized and comma-separated parameter values e.g. "move(agent-1,sq-1-1,sq-2-1)" - distance is the number of actions in the plan (i.e. each action has cost 1) - visited is the total number of nodes that were added to the frontier during the execution of the algorithm - expanded is the total number of nodes that were expanded (i.e. whose neighbors were added to the frontier) """ def heuristic(state, action): return pathfinding.default_heuristic def isgoal(state): return True start = graph.Node() return pathfinding.astar( start, heuristic if useheuristic else pathfinding.default_heuristic, isgoal)
def test_unvisited_neighbors(self): node1 = graph.Node("1") node2 = graph.Node("2") node3 = graph.Node("3") node1.add_neighbor(node2, 3) node1.add_neighbor(node3, 4) self.assertEqual( list( sorted(node1.get_unvisited_neighbors(), key=lambda node: node.label)), [node2, node3]) node3.visited = True self.assertEqual(list(node1.get_unvisited_neighbors()), [node2])
def create_nodes(node_ids): nodes = [] for node_id in node_ids: node = graph.Node(node_id) nodes_dict[node_id] = node nodes.append(node) return nodes
def baseline_segmentor(self, inkml): strokes = self.get_strokes_as_list(inkml.expression_trace) segments = [] for i in range(len(strokes)): segment = graph.Node(i, 'Node' + str(i), strokes[i]) segments.append(segment) return segments
def build_graph(dfs_codes): g = graph.Graph() numnodes = max([x[0] for x in dfs_codes] + [x[1] for x in dfs_codes]) + 1 for i in range(numnodes): n = graph.Node() g.nodes.append(n) for idx, c in enumerate(dfs_codes): g.nodes[c.fromn].id = c.fromn g.nodes[c.fromn].label = c.from_label g.nodes[c.to].id = c.to g.nodes[c.to].label = c.to_label e = graph.Edge() e.id = g.nedges e.fromn = c.fromn e.to = c.to e.label = c.edge_label g.nodes[c.fromn].edges.append(e) e2 = graph.Edge() e2.id = e.id e2.label = e.label e2.fromn = c.to e2.to = c.fromn g.nodes[c.to].edges.append(e2) g.nedges += 1 return g
def __getStartNode(self): line = self.fobj.readline() self.linecount+=1 node = line.split()[0] ##print 'start node',node if not self.nodes.get(node, None): self.nodes[node] = graph.Node(node) return self.nodes[node]
def test_bool(self): g = graph.UGraph() self.assertFalse(g) self.assertEqual(0, len(g)) n = graph.Node('hello') g.addnode(n) self.assertTrue(g) self.assertEqual(1, len(g))
def testNewGraph(self): #self.assertRaises(Exception, Graph) g = graph.Graph("graph1") n = graph.Node("node1") e = graph.Edge(1, 2) g.addnode("node2") self.assertTrue(len(g.nodes) == 1)
def build_node(node_rep): string_rep = "".join(node_rep) new_node = graph.Node(shared.NODE_ID, node_rep) shared.NODE_ID += 1 shared.GRAPH_NODES[string_rep] = new_node shared.HEURISTIC[new_node] = get_heuristic(node_rep) return new_node