OK, so I want to increase the efficency of this R code using multiprocessing only (do…
- User
OK, so I want to increase the efficency of this R code using multiprocessing only (do not change anything except adding multiprocessing):
mutate <- function(selected_players_df, position_vectors_list, probability) { n_rows <- nrow(selected_players_df) selected_players_matrix <- selected_players_df
select_random_player <- function(idx_list, selected_players = NULL) { repeat { random_idx <- sample(idx_list, 1) if (!random_idx %in% selected_players) { return(random_idx) } } }
for (i in 1:n_rows) { for (pos_idx in 1:length(position_vectors_list)) { if (runif(1) <= probability) { selected_players_matrix[i, pos_idx] <- select_random_player(position_vectors_list[[pos_idx]], selected_players = selected_players_matrix[i, ]) } } }
selected_players_df <- data.frame(selected_players_matrix) return(selected_players_df) }
target <- function(row1,penalty=5) { position_ratings <- c("RWRating", "STRating", "GKRating", "CMRating", "LWRating", "CDMRating", "LMRating", "CFRating", "CBRating", "CAMRating", "LBRating", "RBRating", "RMRating", "LWBRating", "RWBRating")
row1<-as.matrix(row1,nrow=1) parent_data <- Data[row1, ] ratings <- parent_data[position_ratings] ratings_log <- log(ratings)
potential_minus_age <- 0.15*parent_dataAge int_reputation <- parent_data$IntReputation
sumratings<-0 rating_list<-c() for (i in 1:15){ temp<-ratings_log[i,i] sumratings<-sumratings+temp rating_list<-append(rating_list, temp)
}
Apply constraints
constraint_penalty <- 0 if (sum(parent_dataValueEUR) > 250000000) { constraint_penalty <- constraint_penalty + log((sum(parent_dataValueEUR)-250000000)^penalty) } if (sum(parent_dataWageEUR) > 250000) { constraint_penalty <- constraint_penalty + log((sum(parent_dataWageEUR)-250000)^penalty) } if (any(rating_list < 1.2)) { constraint_penalty <- constraint_penalty + 1.2^penalty }
potential_minus_age
target_value <- -(sumratings+0.3*sum(potential_minus_age) +sum(int_reputation))+constraint_penalty
return(target_value) }
for (gen in 1:num_generations) {
parent_pop<- as.data.frame(matrix(NA, n_rows, length(position_vectors))) for(c in 1:(population_size)){ parent_pop[c,]<-tournament_selection (parents,t_size=2,penalty=penalty)
}
offspring_temp<- as.data.frame(matrix(NA, 1, length(position_vectors)))
for (c in seq(1, population_size, by = 2)){ parent1<-parent_pop[c,] parent2<-parent_pop[c+1,]
offsprings<-crossover(parent1, parent2,corssover_point) o1<-unlist(offsprings[1,]) o2<-unlist(offsprings[2,]) offspring_temp<-rbind(offspring_temp,o1) offspring_temp<-rbind(offspring_temp,o2)} offspring_temp <- offspring_temp[-1, ]
parents<- mutate(offspring_temp, position_vectors_list, probability=probability) row.names(parents) <- 1:population_size solutions<-apply(parents, 1, target) idx_sol<-which.min(solutions)[[1]] temp_best<-parents[idx_sol[[1]],] temp_target_value<-solutions[idx_sol[[1]]]
if(temp_target_value<=global_best_value){ global_best<-temp_best global_best_value<-temp_target_value
}
penalty<-penalty+0.5 if(penalty>=9){ penalty<-0 }
}
- Assistant
You can use the "parallel" package in R to parallelize the "for" loops. Here's the code with the necessary modifications for multiprocessing:
#Load required libraries library(parallel) #.... #Find the number of cores in the system to utilize for parallel processing num_cores <- detectCores() #.... #Replace the loops with "parLapply" or "mclapply" functions for (gen in 1:num_generations) { parent_pop <- do.call("rbind", mclapply(1:population_size, function(x) { tournament_selection(parents, t_size = 2, penalty = penalty) }, mc.cores = num_cores)) offspring_temp <- do.call("rbind", mclapply(seq(1, population_size, by = 2), function(y) { parent1 <- parent_pop[y,] parent2 <- parent_pop[y+1,] offsprings <- crossover(parent1, parent2, corssover_point) o1 <- unlist(offsprings[1,]) o2 <- unlist(offsprings[2,]) rbind(o1, o2) }, mc.cores = num_cores)) parents <- mutate(offspring_temp, position_vectors_list, probability = probability) row.names(parents) <- 1:population_size # Calculate solutions in parallel solutions <- unlist(mclapply(1:(population_size), function(z) { target(parents[z,]) }, mc.cores = num_cores)) idx_sol <- which.min(solutions)[[1]] temp_best <- parents[idx_sol[[1]],] temp_target_value <- solutions[idx_sol[[1]]] if(temp_target_value <= global_best_value) { global_best <- temp_best global_best_value <- temp_target_value } penalty <- penalty + 0.5 if(penalty >= 9) { penalty <- 0 } }All the "for" loops have been replaced with the "mclapply" function so the code runs in parallel using the available cores (
num_cores). Remember to install and load the "parallel" package withinstall.packages("parallel")in your R environment before running the code. - User
O I tried this code and I am getting error: Error in mclapply(1:population_size, function(x) { : argument 'mc.cores' > 1 nie jest wspierany w systemach Window
I also tried switching to parallel::mclapply and got this error: Error in parallel::mclapply(1:population_size, function(x) { : argument 'mc.cores' > 1 nie jest wspierany w systemach Windows
- Assistant
I apologize for the confusion. It seems you are using a Windows system, and
mclapplydoes not support multiple cores on Windows. Instead, you can use theparLapplyfunction from the "parallel" package, which works on both Unix and Windows systems. Here's the code modified forparLapply:#Load required libraries library(parallel) #.... #Find the number of cores in the system to utilize for parallel processing num_cores <- detectCores() #Create a cluster with the detected number of cores cl <- makeCluster(num_cores) #Register the cluster registerDoParallel(cl) #.... #Replace the loops with "parLapply" functions for (gen in 1:num_generations) { parent_pop <- do.call("rbind", parLapply(cl, 1:population_size, function(x) { tournament_selection(parents, t_size = 2, penalty = penalty) })) offspring_temp <- do.call("rbind", parLapply(cl, seq(1, population_size, by = 2), function(y) { parent1 <- parent_pop[y,] parent2 <- parent_pop[y+1,] offsprings <- crossover(parent1, parent2, corssover_point) o1 <- unlist(offsprings[1,]) o2 <- unlist(offsprings[2,]) rbind(o1, o2) })) parents <- mutate(offspring_temp, position_vectors_list, probability = probability) row.names(parents) <- 1:population_size # Calculate solutions in parallel solutions <- unlist(parLapply(cl, 1:(population_size), function(z) { target(parents[z,]) })) idx_sol <- which.min(solutions)[[1]] temp_best <- parents[idx_sol[[1]],] temp_target_value <- solutions[idx_sol[[1]]] if(temp_target_value <= global_best_value) { global_best <- temp_best global_best_value <- temp_target_value } penalty <- penalty + 0.5 if(penalty >= 9) { penalty <- 0 } } #Stop the cluster stopCluster(cl)This code uses the
parLapplyfunction instead ofmclapply, and should work on your Windows system. Don't forget to add the cluster setup and cleanup steps before and after your parallelized code (makeCluster,stopCluster, andregisterDoParallel).