Pengantar
Algoritma Genetik (AG) adalah teknik pemrograman yang mengikuti prinsip evolusi biologis dan seleksi alam. Teknik ini sering digunakan dalam pencarian solusi optimal untuk masalah yang kompleks di mana metode tradisional tidak efisien. Dalam konteks statistika dan pemrograman linear, Algoritma Genetik dapat digunakan untuk optimasi model prediktif berdasarkan set data yang diberikan. AG beroperasi dengan menggunakan populasi dari solusi potensial yang disebut individu atau kromosom. Proses evolusi ini meliputi seleksi, rekombinasi (crossover), dan mutasi.
Langkah-langkah dasar dalam Algoritma Genetik adalah:
Inisialisasi Populasi: Membuat populasi awal yang terdiri dari individu-individu secara acak.
Evaluasi: Menghitung kecocokan setiap individu dalam populasi.
Seleksi: Memilih individu-individu terbaik sebagai orang tua yang akan menghasilkan keturunan.
Crossover (Rekombinasi): Menggabungkan informasi genetik dari orang tua untuk menghasilkan keturunan baru.
Mutasi: Memodifikasi keturunan secara acak untuk menjaga keragaman genetik.
Penggantian: Mengganti individu lama dengan yang baru untuk generasi berikutnya.
Iterasi: Mengulangi proses dari evaluasi hingga penggantian sampai kriteria berhenti terpenuhi.
Modul R Kuliah
Berikut adalah contoh implementasi Algoritma Genetik menggunakan bahasa pemrograman R:
# Create a vector for each column
x1 <- c(15, 11, 18, 10, 18, 13, 18, 12, 19, 10, 18, 17, 15, 15, 13, 19, 11)
x2 <- c(13, 15, 15, 16, 16, 16, 10, 16, 11, 16, 20, 13, 12, 11, 17, 12, 12)
x3 <- c(20, 19, 17, 13, 20, 18, 17, 12, 10, 19, 17, 19, 14, 11, 18, 13, 20)
x4 <- c(16, 20, 10, 20, 20, 10, 19, 17, 19, 17, 11, 15, 10, 12, 11, 11, 10)
x5 <- c(13, 14, 18, 11, 11, 10, 10, 10, 11, 19, 17, 20, 20, 19, 20, 16, 17)
x6 <- c(11, 19, 15, 12, 10, 17, 15, 17, 12, 16, 20, 20, 19, 16, 17, 16, 15)
x7 <- c(16, 10, 13, 20, 20, 20, 13, 19, 20, 15, 14, 18, 14, 14, 14, 18, 20)
x8 <- c(19, 14, 17, 20, 18, 11, 14, 11, 19, 10, 19, 17, 17, 17, 10, 17, 15)
x9 <- c(16, 15, 19, 12, 10, 16, 10, 19, 13, 10, 20, 16, 13, 14, 11, 20, 10)
x10 <- c(12, 17, 20, 10, 10, 17, 15, 12, 12, 20, 10, 17, 18, 12, 18, 19, 19)
x11 <- c(10, 14, 13, 20, 15, 12, 12, 18, 10, 12, 20, 17, 20, 12, 20, 17, 14)
y <- c(96, 88, 118, 91, 122, 102, 102, 101, 114, 89, 127, 111, 93, 90, 108, 117, 81)
# Combine the vectors into a dataframe
x <- data.frame(x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11)
dd <- x
dd$y <- y
dd
Data dd akan digunakan pada analisis selanjutnya.
Contoh dasar genetic algorithm
a <- c(1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0)
xterpilih <- x[, which(a == 1)]
ddterpilih <- cbind(xterpilih, y)
regresi <- lm(y~., data=ddterpilih)
summary(regresi)$adj.r.squared
## [1] 0.5193257
a <- c(1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1)
xterpilih <- x[, which(a == 1)]
ddterpilih <- cbind(xterpilih, y)
regresi <- lm(y~., ddterpilih)
summary(regresi)$adj.r.squared
## [1] 0.645484
Dengan mengubah sedikit populasi, kita bisa mendapatkan R2 yang lebih baik.
Tahapan genetic algorithm
Inisialisasi populasi
npop = 5
populasi <- matrix(rbinom(55, 1, 0.2), nrow=npop, ncol=11)
populasi
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11]
## [1,] 0 0 0 0 0 0 1 0 0 1 0
## [2,] 0 0 1 0 0 0 0 0 0 0 0
## [3,] 0 1 0 0 0 0 0 0 0 0 0
## [4,] 0 0 0 0 0 1 0 0 0 0 1
## [5,] 0 0 0 0 1 0 0 0 0 0 1
Bagian ini menginisialisasi populasi awal. Populasi terdiri dari 5
individu (baris), dan setiap individu memiliki 11 gen (kolom). Fungsi
rbinom digunakan untuk menghasilkan nilai biner acak (0
atau 1) untuk setiap gen.
Evaluasi fitness (R2-adj)
fitness = rep(0, npop)
for (i in 1:npop){
a <- populasi[i,]
xterpilih <- x[, which(a == 1)]
ddterpilih <- data.frame(cbind(xterpilih, y))
regresi <- lm(y~., data=ddterpilih)
fitness[i] = summary(regresi)$adj.r.squared
}
Bagian ini mengevaluasi fitness setiap individu dalam populasi. Fitness dihitung sebagai nilai R-squared dari model regresi linier yang dibangun menggunakan subset fitur yang dipilih oleh gen dalam individu.
Seleksi
nseleksi <- 2
terbaik <- order(fitness, decreasing=TRUE)[1:nseleksi]
ambilterbaik = populasi[terbaik, ]
Bagian ini melakukan seleksi individu terbaik dari populasi berdasarkan nilai fitness mereka. Dua individu dengan fitness tertinggi dipilih.
Crossover
anak1 <- c(ambilterbaik[1, 1:5], ambilterbaik[2, 6:11])
anak2 <- c(ambilterbaik[2, 1:5], ambilterbaik[1, 6:11])
populasi <- rbind(ambilterbaik, anak1, anak2)
Bagian ini melakukan operasi crossover untuk menghasilkan individu baru. Dua individu terbaik dipilih sebagai orang tua, dan gen mereka dikombinasikan untuk menghasilkan dua individu baru (anak).
Mutasi
mutasi = matrix(rbinom(nrow(populasi)*ncol(populasi), 1, 1/10),
nrow=nrow(populasi), ncol=ncol(populasi))
populasi = abs(populasi - mutasi)
Bagian ini melakukan operasi mutasi pada populasi. Setiap gen dalam setiap individu memiliki peluang tertentu untuk berubah (dari 0 menjadi 1, atau sebaliknya).
Tahapan Ver 2
Fungsi-fungsi
# Fungsi untuk evaluasi fitness
eval_fitness <- function(populasi, x, y) {
fitness = rep(0, nrow(populasi))
for (i in 1:nrow(populasi)){
a <- populasi[i,]
xterpilih <- x[, which(a == 1)]
ddterpilih <- data.frame(cbind(xterpilih, y))
regresi <- lm(y~., data=ddterpilih)
fitness[i] = summary(regresi)$adj.r.squared
}
return(fitness)
}
# Fungsi untuk seleksi
seleksi <- function(populasi, fitness, nseleksi) {
terbaik <- order(fitness, decreasing=TRUE)[1:nseleksi]
return(populasi[terbaik, ])
}
# Fungsi untuk crossover
crossover <- function(ambilterbaik) {
n_genes <- ncol(ambilterbaik)
half_point <- floor(n_genes / 2) # Adjust to always be an integer
populasi <- ambilterbaik
for (t1 in 1:(nrow(ambilterbaik)-1)){
for (t2 in (t1+1):nrow(ambilterbaik)){
anak1 <- c(ambilterbaik[t1, 1:half_point], ambilterbaik[t2, (half_point+1):n_genes])
anak2 <- c(ambilterbaik[t2, 1:half_point], ambilterbaik[t1, (half_point+1):n_genes])
populasi <- rbind(populasi, anak1, anak2)
}
}
return(populasi)
}
# Fungsi untuk mutasi
mutasi <- function(populasi, j) {
mutasi = matrix(rbinom(nrow(populasi)*ncol(populasi), 1, 1/(j*10)),
nrow=nrow(populasi), ncol=ncol(populasi))
return(abs(populasi - mutasi))
}
Iterasi Genetic algorithm
# Inisialisasi populasi
npop = 5
populasi <- matrix(rbinom(55, 1, 0.2), nrow=npop, ncol=11)
# Algoritma Genetik
simpanfitness = NULL
for (j in 1:50){
fitness = eval_fitness(populasi, x, y)
simpanfitness <- c(simpanfitness, mean(fitness))
ambilterbaik = seleksi(populasi, fitness, 4)
populasi = crossover(ambilterbaik)
if (j < 50){
populasi = mutasi(populasi, j)
}
}
# Plot fitness
plot(simpanfitness, type="b")
populasi
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11]
## anak1 1 1 1 1 0 0 1 1 0 1 1
## anak1 1 1 1 1 0 0 1 1 0 1 1
## anak1 1 1 1 1 0 0 1 1 0 1 1
## anak1 1 1 1 1 0 0 1 1 0 1 1
## anak1 1 1 1 1 0 0 1 1 0 1 1
## anak2 1 1 1 1 0 0 1 1 0 1 1
## anak1 1 1 1 1 0 0 1 1 0 1 1
## anak2 1 1 1 1 0 0 1 1 0 1 1
## anak1 1 1 1 1 0 0 1 1 0 1 1
## anak2 1 1 1 1 0 0 1 1 0 1 1
## anak1 1 1 1 1 0 0 1 1 0 1 1
## anak2 1 1 1 1 0 0 1 1 0 1 1
## anak1 1 1 1 1 0 0 1 1 0 1 1
## anak2 1 1 1 1 0 0 1 1 0 1 1
## anak1 1 1 1 1 0 0 1 1 0 1 1
## anak2 1 1 1 1 0 0 1 1 0 1 1
simpanfitness[50]
## [1] 0.9778621
Genetic Using genalg Package
library(genalg)
The genalg package in R provides tools for implementing
genetic algorithms, which are used for optimization problems where
traditional methods might be less efficient. Genetic algorithms are
inspired by the process of natural selection, where the best solutions
tend to survive and get refined over time. This package supports both
binary and floating-point chromosomes, making it versatile for various
types of optimization tasks.
Key Features:
Implements genetic algorithms in R.
Supports both binary and floating-point chromosomes.
Includes functions for running the genetic algorithm, plotting the results, and summarizing outcomes.
Allows customization of the genetic algorithm process through various parameters such as population size, mutation chance, and number of iterations.
The rbga.bin Function
The rbga.bin function is specifically designed for
optimizing binary chromosomes. This function is particularly useful for
tasks like feature selection, where each gene (bit) in the chromosome
can represent the inclusion or exclusion of a feature in a model.
Function Usage:
rbga.bin(size, suggestions=NULL, popSize=200, iters=100, mutationChance=NA, elitism=NA, zeroToOneRatio=10, monitorFunc=NULL, evalFunc=NULL, showSettings=FALSE, verbose=FALSE)
Parameters:
size: The number of genes in each chromosome.popSize: The size of the population in each generation.iters: The number of iterations the genetic algorithm should run.mutationChance: The probability of a gene mutating. By default, this is set to 1/(size+1), balancing the exploration and exploitation.elitism: The number of top solutions that are automatically passed to the next generation. Helps in retaining the best solutions.zeroToOneRatio: Controls the initialization of the genes. A high value means more genes will be set to zero initially, which can be critical depending on the problem context.monitorFunc: A custom function that can be used to monitor the progress of the algorithm after each generation.evalFunc: The evaluation function that calculates the fitness of each chromosome. This is crucial as it defines the objective of the optimization.showSettings: If true, the settings of the genetic algorithm will be printed before the execution starts.verbose: Controls the verbosity of the output during the execution of the algorithm.
Key Operations:
Initialization: Randomly generates an initial population of chromosomes based on the
sizeandzeroToOneRatio.Evaluation: Uses the
evalFuncto compute the fitness of each chromosome.Selection: Selects the best-performing chromosomes based on the fitness scores, possibly enhanced by elitism.
Crossover: Pairs selected chromosomes and combines their genes to produce new offspring, aiming to inherit successful traits from parents.
Mutation: Introduces random changes to the genes of the new chromosomes, which helps in maintaining genetic diversity within the population.
Visualization and Monitoring:
The progress and effectiveness of the genetic algorithm can be visualized using the
plot.rbgafunction, which can show various aspects like the minimum and average fitness over iterations.The
monitorFunccan be used to implement custom monitoring logic, which can be helpful in complex optimizations where intermediate results need to be analyzed.
This function and the genalg package as a whole provide
a robust framework for tackling optimization problems where the solution
space is vast and not easily navigable through conventional optimization
techniques. The package’s flexibility and the power of genetic
algorithms make it a valuable tool in the R ecosystem for statistical
and machine learning applications.
Kuliah dataset
data=dd
Define the fitness function
fitness <- function(chromosome) {
selected <- which(chromosome == 1) # select features based on the chromosome
if(length(selected) == 0) return(0) # if no features selected, fitness is 0
fit <- lm(y ~ ., data = data[, c(selected, ncol(data))]) # fit model
return(-summary(fit)$adj.r.squared) # return adjusted R-squared
}
Run the genetic algorithm
GA_results <- rbga.bin(size = ncol(data)-1, popSize = 50, iters = 50, mutationChance = 0.1, zeroToOneRatio=2, evalFunc = fitness, verbose=T)
## Testing the sanity of parameters...
## Not showing GA settings...
## Starting with random values in the given domains...
## Starting iteration 1
## Calucating evaluation values... .................................................. done.
## Creating next generation...
## sorting results...
## applying elitism...
## applying crossover...
## applying mutations... 46 mutations applied
## Starting iteration 2
## Calucating evaluation values... .............................. done.
## Creating next generation...
## sorting results...
## applying elitism...
## applying crossover...
## applying mutations... 43 mutations applied
## Starting iteration 3
## Calucating evaluation values... .................................. done.
## Creating next generation...
## sorting results...
## applying elitism...
## applying crossover...
## applying mutations... 55 mutations applied
## Starting iteration 4
## Calucating evaluation values... ................................... done.
## Creating next generation...
## sorting results...
## applying elitism...
## applying crossover...
## applying mutations... 43 mutations applied
## Starting iteration 5
## Calucating evaluation values... .................................. done.
## Creating next generation...
## sorting results...
## applying elitism...
## applying crossover...
## applying mutations... 36 mutations applied
## Starting iteration 6
## Calucating evaluation values... ................................... done.
## Creating next generation...
## sorting results...
## applying elitism...
## applying crossover...
## applying mutations... 51 mutations applied
## Starting iteration 7
## Calucating evaluation values... ............................ done.
## Creating next generation...
## sorting results...
## applying elitism...
## applying crossover...
## applying mutations... 45 mutations applied
## Starting iteration 8
## Calucating evaluation values... .................................... done.
## Creating next generation...
## sorting results...
## applying elitism...
## applying crossover...
## applying mutations... 44 mutations applied
## Starting iteration 9
## Calucating evaluation values... ............................... done.
## Creating next generation...
## sorting results...
## applying elitism...
## applying crossover...
## applying mutations... 41 mutations applied
## Starting iteration 10
## Calucating evaluation values... ..................................... done.
## Creating next generation...
## sorting results...
## applying elitism...
## applying crossover...
## applying mutations... 49 mutations applied
## Starting iteration 11
## Calucating evaluation values... ............................... done.
## Creating next generation...
## sorting results...
## applying elitism...
## applying crossover...
## applying mutations... 35 mutations applied
## Starting iteration 12
## Calucating evaluation values... ................................. done.
## Creating next generation...
## sorting results...
## applying elitism...
## applying crossover...
## applying mutations... 42 mutations applied
## Starting iteration 13
## Calucating evaluation values... .................................. done.
## Creating next generation...
## sorting results...
## applying elitism...
## applying crossover...
## applying mutations... 42 mutations applied
## Starting iteration 14
## Calucating evaluation values... .................................. done.
## Creating next generation...
## sorting results...
## applying elitism...
## applying crossover...
## applying mutations... 35 mutations applied
## Starting iteration 15
## Calucating evaluation values... ................................. done.
## Creating next generation...
## sorting results...
## applying elitism...
## applying crossover...
## applying mutations... 40 mutations applied
## Starting iteration 16
## Calucating evaluation values... .................................... done.
## Creating next generation...
## sorting results...
## applying elitism...
## applying crossover...
## applying mutations... 50 mutations applied
## Starting iteration 17
## Calucating evaluation values... ............................... done.
## Creating next generation...
## sorting results...
## applying elitism...
## applying crossover...
## applying mutations... 46 mutations applied
## Starting iteration 18
## Calucating evaluation values... ............................... done.
## Creating next generation...
## sorting results...
## applying elitism...
## applying crossover...
## applying mutations... 55 mutations applied
## Starting iteration 19
## Calucating evaluation values... ..................................... done.
## Creating next generation...
## sorting results...
## applying elitism...
## applying crossover...
## applying mutations... 56 mutations applied
## Starting iteration 20
## Calucating evaluation values... ................................. done.
## Creating next generation...
## sorting results...
## applying elitism...
## applying crossover...
## applying mutations... 49 mutations applied
## Starting iteration 21
## Calucating evaluation values... ................................. done.
## Creating next generation...
## sorting results...
## applying elitism...
## applying crossover...
## applying mutations... 53 mutations applied
## Starting iteration 22
## Calucating evaluation values... ................................... done.
## Creating next generation...
## sorting results...
## applying elitism...
## applying crossover...
## applying mutations... 48 mutations applied
## Starting iteration 23
## Calucating evaluation values... ................................ done.
## Creating next generation...
## sorting results...
## applying elitism...
## applying crossover...
## applying mutations... 58 mutations applied
## Starting iteration 24
## Calucating evaluation values... ................................ done.
## Creating next generation...
## sorting results...
## applying elitism...
## applying crossover...
## applying mutations... 45 mutations applied
## Starting iteration 25
## Calucating evaluation values... .................................. done.
## Creating next generation...
## sorting results...
## applying elitism...
## applying crossover...
## applying mutations... 48 mutations applied
## Starting iteration 26
## Calucating evaluation values... ................................ done.
## Creating next generation...
## sorting results...
## applying elitism...
## applying crossover...
## applying mutations... 38 mutations applied
## Starting iteration 27
## Calucating evaluation values... ................................. done.
## Creating next generation...
## sorting results...
## applying elitism...
## applying crossover...
## applying mutations... 37 mutations applied
## Starting iteration 28
## Calucating evaluation values... .................................... done.
## Creating next generation...
## sorting results...
## applying elitism...
## applying crossover...
## applying mutations... 44 mutations applied
## Starting iteration 29
## Calucating evaluation values... ................................. done.
## Creating next generation...
## sorting results...
## applying elitism...
## applying crossover...
## applying mutations... 45 mutations applied
## Starting iteration 30
## Calucating evaluation values... .................................... done.
## Creating next generation...
## sorting results...
## applying elitism...
## applying crossover...
## applying mutations... 46 mutations applied
## Starting iteration 31
## Calucating evaluation values... ............................... done.
## Creating next generation...
## sorting results...
## applying elitism...
## applying crossover...
## applying mutations... 46 mutations applied
## Starting iteration 32
## Calucating evaluation values... .................................. done.
## Creating next generation...
## sorting results...
## applying elitism...
## applying crossover...
## applying mutations... 44 mutations applied
## Starting iteration 33
## Calucating evaluation values... .................................. done.
## Creating next generation...
## sorting results...
## applying elitism...
## applying crossover...
## applying mutations... 52 mutations applied
## Starting iteration 34
## Calucating evaluation values... ..................................... done.
## Creating next generation...
## sorting results...
## applying elitism...
## applying crossover...
## applying mutations... 39 mutations applied
## Starting iteration 35
## Calucating evaluation values... ................................ done.
## Creating next generation...
## sorting results...
## applying elitism...
## applying crossover...
## applying mutations... 51 mutations applied
## Starting iteration 36
## Calucating evaluation values... .................................. done.
## Creating next generation...
## sorting results...
## applying elitism...
## applying crossover...
## applying mutations... 45 mutations applied
## Starting iteration 37
## Calucating evaluation values... .............................. done.
## Creating next generation...
## sorting results...
## applying elitism...
## applying crossover...
## applying mutations... 49 mutations applied
## Starting iteration 38
## Calucating evaluation values... ................................. done.
## Creating next generation...
## sorting results...
## applying elitism...
## applying crossover...
## applying mutations... 35 mutations applied
## Starting iteration 39
## Calucating evaluation values... .............................. done.
## Creating next generation...
## sorting results...
## applying elitism...
## applying crossover...
## applying mutations... 46 mutations applied
## Starting iteration 40
## Calucating evaluation values... ............................... done.
## Creating next generation...
## sorting results...
## applying elitism...
## applying crossover...
## applying mutations... 44 mutations applied
## Starting iteration 41
## Calucating evaluation values... .............................. done.
## Creating next generation...
## sorting results...
## applying elitism...
## applying crossover...
## applying mutations... 36 mutations applied
## Starting iteration 42
## Calucating evaluation values... .............................. done.
## Creating next generation...
## sorting results...
## applying elitism...
## applying crossover...
## applying mutations... 43 mutations applied
## Starting iteration 43
## Calucating evaluation values... ............................. done.
## Creating next generation...
## sorting results...
## applying elitism...
## applying crossover...
## applying mutations... 42 mutations applied
## Starting iteration 44
## Calucating evaluation values... ...................................... done.
## Creating next generation...
## sorting results...
## applying elitism...
## applying crossover...
## applying mutations... 39 mutations applied
## Starting iteration 45
## Calucating evaluation values... ................................. done.
## Creating next generation...
## sorting results...
## applying elitism...
## applying crossover...
## applying mutations... 53 mutations applied
## Starting iteration 46
## Calucating evaluation values... ............................... done.
## Creating next generation...
## sorting results...
## applying elitism...
## applying crossover...
## applying mutations... 50 mutations applied
## Starting iteration 47
## Calucating evaluation values... ............................... done.
## Creating next generation...
## sorting results...
## applying elitism...
## applying crossover...
## applying mutations... 43 mutations applied
## Starting iteration 48
## Calucating evaluation values... .............................. done.
## Creating next generation...
## sorting results...
## applying elitism...
## applying crossover...
## applying mutations... 52 mutations applied
## Starting iteration 49
## Calucating evaluation values... ................................... done.
## Creating next generation...
## sorting results...
## applying elitism...
## applying crossover...
## applying mutations... 44 mutations applied
## Starting iteration 50
## Calucating evaluation values... ................................. done.
Summary of the results
GA_results
## $type
## [1] "binary chromosome"
##
## $size
## [1] 11
##
## $popSize
## [1] 50
##
## $iters
## [1] 50
##
## $suggestions
## NULL
##
## $population
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11]
## [1,] 1 1 1 1 0 0 1 1 0 1 1
## [2,] 1 1 1 1 0 0 1 1 0 1 1
## [3,] 1 1 1 1 0 0 1 1 0 1 1
## [4,] 1 1 1 1 0 0 1 1 0 1 1
## [5,] 1 1 1 1 0 0 1 1 0 1 1
## [6,] 1 1 1 1 0 0 1 1 0 1 1
## [7,] 1 1 1 1 0 0 1 1 0 1 1
## [8,] 1 1 1 1 0 0 1 1 0 1 1
## [9,] 1 0 1 1 0 0 1 1 0 1 1
## [10,] 1 1 1 1 0 0 1 1 0 1 1
## [11,] 1 1 1 1 0 0 1 1 0 0 1
## [12,] 1 1 1 1 0 0 1 1 0 1 0
## [13,] 1 1 1 1 0 0 1 1 0 1 1
## [14,] 1 1 1 1 0 0 1 1 0 1 1
## [15,] 1 1 1 1 0 0 1 1 0 1 1
## [16,] 1 1 0 1 0 0 1 0 0 1 0
## [17,] 1 1 1 1 0 0 1 1 0 1 1
## [18,] 1 1 1 1 0 0 1 0 0 0 1
## [19,] 1 1 0 1 0 0 1 1 0 1 1
## [20,] 1 1 1 1 0 0 1 1 0 0 1
## [21,] 1 1 1 1 0 0 1 1 0 1 1
## [22,] 1 1 1 1 0 0 1 1 0 1 1
## [23,] 1 1 1 1 0 0 1 1 0 0 1
## [24,] 1 1 1 1 0 1 0 1 0 1 1
## [25,] 1 0 1 1 0 0 1 1 0 0 1
## [26,] 1 1 1 1 0 1 1 1 0 1 1
## [27,] 1 1 1 1 0 0 1 1 0 1 1
## [28,] 1 1 0 0 0 0 1 1 0 1 1
## [29,] 1 1 1 1 0 0 1 1 0 1 1
## [30,] 1 1 1 1 0 0 1 1 0 1 1
## [31,] 1 1 1 1 0 0 1 1 0 1 1
## [32,] 1 1 1 1 0 0 1 1 1 1 1
## [33,] 1 1 1 1 0 0 1 1 0 1 1
## [34,] 1 1 1 1 0 0 1 1 0 1 0
## [35,] 1 0 1 1 0 0 1 1 0 1 1
## [36,] 1 0 1 0 0 0 1 1 0 1 1
## [37,] 1 1 1 1 0 0 1 1 0 1 1
## [38,] 0 1 1 1 0 0 1 1 0 1 1
## [39,] 1 0 1 1 0 0 1 1 0 1 1
## [40,] 1 1 1 0 0 0 1 1 1 1 0
## [41,] 1 1 1 1 0 0 1 1 0 1 1
## [42,] 1 1 1 1 1 0 1 1 0 1 1
## [43,] 1 1 1 1 0 0 0 1 0 0 1
## [44,] 1 0 1 0 0 0 1 1 0 1 1
## [45,] 1 1 1 1 1 0 0 1 0 1 0
## [46,] 1 1 1 1 0 0 1 1 0 0 1
## [47,] 0 1 1 1 0 0 1 1 0 1 1
## [48,] 1 1 1 1 0 0 1 1 0 1 1
## [49,] 1 1 1 1 0 0 1 1 0 1 1
## [50,] 1 1 1 1 0 0 1 1 0 1 1
##
## $elitism
## [1] 10
##
## $mutationChance
## [1] 0.1
##
## $evaluations
## [1] -0.9778621 -0.9778621 -0.9778621 -0.9778621 -0.9778621 -0.9778621
## [7] -0.9778621 -0.9778621 -0.9778621 -0.9778621 -0.9635084 -0.9772940
## [13] -0.9778621 -0.9778621 -0.9778621 -0.9667418 -0.9778621 -0.9414245
## [19] -0.9778621 -0.9635084 -0.9778621 -0.9778621 -0.9635084 -0.9493324
## [25] -0.6640139 -0.9778621 -0.9778621 -0.9599135 -0.9778621 -0.9778621
## [31] -0.9778621 -0.9749025 -0.9778621 -0.9772940 -0.6609685 -0.6943459
## [37] -0.9778621 0.3112949 -0.6609685 -0.9620248 -0.9778621 -0.9748654
## [43] -0.9355680 -0.6943459 -0.9373579 -0.9635084 0.3112949 -0.9778621
## [49] -0.9778621 -0.9778621
##
## $best
## [1] -0.9472880 -0.9581761 -0.9634812 -0.9772940 -0.9772940 -0.9772940
## [7] -0.9772940 -0.9772940 -0.9772940 -0.9778621 -0.9778621 -0.9778621
## [13] -0.9778621 -0.9778621 -0.9778621 -0.9778621 -0.9778621 -0.9778621
## [19] -0.9778621 -0.9778621 -0.9778621 -0.9778621 -0.9778621 -0.9778621
## [25] -0.9778621 -0.9778621 -0.9778621 -0.9778621 -0.9778621 -0.9778621
## [31] -0.9778621 -0.9778621 -0.9778621 -0.9778621 -0.9778621 -0.9778621
## [37] -0.9778621 -0.9778621 -0.9778621 -0.9778621 -0.9778621 -0.9778621
## [43] -0.9778621 -0.9778621 -0.9778621 -0.9778621 -0.9778621 -0.9778621
## [49] -0.9778621 -0.9778621
##
## $mean
## [1] -0.2690320 -0.6756853 -0.8587016 -0.8541802 -0.8730625 -0.8802842
## [7] -0.8933901 -0.8202361 -0.9002868 -0.8988523 -0.9119682 -0.8696115
## [13] -0.8957924 -0.9015940 -0.8807019 -0.9482497 -0.8823170 -0.9119482
## [19] -0.9230023 -0.9477794 -0.9126958 -0.9073535 -0.9222753 -0.8550027
## [25] -0.9407666 -0.9061921 -0.9120482 -0.8677136 -0.8821515 -0.9248129
## [31] -0.8330259 -0.8376146 -0.8788927 -0.8319067 -0.9282630 -0.9152388
## [37] -0.9063461 -0.9087751 -0.9409202 -0.8672055 -0.8591239 -0.9102493
## [43] -0.9364938 -0.8524663 -0.8137110 -0.9184808 -0.8931891 -0.8531626
## [49] -0.8282846 -0.8908589
##
## attr(,"class")
## [1] "rbga"
Visualize the eval
plot(GA_results)
Synthetic dataset
For the purposes of this module, we will generate a synthetic dataset. This dataset will consist of multiple predictor variables and a response variable. These variables simulate real-world data where the goal is to select the most relevant features for predicting the response.
# Load necessary library
if(!require(genalg)) install.packages("genalg", dependencies=TRUE)
library(genalg)
# Generate synthetic data
set.seed(123) # for reproducibility
n <- 100 # number of observations
p <- 10 # number of variables
X <- matrix(rnorm(n * p), ncol=p) # predictor matrix
beta <- runif(p, -1, 1) # random coefficients
y <- X %*% beta + rnorm(n) # response variable
data <- data.frame(X, y = y)
Genetic Algorithm Setup:
We will setup a Genetic Algorithm to select a subset of features that
best predict our response variable y. The fitness of each
subset will be determined by the adjusted R-squared of a linear model
fitted using those features.
# Define the fitness function
fitness <- function(chromosome) {
selected <- which(chromosome == 1) # select features based on the chromosome
if(length(selected) == 0) return(0) # if no features selected, fitness is 0
fit <- lm(y ~ ., data = data[, c(selected, ncol(data))]) # fit model
return(-summary(fit)$adj.r.squared) # return adjusted R-squared
}
# Run the genetic algorithm
GA_results <- rbga.bin(size = p, popSize = 50, iters = 100, mutationChance = 0.01, elitism = T, evalFunc = fitness)
Summary of the results
GA_results
## $type
## [1] "binary chromosome"
##
## $size
## [1] 10
##
## $popSize
## [1] 50
##
## $iters
## [1] 100
##
## $suggestions
## NULL
##
## $population
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
## [1,] 1 1 1 0 1 1 1 1 1 1
## [2,] 1 0 1 0 1 1 1 1 1 1
## [3,] 1 1 1 0 1 1 1 1 1 1
## [4,] 1 1 1 0 1 1 1 1 1 1
## [5,] 1 1 1 0 1 1 1 1 1 1
## [6,] 1 1 1 0 1 1 1 1 1 1
## [7,] 1 1 1 0 1 1 1 1 1 1
## [8,] 1 1 1 0 1 1 1 1 1 1
## [9,] 1 1 1 0 1 1 1 1 1 1
## [10,] 1 1 1 0 1 1 1 1 1 1
## [11,] 1 1 1 0 1 1 1 1 1 1
## [12,] 1 1 1 0 1 1 1 1 1 1
## [13,] 1 1 1 0 1 1 1 1 1 1
## [14,] 1 1 1 0 1 1 1 1 1 1
## [15,] 1 1 1 0 1 1 1 1 1 1
## [16,] 1 1 1 0 1 1 1 1 1 1
## [17,] 1 1 1 0 1 1 1 1 1 1
## [18,] 1 1 1 0 1 1 1 1 1 1
## [19,] 1 1 1 0 1 1 1 1 1 1
## [20,] 1 1 1 0 1 1 1 1 1 1
## [21,] 1 1 1 0 1 1 1 1 1 1
## [22,] 1 1 1 0 1 1 1 1 1 1
## [23,] 1 1 1 0 1 1 1 1 1 1
## [24,] 1 1 1 0 1 1 1 1 1 1
## [25,] 1 1 1 0 1 1 1 1 1 1
## [26,] 1 1 1 0 1 1 1 1 1 1
## [27,] 1 1 1 0 1 1 1 1 1 1
## [28,] 1 1 1 0 1 1 1 1 1 1
## [29,] 1 1 1 0 1 0 1 1 1 1
## [30,] 1 1 1 0 1 1 1 1 1 1
## [31,] 1 1 1 0 1 1 1 1 1 1
## [32,] 1 1 1 0 1 1 1 1 1 1
## [33,] 1 1 1 0 1 1 1 1 1 1
## [34,] 1 1 1 0 1 1 1 1 1 1
## [35,] 1 1 1 0 1 1 1 1 1 1
## [36,] 1 1 1 0 1 1 1 1 1 1
## [37,] 1 1 1 0 1 1 1 1 1 1
## [38,] 1 1 1 0 1 1 1 1 1 1
## [39,] 1 1 1 0 1 1 1 1 1 1
## [40,] 1 1 1 0 1 1 1 1 1 1
## [41,] 1 1 1 0 1 1 1 1 1 1
## [42,] 1 1 1 0 1 1 1 1 1 1
## [43,] 1 1 1 0 1 1 1 1 1 1
## [44,] 1 1 1 0 1 1 1 1 1 1
## [45,] 1 1 1 0 1 1 1 1 1 1
## [46,] 1 1 1 0 1 1 1 1 1 1
## [47,] 1 1 1 0 1 1 1 1 1 1
## [48,] 1 1 1 0 1 1 1 1 1 0
## [49,] 1 1 1 0 1 1 1 1 1 1
## [50,] 1 1 1 0 1 1 1 1 1 1
##
## $elitism
## [1] TRUE
##
## $mutationChance
## [1] 0.01
##
## $evaluations
## [1] -0.7715529 -0.6011926 -0.7715529 -0.7715529 -0.7715529 -0.7715529
## [7] -0.7715529 -0.7715529 -0.7715529 -0.7715529 -0.7715529 -0.7715529
## [13] -0.7715529 -0.7715529 -0.7715529 -0.7715529 -0.7715529 -0.7715529
## [19] -0.7715529 -0.7715529 -0.7715529 -0.7715529 -0.7715529 -0.7715529
## [25] -0.7715529 -0.7715529 -0.7715529 -0.7715529 -0.7577728 -0.7715529
## [31] -0.7715529 -0.7715529 -0.7715529 -0.7715529 -0.7715529 -0.7715529
## [37] -0.7715529 -0.7715529 -0.7715529 -0.7715529 -0.7715529 -0.7715529
## [43] -0.7715529 -0.7715529 -0.7715529 -0.7715529 -0.7715529 -0.7715529
## [49] -0.7715529 -0.7715529
##
## $best
## [1] -0.3001773 -0.3960330 -0.4520020 -0.5546368 -0.6884621 -0.6884621
## [7] -0.6884621 -0.6884621 -0.7539872 -0.7539872 -0.7539872 -0.7539872
## [13] -0.7539872 -0.7689687 -0.7689687 -0.7689687 -0.7689687 -0.7689687
## [19] -0.7689687 -0.7689687 -0.7689687 -0.7689687 -0.7689687 -0.7689687
## [25] -0.7689687 -0.7689687 -0.7689687 -0.7689687 -0.7689687 -0.7715529
## [31] -0.7715529 -0.7715529 -0.7715529 -0.7715529 -0.7715529 -0.7715529
## [37] -0.7715529 -0.7715529 -0.7715529 -0.7715529 -0.7715529 -0.7715529
## [43] -0.7715529 -0.7715529 -0.7715529 -0.7715529 -0.7715529 -0.7715529
## [49] -0.7715529 -0.7715529 -0.7715529 -0.7715529 -0.7715529 -0.7715529
## [55] -0.7715529 -0.7715529 -0.7715529 -0.7715529 -0.7715529 -0.7715529
## [61] -0.7715529 -0.7715529 -0.7715529 -0.7715529 -0.7715529 -0.7715529
## [67] -0.7715529 -0.7715529 -0.7715529 -0.7715529 -0.7715529 -0.7715529
## [73] -0.7715529 -0.7715529 -0.7715529 -0.7715529 -0.7715529 -0.7715529
## [79] -0.7715529 -0.7715529 -0.7715529 -0.7715529 -0.7715529 -0.7715529
## [85] -0.7715529 -0.7715529 -0.7715529 -0.7715529 -0.7715529 -0.7715529
## [91] -0.7715529 -0.7715529 -0.7715529 -0.7715529 -0.7715529 -0.7715529
## [97] -0.7715529 -0.7715529 -0.7715529 -0.7715529
##
## $mean
## [1] -0.08772023 -0.17171212 -0.26892032 -0.35229428 -0.45770998 -0.53546072
## [7] -0.61954610 -0.66982573 -0.68509291 -0.68117780 -0.68088769 -0.68510930
## [13] -0.68239865 -0.69181055 -0.71853304 -0.74921138 -0.76605125 -0.76050318
## [19] -0.75629309 -0.75762589 -0.75868450 -0.75678862 -0.75686842 -0.75732446
## [25] -0.73833418 -0.76214298 -0.76107434 -0.76076015 -0.76536367 -0.75245928
## [31] -0.76025690 -0.76939950 -0.74392544 -0.76741056 -0.75029341 -0.76728268
## [37] -0.75550819 -0.76220291 -0.76607658 -0.76259174 -0.76454898 -0.75936453
## [43] -0.77122557 -0.76071934 -0.77020189 -0.76320804 -0.77127726 -0.76617894
## [49] -0.76379328 -0.76187563 -0.76020214 -0.76391713 -0.77044349 -0.75917688
## [55] -0.75712222 -0.75442215 -0.73330748 -0.76515526 -0.76655961 -0.75537943
## [61] -0.76071397 -0.76996492 -0.76783388 -0.76950550 -0.76442667 -0.76577626
## [67] -0.77122557 -0.76512360 -0.76820725 -0.75783882 -0.76098675 -0.76484269
## [73] -0.77150118 -0.76928718 -0.76992290 -0.76964730 -0.76249614 -0.76922989
## [79] -0.77155286 -0.76612726 -0.76584263 -0.76581883 -0.75626907 -0.76674825
## [85] -0.76249614 -0.76964730 -0.77047749 -0.76515526 -0.76327194 -0.75855413
## [91] -0.76410464 -0.75931030 -0.76643865 -0.77127726 -0.75497639 -0.75739782
## [97] -0.76192879 -0.76625065 -0.75756720 -0.76787005
##
## attr(,"class")
## [1] "rbga"
Results Visualization:
Visualizing the optimization process can help understand how the GA evolved over iterations.
# Plotting GA optimization
plot(GA_results)
This plot demonstrates how the fitness of the best solution (as measured by the adjusted R-squared) has improved over iterations. It provides insight into the effectiveness of genetic algorithms for feature selection in statistical models.
IRIS Dataset
In the given example, the Genetic Algorithm is used to identify significant features within a dataset that includes both original and randomly generated variables. Here, the original dataset is the well-known Iris dataset, and the example aims to demonstrate the effectiveness of the Genetic Algorithm in distinguishing the truly informative features from noise.
The Iris dataset is enhanced by adding 36 random variables to the original four variables. This creates a challenge where the feature selection algorithm needs to identify which of the 40 variables are the original, meaningful ones.
# Load necessary libraries
if(!require(genalg)) install.packages("genalg", dependencies=TRUE)
library(genalg)
if(!require(MASS)) install.packages("MASS", dependencies=TRUE)
## Loading required package: MASS
library(MASS)
# Load and prepare the Iris dataset
data(iris)
set.seed(123) # for reproducibility
X <- cbind(scale(iris[,1:4]), matrix(rnorm(36 * 150), 150, 36)) # Add noise variables
Y <- iris[,5] # Species information
Genetic Algorithm Setup:
A custom evaluation function is defined to measure the fitness of each subset of features. This function uses Linear Discriminant Analysis (LDA) to assess how well the selected features can predict the Iris species. The fitness is inversely related to the misclassification rate.
# Define the fitness evaluation function
iris.evaluate <- function(indices) {
result = 1
if (sum(indices) > 2) {
huhn <- lda(X[,indices==1], Y, CV=TRUE)$posterior
result = sum(Y != dimnames(huhn)[[2]][apply(huhn, 1,
function(x)
which(x == max(x)))]) / length(Y)
}
result
}
Running the Genetic Algorithm:
The GA is initialized and executed with a population size of 50 and 100 iterations. The mutation chance and elitism are adjusted to balance exploration and exploitation.
# Run the genetic algorithm
GA_iris <- rbga.bin(size = 40, popSize = 50, iters = 100, mutationChance = 0.05, zeroToOneRatio = 10, evalFunc = iris.evaluate)
GA_iris
## $type
## [1] "binary chromosome"
##
## $size
## [1] 40
##
## $popSize
## [1] 50
##
## $iters
## [1] 100
##
## $suggestions
## NULL
##
## $population
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13]
## [1,] 0 0 0 1 0 0 0 0 1 0 0 0 0
## [2,] 0 0 0 1 0 0 0 0 1 0 0 0 0
## [3,] 0 0 0 1 0 0 0 0 1 0 0 0 0
## [4,] 0 0 0 1 0 0 0 0 1 0 0 0 0
## [5,] 0 0 0 1 0 0 0 0 1 0 0 0 0
## [6,] 0 0 0 1 0 0 0 0 1 0 0 0 0
## [7,] 0 0 0 1 0 0 0 0 1 0 0 0 0
## [8,] 0 0 0 1 0 0 0 0 1 0 0 0 0
## [9,] 0 0 0 1 0 0 0 0 1 0 0 0 0
## [10,] 0 0 0 1 0 0 0 0 1 0 0 0 0
## [11,] 0 0 0 1 0 0 0 0 1 0 0 0 0
## [12,] 0 0 0 1 0 0 0 0 1 0 0 0 0
## [13,] 0 0 0 1 0 0 0 0 1 0 0 0 0
## [14,] 0 0 0 1 0 0 0 0 1 0 0 0 0
## [15,] 0 0 0 1 0 0 0 0 1 0 0 0 0
## [16,] 0 0 0 1 0 0 0 0 1 0 0 1 0
## [17,] 0 0 0 1 0 0 0 0 0 0 0 0 0
## [18,] 0 0 0 1 0 0 0 0 1 0 0 0 0
## [19,] 0 0 0 1 0 0 0 0 1 0 0 0 0
## [20,] 0 0 0 1 0 0 0 0 1 0 0 0 0
## [21,] 0 0 0 1 0 0 0 0 1 0 0 0 0
## [22,] 0 0 0 1 0 0 0 0 1 0 0 0 0
## [23,] 0 0 0 1 0 0 0 0 1 0 0 0 0
## [24,] 0 0 0 1 0 0 0 0 1 0 0 0 0
## [25,] 0 0 0 1 0 0 0 0 1 0 0 0 0
## [26,] 0 0 0 1 0 0 0 0 1 0 0 0 0
## [27,] 0 0 0 1 0 0 0 0 1 0 0 0 0
## [28,] 0 0 0 1 0 0 0 0 1 0 0 0 0
## [29,] 0 0 0 1 0 0 0 0 1 0 0 0 0
## [30,] 0 0 0 1 0 0 0 0 1 0 0 0 0
## [31,] 0 0 0 0 0 0 0 0 1 0 0 0 0
## [32,] 0 0 0 1 0 0 0 0 1 0 0 0 0
## [33,] 0 0 0 1 0 0 0 0 1 0 0 0 0
## [34,] 0 0 0 1 0 0 0 0 1 0 0 0 0
## [35,] 0 0 0 1 0 0 0 0 0 0 0 0 0
## [36,] 0 0 0 1 0 0 0 0 1 0 0 0 0
## [37,] 0 0 0 1 0 0 0 0 1 0 0 0 0
## [38,] 0 0 0 1 0 0 0 0 1 0 0 0 0
## [39,] 0 0 0 1 0 0 0 0 1 0 0 0 0
## [40,] 0 0 0 1 0 0 0 0 1 0 0 0 0
## [41,] 0 0 0 1 0 0 0 0 1 0 0 0 0
## [42,] 0 0 0 0 0 0 0 0 1 0 0 0 0
## [43,] 0 0 0 1 0 0 0 0 1 0 0 0 0
## [44,] 0 0 0 1 0 0 0 0 1 0 0 0 0
## [45,] 0 0 0 1 0 0 0 0 1 0 0 0 0
## [46,] 0 0 0 1 0 0 0 0 1 0 0 0 0
## [47,] 0 0 0 1 0 0 0 0 1 0 0 0 0
## [48,] 0 0 0 1 0 0 0 0 1 0 0 0 0
## [49,] 0 0 0 1 0 0 0 0 1 0 0 0 0
## [50,] 0 0 0 1 0 0 0 0 1 0 0 0 0
## [,14] [,15] [,16] [,17] [,18] [,19] [,20] [,21] [,22] [,23] [,24] [,25]
## [1,] 0 0 0 0 0 0 0 0 0 1 0 0
## [2,] 0 0 0 0 0 0 0 0 0 1 0 0
## [3,] 0 0 0 0 0 0 0 0 0 1 0 0
## [4,] 0 0 0 0 0 0 0 0 0 1 0 0
## [5,] 0 0 0 0 0 0 0 0 0 1 0 0
## [6,] 0 0 0 0 0 0 0 0 0 1 0 0
## [7,] 0 0 0 0 0 0 0 0 0 1 0 0
## [8,] 0 0 0 0 0 0 0 0 0 1 0 0
## [9,] 0 0 0 0 0 0 0 0 0 1 0 0
## [10,] 0 0 0 0 0 0 0 0 0 1 0 0
## [11,] 0 0 0 0 0 0 0 0 0 1 0 0
## [12,] 0 0 0 0 0 0 0 0 0 1 0 0
## [13,] 0 0 0 0 0 0 0 0 0 1 0 0
## [14,] 0 0 0 0 0 0 0 0 0 1 0 0
## [15,] 0 0 0 0 0 0 0 0 0 1 0 0
## [16,] 0 0 0 0 0 0 0 0 0 0 0 0
## [17,] 0 0 0 0 0 0 0 0 0 1 0 0
## [18,] 0 0 0 0 0 0 0 0 0 0 0 0
## [19,] 0 0 0 0 0 0 0 0 0 1 0 0
## [20,] 0 0 0 0 0 0 0 0 0 0 0 0
## [21,] 0 0 0 0 0 0 0 0 0 1 0 0
## [22,] 0 0 0 0 0 0 0 0 0 1 0 0
## [23,] 0 0 0 0 0 0 0 0 0 1 0 0
## [24,] 0 0 0 0 0 0 0 0 0 0 0 0
## [25,] 0 0 0 0 0 0 0 0 0 1 0 0
## [26,] 0 0 0 0 0 0 0 0 0 1 0 0
## [27,] 0 0 0 0 0 0 0 0 0 1 0 0
## [28,] 0 0 0 0 0 0 0 0 0 1 0 0
## [29,] 0 0 0 0 0 0 0 0 0 1 0 0
## [30,] 0 0 0 0 0 0 0 0 0 1 0 0
## [31,] 0 0 0 0 0 0 0 0 0 1 0 0
## [32,] 0 0 0 0 0 0 0 0 0 1 0 0
## [33,] 0 0 0 0 0 0 0 0 0 1 0 0
## [34,] 0 0 0 0 0 0 0 0 0 1 0 0
## [35,] 0 0 0 0 0 0 0 0 0 1 0 0
## [36,] 0 0 0 0 0 0 0 0 0 1 0 0
## [37,] 0 0 0 0 0 0 0 0 0 1 0 0
## [38,] 0 0 0 0 0 0 0 0 0 1 0 0
## [39,] 0 0 0 0 0 0 0 0 0 1 0 0
## [40,] 0 0 0 0 0 0 0 0 0 1 0 0
## [41,] 0 0 0 0 0 0 0 0 0 1 0 0
## [42,] 0 0 0 0 0 0 0 0 0 1 0 0
## [43,] 0 0 0 0 0 0 0 0 0 1 0 0
## [44,] 0 0 0 0 0 0 0 0 0 1 0 0
## [45,] 0 0 0 0 0 0 0 0 0 1 0 0
## [46,] 0 0 0 0 0 0 0 0 0 1 0 0
## [47,] 0 0 0 0 0 0 0 0 0 1 0 0
## [48,] 0 0 0 0 0 0 0 0 0 1 0 0
## [49,] 0 0 0 0 0 0 0 0 0 1 0 0
## [50,] 0 1 0 0 0 1 0 0 0 1 0 0
## [,26] [,27] [,28] [,29] [,30] [,31] [,32] [,33] [,34] [,35] [,36] [,37]
## [1,] 0 0 0 0 0 0 0 0 0 0 0 1
## [2,] 0 0 0 0 0 0 0 0 0 0 0 1
## [3,] 0 0 0 0 0 0 0 0 0 0 0 0
## [4,] 0 0 0 0 0 0 0 0 0 0 0 0
## [5,] 0 0 0 0 0 0 0 0 0 0 0 1
## [6,] 0 0 0 0 0 0 0 0 0 0 0 0
## [7,] 0 0 0 0 0 0 0 0 0 0 0 1
## [8,] 0 0 0 0 0 0 0 0 0 0 0 1
## [9,] 0 0 0 0 0 0 0 0 0 0 0 1
## [10,] 0 0 0 0 0 0 0 0 0 0 0 1
## [11,] 0 0 0 0 0 0 0 0 0 0 0 0
## [12,] 0 0 0 0 0 0 0 0 0 0 0 0
## [13,] 0 0 0 0 0 0 0 0 0 0 0 1
## [14,] 0 0 0 0 0 0 0 0 0 0 0 0
## [15,] 0 0 0 0 0 0 0 0 0 0 0 0
## [16,] 0 0 0 0 0 0 0 0 0 0 0 1
## [17,] 0 0 0 0 0 0 0 0 0 0 0 0
## [18,] 0 0 0 0 0 0 0 0 0 0 0 1
## [19,] 0 0 0 0 0 0 0 0 0 0 0 1
## [20,] 0 0 0 0 0 0 0 0 0 0 0 1
## [21,] 0 0 0 0 0 0 0 0 0 0 0 1
## [22,] 0 0 0 0 0 0 0 0 0 0 0 1
## [23,] 0 0 0 0 0 0 0 0 0 0 0 1
## [24,] 0 0 0 0 0 0 0 0 0 0 0 1
## [25,] 0 0 0 0 0 0 0 0 0 0 0 1
## [26,] 0 0 0 0 0 0 0 0 0 0 0 0
## [27,] 0 0 0 0 0 0 0 0 0 0 0 1
## [28,] 0 0 0 0 0 0 0 0 0 0 0 0
## [29,] 0 0 0 0 0 0 0 0 0 0 0 0
## [30,] 0 0 0 0 0 0 0 0 0 0 0 0
## [31,] 0 0 0 0 0 0 0 0 0 1 0 1
## [32,] 0 0 0 0 0 0 0 0 0 0 0 1
## [33,] 0 0 0 0 0 0 0 0 0 0 0 1
## [34,] 0 0 0 0 0 0 0 0 0 0 0 1
## [35,] 0 0 0 0 0 0 0 0 0 0 0 0
## [36,] 0 0 0 0 0 0 0 0 0 0 0 1
## [37,] 0 0 0 0 0 0 0 0 0 0 0 1
## [38,] 0 0 0 0 0 0 0 0 0 0 0 0
## [39,] 0 0 0 0 0 0 0 0 0 0 0 1
## [40,] 0 0 0 0 0 0 0 0 0 0 0 1
## [41,] 0 0 0 0 0 0 0 0 0 1 1 1
## [42,] 0 0 0 0 0 0 0 0 0 0 0 0
## [43,] 0 0 0 0 0 0 0 0 0 0 0 0
## [44,] 0 0 0 0 0 0 0 0 0 0 0 1
## [45,] 0 0 0 0 0 0 0 0 0 0 0 0
## [46,] 0 0 0 0 0 0 0 0 0 0 0 1
## [47,] 0 0 0 0 0 0 0 0 0 0 0 1
## [48,] 0 0 0 0 0 0 0 0 0 0 0 1
## [49,] 0 0 0 0 0 0 0 0 0 0 0 0
## [50,] 0 0 0 0 0 0 0 0 0 0 0 0
## [,38] [,39] [,40]
## [1,] 0 0 1
## [2,] 0 0 1
## [3,] 1 0 1
## [4,] 1 0 1
## [5,] 0 0 1
## [6,] 1 0 1
## [7,] 0 0 1
## [8,] 0 0 1
## [9,] 0 0 1
## [10,] 0 0 1
## [11,] 1 0 1
## [12,] 0 0 1
## [13,] 0 0 1
## [14,] 1 0 1
## [15,] 1 0 1
## [16,] 0 0 1
## [17,] 1 0 1
## [18,] 0 0 1
## [19,] 0 0 1
## [20,] 0 0 1
## [21,] 0 0 1
## [22,] 0 0 1
## [23,] 0 0 1
## [24,] 0 0 1
## [25,] 0 0 1
## [26,] 1 0 1
## [27,] 0 0 1
## [28,] 1 0 1
## [29,] 1 0 1
## [30,] 1 0 1
## [31,] 0 0 0
## [32,] 0 0 1
## [33,] 1 0 1
## [34,] 0 0 1
## [35,] 0 0 1
## [36,] 0 0 1
## [37,] 0 0 1
## [38,] 0 0 1
## [39,] 0 0 1
## [40,] 0 0 1
## [41,] 0 0 1
## [42,] 1 0 1
## [43,] 1 0 1
## [44,] 0 0 1
## [45,] 1 0 1
## [46,] 0 0 1
## [47,] 0 0 1
## [48,] 0 0 1
## [49,] 1 0 1
## [50,] 1 0 1
##
## $elitism
## [1] 10
##
## $mutationChance
## [1] 0.05
##
## $evaluations
## [1] 0.02666667 0.02666667 0.02666667 0.02666667 0.02666667 0.02666667
## [7] 0.02666667 0.02666667 0.02666667 0.02666667 0.02666667 0.03333333
## [13] 0.02666667 0.02666667 0.02666667 0.03333333 0.04000000 0.02666667
## [19] 0.02666667 0.02666667 0.02666667 0.02666667 0.02666667 0.02666667
## [25] 0.02666667 0.02666667 0.02666667 0.02666667 0.02666667 0.02666667
## [31] 0.66000000 0.02666667 0.04000000 0.02666667 0.04666667 0.02666667
## [37] 0.02666667 0.03333333 0.02666667 0.02666667 0.02666667 0.69333333
## [43] 0.02666667 0.02666667 0.02666667 0.02666667 0.02666667 0.02666667
## [49] 0.02666667 0.03333333
##
## $best
## [1] 0.03333333 0.03333333 0.02666667 0.02666667 0.02666667 0.02666667
## [7] 0.02666667 0.02666667 0.02666667 0.02666667 0.02666667 0.02666667
## [13] 0.02666667 0.02666667 0.02666667 0.02666667 0.02666667 0.02666667
## [19] 0.02666667 0.02666667 0.02666667 0.02666667 0.02666667 0.02666667
## [25] 0.02666667 0.02666667 0.02666667 0.02666667 0.02666667 0.02666667
## [31] 0.02666667 0.02666667 0.02666667 0.02666667 0.02666667 0.02666667
## [37] 0.02666667 0.02666667 0.02666667 0.02666667 0.02666667 0.02666667
## [43] 0.02666667 0.02666667 0.02666667 0.02666667 0.02666667 0.02666667
## [49] 0.02666667 0.02666667 0.02666667 0.02666667 0.02666667 0.02666667
## [55] 0.02666667 0.02666667 0.02666667 0.02666667 0.02666667 0.02666667
## [61] 0.02666667 0.02666667 0.02666667 0.02666667 0.02666667 0.02666667
## [67] 0.02666667 0.02666667 0.02666667 0.02666667 0.02666667 0.02666667
## [73] 0.02666667 0.02666667 0.02666667 0.02666667 0.02666667 0.02666667
## [79] 0.02666667 0.02666667 0.02666667 0.02666667 0.02666667 0.02666667
## [85] 0.02666667 0.02666667 0.02666667 0.02666667 0.02666667 0.02666667
## [91] 0.02666667 0.02666667 0.02666667 0.02666667 0.02666667 0.02666667
## [97] 0.02666667 0.02666667 0.02666667 0.02666667
##
## $mean
## [1] 0.72066667 0.45533333 0.26466667 0.11493333 0.09386667 0.09120000
## [7] 0.10880000 0.06333333 0.02906667 0.02906667 0.02973333 0.05573333
## [13] 0.09853333 0.07586667 0.07080000 0.08360000 0.06946667 0.08546667
## [19] 0.02893333 0.08280000 0.07466667 0.05573333 0.02866667 0.07400000
## [25] 0.09746667 0.05586667 0.05413333 0.02866667 0.08293333 0.08253333
## [31] 0.05586667 0.07026667 0.06133333 0.06986667 0.06826667 0.04240000
## [37] 0.04133333 0.05533333 0.04186667 0.02840000 0.05640000 0.08360000
## [43] 0.02933333 0.07000000 0.05600000 0.04213333 0.04426667 0.06893333
## [49] 0.09680000 0.02906667 0.04253333 0.06853333 0.06746667 0.05626667
## [55] 0.08520000 0.05653333 0.06973333 0.06213333 0.05533333 0.05706667
## [61] 0.08480000 0.05693333 0.04173333 0.09026667 0.05560000 0.04280000
## [67] 0.02786667 0.08306667 0.09653333 0.09666667 0.04093333 0.04266667
## [73] 0.07200000 0.04453333 0.09480000 0.06893333 0.05693333 0.06986667
## [79] 0.04200000 0.05453333 0.03000000 0.04226667 0.04453333 0.04386667
## [85] 0.08413333 0.06933333 0.04080000 0.06813333 0.05680000 0.04226667
## [91] 0.09626667 0.04320000 0.05493333 0.05760000 0.06920000 0.05426667
## [97] 0.05600000 0.05573333 0.06786667 0.05413333
##
## attr(,"class")
## [1] "rbga"
Results Visualization and Conclusion:
Visualize the progress and results of the Genetic Algorithm to evaluate its performance in selecting the original Iris dataset variables from the noise.
# Plot the evolution of the fitness values
plot(GA_iris)
This module provides insights into using Genetic Algorithms for feature selection in complex datasets. The ability of GAs to identify relevant features among numerous irrelevant ones demonstrates their power in applications requiring robust feature selection methodologies.