def get_dot_node(node: Node): attributes = dict() attributes['shape'] = 'box' attributes['xlabel'] = node.cost attributes['label'] = node.label attributes['pos'] = '{},{}!'.format(node.x, node.y) if node.is_visiting(): attributes['style'] = 'filled' attributes['fillcolor'] = 'yellow' if node.is_pruned(): attributes['style'] = 'filled' attributes['fillcolor'] = 'red' if node.is_minimum_local(): attributes['style'] = 'filled' attributes['fillcolor'] = 'orange' if node.is_unvisited() and node.is_minimum_global(): attributes['style'] = 'filled' attributes['fillcolor'] = 'green' return '{} [{}]'.format( node.node_id, ','.join(['{}="{}"'.format(x, y) for x, y in attributes.items()]))
def test_node(self): node1 = Node() node2 = Node('test') self.assertEqual(node1.label, '') self.assertEqual(node2.label, 'test')
def test_node(self): node1 = Node() node2 = Node('test') self.assertEqual(node1.name, '') self.assertEqual(node2.name, 'test')
def create_graph(s_objects, p_objects): print 'Creating graph ...' graph = Graph() node_id = 0 # use this as node id as some segments might have similar id # add the imp object nodes for s_obj in s_objects: node = Node(node_id, \ seg_id=s_obj.id, \ x_pos=s_obj.pos[1], \ y_pos=s_obj.pos[0], \ b_fix=True, \ size=s_obj.size, \ color_energy=s_obj.color_energy, \ color=s_obj.color, \ saliency=s_obj.saliency, \ radius=np.sqrt(s_obj.size)/4) graph.add_node(node) node_id += 1 max_snode_id = node_id # add the people node for p_obj in p_objects: node = Node(node_id,\ seg_id=p_obj.id, \ x_pos=p_obj.pos[1], \ y_pos=p_obj.pos[0], \ b_fix=False, \ size=p_obj.size, \ color_energy=1.2*p_obj.color_energy, \ color=p_obj.color, \ saliency=1.2*p_obj.saliency, \ radius=np.sqrt(p_obj.size)/4) graph.add_node(node) # add this node to the list of people nodes graph.add_pnode(node) graph.num_people += 1 # create edges from the people node to rest of nodes for dst_id in range(max_snode_id): src_id = node_id src_node = node dst_node = graph.get_node(dst_id) assert src_node is not None assert dst_node is not None graph.add_edge(src_node, dst_node, spring_length=0.0) node_id += 1 return graph
def test_edge(self): node1 = Node() node2 = Node('test') edge1 = Edge(node1, node2) edge2 = Edge(node1, node1, 'operation') self.assertEqual(edge1.label, '') self.assertEqual(edge1.source, node1) self.assertEqual(edge1.target, node2) self.assertEqual(edge2.label, 'operation') self.assertEqual(edge2.source, edge2.target)
def generate_graph(n_nodes=100, n_init_nodes=3, n_conn_per_node=2, randomstate=np.random.RandomState(1234)): num_nodes = 0 # current number of nodes nodes = [] # list of nodes tot_degree = 0 # sum of all degrees in the network # INITIALIZATION - CREATING INITIAL NODES for i in range(n_init_nodes): newnode = Node() for node in nodes: node.attach(newnode) # the initial nodes are connected as a "Clique" newnode.attach(node) tot_degree += 2 nodes.append(newnode) num_nodes += 1 # CREATING REST OF THE GRAPH while num_nodes < n_nodes: random_numbers = randomstate.randint(low=0, high=tot_degree, size=n_conn_per_node) # select nodes to attach attached = [] # prevent attaching a node to another twice # the new node newnode = Node() for n in random_numbers: acc = 0 for node in nodes: acc += node.degree if n < acc and node not in attached: attached.append(node) node.attach(newnode) newnode.attach(node) tot_degree += 2 break nodes.append(newnode) num_nodes += 1 return nodes, tot_degree
def test_graph(self): node1 = Node() node2 = Node() edge1 = Edge(node1, node2) graph = Graph() graph.add_node(node1) graph.add_node(node2) graph.add_edge(edge1) self.assertEqual(len(graph.nodes), 2) self.assertEqual(graph.nodes, {node1.id: node1, node2.id: node2}) self.assertEqual(len(graph.edges), 1) self.assertEqual(graph.edges, [edge1])
def _build_graph(self): temp = None for _, s in self._model.services.items(): self._graph.add_node(Node(s.name)) for _, s in self._model.services.items(): for _, o in s.operations.items(): source = self._graph.node(s.name) if not o.dependencies: self._graph.add_edge(Edge(source, source, o.name)) else: for d in o.dependencies: target = self._graph.node(d.service.name) if temp == target: continue temp = target try: self._graph.add_edge(Edge(source, target, o.name)) except UnknownOperation(d.service.name, d.name): pass
def create_graph_0(dump_path, nodes_file, edges_file): print 'Creating graph ...' graph = Graph() # add the nodes nodes_list = dump_path + nodes_file nodes_data = np.loadtxt(nodes_list, skiprows=1) n_nodes, n_dim = nodes_data.shape for i in range(n_nodes): node_id = int(nodes_data[i,0]) if node_id < 0: continue x_pos = nodes_data[i,1] y_pos = nodes_data[i,2] size = nodes_data[i,3] b_fix = int(nodes_data[i,4]) rgb_color = nodes_data[i,5:8] color_energy = get_color_energy(rgb_color, size) print color_energy node = Node(node_id,\ seg_id=node_id, \ x_pos=x_pos, \ y_pos=y_pos, \ b_fix=b_fix, \ size=size, \ color_energy=color_energy, \ color=rgb_color, \ saliency=1.0, \ radius=size/2) graph.add_node(node) # add this node to the list of people nodes # graph.add_pnode(node) # graph.num_people += 1 edges_list = dump_path + edges_file edges_data = np.loadtxt(edges_list, skiprows=1) n_edges, n_dim = edges_data.shape for i in range(n_edges): src_id = edges_data[i,0] if src_id < 0: continue dst_id = edges_data[i,1] length = edges_data[i,2] src_node = graph.get_node(src_id) dst_node = graph.get_node(dst_id) assert src_node is not None assert dst_node is not None graph.add_edge(src_node, dst_node, spring_length=length) return graph
def test_check_cycles(self): G = Graph() a, b, c, d, e, f, g = Node('A'), Node('B'), Node('C'), Node('D'), Node( 'E'), Node('F'), Node('G') N = [a, b, c, d, e, f, g] for node in N: G.add_node(node) E = [ Edge(a, b), Edge(b, c), Edge(c, d), Edge(a, d), Edge(d, e), Edge(e, a), Edge(e, f), Edge(f, g), Edge(g, f), ] for edge in E: G.add_edge(edge) self.assertEqual(Graph.check_cycles(G, True), [[g, f], [e, d, c, b, a]]) self.assertEqual(Graph.check_cycles(G, False), [[g, f]])
def create_graph_0(dump_path, nodes_file, edges_file): print 'Creating graph ...' graph = Graph() print 'done.' # add the nodes nodes_list = dump_path + nodes_file nodes_data = np.loadtxt(nodes_list, skiprows=1) n_nodes, n_dim = nodes_data.shape for i in range(n_nodes): node_id = int(nodes_data[i, 0]) if node_id < 0: continue x_pos = nodes_data[i, 1] y_pos = nodes_data[i, 2] energy = nodes_data[i, 3] size = nodes_data[i, 4] fix = int(nodes_data[i, 5]) node = Node(node_id, x_pos=x_pos, y_pos=y_pos, b_fix=fix, size=size, color_energy=energy) graph.add_node(node) edges_list = dump_path + edges_file edges_data = np.loadtxt(edges_list, skiprows=1) n_edges, n_dim = edges_data.shape for i in range(n_edges): src_id = edges_data[i, 0] if src_id < 0: continue dst_id = edges_data[i, 1] length = edges_data[i, 2] src_node = graph.get_node(src_id) dst_node = graph.get_node(dst_id) assert src_node is not None assert dst_node is not None graph.add_edge(src_node, dst_node, spring_length=length) return graph
def generate_node_and_add_to_graph(node_id, label, cost, x, y, graph): n = Node(node_id=node_id, label=label, cost=cost, x=x, y=y * 1.6) graph.add_node(n) return n