I'm working on Genetic Algorithms in R for optimizing KMeans and try to figure out why Im stuck on defining Fitness_function. The Fitness_Function already defined in step 3 but it won't run, it shows an error:
Error in ga(type = "real-valued", Fitness = function(params) Fitness_function(params, : A fitness function must be provided
I already tried to add another Fitness_Function but its make it worse which I cant go forward to the next step because there's same Fitness_Function
Here's what Im trying:
# Step 1: Load required libraries
library(GA)
library(cluster)
library(factoextra)
library(readxl)
# Step 2: Generate or import your data (replace this with your data)
data <- read_xlsx("C:/Users/User/Downloads/input.xlsx",
col_names = FALSE)
# Step 3: Define the Fitness function (K-Means clustering quality)
Fitness_function <- function(params, data) {
n_clusters <- 2
cluster_centers <- matrix(params, ncol = ncol(data), byrow = TRUE)
kmeans_result <- kmeans(data, centers = cluster_centers, iter.max
= 100)
return(-kmeans_result$tot.withinss)
# Negative since GA maximizes the Fitness
}
# Step 4: Define the search space (parameter space for cluster centers)
n_features <- ncol(data)
# Number of features (columns) in 'data'
search_space <- matrix(rep(c(min(data), max(data)), n_features),
nrow = 2)
# Step 5: Run the Genetic Algorithm with more runs and a larger population size
ga_result <- ga(type = "real-valued",
Fitness = function(params) Fitness_function(params, data),
lower = search_space[1, ],
upper = search_space[2, ],
maxiter = 200,
run = 200,
popSize = 100,
# Increase the population size for the GA
pCrossover = 0.8,
pMutation = 0.1,
elitism = 1
)