放弃原来的rewiring rate(实际上是breaking rate)

This commit is contained in:
wjsjwr 2018-10-13 19:40:05 +08:00
parent f36f3ae836
commit 570c4d0c01
7 changed files with 106 additions and 198 deletions

75
br_plot.py Normal file
View File

@ -0,0 +1,75 @@
import numpy as np
from matplotlib import pyplot as plt
import scipy as sp
from scipy.stats import pearsonr
from matplotlib import markers
from sys import argv
blue = '#0984e3'
red = '#d63031'
def error(f,x,y):
return sp.sum((f(x)-y)**2)
def p2(e, rw, postfix, show=True, showline=True):
# p2散点图
fig = plt.figure(figsize=(4, 3))
ax = fig.gca()
if showline:
fp1,residuals,rank,sv,rcond = sp.polyfit(e, rw, 1, full=True)
print("残差:",residuals)
print('Model parameter:',fp1)
print("Other parameters: rank=%s, sv=%s, rcond=%s"%(str(rank), str(sv), str(rcond)))
f1 = sp.poly1d(fp1)
print("error= %f" % error(f1, e, rw))
fx = sp.linspace(np.min(e), np.max(e), 2)
plt.plot(fx,f1(fx),linewidth=2,color=red, ls='--', zorder=0)
plt.scatter(e, rw, color='white', edgecolors=blue, linewidths=2, zorder=101)
ax.set_xlabel(r'$TtD$', family='sans-serif', size=20)
ax.set_ylabel(r'Breaking Rate', family='sans-serif', size=20)
# ax.set_xlim(0, 1440)
# ax.set_xticks(sp.linspace(int(min(e)*0.9), int(max(e)*1.1), 4))
ax.tick_params(labelsize=14)
ax.set_ylim(0, 0.6)
ax.set_yticks(sp.linspace(0, 0.6, 5))
plt.tight_layout()
if show:
plt.show()
else:
plt.savefig("graph/eid_co_sca_%s.eps" % postfix)
# 皮尔逊相关系数
print("pearson: %f, p-value: %f" % pearsonr(e, rw))
def p2c(e,c,m,s):
p2(e,c,m,s,False)
def plot(mode, show):
rw = np.loadtxt("outputs/BR_%s.csv" % mode, delimiter=',')
e = np.loadtxt("outputs/EID_%s.csv" % mode, delimiter=',')
p2(e[:-1], rw[1:], mode, show)
# if mode == 'SURVIVE':
# p2(e[:-1], rw[1:], mode, show)
# else:
# p2c(e[:-1], rw[1:], mode, show)
if __name__ == '__main__':
# eid_plot c/s 1/2 t/f
mode = 'CLASSIC' if argv[1] == 'c' else 'SURVIVE'
show = argv[2] == 't'
plot(mode, show)
"""
SURVIVE
残差 [ 0.06081364]
Model parameter: [ -3.54739130e-04 4.41956696e-01]
Other parameters: rank=2, sv=[ 1.40523958 0.15906514], rcond=3.10862446895e-15
error= 0.060814
pearson: -0.837958, p-value: 0.000183
CLASSIC
pearson: 0.384519, p-value: 0.174626
"""

27
breaking_rate.py Normal file
View File

@ -0,0 +1,27 @@
import csv
import numpy as np
from island.match import Match
from island.matches import Matches
def calc_and_save(mode):
matches = Matches.from_profile(mode)
ans = np.zeros(15)
div = np.zeros(15)
for m in matches.data:
maxr = int(m.query('game', 'created').first()['info']['game_end_at'])
for i in range(1, maxr):
rows = m.query('action', 'done').where(lambda x: x['rno']==i).raw_data
br = 0
for r in rows:
if m.query('action', 'done').where(lambda x: x['rno']==i+1 and ((x['a']==r['a'] and x['b']==r['b']) or (x['a']==r['b'] and x['b']==r['a']))).count() == 0:
br += 1
ans[i] += br
div[i] += len(rows)
div[np.where(div==0)[0]] = 1
ans /= div
with open("outputs/BR_%s.csv"%mode, 'w') as f:
csv.writer(f).writerow(ans)
if __name__ == '__main__':
calc_and_save('CLASSIC')
calc_and_save('SURVIVE')

1
outputs/BR_CLASSIC.csv Normal file
View File

@ -0,0 +1 @@
0.0,0.268817204301,0.194871794872,0.161764705882,0.144278606965,0.147058823529,0.127962085308,0.148325358852,0.0873786407767,0.111111111111,0.0903225806452,0.120967741935,0.102803738318,0.0833333333333,0.0
1 0.0 0.268817204301 0.194871794872 0.161764705882 0.144278606965 0.147058823529 0.127962085308 0.148325358852 0.0873786407767 0.111111111111 0.0903225806452 0.120967741935 0.102803738318 0.0833333333333 0.0

