The .py file is used to generate semigroup mappings for each regime which can be used as input to GAP (https://www.gap-system.org/) The .g file can then be read into GAP for analysis and figure generation.
/PDsemi.py Secret
Created
June 30, 2020 21:46
Code used to generate and analyze the prisoner's dilemma as a transformation semigroup.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#this can be changed to include any of the constant mappings (d1-d6, c1-c6) | |
PD:=Semigroup(t); | |
#PDstates:=["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "40", "41", "42", "43", "44", "45", "46", "47", "48", "49", "50", "51", "52", "53", "54", "55", "56", "57", "58", "59", "60", "61", "62", "63"]; | |
PDstates:=["000000", "000001", "000010", "000011", "000100", "000101", "000110", "000111", "001000", "001001", "001010", "001011", "001100", "001101", "001110", "001111", "010000", "010001", "010010", "010011", "010100", "010101", "010110", "010111", "011000", "011001", "011010", "011011", "011100", "011101", "011110", "011111", "100000", "100001", "100010", "100011", "100100", "100101", "100110", "100111", "101000", "101001", "101010", "101011", "101100", "101101", "101110", "101111", "110000", "110001", "110010", "110011", "110100", "110101", "110110", "110111", "111000", "111001", "111010", "111011", "111100", "111101", "111110", "111111"]; | |
PDsymbols:=["t"]; | |
skel_PD:=Skeleton(PD); | |
DisplayHolonomyComponents(skel_PD); | |
#subduction chain | |
Splash(DotSubductionEquivalencePoset(skel_PD)); | |
dot_PD:=DotSkeleton(skel_PD,rec(states:=PDstates,symbols:=PDsymbols)); | |
Splash(dot_PD); | |
d_label:=DotSemigroupActionWithNames(PD,[1..64],OnPoints,PDstates,PDsymbols); | |
Splash(d_label); | |
PD_Coordinatized:=HolonomyCascadeSemigroup(PD); | |
SplashList(DotRepHolonomyGroups(skel_PD, rec(states:=PDstates,symbols:=PDsymbols))); | |
SplashList(DotNaturalSubsystems(skel_PD, rec(states:=PDstates,symbols:=PDsymbols))); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import networkx as nx | |
from itertools import product | |
import sys | |
import numpy as np | |
import matplotlib.pyplot as plt | |
def play(G,node,nb,b): | |
if (G.node[node]['strategy']==0 and G.node[nb]['strategy']==0): | |
return 1 | |
elif (G.node[node]['strategy']==1 and G.node[nb]['strategy']==0): | |
return 0 | |
elif (G.node[node]['strategy']==0 and G.node[nb]['strategy']==1): | |
return b | |
elif (G.node[node]['strategy']==1 and G.node[nb]['strategy']==1): | |
return 3 | |
def imitate(G,node): | |
original=G.node[node]['payoff'] | |
choice=original | |
sList=[G.node[node]['strategy']] | |
for nb in G.neighbors(node): | |
if (G.node[nb]['payoff']>original and G.node[nb]['payoff']>choice): | |
choice=G.node[nb]['payoff'] | |
sList=[G.node[nb]['strategy']] | |
elif (G.node[nb]['payoff']==choice and G.node[nb]['payoff']>original): | |
sList.append(G.node[nb]['strategy']) | |
if len(sList)==0: | |
return original | |
if len(sList)==1: | |
return sList[0] | |
elif len(sList)>1: | |
if 1 in sList: | |
return 1 | |
else: return 0 | |
def tAction(G): | |
for node in G.nodes(): | |
for nb in G.neighbors(node): | |
G.node[node]['payoff']+=play(G,node,nb,b) | |
for node in G.nodes(): | |
G.node[node]['next']=imitate(G,node) | |
for node in G.nodes(): | |
G.node[node]['strategy']=G.node[node]['next'] | |
def dAction(G,node): | |
G.node[node]['strategy']=0 | |
def cAction(G,node): | |
G.node[node]['strategy']=1 | |
N=2 | |
M=3 | |
G=nx.grid_graph(dim=[N,M],periodic=True) | |
states=list(product([0,1],repeat=N*M)) | |
bList=[4.5,4,3.5,3] | |
regList=["A","B","C","D"] | |
for k,b in enumerate(bList): | |
tMap={} | |
dMap=[dict() for x in range(N*M)] | |
cMap=[dict() for x in range(N*M)] | |
for x in range(len(states)): | |
strategies={} | |
clear={} | |
i=0 | |
for node in G.nodes(): | |
strategies[node]=states[x][i] | |
clear[node]=0 | |
i+=1 | |
nx.set_node_attributes(G, strategies, 'strategy') | |
nx.set_node_attributes(G, clear, 'next') | |
nx.set_node_attributes(G, clear, 'payoff') | |
tAction(G) | |
for node in G.nodes(): | |
strategies[node]=G.node[node]['strategy'] | |
for y in range(len(states)): | |
if tuple(strategies.values())==states[y]: | |
tMap[x+1]=y | |
i=0 | |
for nodeSwitch in G.nodes(): | |
for x in range(len(states)): | |
strategies={} | |
clear={} | |
j=0 | |
for node in G.nodes(): | |
strategies[node]=states[x][j] | |
j+=1 | |
nx.set_node_attributes(G, strategies, 'strategy') | |
dAction(G,nodeSwitch) | |
for node in G.nodes(): | |
strategies[node]=G.node[node]['strategy'] | |
for y in range(len(states)): | |
if tuple(strategies.values())==states[y]: | |
dMap[i][x]=y | |
i+=1 | |
i=0 | |
for nodeSwitch in G.nodes(): | |
for x in range(len(states)): | |
strategies={} | |
clear={} | |
j=0 | |
for node in G.nodes(): | |
strategies[node]=states[x][j] | |
j+=1 | |
nx.set_node_attributes(G, strategies, 'strategy') | |
cAction(G,nodeSwitch) | |
for node in G.nodes(): | |
strategies[node]=G.node[node]['strategy'] | |
for y in range(len(states)): | |
if tuple(strategies.values())==states[y]: | |
cMap[i][x]=y | |
i+=1 | |
with open('PDsemi'+regList[k]+'.g','w') as f: | |
f.write('LoadPackage("SgpDec");\n') | |
for elem in tMap: | |
tMap[elem]+=1 | |
f.write("t:=Transformation("+str(list(tMap.values()))+");\n") | |
for i in range(len(dMap)): | |
for elem in dMap[i]: | |
dMap[i][elem]+=1 | |
f.write("d"+str(i+1)+":=Transformation("+str(list(dMap[i].values()))+");\n") | |
for i in range(len(cMap)): | |
for elem in cMap[i]: | |
cMap[i][elem]+=1 | |
f.write("c"+str(i+1)+":=Transformation("+str(list(cMap[i].values()))+");\n") | |
print('PDstates:=[', end='') | |
for i in range(len(states)): | |
print('"', end='') | |
for elem in states[i]: | |
print(elem, end='') | |
print('"', end='') | |
if(i<len(states)-1): | |
print(', ', end='') | |
else: | |
print('];') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment