So I have this code: " Mutation 2 positionvectorslist <- list(RWidx, STidx, GKidx,…
- User
So I have this code: "
Mutation 2
position_vectors_list <- list(RW_idx, ST_idx, GK_idx, CM_idx, LW_idx, CDM_idx, LM_idx, CF_idx, CB_idx, CAM_idx, LB_idx, RB_idx, RM_idx, LWB_idx, RWB_idx)
Generate random numbers for each DataFrame row
n_rows <- 100 random_numbers <- runif(n_rows, 0, 1)
Define a function to randomly select a unique index
select_random_player <- function(idx_list, probability = 0.09, selected_players = NULL) { if (runif(1) <= probability) { repeat { random_idx <- sample(idx_list, 1) if (!random_idx %in% selected_players) { return(random_idx) } } } else { return(NA) } }
Create a new matrix with 100 rows and the same number of columns as position_vectors
selected_players_matrix <- matrix(NA, n_rows, length(position_vectors_list))
Loop through each row of the matrix
for (i in 1:n_rows) {
Loop through each position vector index
for (pos_idx in 1:length(position_vectors_list)) { # Call the function to select a random unique player selected_players_matrix[i, pos_idx] <- select_random_player(position_vectors_list[[pos_idx]], probability = 0.09, selected_players = selected_players_matrix[i, ]) } }
Convert the matrix to a data.frame
selected_players_df <- data.frame(selected_players_matrix)
Remove temporary variables
rm(position_vectors_list, select_random_player)
} " in R. Change it to function mutate. Apply that to genetic algorithm, create functions for crossover. Also 2 best solutions from iteration must be in parents pull for next iteration. Function for evaluated the fitness is given as "target". Remeber, that we encode the data based on the indexes from very specific group. The current GA looks like that:
tournament_selection <- function(parents, t_size) {
Select t_size random parents
random_parents_idx <- sample(nrow(parents), t_size, replace = FALSE) random_parents <- parents[random_parents_idx, ]
Evaluate the target function for each selected parent
random_parents_fitness <- apply(random_parents, 1, target)
Select the best parent based on the target function value
best_parent_idx <- which.max(random_parents_fitness) return(random_parents[best_parent_idx, ]) }
Crossover function
crossover <- function(parent1, parent2) {
Choose a random crossover point
crossover_point <- sample(2:(ncol(parent1) - 1), 1)
Swap the position vectors after the crossover point
offspring1 <- c(parent1[1:crossover_point], parent2[(crossover_point + 1):ncol(parent1)]) offspring2 <- c(parent2[1:crossover_point], parent1[(crossover_point + 1):ncol(parent2)])
return(rbind(offspring1, offspring2)) }
- Assistant
- User
In this mutate function. Each row must have inuqe player IDs. The position_vectors_list contains the indexes that might be used to mutate offsprinng. Is it still ensured?
- Assistant
Apologies for the confusion, and thank you for pointing it out. In the mutate function, we need to check for unique player IDs in the entire offspring, not just the specific position. Here is the updated mutate function:
R# Mutation function mutate <- function(individual, position_vectors_list, probability = 0.09) { for (pos_idx in 1:length(position_vectors_list)) { if (runif(1) <= probability) { repeat { random_idx <- sample(position_vectors_list[[pos_idx]], 1) if (!random_idx %in% individual) { individual[pos_idx] <- random_idx break } } } } return(individual) }With this updated mutate function, each row (individual) will still have unique player IDs, even after mutation. The position_vectors_list is used for selecting the indices to mutate in each offspring, and the function ensures that only unique indices are chosen by checking if the randomly selected index is already in the individual. If it is, then the loop continues to sample another random index until a unique one is found.
- User
Furthermore create the initial population with function mutate and probability =1. Now I am thinking over the target() function. So I have a data with columns: "GKDiving" "GKHandling"
[71] "GKKicking" "GKPositioning" "GKReflexes" "STRating" "LWRating" "LFRating" "CFRating"
[78] "RFRating" "RWRating" "CAMRating" "LMRating" "CMRating" "RMRating" "LWBRating"
[85] "CDMRating" "RWBRating" "LBRating" "CBRating" "RBRating" "GKRating" I also have a position_vectors created from that data: RW_idx <- which(grepl("RW", DataPositions)) GK_idx <- which(grepl("GK", DataPositions)) LW_idx <- which(grepl("LW", DataPositions)) LM_idx <- which(grepl("LM", DataPositions)) CB_idx <- which(grepl("CB", DataPositions)) LB_idx <- which(grepl("LB", DataPositions)) RM_idx <- which(grepl("RM", DataPositions)) RWB_idx <- which(grepl("RWB", Data$Positions)) - Assistant
Here's how you can create the initial population using the mutate function with a probability of 1, and create the target function to calculate the desired value for a given parent vector:
R