1
outputs/BR_SURVIVE.csv Normal file
View File

@ -0,0 +1 @@
0.0,0.378205128205,0.40625,0.237442922374,0.167539267016,0.121951219512,0.148648648649,0.0625,0.0166666666667,0.0508474576271,0.0779220779221,0.0740740740741,0.25,0.9,0.25
1 0.0 0.378205128205 0.40625 0.237442922374 0.167539267016 0.121951219512 0.148648648649 0.0625 0.0166666666667 0.0508474576271 0.0779220779221 0.0740740740741 0.25 0.9 0.25

View File

@ -1 +1 @@
108.837209302,-33.488372093,-66.976744186,8.37209302326,-64.1860465116,-78.1395348837,-61.3953488372,-29.3023255814,-118.604651163,-127.741935484,-198.445229682,-110.769230769,-176.115107914,-564.705882353,0.0 108.837209302,-33.488372093,-66.976744186,8.37209302326,-64.1860465116,-78.1395348837,-61.3953488372,-29.3023255814,-118.604651163,-127.741935484,-198.445229682,-110.769230769,-176.115107914,-564.705882353,0.0

1 108.837209302 -33.488372093 -66.976744186 8.37209302326 -64.1860465116 -78.1395348837 -61.3953488372 -29.3023255814 -118.604651163 -127.741935484 -198.445229682 -110.769230769 -176.115107914 -564.705882353 0.0 0.0

View File

@ -1 +1 @@
-737.628803245,-800.8735363,-1231.79775281,-1322.91139241,-1377.45454545,-1382.1686747,-1373.72384937,-1433.71179039,-1426.54205607,-1408.92086331,-1440.0,-1440.0,-600.0,-1440.0,0.0 -737.628803245,-800.8735363,-1231.79775281,-1322.91139241,-1377.45454545,-1382.1686747,-1373.72384937,-1433.71179039,-1426.54205607,-1408.92086331,-1440.0,-1440.0,-600.0,-1440.0,0.0

1 -737.628803245 -800.8735363 -1231.79775281 -1322.91139241 -1377.45454545 -1382.1686747 -1373.72384937 -1433.71179039 -1426.54205607 -1408.92086331 -1440.0 -1440.0 -600.0 -1440.0 0.0 0.0

View File

