# Evaluating Algorithm Efficiency for Optimizing Experimental Designs with Correlated Data
## Appendix A R-Code for the Algorithms
### Simulated Annealing Algorithm (SA)
Generate Multiple RCBD_

MultipleDesigns<- function(DesN, blocks, Treatments,rb,cb, Tr, Tc,Amat=FALSE, criteria=\"A\", h2, rhox,rhoy, s20,irregular=FALSE) { matrix0<-list() initialValues1<-c() initialValues2<-c() for(i in 1:DesN){ print(sprintf(\"generating initial design:%d\",i,\"complete\
\", sep=\"\")) flush.console() matrix0[[i]]<-rcbd(blocks, Treatments,rb,cb, Tr, Tc, irregular) initialValues1[i]<-VarCov.rcbd(matdf=matrix0[[i]], rhox,rhoy, h2, s20, Tr, Tc, criteria, Amat, irregular)[[i]] a<-which.min(initialValues1) newmatdfA<-matrix0[a][[i]] min_initialValues1<-initialValues1[a][[i]] } return(list(newmatdf=newmatdfA, trace0=initialValues1, min_value=min_initialValues1, meanA=mean(initialValues1))) }

## Appendix C Generate a Numerator Relationship Matrix

GenA<-function(male, female){ if(nargs()==1){stop(\"requiremaleandfemaleentries\") } if(length(male)!=length(female)){ stop(\"lengthofmaleandfemalediffer\") } male[is.na(male)]<-0#convertallNAtozeros female[is.na(female)]<-0#convertallNAtozeros n<-length(male) N<-n+1 A<-matrix(0,ncol=N,nrow=N) male<-(male==0)*(N)+male female<-(female==0)*N+female for(iin1:n){ A[i,i]<-1+A[male[i],female[i]]/2 for(jin(i+1):n){ if(j>n) break A[i,j]<-(A[i,male[j]]+A[i,female[j]])/2 A[j,i]<-A[i,j] } } A<-as(A,\"sparseMatrix\") return(A[1:n,1:n]) }

## Appendix D Calculate the Variance-Covariance Matrix

VarCov.rcbd<-function(matdf, rhox, rhoy, h2, s20, Tr, Tc, criteria=\"A\", Amat=FALSE,irregular=FALSE){ if(nov(matdf)==length(unique(matdf[,\"Treatments\"]))){ X<-as.matrix(matdf[,\"Reps\"]) } if(nov(matdf)>length(unique(matdf[,\"Treatments\"]))){ X<-Matrix::sparse.model.matrix(\"as.factor(matdf[,\"Reps\"])-1) } s2e<-(1-s20)*(1-h2) stopifnot(s2e>0) m=length(unique(matdf[,\"Treatments\"])) if(is.matrix(Amat)){ G<-h2*as.matrix(Amat) Ginv<-round(chol2 inv(chol(as.matrix(G))),7) Ginv<-as(Ginv,\"sparseMatrix\") } else{ Ginv<-round((1/h2)*Matrix::Diagonal(m),7) Ginv<-as(Ginv,\"sparseMatrix\") } Z<-Matrix::sparse.model.matrix(\"as.factor(matdf[,\"Treatments\"])-1)
#calculatingRanditsinverseforspatialanalysis bb<-length(unique(matdf[,\"Reps\"])) matdf<-matdf[order(matdf[,\"Row\"],matdf[,\"Col\"]),] if(irregular==TRUE){ R<-Matrix::Diagonal(nov(matdf)) for(iin1:(nov(matdf)-1)) { x1<-matdf[,\"Col\"][i]

y1 <- matdf[,\"Row\"][i] for {j in (i+1):nrow(matdf))} x2 <- matdf[,\"Col\"][j] y2 <- matdf[,\"Row\"][j] R[i,j]<-(rhox\"abs(x2 -x1))*(rhoy\"abs(y2 -y1)) } } R = as.matrix(round(s2e*R,7)) R[lower.tri(R)] <- t(R)[lower.tri(R)] R <- as(R, \"sparseMatrix\") Rinv <- round(chol2 inv(chol(R)),7) Rinv <- as(Rinv, \"sparseMatrix\") } if(irregular==FALSE){ sigx <- Matrix::Diagonal(Tc) sigx <- rhox\"abs(row(sigx) - col(sigx)) sigy <- Matrix::Diagonal(Tr) sigy <- rhoy\"abs(row(sigy) - col(sigy)) R <- round(s2e * kronecker(sigy, sigx),7) R <- as(R, \"sparseMatrix\") Rinv <- round(chol2 inv(chol(R)),7) Rinv <- as(Rinv, \"sparseMatrix\") } C11 <- Matrix::crossprod(as.matrix(X), as.matrix(Rinv)) %*% as.matrix(X) C11 inv <- solve(C11) k1 <- Rinv %*% as.matrix(X) k2 <- Matrix::tcrossprod(as.matrix(C11 inv), as.matrix(X)) k3 <- k2 %*% Rinv K <- k1 %*% k3 K <- as(K, \"sparseMatrix\") temp0 <- Matrix::crossprod(Z, Rinv) %*% Z + Ginv - Matrix::crossprod(Z, K) %*% Z C22 <- solve(temp0) C22 <- as(C22, \"sparseMatrix\") Ginv = round(Ginv,7) Rinv = Matrix::drop0(round(Rinv,7)) K = round(K,7) C22 = round(C22,7) if (criteria == \"A\") { return(c(traceI = sum(Matrix::diag(C22)), Ginv = Ginv, Rinv = Rinv, K=K)) } if (criteria == \"D\") { deTm = Matrix::det(C22) return(c(doptimI = log(deTm), Ginv = Ginv, Rinv = Rinv, K=K)) } }

## References

* (1) [PERSON]; [PERSON]; [PERSON]; [PERSON] _Statistical Methods in Biology_; Design and Analysis of Experiments and Regression; Chapman & Hall: Boca Raton, FL, USA, 2015. * (2) [PERSON]; [PERSON]; [PERSON]; [PERSON] BLUP for phenotypic selection in plant breeding and variety testing. _Euphytica_**2008**, _161_, 209-228. [CrossRef]
* (3) [PERSON]; [PERSON] _Cyclic and Computer Generated Designs_, 2 nd ed.