# Evaluating Algorithm Efficiency for Optimizing Experimental Designs with Correlated Data
## Appendix A R-Code for the Algorithms
### Simulated Annealing Algorithm (SA)
``` Optimize_SimAnn_rcbd<-function(matdf,n,traceI,criteria,Rinv,Ginv,K) { newmatdf <- matdf trace <- traceI mat <- NULL mat <- rbind(mat, c(value = trace, iterations = 0)) Design_best <- newmatdf Des <- list() TRACE <- c() newmatdf <- SwapPair(matdf = matdf) for(i in 2:n) { newmatdf <- SwapPair(matdf = newmatdf) TRACE[i] <- NewValue.rcbd(matdf=newmatdf, criteria, Rinv, Ginv, K) Des[[i]] <- newmatdf if(NewValue.rcbd(matdf=newmatdf, criteria, Rinv, Ginv, K) < trace) { print(sprintf(\"Swapping within blocks: %d\", i, \"complete\
\", sep = \"\")) Design_best <- Des[[i]] <- newmatdf Design_best <- newmatdf trace <- NewValue.rcbd(matdf=newmatdf, criteria, Rinv, Ginv, K) mat <- rbind(mat, c(trace = trace, iterations = i)) } Temp<-c() if(NewValue.rcbd(matdf=newmatdf, criteria, Rinv, Ginv, K) > trace) { diff <- setdiff(NewValue.rcbd(matdf=newmatdf, criteria, Rinv, Ginv, K),trace) Temp[i] <- 1/i accept = exp(-diff/Temp[i]) u = runif(1) if(u < accept){ Design_best <- Des[[i]] <- newmatdf Design_best <- newmatdf trace <- NewValue.rcbd(matdf=newmatdf, criteria, Rinv, Ginv, K)} if(u>accept&nrow(mat)<=1){ newmatdf<-matdf Des[[i]]<-matdf Design_best<-matdf } if(u>accept&nrow(mat)>1){ newmatdf<-Des[[length(Des)-1]] Des[[i]]<-newmatdf Design_best<-newmatdf } } ODE=(((mat[1,\"value\"])-(mat[nrow(mat),\"value\"]))/(mat[1,\"value\"]))*100 print(sprint(\"ODEduetosimulatedannealingis:%r\",ODE,\"complete\
\", sep=\"\")) list(TRACE=c(as.vector(mat[1,\"value\"]),TRACE[tis.na(TRACE)]),mat=mat, Design_best=Design_best) }

### Greedy Pairwise Algorithm (GP)

OptimizeGreedy.rcbd<- function(matdf,n,traceI,criteria,gsize,Rinv,Ginv,K){ newmatdf<-matdf trace<-traceI mat<-NULL mat<-rbind(mat,c(value=trace,iterations=0)) Design_best<-newmatdf Des<-list() TRACE<-c() newmatdf<-SwapGreedy(matdf=matdf,gsize=gsize) for(iin2:n){ newmatdf<-SwapGreedy(matdf=newmatdf,gsize=gsize) TRACE[i]<-NewValue.rcbd(matdf=newmatdf,criteria,Rinv,Ginv,K) Des[[i]]<-newmatdf if(NewValue.rcbd(matdf=newmatdf,criteria,Rinv,Ginv,K)<trace){ print(sprint(\"Swappinggreedlywithinblocks:%d\",i,\"complete\
\", sep=\"\")) Design_best<-Des[[i]]<-newmatdf Design_best<-newmatdf trace<-NewValue.rcbd(matdf=newmatdf,criteria,Rinv,Ginv,K) mat<-rbind(mat,c(trace=trace,iterations=i)) } if(NewValue.rcbd(matdf=newmatdf,criteria,Rinv,Ginv,K)>trace&nrow(mat)<=1){ newmatdf<-matdf Des[[i]]<-matdf Design_best<-matdf if(NewValue.rcbd(matdf=newmatdf,criteria,Rinv,Ginv,K)>trace&nrow(mat)>1){ newmatdf<-Des[[length(Des)-1]] Des[[i]]<-newmatdf Design_best<-newmatdf } ODE=(((mat[1,\"value\"])-(mat[nrow(mat),\"value\"]))/(mat[1,\"value\"]))*100 print(sprint(\"ODE due to greedly swapping pairs of treatments within blocks is: %f\", ODE, \"complete\
\", sep = \"\")) list(TRACE = c(as.vector(mat[1, \"value\"]), TRACE[lis.na(TRACE)]), mat = mat, Design_best = Design_best) }

### Genetic Neighborhood Algorithm (GN)

Optimize_GNN_rcbd<- function(matdf,n,traceI,criteria,Amat, Rinv, Ginv, K) { newmatdf <- matdf trace <- traceI mat <- NULL mat <- rbind(mat, c(value = trace, iterations = 0)) Design_best <- newmatdf Des <- list() TRACE <- c() newmatdf <- Neighbor_rcbd(matdf,Amat) for (i in 2:n) { newmatdf <- Neighbor_rcbd(matdf,Amat) TRACE[i] <- NewValue.rcbd(matdf=newmatdf, criteria, Rinv, Ginv, K) Des[[i]] <- newmatdf if (NewValue.rcbd(matdf=newmatdf, criteria, Rinv, Ginv, K) < trace) { print(sprint(\"Swapping treatments: %d\", i, \"complete\
\", sep = \"\")) Design_best <- Des[[i]] <- newmatdf  Design_best <- newmatdf } if(NewValue.rcbd(matdf=newmatdf, criteria, Rinv, Ginv, K) newmatdf <- rbind(mat, c(trace = trace, iterations = i)) } if(NewValue.rcbd(matdf=newmatdf, criteria, Rinv, Ginv, K) > trace && nrow(mat)<=1){ newmatdf <- matdf Des[[i]] <- matdf Design_best <- matdf } } if(NewValue.rcbd(matdf=newmatdf, criteria, Rinv, Ginv, K) > trace && nrow(mat)>1){ newmatdf <- Des[[length(Des) - 1]] Des[[i]] <- newmatdf Design_best <- newmatdf } } ODE = (((mat[1,\"value\"]) - (mat[nrow(mat),\"value\"]))/(mat[1,\"value\"]))*100 print(sprint(\"ODE due to applying GNN procedure: %f\", ODE, \"complete\
\", sep = \"\")) list(TRACE = c(as.vector(mat[1, \"value\"]), TRACE[lis.na(TRACE)]), mat = mat, Design_best = Design_best) }

The GN algorithm also requires the function _Neighbor_rcbd_:

Neighbor_rcbd<-function(matdf,Amat) { bl<-sample(matdf[,\"Reps\"],1) temp1<- matdf[matdf[, \"Reps\"] == bl,] rb <-length(unique(temp1[,\"Row\"])) cb <-length(unique(temp1[,\"Col\"])) mat<-matrix(c(temp1[,\"Treatments\"]),nrow=rb,ncol=cb,byrow=TRUE) x<-sample(temp1[,\"Treatments\"],1)m2<-cbind(NA,rbind(NA,mat,NA),NA) cord <- expand.grid(Row = 1:rb, Col = 1:cb) ret<-c() for(i in 1:-1) for(j in 1:-1) if(i1=0 || j!=0) ret<-rbind(ret,m2[cord$Row+i+1+nrow(m2)*(cord$Col+j)]) neigh<-ret[,which(mat=x)] neigh<-c(x,neigh[[is.na(neigh)]) # add the genotype plus its neighbors test<-t(combn(neigh,2)) temp<-cbind(test[,1],test[,2],Amat[test]) swapsData<-subset(temp,temp[,3] >= 0.25) samp1 <- setdiff(as.vector(temp1[,\"Treatments\"]),c(x,as.vector(temp[,2]))) if(nrow(swapsData)!=0 && length(samp1)!=0) { val1<-c() val2<-c() for(i in 1:nrow(swapsData)) { val1[i]<-swapsData[i,1] val2[i]<-sample(samp1,1) matdf<-Swap_Specific(matdf,g1=val1[i],g2=val2[i],bl=bl) } return(matdf[order(matdf[,\"Row\"],matdf[,\"Col\"]),]) } if(nrow(swapsData)!=0 && length(samp1)==0) { val1<-c() val2<-c() for(i in 1:nrow(swapsData)) { val1[i]=swapsData[i,1] val2[i]=swapsData[i,2] matdf<-Swap_Specific(matdf,g1=val1[i],g2=val2[i],bl=bl) } return(matdf[order(matdf[,\"Row\"],matdf[,\"Col\"]),]) } if(nrow(swapsData)==0) return(SwapPair(matdf)) }

## Appendix B R-Code for Generating Initial Randomized Complete Block Design (RCBD)

### Generate a RCBD

rcbd<- function(blocks, Treatments,rb,cb, Tr, Tc, irregular=FALSE) { genot0 <- as.numeric(as.factor(Treatments)) n <- length(Treatments) Treatments <- c(replicate(blocks, sample(genot0, n, replace = FALSE))) Reps <- rep(1:blocks, each = length(unique(Treatments))) if(blocks==1){ cord <- cbind(Row=rep(1:Tr,each=Tc), Col=rep(1:Tc,Tr)) matdf <- cbind(cord, Reps=c(rep(1,n)),Treatments) row.names(matdf)<-NULL } if(cb == Tc & Tr > rb && irregular==FALSE)cord<-cbind(Row=rep(1:Tr,each=cb), Col=rep(1:Tc,rb)) matdf<-cbind(cord,Reps, Treatments) row.names(matdf)<-NULL } if(rb==Tr&Tc>cb&irregular==FALSE){ Row=rep(rep(1:Tr,each=cb),Tc/cb) Col<-rep(split(1:Tc, cut(seq_along(1:Tc), blocks, labels = FALSE)),each=Tr) Col<-unlist(Col) cord<-cbind(Row,Col) matdf<-cbind(cord,Reps, Treatments) row.names(matdf)<-NULL } if(Tr>rb&Tc>cb&irregular==FALSE){ Row=rep(rep(1:Tr,each=cb),Tc/cb) Col<-rep(split(1:Tc, cut(seq_along(1:Tc), Tc/cb, labels = FALSE)),each=Tr) Col<-unlist(Col) cord<-cbind(Row,Col) matdf<-cbind(cord,Reps, Treatments) row.names(matdf)<-NULL } if(irregular==TRUE){ cord<-cbind(Row=Row,Col=Col) matdf<-cbind(cord, Reps, Treatments) row.names(matdf)<-NULL } matdf[order(matdf[,\"Row\"],matdf[,\"Col\"]),] } ```

_Appendix B.2.