migrate scripts on macOS

This commit is contained in:
wJsJwr 2018-03-11 17:27:59 +08:00
parent 9fd025d3a7
commit da43bcc09a
6 changed files with 376 additions and 0 deletions

34
calc_survivals.py Normal file
View File

@ -0,0 +1,34 @@
import json
from pathlib import Path
from island.match import Match
result = {}
for file in Path('wos-data-new').iterdir():
p = Path(file)
if p.suffix == '.json':
name = p.stem
m = Match.read_from_json(str(file))
info = m.query('game', 'created').select('info').first()['info']
conf = json.loads(info['config'])
game_end_at = int(info['game_end_at'])
survivals = {}
foods = {}
survivals[0] = []
for p in m.query('player', 'join').select('pid').raw_data:
foods[p['pid']] = conf['start_resource']
survivals[0].append(p['pid'])
for i in range(1, game_end_at+1):
survivals[i] = []
for a in m.query('action', 'done').where(lambda x: x['rno'] == i).raw_data:
foods[a['a']] += conf['payoffs']["%s%s"%(a['act_a'], a['act_b'])][0] * a['tr'] / 1440.0
foods[a['b']] += conf['payoffs']["%s%s"%(a['act_a'], a['act_b'])][1] * a['tr'] / 1440.0
for j in foods.keys():
foods[j] -= conf['rounds']['consumption']
if foods[j] > 0:
survivals[i].append(j)
result[name] = survivals
print(json.dumps(result))

52
comp_co_per_match.py Normal file
View File

