0

Currently, I'm working with Genetic Algorithms and I have the following code which produces one graph per iteration of my algorithm optimising a fitness function:

popSize <- 4
e<-5
d<-10
cromSize <- e+d
potDec<-2^d
fitness.f <- function(x){
abs((x-5)/(2+sin(x)))
}
eval <- function(x){
evaluation <- matrix(ncol = 2, nrow = nrow(x), rep(0,times = nrow(x)), dimnames = list(paste("I", c(1:nrow(x)), sep = ""), c("X", "Fit")))
for(i in 1:nrow(x)){
    count <- 0
    for(j in 1:cromSize){
        count <- x[i,j]*(2^(cromSize-j)) + count
    }
    count<-count/potDec
    evaluation[i,1] <- count
    evaluation[i,2] <- fitness.f(count)
}
evaluation <- cbind(evaluation, x)
evaluation <- evaluation[order(evaluation[,2], decreasing = T),]
return(evaluation)
}
cross.f <- function(r, pcrossover){
if(runif(1)<=pcrossover){
    hijos <- matrix(ncol = cromSize, nrow = 2, dimnames = list(paste("I",c((nrow(r)+1):(nrow(r)+2)), sep = ""),c()))
    c <- r[sample(1:popSize,2),]
    tam_cross <- sample(1:(cromSize-1),1)
    for(i in 1:2){
        for(j in 1:(cromSize-tam_cross)){
            hijos[i,j] <- c[i,j] 
        }
        for(k in (cromSize-tam_cross+1):cromSize){
            if(i==1){
                hijos[i,k] <- c[i+1,k]  
            }else{
                hijos[i,k] <- c[i-1,k]
            }
        }
    }
    r <- rbind(r,hijos)
}
return(r)
}
mut.f <- function(r, pmutation){
c <- matrix(ncol = cromSize, nrow = 1, r[sample(1:popSize,1),], dimnames = list(paste("I",(nrow(r)+1), sep = ""),c()))
vecAle <- c(runif(cromSize))
count <- 0
for(j in 1:cromSize){
    if(vecAle[j]<=pmutation){
        count <- count+1
        c[1,j]=abs(c[1,j]-1)
    }
}
if(count>0){
    r <- rbind(r,c)
}
return(r)
}
selection.f <- function(s){
fit <- eval(s)
aux <- fit[1:popSize,3:(3+cromSize-1)]
row.names(aux) <- paste("I", c(1:popSize), sep = "")
return(aux)
}
main.f <- function(maxIter){
min <- -0
max<-(2^e)+1
x <- c(0,1) 
s <- matrix(ncol = cromSize, nrow = popSize, sample(x, cromSize*popSize, replace = T) , dimnames = list(paste("I", c(1:popSize), sep = ""), paste("C", c(1:cromSize), sep = "")))
layout(matrix(c(1,2), 2, 1, byrow = TRUE))
par(mai = c(.7,.7,.7,.4), mgp = c(1,1,0))
plot(fitness.f, xlim = c(min - 0.1, max - 1), main = "Iteración = 0", ylab = "Fit", yaxt = "n")
fit <- eval(s)
points(fit, col = "red")
plot(density(fit[,1]), main = "Densidad de población en la iteracion 0", xaxt = "n", yaxt = "n", xlab = "")
pcrossover <- 0.9
pmutation <- 0.3
  for (i in 1:maxIter){
    #debug(cross.f)
    s <- cross.f(s, pcrossover)
    s <- mut.f(s, pmutation)
    #debug(selection.f)
    s <- selection.f(s)
    fit <- eval(s)
    layout(matrix(c(1,2), 2, 1, byrow = TRUE))
    par(mai = c(.7,.7,.7,.4), mgp = c(1,1,0))
    plot(fitness.f, xlim = c(min - 0.1, max - 1), main = c(paste("Iteración =",i)), ylab = "Fit", yaxt = "n")
    points(fit, col = "red")
    poblacion <- density(fit[,1])
       }
}
main.f(20)

I'm new to the tidyverse and I'd like to know how can I change the plots inside the for loops into ggplot2 in order to make them more "tidy"?

4
  • Is a single plot being continually updated at each iteration or is a new plot being made? If the former, look at previous SO answers regarding this topic: stackoverflow.com/questions/44317502/… Commented May 3, 2018 at 23:00
  • 3
    Your code is way too complex. Please pare it down until you have a minimal example that contains the essence of your question. Commented May 3, 2018 at 23:48
  • 1
    If the only part you need help with is plotting, can you just post the data that you have as output, and we can help with the plot? Commented May 4, 2018 at 0:47
  • 2
    I don't think there's a function to "change the plots ... into ggplot2". Unfortunately, if you want to change a plotting mechanism from base-R to ggplot2, you need to learn it and do it from scratch. There are many tutorials, some might provide basic translations, but in general it's a new graphing system without a direct conversion mechanism. Commented May 4, 2018 at 1:09

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.