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

Adebisi Lives #12

Open
wants to merge 24 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# PrisonersDilemmaTournament

This is my CS 269i class project.
This is my CS 269i class project. Check out my webpage for context! https://htwins.net/prisoners-dilemma/

Watch This Place's awesome videos about iterated Prisoner's Dilemma for context!
Watch This Place's awesome videos about iterated Prisoner's Dilemma for more context!

https://www.youtube.com/watch?v=t9Lo2fgxWHw

Expand Down Expand Up @@ -44,3 +44,7 @@ Each pairing simulation runs for this many turns:
200-40*np.log(random.random())
```
This means each game is guaranteed to be at least 200 turns long. But then, for every turn after the 200th, there is an equal probability that the game ends. The probability is very low, so there should be no strategizing to defect on the very last turn consequence-free.

---

Please submit your strategies to this Google Form: https://forms.gle/wWwZY9mmSHF2X34P7 by May 26th, 2021 at 10 PM UTC. Your file can be named whatever you want your strategy to be called, like "superCleverDetective.py", but you must keep it a .py file. Also, do not rename the function header within the file - you must keep it as "def strategy(history, memory):". When I’ve got all your strategies collected in one place, I will combine them with that list of 9 example strategies already provided in the GitHub repo. With these (9 + n) strategies all appearing once in this giant "mosh pit", I will run the program "prisonersDilemma.py". Whoever’s single strategy takes the top spot will win $1,000! Second place will win $300, and third place will win $100. (Note: There is a small chance one of the 9 default strategies win, but I hope that doesn't happen!)
4 changes: 3 additions & 1 deletion code/exampleStrats/alwaysCooperate.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
# Reminder: For the history array, "cooperate" = 1, "defect" = 0

def strategy(history, memory):
return 1, None
return "cooperate", None
4 changes: 3 additions & 1 deletion code/exampleStrats/alwaysDefect.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
# Reminder: For the history array, "cooperate" = 1, "defect" = 0

def strategy(history, memory):
return 0, None
return "defect", None
14 changes: 8 additions & 6 deletions code/exampleStrats/detective.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@
# https://ncase.me/trust/
#
# DETECTIVE: First: I analyze you. I start:
# Cooperate, Cheat, Cooperate, Cooperate.
# If you cheat back, I'll act like [Tit for Tat].
# If you never cheta back, I'll act like [alwaysDefect],
# Cooperate, Defect, Cooperate, Cooperate.
# If you defect back, I'll act like [Tit for Tat].
# If you never defect back, I'll act like [alwaysDefect],
# to exploit you. Elementary, my dear Watson.

# Reminder: For the history array, "cooperate" = 1, "defect" = 0

def strategy(history, memory):
testingSchedule = [1,0,1,1]
testingSchedule = ["cooperate","defect","cooperate","cooperate"]
gameLength = history.shape[1]
shallIExploit = memory
choice = None
Expand All @@ -27,8 +29,8 @@ def strategy(history, memory):

if gameLength >= 4:
if shallIExploit:
choice = 0
choice = "defect"
else:
choice = history[1,-1] # Do Tit for Tat

return choice, shallIExploit
return choice, shallIExploit
9 changes: 6 additions & 3 deletions code/exampleStrats/ftft.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
# Forgiving Tit for Tat.
# Choose to defect if and only if the opponent just defected TWICE in a row.

# Reminder: For the history array, "cooperate" = 1, "defect" = 0

def strategy(history, memory):
choice = 1
choice = "cooperate"
if history.shape[1] >= 2 and history[1,-1] == 0 and history[1,-2] == 0: # We check the TWO most recent turns to see if BOTH were defections, and only then do we defect too.
choice = 0
return choice, None
choice = "defect"
return choice, None
10 changes: 6 additions & 4 deletions code/exampleStrats/grimTrigger.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
#
# In this implementation, I used the memory variable to store Grim Trigger's state of mind.
# memory is true if Grim Trigger has been wronged, and false if it hasn't.
#

# Reminder: For the history array, "cooperate" = 1, "defect" = 0

def strategy(history, memory):
wronged = False
if memory is not None and memory: # Has memory that it was already wronged.
Expand All @@ -14,7 +16,7 @@ def strategy(history, memory):
wronged = True

if wronged:
return 0, True
return "defect", True
else:
return 1, False

return "cooperate", False
8 changes: 5 additions & 3 deletions code/exampleStrats/joss.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
# Variant of Tit For Tat that randomly defects to try to take advantage
# of overly forgiving opponents.

# Reminder: For the history array, "cooperate" = 1, "defect" = 0

def strategy(history, memory):
choice = 1
choice = "cooperate"
if random.random() < 0.10 or (history.shape[1] >= 1 and history[1,-1] == 0):
# Choose to defect randomly by 10% chance, OR if and only if the opponent just defected.
choice = 0
return choice, None
choice = "defect"
return choice, None
4 changes: 0 additions & 4 deletions code/exampleStrats/random.py

This file was deleted.

9 changes: 9 additions & 0 deletions code/exampleStrats/randomStrategy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import random

# Reminder: For the history array, "cooperate" = 1, "defect" = 0

def strategy(history, memory):
if random.randint(0,1) == 0:
return "cooperate", None
else:
return "defect", None
12 changes: 7 additions & 5 deletions code/exampleStrats/simpleton.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,18 @@
#
# SIMPLETON: Hi! I try to start by cooperating. If you cooperate
# back, I do the same thing as my last move, even if it was a mistake.
# If you cheat back, I do the opposite thing as my last move, even
# If you defect back, I do the opposite thing as my last move, even
# if it was a mistake.

# Reminder: For the history array, "cooperate" = 1, "defect" = 0

def strategy(history, memory):
choice = None
if history.shape[1] == 0: # We're on the first turn!
choice = 1
choice = "cooperate"
else:
choice = history[0,-1] # I will keep doing the same thing as last move!
choice = "cooperate" if history[0,-1] == 1 else "defect" # I will keep doing the same thing as last move!
if history[1,-1] == 0: # If my opponent defected last turn, I'll just do the opposite thing as my last move:
choice = 1-choice
choice = "defect" if history[0,-1] == 1 else "cooperate" # I will keep doing the same thing as last move!

return choice, None
return choice, None
8 changes: 5 additions & 3 deletions code/exampleStrats/titForTat.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Reminder: For the history array, "cooperate" = 1, "defect" = 0

def strategy(history, memory):
choice = 1
choice = "cooperate"
if history.shape[1] >= 1 and history[1,-1] == 0: # Choose to defect if and only if the opponent just defected.
choice = 0
return choice, None
choice = "defect"
return choice, None
9 changes: 5 additions & 4 deletions code/prisonersDilemma.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,19 @@ def getVisibleHistory(history, player, turn):

def strategyMove(move):
if type(move) is str:
defects = ["defect","confess the crime"]
defects = ["defect","tell truth"]
return 0 if (move in defects) else 1
else:
return move
# Coerce all moves to be 0 or 1 so strategies can safely assume 0/1's only
return int(bool(move))

def runRound(pair):
moduleA = importlib.import_module(STRATEGY_FOLDER+"."+pair[0])
moduleB = importlib.import_module(STRATEGY_FOLDER+"."+pair[1])
memoryA = None
memoryB = None

LENGTH_OF_GAME = int(200-40*np.log(random.random())) # The games are a minimum of 50 turns long. The np.log here guarantees that every turn after the 50th has an equal (low) chance of being the final turn.
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)

for turn in range(LENGTH_OF_GAME):
Expand Down Expand Up @@ -117,4 +118,4 @@ def runFullPairingTournament(inFolder, outFile):
print("Done with everything! Results file written to "+RESULTS_FILE)


runFullPairingTournament(STRATEGY_FOLDER, RESULTS_FILE)
runFullPairingTournament(STRATEGY_FOLDER, RESULTS_FILE)
Loading