Fork/Clone Prisoners Dilemma
Fork/Clone Prisoners Dilemma
How do individuals make strategic decisions? What has a greater impact on making a decision, the prospect of reward or the threat of punishment? How do decisions made in one situation apply to another?
As we learn about version control and collaboration solutions, we will explore and modify existing code that simulates a round-robin tournament among many players. The game is called the Iterative Prisoner's Dilemma (IPD), a fundamental problem in game theory. Game theory is an approach to studying the social sciences, using computation to understand and predict people's behavior. Game theory is used, for example, to understand and predict economic phenomena like stock market fluctuations and political phenomena like revolutions. Nations use game theorists to advise national leaders and negotiators in order to help them understand the group's dynamics and predict results for various actions they are considering taking.
Game theorists use algorithms to describe people's decisions. Consider your decision to buy lunch each day. If the cafeteria director decides to raise the lunch price above a certain point, you will opt not to buy school lunch, right? At what price? Are there other factors? A game theorist could write an algorithm for the decision to buy school lunch in terms of price and other factors.
The Iterative Prisoner's Dilemma is a series of rounds of the Prisoner's Dilemma. The Prisoner's Dilemma was one of the early algorithmic problems in game theory, first posed in 1950. In the Prisoner's Dilemma, you and a partner have committed a crime together, and you are caught without evidence. The police question you and your partner separately.
If both of you collude together and refuse to talk to the police, you both will go free.
If you both confess your crime and betray each other, the liability of the crime will be split between you. You both will receive the standard punishment.
If you stonewall the police, hoping to collude with your partner but your partner betrays you, you will receive an unusually severe punishment while they go free and get to keep the stolen goods.
If you betray your partner while they attempt to collude with you, you will receive the cash and freedom.
In the Iterative Prisoner's Dilemma, many rounds are played, one after another, with the same partner in crime. Betraying in one round will sometimes cause the partner to lose trust and be less likely to collude with you in future rounds. Once partners start betraying each other, round after round they end up turning each other in and doing poorly. Considered together, you and your partner's outcomes are better if you collude round after round. The Prisoner's Dilemma lets social scientists study the conditions under which people will act either selfishly or in the best collective interests of the group.
Our recent capacity to generate, collect, store, and analyze huge amounts of data quickly has caused dramatic changes in all career fields. Simulation was an unimportant tool in social sciences in 1960. The rise in computational power has changed the career fields in social sciences such that simulation is now a fundamental and common tool for many social scientists.
Your teacher has assigned you to a team. You will pair program together on only one of your github files.
Fork, Clone and create a "testing" branch of the Prisoner's Dilemma Python file. Run the file and enter the number of teams you want to run in the simulation. Try just 4 for the first run. Data from the simulation are printed to the screen and stored in a data file. On screen you should see the following information.
Each column shows the score earned per round against each other player. Looking at the second table, which player finished with the highest average score? Note that all the scores are negative.
The simulation program represents each decision by one of the two characters 'c' or 'b' to collude or betray. Before making the decision each round, each player (or algorithm) can consider what has happened in previous rounds. The algorithms have the previous rounds' information in the form of a string. For example 'ccb' indicates that the player colluded in the first two rounds and betrayed in the most recent round. Each player can consider two strings: one for their own history and one for their crime partner's history. In the below example, 5 rounds have been played. Considering the following two histories can you determine each of their scores?
Your history 'ccccc'
Your crime partner's history 'ccccb'
The four players in the first simulation you ran used three different algorithms, as labeled in the second table: loyal, backstabber, loyal vengeful. The code for playing a tournament calls get_action() with getting_team_name=True in the def play_tournament. But you don't need to worry about that code; just focus on the strategies. Three parts of Python code implementing the three algorithmic strategies are shown below, in the else-blocks. The line numbers you see below might not be the same line numbers in the python file. Only the third algorithm considers what has occurred in previous rounds. It uses history and opponent_history.
A ten-round match between player 0 and player 1 results as follows:
Player 0: 'cccccccccc' Score = -5000
Player 1: 'bbbbbbbbbb' Score = +1000
Analyze the code for these algorithms and record the results you expect from a ten-round match of the IPD between player 1 and player 2, following the previous example matching player 0 to player 1.
Now open the file tournament.txt. The simulation program will have stored that file in the same directory as the simulation program itself. At the top of the file is the record for team 1 vs. team 0, showing their final scores per round, the names for their strategies, and a record of their decisions in b and c strings. When the simulation runs, it runs 200 to 300 rounds of the dilemma between each pair of strategies. Scroll down until you find the data stored for team 2 vs. team 1. Is it different than your prediction? If so, how?
Run the tournament again using all strategies. Which strategy earned the highest average score? Notice that a strategy's success is dependent upon the strategies it is collaborating with and competing against.
With your partner brainstorm algorithms for making the collude vs. betray decision. Implement your algorithm in Python in the section of the code reserved for your team. You will be replacing the code in the else-block using your algorithm. In that section of code, also give a name to your algorithm's strategy. Test your algorithm by running the file making sure your team number is included. If it appears to be working, examine the tournament.txt file to see how your algorithm performed against each other strategy. If your string of results is not as long as your opponent's string of results your code is not working. Decide on one of your algorithms to contribute to the whole-class tournament.
Merge your testing branch into your "master" branch of your fork of the IPD Repository. Be careful not to make any changes anywhere else in the code. Commit this to your local repository and then push to your remote repository. To merge your changes into the original class code, create a Pull Request by clicking on the green symbol to enter the compare and pull request screen. The top line shows what two things you are comparing. In this case it should show the master branch of the original repository in your school's organization account (the base fork) and your master branch in your forked copy of the repository (the head fork). If it does not, click on the "Edit" button to change it.
Click on "Create pull request" and add in a title a comment (much like a commit message). In the pull request type the names of your group members. Finish by once again clicking "Create pull request."
Your teacher will now get a message that someone has code they would like to add into the original repository. If everything was done correctly, your class's code will be easily merged into the original class repo's master branch. The current commit in that master branch will have the original code modified with the code of every other team. If two different developers both make changes on the same line of code, there will be a merge conflict which will have to be resolved by your instructor. After all teams have entered their code into the competition your instructor will run the tournament.