The issue is caused by the "offspring" variable in the for loop at the end of the code. Since the "offspring" variable is returned as a matrix with 2 rows and columns equal to the number of positions, it is returning a matrix instead of a vector when indexing with offspring[1, ] and offspring[2, ]. To fix the error, simply convert the indexed offspring into vectors when assigning values to the offspring_population matrix, like this:
User
I am also having an error after running the code you just written:
Error in match.names(clabs, names(xi)) :
nazwy nie zgadzają się z poprzednimi nazwami
Assistant
I apologize for the confusion. It looks like the error comes from mixing inconsistent column names when using rbind() to replace the old population with the offspring and elites. To fix the issue, I suggest converting the 'elites', 'offspring_population', and 'population' into matrices with numeric values.
In the "preserve_elites" function, return elites as a matrix instead of a data frame:
R
User
Now the error is:
Error in population[elite_indices, ] : subscript out of bounds
Assistant
I apologize for the confusion again. It looks like the error is related to the incorrect dimensions of the population matrix. In your initial_population creation, you have not specified the column names, which leads to a higher number of columns than expected. To fix this, you should specify the column names when creating the initial_population matrix.
Here is the updated initial_population matrix creation:
R
# Create initial population
n_rows <- 100
initial_population <- matrix(NA, n_rows, length(position_vectors), dimnames=list(NULL,colnames(Data[position_vectors])))
for (i in 1:n_rows) {
individual <- initialize_individual(position_vectors)
initial_population[i, ] <- individual
}
population <- initial_population
User
initial_population <- matrix(NA, n_rows, length(position_vectors), dimnames=list(NULL,colnames(Data[position_vectors])))
Error in [.default(Data, position_vectors) :
invalid subscript type 'list'
Assistant
Sorry for the confusion. Since position_vectors is a list and not a vector, we need to adjust the column name extraction. Here's the fix for the column names of the initial_population matrix:
R
# Create initial population
n_rows <- 100
pos_colnames <- names(Data)[unlist(position_vectors)]
initial_population <- matrix(NA, n_rows, length(position_vectors), dimnames=list(NULL, pos_colnames))
for (i in 1:n_rows) {
individual <- initialize_individual(position_vectors)
initial_population[i, ] <- individual
}
population <- initial_population
Perform crossover and mutation to generate offspring
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)
code
# 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, ] <- offspring[1, ]
offspring_population[(i + 1), ] <- offspring[2, ]
}
Replace the old population with the offspring and elites
So the final part of the code should now look like this:
code
for (gen in 1:num_generations) {
# Preserve elites
elites <- preserve_elites(population, num_elites)
# Create an empty matrix to store offspring
offspring_population <- matrix(NA, population_size - num_elites, ncol(population))
# Perform crossover and mutation to generate offspring
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, ])
}
# Replace the old population with the offspring and elites
population <- rbind(elites, 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)
Check and make sure this will resolve the error in your code. Good luck!
Then, at the beginning of the main code, initialize the population as a matrix instead of a data frame:
R
# Create initial population
n_rows <- 100
initial_population <- matrix(NA, n_rows, length(position_vectors), dimnames=list(NULL,NULL))
for (i in 1:n_rows) {
individual <- initialize_individual(position_vectors)
initial_population[i, ] <- individual
}
population <- initial_population
Lastly, just before the for loop starts, convert the 'population' into a matrix:
R
population <- as.matrix(population)
Now your whole code should look like this:
R
# ... (the rest of the code before preserve_elites)
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))
}
# ... (the rest of the code before the main section)
# Create initial population
n_rows <- 100
initial_population <- matrix(NA, n_rows, length(position_vectors), dimnames=list(NULL,NULL))
for (i in 1:n_rows) {
individual <- initialize_individual(position_vectors)
initial_population[i, ] <- individual
}
population <- initial_population
# ... (the rest of the code before the for loop)
population <- as.matrix(population)
# ... (the for loop, the rest of the code, and remove temporary variables)
Please try running the updated code with the changes mentioned above.
With this change, the previous error should be resolved. Here's the whole code block with the correction applied:
R
# ... (the rest of the code before the main section)
# Create initial population
n_rows <- 100
initial_population <- matrix(NA, n_rows, length(position_vectors), dimnames=list(NULL,colnames(Data[position_vectors])))
for (i in 1:n_rows) {
individual <- initialize_individual(position_vectors)
initial_population[i, ] <- individual
}
population <- initial_population
# ... (the rest of the code before the for loop)
population <- as.matrix(population)
# ... (the for loop, the rest of the code, and remove temporary variables)
Please try running the updated code with the changes mentioned above.
Created a new variable, pos_colnames, to store column names of Data corresponding to position_vectors.
Replaced colnames(Data[position_vectors]) with pos_colnames in the dimnames parameter of the matrix function.
Here is the updated complete code block with the latest changes:
R
# ... (the rest of the code before the main section)
# Create initial population
n_rows <- 100
pos_colnames <- names(Data)[unlist(position_vectors)]
initial_population <- matrix(NA, n_rows, length(position_vectors), dimnames=list(NULL, pos_colnames))
for (i in 1:n_rows) {
individual <- initialize_individual(position_vectors)
initial_population[i, ] <- individual
}
population <- initial_population
# ... (the rest of the code before the for loop)
population <- as.matrix(population)
# ... (the for loop, the rest of the code, and remove temporary variables)
Please try running the updated code with the changes mentioned above.