Advertisement
Guest User

Untitled

a guest
Aug 18th, 2018
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.85 KB | None | 0 0
  1. def create_sp_graph(n=30, l=0.001, spmax = 0.4):
  2.  
  3. #first create a regular ring
  4. G = nx.watts_strogatz_graph(n, 6, 0, seed=None)
  5.  
  6. #then rewire all edges
  7. nx.double_edge_swap(G, len(G.edges)/2, 100000)
  8. sp_t = sp_of_graph(G)
  9.  
  10. for i in range(20000000):
  11. orig_G = G.copy()
  12. sp_orig = sp_t
  13.  
  14. if i%100 == 0:
  15. print('goal:',spmax,' [',i/200000,'%], sp val: ',sp_orig)
  16.  
  17.  
  18. if i%2000 == 0:
  19. nx.write_adjlist(G, 'z1000_d6_sp'+str(sp_t)+'_iter'+str(i)+'.adjlist')
  20.  
  21.  
  22. edge = random.choice(list(G.edges))
  23.  
  24. G.remove_edge(edge[0],edge[1])
  25.  
  26. a = random.randint(0,n)
  27. b = random.randint(0,n)
  28. while ((a,b) in G.edges()):
  29. a = random.randint(0,n)
  30. b = random.randint(0,n)
  31. G.add_edge(a,b)
  32.  
  33. if not nx.has_path(G,edge[0],edge[1]):
  34. G = orig_G.copy()
  35. continue
  36.  
  37. sp_t = sp_of_graph(G)
  38. prob = l*(spmax - sp_t)
  39.  
  40. if (not(sp_t > sp_orig) or (random.random() < prob)):
  41. G = orig_G.copy()
  42. sp_t = sp_orig
  43. continue
  44.  
  45. if (sp_t > spmax):
  46. print(sp_t)
  47. return G
  48. print(sp_t)
  49. return G
  50.  
  51. def sp_of_graph(G):
  52. return np.mean([sp_of_node(G, x) for x in G.nodes])
  53.  
  54. def sp_of_node(G,n):
  55. RA = set()
  56. for node in G.neighbors(n):
  57. RA.add(node)
  58. RA.update(G.neighbors(node))
  59. return np.mean([sp_3(G, n, x) for x in RA])
  60.  
  61. def kdelta(G,a,b):
  62. return int((a,b) in G.edges)
  63.  
  64. def sp_3(G, A, B):
  65. if A == B:
  66. return 1
  67. bneigh = set(nx.neighbors(G,B))
  68. union = bneigh & set(nx.neighbors(G,A))
  69. res = 2 * kdelta(G,A,B) + len(union)
  70. return np.divide(res,len(bneigh) + 1 )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement