# Iterate over the Payoff matrix
Fitness <- rep(0,Npop) # Assign space for fitness
for (Receiver in 1:2 ) # Individual receiving payoff
{
for (I.Opponent in 1:2) # Opponent
{
Fitness[Morph==Receiver & Opponent==I.Opponent]<- PayoffMatrix
[Receiver,I.Opponent]
} # End of I.opponent loop
} # End of Receiver loop
return(Fitness)
} # End of function
5.11.3.4 The main program
To calculate the preceding we need to keep track of the entire behavioral history
of each individual and for the final output the mean probability of adopting the
Hawk behavior. This is a simple bookkeeping problem but care should be taken to
ensure correct indexing. Parameter values were set at the values used by Harley
(1981). After setting up the parameter values and preassigning space for the
various matrices the results for the first trial are calculated and then the remain-
ing trials addressed using a loop. The coding for the main program is
######################## MAIN PROGRAM #########################
set.seed(100) # Initialize random number generator
Npop <- 30 # Set population size
MaxTrial <- 200 # Number of generations
r <- 14.4 # Residual value
m <- 0.99 # Memory coefficient
# Set up a matrix for the output
# Rows ¼ trial number Col 1 ¼ trial number, Cols 2- Npopþ1 ¼ Indivi-
duals
Nplusl <- Npopþ1 # Extra col for trial number
Output <- matrix(0,MaxTrial,Nplus1)# Create file for output
P1.t <- matrix(0.5,Npop,1) # Vector of Learned Probabil-
ities
Mean.P1.t <- matrix(0,MaxTrial,1) # matrix for Mean of Pi(t)
# Set up array for payoffs
#1
st
dimension ¼ trial, 2
nd
dimension ¼ Behavior, 3
rd
dimension ¼
Individual
Payoff <- array(0, c(MaxTrial,2,Npop)) # Array of payoffs
PayoffMatrix <- matrix(c(0,12,18,15),2,2) # Set up payoff matrix
P=0.2
############### Calculate Payoffs for first trial ###############
Output[1,1] <- 1 # Store Trial number in first column
Output[1,2 :Nplus 1] <- P1.t #Store Probability for each individual
Morph <- MORPH(P1.t, Npop) # Call function to determine behaviors
GAME THEORETIC MODELS 335