Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Large (52%) Performance Gains #37

Open
wants to merge 2 commits into
base: gemstone
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 5 additions & 18 deletions code/prisonersDilemma.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,6 @@
# D = defect, betray, sabotage, free-ride, etc.
# C = cooperate, stay silent, comply, upload files, etc.


# Returns a 2-by-n numpy array. The first axis is which player (0 = us, 1 = opponent)
# The second axis is which turn. (0 = first turn, 1 = next turn, etc.
# For example, it might have the values
#
# [[0 0 1] a.k.a. D D C
# [1 1 1]] a.k.a. C C C
#
# if there have been 3 turns, and we have defected twice then cooperated once,
# and our opponent has cooperated all three times.
def getVisibleHistory(history, player, turn):
historySoFar = history[:,:turn].copy()
if player == 1:
historySoFar = np.flip(historySoFar,0)
return historySoFar

def strategyMove(move):
if type(move) is str:
defects = ["defect","tell truth"]
Expand All @@ -43,12 +27,15 @@ def runRound(pair):

LENGTH_OF_GAME = int(200-40*np.log(1-random.random())) # The games are a minimum of 200 turns long. The np.log here guarantees that every turn after the 200th has an equal (low) chance of being the final turn.
history = np.zeros((2,LENGTH_OF_GAME),dtype=int)
historyFlipped = np.zeros((2,LENGTH_OF_GAME),dtype=int)

for turn in range(LENGTH_OF_GAME):
playerAmove, memoryA = moduleA.strategy(getVisibleHistory(history,0,turn),memoryA)
playerBmove, memoryB = moduleB.strategy(getVisibleHistory(history,1,turn),memoryB)
playerAmove, memoryA = moduleA.strategy(history[:,:turn].copy(),memoryA)
playerBmove, memoryB = moduleB.strategy(historyFlipped[:,:turn].copy(),memoryB)
history[0,turn] = strategyMove(playerAmove)
history[1,turn] = strategyMove(playerBmove)
historyFlipped[0,turn] = history[1,turn]
historyFlipped[1,turn] = history[0,turn]

return history

Expand Down