In this assignment, you will write a function to play an evolutionary game aimed at understanding the evolution of cooperation, a version of the Prisoner’s Dilemma. Consider a general case where members of a species may or may not participate in a cooperative food exchange. Thus, one can choose to cooperate and share food resources, or defect to keep resources for oneself. You can alter your behavior based on the behavior of another player. The basic payoff matrix is as follows:
| Cooperate | Defect | |
|---|---|---|
| Cooperate | V | S |
| Defect | R | C |
Here, V is the fitness boost obtained if boh individuals cooperate; we will use V = 1. C is the fitness cost if both defect and are therefore short on food (C = -4). S is the fitness cost if you choose to cooperate (share food) but your opponent defects, such that you end up with even less food (S = -5). Lastly, R is the fitness benefit you obtain if you defect (don’t share) but the other player cooperates, giving you excess food (R = 2).
Write a function to play the cooperation game. Your function must follow the format specified below, which includes names for arguments. Replace NameOfFunction with your name (first and last, no spaces). You will play an unknown amount of games against each student (determined randomly). For each set of games, your function will be given the game (round) number (GameNum) and the vector of plays by you (MyMoves) and the opponent (OpMoves) from previous rounds. In other words, on the first round MyMoves and OpMoves will be empty. On round two, these objects will contain a single value (cooperate or defect) from the first round for you and the other player. For round three, these objects will contain a vector of length two with the two plays in order for each player. So on and so forth. Plays (or behaviors) will be recorded as 1 = cooperate and 0 = defect.
You will play an unknown number of games against each student in the class (the number of games will be determined “randomly”). Your function should return 1 (cooperate) or 0 (defect) for each round. For full credit, you must meaningfully use the information from the previous rounds of the game (game number, your decisions, opponents decisions, etc.) to make the decision of what to play. You may do this however you like, but I imagine if and else statements will be useful.
Here is a template for your function:
NameOfFunction <- function(GameNum = 1, MyMoves = NA, OpMoves = NA) {
## do something based on input then return 1 for cooperate or 0
## for defect
return(1)
}
Your function should return 1 (cooperate) or 0 (defect) after each round. You will thus want to edit the code within the function template. Again for full credit, your function must use the information from previous rounds of the game to make decisions of what to play. You can use the current game number, the number of times your opponent as defected, a binomial probability that your opponent will defect based on previous rounds, or anything else your imagination comes up with to determine your next move. You can find the rubric for this assignment on the course website. For this assignment, you will turn in an .R script containing your function.