vic
vic

Reputation: 400

Genetic Algorithm in R: Specify number of 1s in binary chromosomes

I am using the rbga function, but my question still stands for other genetic algorithm implementations in R. Is there a way to specify the number of 1s in binary chromosomes?

I have the following example provided by the library documentation.

data(iris)
library(MASS)
X <- as.data.frame(cbind(scale(iris[,1:4]), matrix(rnorm(36*150), 150, 36)))
Y <- iris[,5]

iris.evaluate <- function(indices) {
    print("Chromosome")
    print(indices)
    print("================================")
    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
}

monitor <- function(obj) {
    minEval = min(obj$evaluations);
    plot(obj, type="hist");
}

woppa <- rbga.bin(size=40, mutationChance=0.05, zeroToOneRatio=10,
                  evalFunc=iris.evaluate, showSettings=TRUE, verbose=TRUE)

Here are some of the chromosomes.

"Chromosome"
 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 1 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0
"================================"
"Chromosome"
 0 0 1 1 0 0 1 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 1 0 0 1 0 0 0 0 0 1 0 0 0
"================================"
"Chromosome"
 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 1 0 0 1 0 0 0 0 0 0 0 0
"================================"
"Chromosome"
 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0
"================================"

The 1s (i.e., the chosen characteristics) are 5, 8, 5 and 4 respectively.

I am trying to follow the technique specified in a paper and they claim that they apply a genetic algorithm and in the end they pick a specific number of characteristics.

  1. Is it possible to specify in a genetic algorithm the number of characteristics that I want my solution(s)/chromosome(s) to have?

  2. Could this be done on the final solution/chromosome and if yes how?

Upvotes: 1

Views: 86

Answers (0)

Related Questions