# Choice.Flag[1:2] # Flag for multiple optima
Temp1 <- c(F.vectors[Index,1], 1)
Temp2 <- c( Best.in.Patch[1,2], Best.in.Patch[1,3])
Temp3 <- c( Best.in.Patch[2,2], Best.in.Patch[2,3])
# Add Temp1, Temp2, Temp3 & Choice to bottom of F.vectors and rename
to Temp
Temp <- rbind(F.vectors, Temp1, Temp2, Temp3, Choice.Flag[1:2])
return (Temp)
} # End of function
# ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ....
# Function to iterate over states of X
OVER.STATES <- function(F.vectors, Xcritical, Xmax, Xinc,
Npatch, Psurvival, Max.Index, Wmax, A, Xegg, Xclutch, P1)
{
# Create matrix for output. Note that we use seven columns
Store <- matrix(0, Max.Index,7)
# Iterate over X ¼ Biomass X[1] is zero so skip
for ( Index in 2 : Max.Index) # Iterate over states of X
{
# For given X call Over.Patches to determine F(x,t) and best patch
X <- (Index-1)*Xinc þ Xcritical
Temp <- OVER.PATCHES(X, F.vectors, Xcritical, Xmax, Xinc,
Npatch, Psurvival,Wmax, A, Xegg, Xclutch, P1)
# Extract components. Last row-2 is F(x,t) and dummy variable
# Last row-1 is best clutch and egg size for host type 1
# Last row is best clutch and egg size for host type 2
# Last row is flage indicating multiple equal choices
n <- nrow(Temp)-4
F.vectors<- Temp[1:n,] # Extracting F.vectors
# Add the seven output values (omit dummy) to storage
Store[Index,] <- c(Temp[nþ1,1], Temp[nþ2,1:2], Temp[nþ3,1:2],
Temp[nþ4, 1:2])
} # End of X loop
# Add Store values to end of F.vectors for pass back to main program
Temp <- cbind(F.vectors, Store) # Combined by columns
return(Temp) # Return F.vectors and Store
} # End of function
# ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ....
# MAIN PROGRAM
# Initialize parameters
# Create the state variable (X¼reproductive biomass)
Xmax <- 10; Xcritical <- 0; Xinc <-1
Max.Index <-1þ (Xmax-Xcritical)/Xinc
# Parameter values on the two hosts
Wmax <- c(10, 20) # Maximum fitness
DYNAMIC PROGRAMMING 397