} # End of I.opponent loop
} # End of Receiver loop
# Nowcalculate the relative fitness of hawks ¼New proportion of Hawks
P.Hawk <- sum(Fitness[Morph==1])/sum(Fitness) # Mean fitness of Hawk males
return(P.Hawk)
}
####################### Main program #######################
set.seed(100) # Initialize random number generator
Npop <- 100 # Set population size
MaxGen <- 100 # Number of generations
Output <- matrix(0,MaxGen,2) # Create file for output
P.Hawk <- 0.5 # Initial proportion of Hawks
Nos.of.Hawks <- Npop*P.Hawk # Initial number of Hawks
# Set up morph vector initially with all doves
Morph <- matrix(2,Npop,1)
# Convert first Nos.of.Hawks rows to Hawks
Morph[1:Nos.of.Hawks] <-1
PayoffMatrix <- matrix(c(-1,0,8,4),2,2) # Set up fitness matrix
# Calculate theoretical frequency
a <- PayoffMatrix[1,1]
b <- PayoffMatrix[1,2]
c <- PayoffMatrix[2,1]
d <- PayoffMatrix[2,2]
P.Hawks <- (b-d)/(bþc-a-d)
for (Igen in 1:MaxGen) # Iterate over generations
{
Output[Igen,1] <- Igen # Store Generation number in 1st column
# Calculate the proportion of each type
Nos.of.Hawks <- length(Morph[Morph==1]) # Number of hawks
Output[Igen,2] <- Nos.of.Hawks/Npop # Proportion of Hawks
# Calculate new proportion of hawks by applying fitness criterion
P.Hawk <- FITNESS(Morph, Npop, PayoffMatrix)
# Calculate the new population, making sure Nos.of.Hawks is an
integer
Nos.of.Hawks <- round(Npop*P.Hawk)
Morph[1:Npop,1] <- 2 # Initially set rows to Dove
Morph[1:Nos.of.Hawks,1] <- 1 # Convert first Nos.of.Hawks rows
to hawks
} # End of Igen loop
# Plot Output
plot(Output[,1], Output[,2],type=’l’, xlab=’Generation’,
ylab=’Proportion of hawks’)
lines(Output[,1], rep(P.Hawks,MaxGen)) # Plot theoretical
expectation
# Do t test on proportion after generation 20 to see if it conforms
GAME THEORETIC MODELS 285