## Monday, November 19, 2007

### An R-based genetic algorithm

During my PhD I wrote a simple but effective genetic algorithm package for R. Because there was a bug recently found, and there is interest in extending the functionality, I have set up a SourceForge project called genalg.

The package provides GA support for binary and real-value chromosomes (and integer chromosomes is something that will be added soon), and allows to use custom evaluation functions. Here is some example code:
# optimize two values to match pi and sqrt(50)evaluate <- function(string=c()) {    returnVal = NA;    if (length(string) == 2) {        returnVal = abs(string[1]-pi) + abs(string[2]-sqrt(50));    } else {        stop("Expecting a chromosome of length 2!");    }    returnVal}monitor <- function(obj) {    # plot the population    xlim = c(obj$stringMin[1], obj$stringMax[1]);    ylim = c(obj$stringMin[2], obj$stringMax[2]);    plot(obj\$population, xlim=xlim, ylim=ylim, xlab="pi", ylab="sqrt(50)");}rbga.results = rbga(c(1, 1), c(5, 10), monitorFunc=monitor,    evalFunc=evaluate, verbose=TRUE, mutationChance=0.01)plot(rbga.results)plot(rbga.results, type="hist")plot(rbga.results, type="vars")

#### 8 comments:

1. What is the bug you are talking about and where can I find more info about it? I plan to use genalg in my next project.

2. Thanks for the great genalg package. Are you going to update it to allow "integer chromosomes"?
I would be very happy about it!

Greetz from Germany,
Michael

3. Thanks for the great genalg package. Are you going to update it to allow "integer chromosomes"?
I would be very happy about it!

Greetz from Germany,
Michael

1. Hi Micha, yeah, there is need for that. There is initial integer chromosome support, as I have used that myself, but I am not sure when I will have time to work on this package again :(

4. Hi Egon, I noticed you have not written the integer version yet. I made some simple changes to your binary algorithm to make it work for integers. I can share it with you if you would like. I also changed the way the mutation was being done to make it a bit faster. I may write a short blog post on it soon.

I also have a question. Why does your function try to avoid having the chromosome be all zeros?

1. That makes you the third in about a year to work on it! Please send me an email (first.lastname at gmail) and then I can put you into contact with the others.

The bias away from a full-zero chromosome is there because I was doing feature selection, where no selected features can be skipped without calculating fitness.

5. For posterity, here is the link to the implementation of the genetic algorithm for integers. It also includes a simple example at the end.

https://docs.google.com/open?id=0B21DLJKq18K9akRxT0tKMFQxcTg

6. great example!
this was exactly what i was looking for!
thanks