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))
}

# Genetic algorithm parameters
population_size <- 100
num_generations <- 100
tournament_size <- 5

# Create a matrix to store the population
population <- selected_players_df

# Run the genetic algorithm for the specified number of generations
for (gen in 1:num_generations) {
  # Create an empty matrix to store offspring
  offspring_population <- matrix(NA, population_size, ncol(population))
  
  # Perform crossover and mutation to generate offspring
  for (i in seq(1, population_size, 2)) {
    # Select two parents using tournament selection
    parent1 <- tournament_selection(population, tournament_size)
    parent2 <- tournament_selection(population, tournament_size)
    
    # Perform crossover to generate offspring
    offspring <- crossover(parent1, parent2)
    
    # Add the generated offspring to the offspring population matrix
    offspring_population[i, ] <- offspring[1, ]
    offspring_population[(i + 1), ] <- offspring[2, ]
  }
  
  # Replace the old population with the offspring
  population <- offspring_population
  
  # Calculate the fitness for the current population
  population_fitness <- apply(population, 1, target)
  
  # Get the best solution in the current population
  best_solution <- population[which.max(population_fitness), ]
  best_fitness <- max(population_fitness)
  
  
}

# Remove temporary variables
rm(offspring_population, population_fitness)