diff --git a/break_tie.py b/break_tie.py index 8d0ebd5..73ed3e2 100644 --- a/break_tie.py +++ b/break_tie.py @@ -3,7 +3,7 @@ from matplotlib import pyplot as plt from island.match import Match from island.matches import Matches -mode = 'SURVIVE' +mode = 'CLASSIC' matches = Matches.from_profile_expr(lambda r: mode in r) labels = ['Stay Connected', 'Break Tie'] @@ -40,11 +40,12 @@ print(percents) _all = sum(percents) / 100 percents[0] /= _all percents[1] /= _all +print(percents) plt.figure() plt.pie(percents, labels=labels, autopct="%1.2f%%", pctdistance=1.1, labeldistance=2,startangle=90, colors=['#00b894', '#fdcb6e']) plt.legend() plt.axis('equal') # Equal aspect ratio ensures that pie is drawn as a circle. -# plt.show() -plt.savefig("graph/break_tie_%s_%s.eps"%(op, mode)) +plt.show() +# plt.savefig("graph/break_tie_%s_%s.eps"%(op, mode)) diff --git a/break_tie_bar.py b/break_tie_bar.py new file mode 100644 index 0000000..16915cd --- /dev/null +++ b/break_tie_bar.py @@ -0,0 +1,44 @@ +from scipy.stats import chi2_contingency as chi2 +import numpy as np +from matplotlib import pyplot as plt + +''' +survive +''' +break_tie = [13.717872086072923, 86.26126126126125] +stay_connected = [86.28212791392707, 13.738738738738737] +''' +classic +''' +# break_tie = [8.331150117893634, 42.07858048162231] +# stay_connected = [91.66884988210636, 57.9214195183777] + +mode = 'SURVIVE' + +fig = plt.figure(figsize=(3, 3)) +ax = fig.gca() + +index = np.arange(2) +bar_width = 0.35 + +opacity = 1 +error_config = {'ecolor': '0.3'} + +rects1 = ax.bar(index, break_tie, bar_width, + alpha=opacity, color='#fdcb6e', + label='Break Tie') + +rects2 = ax.bar(index + bar_width, stay_connected, bar_width, + alpha=opacity, color='#00b894', + label='Stay Connected') + + +ax.set_ylabel('Frequency of Behavior') +ax.set_title('Behavior after Moves') +ax.set_xticks(index + bar_width / 2) +ax.set_xticklabels(('C', 'D')) +ax.set_ylim(0,100) +fig.legend(loc='lower center') +fig.tight_layout() +# plt.show() +plt.savefig("graph/break_tie_bar_%s.eps" % (mode)) diff --git a/break_tie_chi2.py b/break_tie_chi2.py index ffad095..bd75fbc 100644 --- a/break_tie_chi2.py +++ b/break_tie_chi2.py @@ -9,7 +9,7 @@ print("%f, %e, %f" % (chi, p, dof)) survive: chi = 1189.53, p = 1.149752e-260 -survive: +classic: chi = 611.59, p = 5.031232e-135 ''' diff --git a/food_loss.py b/food_loss.py new file mode 100644 index 0000000..f9f007c --- /dev/null +++ b/food_loss.py @@ -0,0 +1,75 @@ +import json +from matplotlib import pyplot as plt +from island.match import Match +from island.matches import Matches +import numpy as np +from scipy.stats import pearsonr + + +class Solution: + def __init__(self, mode): + self.mode = mode + self.matches = Matches.from_profile_expr(lambda r: mode in r) + + def food_loss(self): + fl = [[],[]] + payoff = { + 'C': { + 'C': 2.6, + 'D': 0 + }, + 'D': { + 'C': 4.5, + 'D': 0.5 + } + } + for m in self.matches.data: + info = m.query('game', 'created').select('info').first()['info'] + game_end_at = int(info['game_end_at']) + + for p in m.query('player', 'join').raw_data: + pid = p['pid'] + for i in range(2, game_end_at): + c, t = 0, 0 + for r in m.query('action', 'done').where(lambda x: x['rno'] == i - 1 and (x['a'] == pid or x['b'] == pid)).raw_data: + if r['act_a' if r['a'] == pid else 'act_b'] == 'C': + c += 1 + t += 1 + is_coop = 0 if c * 2 >= t else 1 + f = -3.0 + for r in m.query('action', 'done').where(lambda x: x['rno'] == i and (x['a'] == pid or x['b'] == pid)).raw_data: + if r['a'] == pid: + f += payoff[r['act_a']][r['act_b']] * r['tr'] / 1440.0 + else: + f += payoff[r['act_b']][r['act_a']] * r['tr'] / 1440.0 + fl[is_coop].append(f) + return fl + + +if __name__ == '__main__': + mode = 'SURVIVE' + s = Solution(mode) + f = s.food_loss() + blue = '#0984e3' + red = '#d63031' + c = [blue, red] + labels = ['C', 'D'] + fig = plt.figure(figsize=(3, 3)) + ax = fig.gca() + error_config = {'ecolor': '0.3', 'capsize': 4} + bplot = ax.boxplot(f, + vert=True, # vertical box alignment + patch_artist=True, # fill with color + notch=True, # notch shape + labels=labels) # will be used to label x-ticks + + for patch, color in zip(bplot['boxes'], c): + patch.set_facecolor(color) + ax.set_ylabel('Frequency') + # ax.set_title('Scores by group and gender') + + ax.legend() + + fig.tight_layout() + plt.show() + # plt.savefig("graph/new_partner_bar_%s.eps" % mode) diff --git a/new_partner_bar.py b/new_partner_bar.py new file mode 100644 index 0000000..3380481 --- /dev/null +++ b/new_partner_bar.py @@ -0,0 +1,109 @@ +import json +from matplotlib import pyplot as plt +from island.match import Match +from island.matches import Matches +import numpy as np +from scipy.stats import pearsonr + +class Solution: + def __init__(self, mode): + self.mode = mode + self.matches = Matches.from_profile_expr(lambda r: mode in r) + self.survivals = {} + with open('survivals.json', 'r') as f: + self.survivals = json.load(f) + self.neighbors = {} + self.read_neighbor() + + def read_neighbor(self): + for i, m in enumerate(self.matches.data): + 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']] + self.neighbors[self.matches.names[i]] = n + + def new_partner_after(self): + re_after = [0] * 2 #[c,d] + su_after = [0] * 2 + for m in self.matches.data: + info = m.query('game', 'created').select('info').first()['info'] + game_end_at = int(info['game_end_at']) + + for p in m.query('player', 'join').raw_data: + pid = p['pid'] + for i in range(2, game_end_at): + previous_round_partner = [] + c,t = 0,0 + for r in m.query('action', 'done').where(lambda x: x['rno'] == i - 1 and (x['a'] == pid or x['b'] == pid)).raw_data: + if r['a'] == pid: + previous_round_partner.append(r['b']) + if r['act_a'] == 'C': + c += 1 + else: + previous_round_partner.append(r['a']) + if r['act_b'] == 'C': + c += 1 + t += 1 + is_coop = 0 if c * 2 >= t else 1 + + new_partner_request = 0 + for r in m.query('action', 'request').where(lambda x: x['rno'] == i and (x['from'] == pid or x['to'] == pid)).raw_data: + if r['from'] == pid: + if r['to'] not in previous_round_partner: + new_partner_request += 1 + else: + if r['from'] not in previous_round_partner: + new_partner_request += 1 + if new_partner_request == 0: + continue + re_after[is_coop] += new_partner_request + new_partner_succ = 0 + for r in m.query('action', 'done').where(lambda x: x['rno'] == i and (x['a'] == pid or x['b'] == pid)).raw_data: + if r['a'] == pid: + if r['b'] not in previous_round_partner: + new_partner_succ += 1 + else: + if r['a'] not in previous_round_partner: + new_partner_succ += 1 + su_after[is_coop] += new_partner_succ + for i, _ in enumerate(su_after): + su_after[i] /= re_after[i] + return su_after + + +if __name__ == '__main__': + mode = 'CLASSIC' + s = Solution(mode) + su_after = s.new_partner_after() + index = np.arange(2) + + blue = '#0984e3' + red = '#d63031' + + fig = plt.figure(figsize=(3, 3)) + ax = fig.gca() + bar_width = 0.5 + opacity = 1 + error_config = {'ecolor': '0.3', 'capsize': 4} + rects1 = ax.bar(index, su_after, bar_width, + color=red, label='New Patner') + + ax.set_ylabel('Frequency') + # ax.set_title('Scores by group and gender') + ax.set_xticks(index) + ax.set_xticklabels(['C', 'D']) + ax.set_ylim(0,1) + ax.set_xlim(-0.5,1.5) + ax.legend() + + fig.tight_layout() + # plt.show() + plt.savefig("graph/new_partner_bar_%s.eps" % mode)