So I updated the code to look like that:
Data<-read.csv("C:/Users/Michał/Desktop/player22.csv")
RW_idx <- which(grepl("RW", DataPositions))STidx<−which(grepl("ST",DataPositions))
GK_idx <- which(grepl("GK", DataPositions))CMidx<−whichPositions))
LW_idx <- which(grepl("LW", DataPositions))CDMidx<−whiPositions))
LM_idx <- which(grepl("LM", DataPositions))CFidx<−whichPositions))
CB_idx <- which(grepl("CB", DataPositions))CAMidx<−whiPositions))
LB_idx <- which(grepl("LB", DataPositions))RBidx<−whichPositions))
RM_idx <- which(grepl("RM", DataPositions))LWBidx<−whiPositions))
RWB_idx <- which(grepl("RWB", Data$Positions))
#############
pop_init <- list()
Create a list of vectors to loop through
position_vectors <- 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)
position_vectors_list<-position_vectors
parents<-population
t_size=5
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))
}
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)
}
preserve_elites <- function(population, num_elites) {
population_fitness <- apply(population, 1, target)
elite_indices <- order(-population_fitness)[1:num_elites]
elites <- population[elite_indices, ]
return(as.matrix(elites))
}
initialize_individual <- function(position_vectors_list) {
individual <- sapply(position_vectors_list, function(pos) sample(pos, 1))
return(individual)
}
Create initial population
n_rows <- 100
initial_population <- matrix(NA, n_rows, length(position_vectors))
for (i in 1:n_rows) {
individual <- initialize_individual(position_vectors)
initial_population[i, ] <- individual
}
Define the target function
target <- function(parent_indices) {
position_ratings <- c("RWRating", "STRating", "GKRating", "CMRating",
"LWRating", "CDMRating", "LMRating", "CFRating",
"CBRating", "CAMRating", "LBRating", "RBRating",
"RMRating", "LWBRating", "RWBRating")
parent_data <- Data[parent_indices, ]
ratings <- parent_data[position_ratings]
ratings_log <- log(ratings)
potential_minus_age <- parent_dataPotential−parentdataAge
log_value_eur_minus_wage_eur <- log(parent_dataWageEUR)
int_reputation <- parent_data$IntReputation
Apply constraints
constraint_penalty <- 0
if (sum(parent_dataValueEUR)>250000000)consWageEUR) > 250000) {
constraint_penalty <- constraint_penalty + 200
}
if (any(ratings_log < 1.2)) {
constraint_penalty <- constraint_penalty + 200
}
target_value <- -(rowSums(ratings_log) + potential_minus_age - log_value_eur_minus_wage_eur + int_reputation) + constraint_penalty
return(target_value)
}
population<-as.matrix(population)
Genetic algorithm parameters
population_size <- 100
num_generations <- 1000
tournament_size <- 5
num_elites <- 2
k<-mutate(initial_population,position_vectors_list)
Run the genetic algorithm
for (gen in 1:num_generations) {
Calculate the fitness for the current population
population_fitness <- apply(population, 1, target)
Preserve elites
elites <- population[order(-population_fitness)[1:num_elites], , drop=FALSE]
elites_fitness <- population_fitness[order(-population_fitness)[1:num_elites]]
Create an empty matrix to store offspring
offspring_population <- matrix(1:(population_size - num_elites) , ncol=ncol(population), byrow=T)
Perform crossover and mutation to generate offspring
offspring_fitness <- vector(mode = 'numeric' length = population_size - num_elites)
for (i in seq(1, population_size - num_elites, 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)
# Mutate offspring
offspring[1, ] <- mutate(offspring[1, ], position_vectors_list)
offspring[2, ] <- mutate(offspring[2, ], position_vectors_list)
# Add the generated offspring to the offspring population matrix
offspring_population[i, ] <- as.numeric(offspring[1, ])
offspring_population[(i + 1), ] <- as.numeric(offspring[2, ])
# Calculate the fitness for each offspring
offspring_fitness[i] <- target(offspring_population[i, ])
offspring_fitness[i + 1] <- target(offspring_population[(i + 1), ])
}
Replace the old population with the offspring and elites
population <- rbind(elites, offspring_population)
population_fitness <- c(elites_fitness, offspring_fitness)
Get the best solution in the current population
best_solution <- population[which.max(population_fitness), , drop=FALSE]
best_fitness <- max(population_fitness)
}
Remove temporary variables
rm(offspring_population, population_fitness)
and I am still having the error:
Replace the old population with the offspring and elites
- population <- rbind(elites, offspring_population)
- population_fitness <- c(elites_fitness, offspring_fitness)
-
Get the best solution in the current population
- best_solution <- population[which.max(population_fitness), , drop=FALSE]
- best_fitness <- max(population_fitness)
- }
Error in population[order(-population_fitness)[1:num_elites], , drop = FALSE] :
subscript out of bounds
COULD you propose a solution where you fix it and the solution where you remove the entire fitness functions?