Advertisement
Guest User

Untitled

a guest
Aug 18th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.80 KB | None | 0 0
  1.  
  2. # coding: utf-8
  3. from networkx import *
  4. from random import random as rnd
  5. from random import shuffle as shf
  6. from random import choice as chc
  7. import matplotlib.pyplot as plt
  8. import math
  9. import numpy as np
  10. import time
  11. from utilities import double_edge_swap_no_triangle
  12.  
  13. def initNodes(G):
  14. for i in G:
  15. G.nodes[i]['p'] = rnd()
  16. G.nodes[i]['q'] = rnd()
  17. G.nodes[i]['p0'] = G.nodes[i]['p']
  18. G.nodes[i]['q0'] = G.nodes[i]['q']
  19. G.nodes[i]['payoff'] = 0
  20. G.nodes[i]['total_payoff'] = 0
  21.  
  22. def reset(G):
  23. for i in G:
  24. G.nodes[i]['p'] = G.nodes[i]['p0']
  25. G.nodes[i]['q'] = G.nodes[i]['q0']
  26. G.nodes[i]['payoff'] = 0
  27. G.nodes[i]['total_payoff'] = 0
  28.  
  29. def prettyPrint(G):
  30. nodes = list(G.nodes.keys())
  31. nodes.sort()
  32. for i in nodes:
  33. print(i,": p =",G.nodes[i]['p'],"q =",G.nodes[i]['q'],"payoff =",G.nodes[i]['payoff'],"total payoff =",G.nodes[i]['total_payoff'])
  34.  
  35. def simulation(G):
  36. step = 60000
  37. global printStep
  38. if printStep==0:
  39. print("Initial state :")
  40. print("M =",G.graph['M'])
  41. prettyPrint(G)
  42. print('-'*10)
  43. runs = 10000 #50000
  44. for i in range(runs):
  45. if printStep==0:
  46. print("Generation :",i+1,"\n#######################")
  47. generation(G)
  48. printStep = (printStep + 1) % step
  49.  
  50. def generation(G):
  51. def bigger(a,b):
  52. return int(a>=b)
  53. global printStep
  54. texts = ["fail","success"]
  55. proposers = list(G.nodes.keys())
  56. shf(proposers)
  57. for i in proposers:
  58. #subM = 0.0
  59. #for j in G[i]:
  60. # if G.nodes[i]['p'] >= G.nodes[j]['q']:
  61. # subM += 1.0
  62. #subM /= len(G[i])
  63. subM = np.mean([bigger(G.nodes[i]['p'],G.nodes[j]['q']) for j in G[i]])
  64. if subM >= G.graph['M']:
  65. G.nodes[i]['payoff'] += 1 - G.nodes[i]['p']
  66. for j in G[i]:
  67. G.nodes[j]['payoff'] += G.nodes[i]['p']/len(G[i])
  68. if printStep==0:
  69. print(i,"propose...",texts[int(subM >= G.graph['M'])],"as M' =",subM)
  70. if printStep==0:
  71. print("+"*10)
  72. update(G)
  73. if printStep==0:
  74. print("+"*10)
  75. prettyPrint(G)
  76. print("#####################")
  77. clean(G)
  78.  
  79. def update(G):
  80. global printStep
  81. nodes = list(G.nodes.keys())
  82. shf(nodes)
  83. beta = 1.0
  84. epsilon = 0.05
  85. for i in nodes:
  86. G.nodes[i]['total_payoff'] += G.nodes[i]['payoff']
  87. target = chc(list(neighbors(G,i)))
  88. proba = 1/(1.0+math.e**(-beta*(G.nodes[target]['payoff']-G.nodes[i]['payoff'])))
  89. disturbP = (rnd() * epsilon * 2) - epsilon
  90. disturbQ = (rnd() * epsilon * 2) - epsilon
  91. if rnd() <= proba:
  92. oldP = G.nodes[i]['p']
  93. oldQ = G.nodes[i]['q']
  94. G.nodes[i]['p'] = G.nodes[target]['p'] + disturbP
  95. G.nodes[i]['p'] = max(0,min(G.nodes[i]['p'],2 - G.nodes[i]['p']))
  96. G.nodes[i]['q'] = G.nodes[target]['q'] + disturbQ
  97. G.nodes[i]['q'] = max(0,min(G.nodes[i]['q'],2 - G.nodes[i]['q']))
  98. if printStep==0:
  99. print(i,"imitate",target,"( p and q from", oldP,oldQ,"to",G.nodes[i]['p'],G.nodes[i]['q'],")")
  100.  
  101. def clean(G):
  102. for i in G:
  103. G.nodes[i]['payoff'] = 0
  104.  
  105. def getMeanPMeanQ(G):
  106. p = 0
  107. q = 0
  108. for i in G:
  109. p += G.nodes[i]['p']
  110. q += G.nodes[i]['q']
  111. p /= len(G.nodes)
  112. q /= len(G.nodes)
  113. return p,q
  114.  
  115. start = time.time()
  116. printStep = 1
  117. M = np.array([1.0/6,2.0/6,3.0/6,4.0/6,5.0/6,1])
  118. pMean = np.zeros(6)
  119. qMean = np.zeros(6)
  120. pMean2 = np.zeros(6)
  121. qMean2 = np.zeros(6)
  122. population = 100
  123. RTRG = nx.watts_strogatz_graph(population, 6, 0, seed=None) # n, k, p, seed
  124. for edge in list(RTRG.edges):
  125. if ((edge[0] + edge[1]) % 2 == 0):
  126. RTRG.remove_edge(edge[0], edge[1])
  127. HRN = nx.watts_strogatz_graph(population, 6, 0, seed=None)
  128. for edge in list(HRN.edges):
  129. if ((edge[0] + edge[1]) % 2 == 0):
  130. HRN.remove_edge(edge[0], edge[1])
  131. num_edges = len(HRN.edges)
  132. r = 0.9
  133. swaps = r * num_edges
  134. double_edge_swap_no_triangle(HRN, nswap=swaps, max_tries=10000)
  135. initNodes(RTRG)
  136. initNodes(HRN)
  137. count = 0.0
  138. total = len(M)*2.0
  139. for i in range(len(M)):
  140. RTRG.graph["M"] = M[i]
  141. HRN.graph["M"] = M[i]
  142. simulation(RTRG)
  143. count+=1
  144. print(str(count/total)+"%")
  145. simulation(HRN)
  146. count+=1
  147. print(str(count/total)+"%")
  148. pMean[i], qMean[i] = getMeanPMeanQ(RTRG)
  149. pMean2[i], qMean2[i] = getMeanPMeanQ(HRN)
  150. reset(RTRG)
  151. reset(HRN)
  152. print("Done in "+str(time.time()-start)+"sec")
  153. plt.subplot(121)
  154. nx.draw(RTRG, with_labels=True)
  155. plt.subplot(122)
  156. nx.draw(HRN, with_labels=True)
  157. plt.subplot(121)
  158. plt.plot(M, pMean)
  159. plt.plot(M, pMean2, "r-")
  160. plt.subplot(122)
  161. plt.plot(M, qMean)
  162. plt.plot(M, qMean2, "r-")
  163. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement