Dear Tiago,
I just send full working example to you. The Network_Prepare.py is to
generate a ER-ER interdependent network as it shows. The MAIN.py is to
simulate the cascading failure in interdependent network.
I will try the newest code on github later. Thank u.
best,
Liu
在 2014年4月30日星期三UTC+8下午3时44分48秒,Tiago Peixoto写道:
>
> On 04/30/2014 09:37 AM, Tiago de Paula Peixoto wrote:
> > On 04/30/2014 03:28 AM, Liu wrote:
> >> Dear Tiago,
> >>
> >> I used G.purge_vertices(in_place=True). But the vertex filter didn't
> work.
> >>
> >> G.set_vertex_filter(prop=exist)
> >> G.purge_vertices(in_place=True)
> >>
> >> gt.graph_draw(G, pos=pos)
> >> # step 1 largest component in network A
> >> UA = gt.GraphView(G, vfilt=v_type)
> >>
> >> line 45:LCA = gt.label_largest_component(UA)
> >> exist.a = False
> >> for v in G.vertices():
> >> if LCA[v]:
> >> exist[v] = True
> >> for n in v.all_neighbours():
> >> if not v_type[n]:
> >> exist[v] = True
> >>
> >> G.set_vertex_filter(exist)
> >> G.purge_vertices(in_place=True)
> >>
> >> Console warns that,
> >> Traceback (most recent call last):
> >> File "/home/lockheed/PycharmProjects/cascade_failures/MAIN.py", line
> 45, in <module>
> >> LCA = gt.label_largest_component(UA)
> >> File
> "/usr/lib/python2.7/dist-packages/graph_tool/topology/__init__.py", line
> 814, in label_largest_component
> >> label.a = (c.a == h.argmax()) & (vfilt.a ^ inv)
> >> ValueError: operands could not be broadcast together with shapes (248)
> (256)
> >
> > This seems to be something else. Could you please provide a full working
> > example, so I can investigate?
>
> I've fixed a related problem in the git version. Please try with
> that. If it does not work, please send the full working example, and
> I'll check it out.
>
> Best,
> Tiago
>
> --
> Tiago de Paula Peixoto <[email protected] <javascript:>>
>
>
#! /usr/bin/env python
# -*- coding: utf-8 -*-
"""
@author: lockheed
Information and Electronics Engineering
Huazhong University of science and technology
E-mail:[email protected]
Created on: 4/17/14 8:09 PM
"""
import graph_tool.all as gt
import numpy as np
# some initialization parameters
# TODO: we start with Erdos-Renyi networks
N = 128
a = 3 # average degree of network A
b = 3 # average degree of network B
'''
# TODO: networks A
NA = gt.random_graph(N, lambda: np.random.poisson(a), directed=False)
colorA = NA.new_vertex_property('int')
colorA.a = 10
NA.vertex_properties['v_type'] = colorA
EdgeA = NA.new_edge_property('int')
EdgeA.a = 7
NA.edge_properties['e_type'] = EdgeA
# TODO: networks B
NB = gt.random_graph(N, lambda: np.random.poisson(b), directed=False)
colorB = NB.new_vertex_property('int')
colorB.a = 0
NB.vertex_properties['v_type'] = colorB
EdgeB = NB.new_edge_property('int')
EdgeB.a = 3
NB.edge_properties['e_type'] = EdgeB
IN = gt.graph_union(NA, NB)
v_type = IN.vertex_properties['v_type']
e_type = IN.edge_properties['e_type']
pos = gt.sfdp_layout(IN)
IN.vertex_properties['pos']=pos
Index = range(0, N)
for i in Index:
new_edge = IN.add_edge(i, i+N)
e_type[new_edge] = 5
gt.graph_draw(IN, pos=pos, vertex_fill_color = v_type, edge_color=e_type, output_size=(1024, 800),output='1.png')
IN.remove_vertex(2, fast=True)
pos = IN.vertex_properties['pos']
v_type = IN.vertex_properties['v_type']
e_type = IN.edge_properties['e_type']
gt.graph_draw(IN, pos=pos, vertex_fill_color = v_type, edge_color=e_type, output_size=(1024, 800),output='2.png')
'''
# TODO: networks A
NA = gt.random_graph(N, lambda: np.random.poisson(a), directed=False)
is_A = NA.new_vertex_property('bool')
is_A.a = True
NA.vertex_properties['v_type'] = is_A
IntraA_Edge = NA.new_edge_property('bool')
IntraA_Edge.a = True
NA.edge_properties['e_type'] = IntraA_Edge
# TODO: networks B
NB = gt.random_graph(N, lambda: np.random.poisson(b), directed=False)
is_A_ = NB.new_vertex_property('bool')
is_A_.a = False
NB.vertex_properties['v_type'] = is_A_
IntraB_Edge = NB.new_edge_property('bool')
IntraB_Edge.a = True
NB.edge_properties['e_type'] = IntraB_Edge
# TODO: combine A and B into interdependent networks
IN = gt.graph_union(NA, NB)
v_type = IN.vertex_properties['v_type']
e_type = IN.edge_properties['e_type']
pos = gt.sfdp_layout(IN)
IN.vertex_properties['pos'] = pos
Index = range(0, N)
for i in Index:
new_edge = IN.add_edge(i, i+N)
e_type[new_edge] = False
gt.Graph(g= IN, prune=True)
IN.save('interdependent_ERs.xml.gz')
#! /usr/bin/env python
# -*- coding: utf-8 -*-
"""
@author: lockheed
Information and Electronics Engineering
Huazhong University of science and technology
E-mail:[email protected]
Created on: 4/17/14 7:50 PM
"""
import graph_tool.all as gt
import numpy.random as np
# TODO: define some parameters
File = 'interdependent_ERs.xml.gz'
p = 0.02
G = gt.load_graph(File)
pos = G.vertex_properties['pos']
v_type = G.vertex_properties['v_type']
e_type = G.edge_properties['e_type']
exist = G.new_vertex_property('bool')
G.vertex_properties['exsit'] = exist
exist.a = True
G.vertex_properties['tag'] = exist
gt.graph_draw(G, pos=pos)
# step 0 initial attack
for v in G.vertices():
if v_type[v]:
if np.random() < p:
exist[v] =False
for n in v.all_neighbours():
if not v_type[n]:
exist[n] = False
G.set_vertex_filter(prop=exist)
G.purge_vertices()
gt.graph_draw(G, pos=pos)
# step 1 largest component in network A
UA = gt.GraphView(G, vfilt=v_type)
LCA = gt.label_largest_component(UA)
exist.a = False
for v in G.vertices():
if LCA[v]:
exist[v] = True
for n in v.all_neighbours():
if not v_type[n]:
exist[v] = True
G.set_vertex_filter(exist)
G.purge_vertices(in_place=True)_______________________________________________
graph-tool mailing list
[email protected]
http://lists.skewed.de/mailman/listinfo/graph-tool