Plotting in R
Budget: €30 – €250 EUR
Write a script in R to generate the patterns of Figure 3 in the following paper: Nowak, MA, & May, RM (1992). Evolutionary games and spatial chaos. Nature , 359 : 826-829 (6398). Requirements: 1. The R plot should "evolve" with each generation / loop / step; 2. There should be a variable named "generation". By changing the value of this variable, the last R plot object produced by your script should correspond to the pattern after the n th step / generation; 3. Comment your script to enhance readability; 4. I attached some "hint" here, FYI. There I created a function named "neighbors", which will return all the neighbors of the [i,j] element of a matrix. Creating your own functions can be very useful sometimes.
7:26 PM
# The evolution of strategies in the prisoner's dilemma in Nowak & May (1992) # Citation: Nowak, Martin & May, Robert. (1992). Evolutionary Games and Spatial Chaos. Nature. 359. 826-829. 10.1038/359826a0. # Strategies: C=0; D=1 # payoffs: CC=1; DC=b (b>1); CD=0; DD=0 # Rules: In each round, every player plays with its 9 immediate neighbours (including self). Player with the highest sum of payoffs occupies that cell. # Color: C->C blue; C->D yellow; D->C green; D->D red library(plot.matrix) #define variables and matrices here neighbors <- function(mat, i,j) { rows <- subset((i-1):(i+1),(i-1):(i+1)>=1 & (i-1):(i+1)<=99) columns <- subset((j-1):(j+1),(j-1):(j+1)>=1 & (j-1):(j+1)<=99) mat[rows,columns] }
7:26 PM
# The evolution of strategies in the prisoner's dilemma in Nowak & May (1992) # Citation: Nowak, Martin & May, Robert. (1992). Evolutionary Games and Spatial Chaos. Nature. 359. 826-829. 10.1038/359826a0. # Strategies: C=0; D=1 # payoffs: CC=1; DC=b (b>1); CD=0; DD=0 # Rules: In each round, every player plays with its 9 immediate neighbours (including self). Player with the highest sum of payoffs occupies that cell. # Color: C->C blue; C->D yellow; D->C green; D->D red library(plot.matrix) #define variables and matrices here neighbors <- function(mat, i,j) { rows <- subset((i-1):(i+1),(i-1):(i+1)>=1 & (i-1):(i+1)<=99) columns <- subset((j-1):(j+1),(j-1):(j+1)>=1 & (j-1):(j+1)<=99) mat[rows,columns] }