# Store results results <- list() counter <- 1 # Store final populations for comparing the best genotypes final_pops <- list() for (scen in names(lambda_scenarios)) { lambda <- lambda_scenarios[[scen]] for (rep in seq_len(nRep)) { DH <- FoundingDH for (cycle in 0:ngen) { # Genetic values in each environment env_gv <- DH@gv[, -1, drop = FALSE] %*% t(covs_tpe) # Record population mean in each environment results[[counter]] <- data.frame( scenario = scen, rep = rep, cycle = cycle, environment = rep(paste0("Env", seq_len(nEnv)), each = nrow(env_gv)), gv = as.vector(env_gv) ) counter <- counter + 1 # Save final population before finishing if (cycle == ngen) { if (is.null(final_pops[[scen]])) { final_pops[[scen]] <- list() } final_pops[[scen]][[rep]] <- env_gv break } # Calculate selection index mean <- rowMeans(env_gv) stab <- apply(env_gv, 1, var) index_vals <- rowMeans(env_gv) - lambda*sqrt(stab) # Select parents selected <- order(index_vals, decreasing = TRUE)[1:nSelParents] Parents <- DH[selected] # Random crossing of selected parents F1 <- randCross(Parents, nCrosses) # Produce DH lines for next generation DH <- makeDH(F1, nDH, keepParents = TRUE) } cat("Completed:", scen, "- Rep:", rep, "\n") } } results_df <- bind_rows(results)