[R] GA optimization in two dimensions

email email8889 at gmail.com
Fri Jan 10 08:09:16 CET 2014


Hi:

I am trying to implement a bandwidth reduction algorithm for a (M x
N) binary matrix using the GA package in R.

I am using the folloging code to get the optimal permutation for rows
and columns (optimization in two dimensions).

M <- matrix(c(0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0,
1),nrow=5, ncol=4)

BW <- function(patt, origMatrix) {
    M1 <- origMatrix[patt[1], patt[2]]
    temp2 <- 0;
    temp3 <- 0;
    for(i in 1:nrow(M1))
    {
        for(j in 1:ncol(M1))
        {
            if(M1[i,j] > 0)
            {
                temp1 <- abs(i - j)
                temp2 <- append(temp2, temp1)
            }
        }
        temp3 <- append(temp3, max(temp2))
        temp2 <- 0
    }
    return(max(temp3))
}

bwFit <- function(patt, ...) 1/BW(patt[1], patt[2],...)

GA <- ga(type = "permutation", fitness = bwFit, origMatrix = M, min =
c(1,1), max = c(5,4), popSize = 100, maxiter = 5000, run = 500,
pmutation = 0.2)


I get this error message: Error in BW(patt[1], patt[2], ...) : unused
argument (patt[2])


In summary, I am unable to implement the optimization in two
dimensions. In the solution, I need two vectors containing the optimal row
and column permutations. Can you suggest a solution?

Regards:
John



More information about the R-help mailing list