@ -1,196 +0,0 @@
import json
from matplotlib import pyplot as plt
from island.match import Match
from island.matches import Matches
import numpy as np
import scipy as sp
from scipy.stats import pearsonr
from matplotlib import markers
def error(f,x,y):
return sp.sum((f(x)-y)**2)
def p1(x, rewires, tau, postfix, show):
fig = plt.figure(figsize=(6.4, 3.6))
ax = fig.gca()
ax.plot(x, rewires, color=green, linewidth=3)
ax.set_ylim(0, 0.5)
ax2 = ax.twinx()
ax2.plot(x, tau, color=red, linewidth=3)
ax2.set_ylim(0, 1440)
ax.set_xlim(2, 15)
ax.set_xlabel("Rounds")
ax.set_ylabel("Rewiring Rate", color=green)
ax.tick_params(axis='y', labelcolor=green)
ax2.set_ylabel("$\\tau_{p}$", family='sans-serif', color=red)
ax2.tick_params(axis='y', labelcolor=red)
plt.tight_layout()
if show:
plt.show()
else:
plt.savefig("graph/tau_p_rewire_plot_%s.eps" % postfix)
def p2c(tau, rewires, show):
# # p2散点图
fig = plt.figure(figsize=(4, 3))
ax = fig.gca()
plt.scatter(tau, rewires, color=green, linewidths=2, zorder=100)
ax.set_xlabel('$\\tau_{f}$', family='sans-serif', size=20)
ax.set_ylabel('Rewiring Rate', size=20)
ax.set_xlim(0, 1440)
ax.set_ylim(0, 0.6)
ax.set_xticks(sp.linspace(0, 1440, 5))
ax.tick_params(labelsize=14)
ax.set_ylim(0, 0.6)
ax.set_yticks(sp.linspace(0, 0.6, 5))
plt.tight_layout()
if show:
plt.show()
else:
plt.savefig("graph/tau_f_rewire_sca_c.eps")
# 皮尔逊相关系数
print("pearson: %f, p-value: %f" % pearsonr(tau, rewires))
def p2(tau, rewires, postfix, show):
# # p2散点图
fig = plt.figure(figsize=(4, 3))
ax = fig.gca()
fp1,residuals,rank,sv,rcond = sp.polyfit(tau, rewires, 1, full=True)
print("残差:",residuals)
print('Model parameter:',fp1)
f1 = sp.poly1d(fp1)
print("error= %f" % error(f1, tau, rewires))
print("Other parameters: rank=%s, sv=%s, rcond=%s" % (str(rank), str(sv), str(rcond)))
# fx = sp.linspace(0,max(tau2),1000)
fx = sp.linspace(0,1440,2)
plt.plot(fx,f1(fx),linewidth=2,color=red, ls='--', zorder=0)
plt.scatter(tau, rewires, color=green, linewidths=2, zorder=100)
# plt.scatter(tau_r, coopr_r, color='white', edgecolors=green, linewidths=2, zorder=101)
ax.set_xlabel('$\\tau_{f}$', family='sans-serif', size=20)
ax.set_ylabel('Rewiring Rate', size=20)
ax.set_xlim(0, 1440)
ax.set_ylim(0, 0.6)
ax.set_xticks(sp.linspace(0, 1440, 5))
ax.tick_params(labelsize=14)
ax.set_ylim(0, 0.6)
ax.set_yticks(sp.linspace(0, 0.6, 5))
plt.tight_layout()
if show:
plt.show()
else:
plt.savefig("graph/tau_f_rewire_sca_%s.eps" % postfix)
# 皮尔逊相关系数
print("pearson: %f, p-value: %f" % pearsonr(tau, rewires))
if __name__ == '__main__':
mode = 'CLASSIC'
matches = Matches.from_profile_expr(lambda r: mode in r)
max_round = 15
survivals = {}
with open('survivals.json', 'r') as f:
survivals = json.load(f)
neighbors = {}
rewires = []
x = np.arange(2, max_round+1)
mwRe = {} # Match-wise frequency of rewiring
mwTau = {} # Match-wise Tau
tau = []
for i in range(len(matches.data)):
m = matches.data[i]
n = {}
for r in m.query('neighbor', 'create').raw_data:
if r['a'] in n:
n[r['a']].append(r['b'])
else:
n[r['a']] = [r['b']]
if r['b'] in n:
n[r['b']].append(r['a'])
else:
n[r['b']] = [r['a']]
neighbors[matches.names[i]] = n
for i in range(1, max_round):
re = []
for j in range(len(matches.data)):
rewire = 0
rows = matches.data[j].query('action', 'done').where(lambda x: x['rno']==i+1).raw_data
for r in rows:
if matches.data[j].query('action', 'done').where(lambda x: x['rno']==i and ((x['a']==r['a'] and x['b']==r['b']) or (x['a']==r['b'] and x['b']==r['a']))).count() == 0:
rewire += 1
if rows:
re.append(float(rewire) / float(len(rows)*2))
mwRe["%s-%d"%(j,i)] = re[-1]
if re:
rewires.append(np.average(re))
for i in range(1, max_round):
tp = []
for j in range(len(matches.data)):
if i == 0:
for r in matches.data[j].query('player', 'join').raw_data:
t = 0
k = r['pid']
if k not in neighbors[matches.names[j]]:
print("[%s(%d)] alone: %d" % (matches.names[j], i+1, k))
else:
t = 1440 * len(neighbors[matches.names[j]][k])
tp.append(t if t < 1440 else 1440)
mwTau["%s-%d"%(j,i)] = tp[-1]
else:
if str(i) not in survivals[matches.names[j]]:
continue
for k in survivals[matches.names[j]][str(i)]:
t = 0
if k not in neighbors[matches.names[j]]:
print("[%s(%d)] alone: %d" % (matches.names[j], i+1, k))
else:
trs = matches.data[j].get_tr(i, k, neighbors[matches.names[j]][k], survivals[matches.names[j]][str(i)])
for l in neighbors[matches.names[j]][k]:
if l in trs and trs[l] > 0:
t += trs[l]
tp.append(t if t < 1440 else 1440)
mwTau["%s-%d"%(j,i)] = tp[-1]
if tp:
tau.append(np.average(tp))
else:
tau.append(0)
green = '#00b894'
red = '#d63031'
# p1折线图
# p1(x, rewires, tau, mode, True)
p2c(tau, rewires, False)
# p2(tau[:12], rewires[:12], mode, False)
'''
classic
残差 [ 0.05873797]
Model parameter: [ 9.81549075e-04 -9.87729952e-01]
error= 0.058738
Other parameters: rank=2, sv=[ 1.41291267 0.06064473], rcond=3.10862446895e-15
pearson: -0.507660, p-value: 0.063859
survive
残差 [ 0.00291864]
Model parameter: [ 0.0001647 -0.02264606]
error= 0.002919
Other parameters: rank=2, sv=[ 1.33444456 0.46824962], rcond=2.6645352591e-15
pearson: 0.947474, p-value: 0.000003
'''