@ -0,0 +1,52 @@
import json
from matplotlib import pyplot as plt
from island.match import Match
from island.matches import Matches
import numpy as np
def calc_co(matches):
x = [0,0,0,0,0,0]
for j in range(len(matches.data)):
if len(matches.data[j].query('action', 'done').raw_data) < 5:
continue
coop = 0
rows = matches.data[j].query('action', 'done').raw_data
for row in rows:
if row['act_a'] == 'C' and row['act_b'] == 'C':
coop += 1
if rows:
per = float(coop) / len(rows)
x[int(per*100)//20] += 1
x[4] += x[5]
x.pop()
s = sum(x)
for i in range(5):
x[i] /= s
return x
casual = Matches('wos-data-casual')
compete = Matches('wos-data-new')
ca = calc_co(casual)
co = calc_co(compete)
fig = plt.figure(figsize=(5,4))
ax = fig.gca()
index = np.arange(5)
bar_width = 0.35
rects1 = ax.bar(index, ca, bar_width, color='#00b894', label='Casual games')
# rects2 = ax.bar(index + bar_width, co, bar_width, color='#005CAF', label='Competition')
rects2 = ax.bar(index + bar_width, co, bar_width, color='#6c5ce7', label='Competition')
ax.set_xticks(index + bar_width / 2)
ax.set_xticklabels(['0~0.2','0.2~0.4','0.4~0.6','0.6~0.8','0.8~1'])
ax.legend()
# fig.autofmt_xdate()
fig.set_size_inches(5, 4)
plt.xlabel('Frequency of Cooperation Actions per Match')
plt.ylabel('Fraction of Matches')
fig.tight_layout()
# plt.show()
plt.savefig('graph/comp_co_per_match.eps')

58
fail_reason.py Normal file
View File

@ -0,0 +1,58 @@
import json
from matplotlib import pyplot as plt
from island.match import Match
from island.matches import Matches
matches = Matches('wos-data-casual')
labels = ['not enough tr', 'other reason']
percents = [0.0, 0.0]
op = 'D'
def get_reason(m, i, target):
r = m.query('action', 'request').where(lambda x: x['rno'] == i+1 and x['from'] == target).raw_data
rs = m.query('action', 'approve').where(lambda x: x['rno'] == i+1 and x['to'] == target).raw_data
rsn = [n['from'] for n in rs]
for j in r:
if j['to'] not in rsn:
tr = 1440
for k in m.query('action', 'request').where(lambda x: x['rno'] == i+1 and x['from'] == j['to']).raw_data:
tr -= k['tr']
for k in m.query('action', 'cancel').where(lambda x: x['rno'] == i+1 and x['from'] == j['to']).raw_data:
tr += m.query('action', 'request').where(lambda x: x['rno'] == i+1 and x['from'] == j['to'] and x['to'] == k['to'] and x['log_id'] < k['log_id']).orderby('log_id').raw_data[-1]['tr']
for k in m.query('action', 'deny').where(lambda x: x['rno'] == i+1 and x['to'] == j['to']).raw_data:
tr += m.query('action', 'request').where(lambda x: x['rno'] == i+1 and x['from'] == j['to'] and x['to'] == k['from'] and x['log_id'] < k['log_id']).orderby('log_id').raw_data[-1]['tr']
for k in m.query('action', 'approve').where(lambda x: x['rno'] == i+1 and x['from'] == j['to']).raw_data:
tr -= k['tr']
print(tr)
if tr >= j['tr']:
percents[1] += 1
else:
percents[0] += 1
for m in matches.data:
info = m.query('game', 'created').select('info').first()['info']
conf = json.loads(info['config'])
game_end_at = int(info['game_end_at'])
for i in range(1, game_end_at):
calced = set()
for row in m.query('action', 'done').where(lambda x: (x['act_a'] == op or x['act_b'] == op) and x['rno'] == i).raw_data:
if row['act_a'] == op and row['a'] not in calced:
get_reason(m, i, row['a'])
calced.add(row['a'])
if row['act_b'] == op and row['b'] not in calced:
get_reason(m, i, row['b'])
calced.add(row['b'])
_all = sum(percents) / 100
percents[0] /= _all
percents[1] /= _all
plt.figure()
plt.pie(percents, labels=labels, autopct='%1.2f%%', startangle=90)
plt.axis('equal') # Equal aspect ratio ensures that pie is drawn as a circle.
plt.show()
# plt.savefig('graph/unlink_has_neighbor.png')

110
neighbor_per_round.py Normal file
View File

@ -0,0 +1,110 @@
import json
from matplotlib import pyplot as plt
from island.match import Match
from island.matches import Matches
from numpy import mean, std
import numpy as np
matches = Matches('wos-data-new')
max_round = 17
survivals = {}
with open('survivals.json', 'r') as f:
survivals = json.load(f)
neighbors = {}
cmean = []
dmean = []
cstd = []
dstd = []
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(max_round):
cneigh = []
dneigh = []
for j in range(len(matches.data)):
rows = matches.data[j].query('action', 'done').where(lambda x: x['rno']==i+1).raw_data
calced = set()
for row in rows:
if row['a'] not in calced:
nn = 0
for k in neighbors[matches.names[j]][row['a']]:
if k in survivals[matches.names[j]][str(i+1)]:
nn += 1
if row['act_a'] == 'C':
cneigh.append(nn)
else:
dneigh.append(nn)
calced.add(row['a'])
if row['b'] not in calced:
nn = 0
for k in neighbors[matches.names[j]][row['b']]:
if k in survivals[matches.names[j]][str(i+1)]:
nn += 1
if row['act_b'] == 'C':
cneigh.append(nn)
else:
dneigh.append(nn)
calced.add(row['b'])
if cneigh:
cm = mean(cneigh)
cs = std(cneigh)
else:
cm = 0
cs = 0
cmean.append(cm)
cstd.append(cs)
if dneigh:
dm = mean(dneigh)
ds = std(dneigh)
else:
dm = 0
ds = 0
dmean.append(dm)
dstd.append(ds)
fig, ax = plt.subplots()
index = np.arange(17)
bar_width = 0.35
opacity = 0.4
error_config = {'ecolor': '0.3', 'capsize': 4}
rects1 = ax.bar(index, cmean, bar_width,
alpha=opacity, color='b',
yerr=cstd, error_kw=error_config,
label='C')
rects2 = ax.bar(index + bar_width, dmean, bar_width,
alpha=opacity, color='r',
yerr=dstd, error_kw=error_config,
label='D')
# ax.set_xlabel('Group')
# ax.set_ylabel('Scores')
# ax.set_title('Scores by group and gender')
ax.set_xticks(index + bar_width / 2)
ax.set_xticklabels(index+1)
ax.legend()
fig.tight_layout()
plt.show()

121
request_success.py Normal file
View File

@ -0,0 +1,121 @@
import json
from matplotlib import pyplot as plt
from island.match import Match
from island.matches import Matches
from numpy import mean, std
import numpy as np
matches = Matches('wos-data-casual')
max_round = 17
c_req_suc_mean = []
d_req_suc_mean = []
c_req_suc_std = []
d_req_suc_std = []
c_req_fail_mean = []
d_req_fail_mean = []
c_req_fail_std = []
d_req_fail_std = []
for i in range(max_round):
cReSu = []
cReFa = []
dReSu = []
dReFa = []
for j in range(len(matches.data)):
rows = matches.data[j].query('action', 'done').where(lambda x: x['rno'] == i+1).raw_data
calced = set()
for row in rows:
if row['a'] not in calced:
r = matches.data[j].query('action', 'request').where(lambda x: x['rno'] == i+2 and x['from'] == row['a']).raw_data
rs = matches.data[j].query('action', 'approve').where(lambda x: x['rno'] == i+2 and x['to'] == row['a']).raw_data
if row['act_a'] == 'C':
cReSu.append(len(rs))
cReFa.append(len(r) - len(rs))
else:
dReSu.append(len(rs))
dReFa.append(len(r) - len(rs))
calced.add(row['a'])
if row['b'] not in calced:
r = matches.data[j].query('action', 'request').where(lambda x: x['rno'] == i+2 and x['from'] == row['b']).raw_data
rs = matches.data[j].query('action', 'approve').where(lambda x: x['rno'] == i+2 and x['to'] == row['b']).raw_data
if row['act_b'] == 'C':
cReSu.append(len(rs))
cReFa.append(len(r) - len(rs))
else:
dReSu.append(len(rs))
dReFa.append(len(r) - len(rs))
calced.add(row['b'])
if cReSu:
cm = mean(cReSu)
cs = std(cReSu)
else:
cm = 0
cs = 0
c_req_suc_mean.append(cm)
c_req_suc_std.append(cs)
if cReFa:
cm = mean(cReFa)
cs = std(cReFa)
else:
cm = 0
cs = 0
c_req_fail_mean.append(cm)
c_req_fail_std.append(cs)
if dReSu:
dm = mean(dReSu)
ds = std(dReSu)
else:
dm = 0
ds = 0
d_req_suc_mean.append(dm)
d_req_suc_std.append(ds)
if dReFa:
dm = mean(dReFa)
ds = std(dReFa)
else:
dm = 0
ds = 0
d_req_fail_mean.append(dm)
d_req_fail_std.append(ds)
fig, ax = plt.subplots()
index = np.arange(17)
bar_width = 0.35
opacity = 0.6
error_config = {'ecolor': '0.3', 'capsize': 4}
rects1 = ax.bar(index, c_req_suc_mean, bar_width,
alpha=opacity, color='g',
# yerr=c_req_suc_std, error_kw=error_config,
label='C-Req-Success')
rects2 = ax.bar(index, c_req_fail_mean, bar_width,
alpha=opacity, color='b',
# yerr=c_req_fail_std, error_kw=error_config,
bottom=c_req_suc_mean,
label='C-Req-Fail')
rects3 = ax.bar(index + bar_width, d_req_suc_mean, bar_width,
alpha=opacity, color='y',
# yerr=d_req_suc_mean, error_kw=error_config,
label='D-Req-Success')
rects4 = ax.bar(index + bar_width, d_req_fail_mean, bar_width,
alpha=opacity, color='r',
# yerr=d_req_fail_mean, error_kw=error_config,
bottom=d_req_suc_mean,
label='D-Req-Fail')
# ax.set_xlabel('Group')
# ax.set_ylabel('Scores')
# ax.set_title('Scores by group and gender')
ax.set_xticks(index + bar_width / 2)
ax.set_xticklabels(index+1)
ax.legend()
fig.tight_layout()
plt.show()

1
survivals.json Normal file

File diff suppressed because one or more lines are too long