Skip to content

Instantly share code, notes, and snippets.

Created June 7, 2014 10:52
Show Gist options
  • Save anonymous/14e40dc51d541be3daf9 to your computer and use it in GitHub Desktop.
Save anonymous/14e40dc51d541be3daf9 to your computer and use it in GitHub Desktop.
Zero determinant strategie
# -*- coding: utf-8 -*-
import numpy as np
import matplotlib.pyplot as plt
# Define gain matrix
T,R,P,S = 5,3,1,0
G = np.array([[[R,R],[S,T]],[[T,S],[P,P]]])
# X chooses to unilaterally set Y score
p1 = 0.9
p4 = 0.1
p2 = (p1*(T-P)-(1+p4)*(T-R))/(R-P)
p3 = ((1-p1)*(P-S)+p4*(R-S))/(R-P)
sY = ((1-p1)*P+p4*R)/(1-p1+p4)
## X chooses to extortionate
#chi = 4
#phi = 0.5 * (P-S)/((P-S)+chi*(T-P))
#p1 = 1-phi*(chi-1)*(R-P)/(P-S)
#p2 = 1-phi*(1+chi*(T-P)/(P-S))
#p3 = phi*(chi+(T-P)/(P-S))
#p4 = 0
# Q stratetey
q1,q2,q3,q4 = 0.9,0.1,0.1,0.2
# Define strategy matrices and gain matrix
M1 = np.array([[p1,p2],[p3,p4]])
M2 = np.array([[q1,q2],[q3,q4]])
prevDefect = [False,False] # True if player defected
curDefect = [False,False]
total1, total2 = 0, 0
tx = []
ty = []
N = 10000
for n in range(N):
x1,x2 = np.random.random(size=2)
curDefect[0] = x1 > M1[prevDefect[0],prevDefect[1]]
curDefect[1] = x2 > M2[prevDefect[1],prevDefect[0]]
total1 = total1 + G[curDefect[0],curDefect[1],0]
total2 = total2 + G[curDefect[0],curDefect[1],1]
prevDefect = curDefect
tx.append(total1/(n+1))
ty.append(total2/(n+1))
print(total1/(N+1),total2/(N+1))
plt.plot(tx)
plt.plot(ty)
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment