mercredi 19 janvier 2022

R: Comparing the Time for Random Searches

I am using the R programming language.

I am trying to optimize the Rastrign Function using both Random Search With Replacement and Random Search Without Replacement:

Rastrigin <- function(x1, x2)
{
  20 + x1^2 + x2^2 - 10*(cos(2*pi*x1) + cos(2*pi*x2))
}

x1 <- x2 <- seq(-5.12, 5.12, by = 0.1)
f <- outer(x1, x2, Rastrigin)

enter image description here

Part 1 : Random Search With Replacement:

To sample with replacement, I generated 100 points (I converted these points to integer format so that it's easier to see which points are duplicates for Part 2), and then evaluated the function at these 100 points. I then extracted the row of data corresponding to the smallest value of the function. I also recorded the time taken:

start.time <- Sys.time()

x1 = as.integer(rnorm(100,5,5))
x2 = as.integer(rnorm(100,5,5))
func_value = 20 + x1^2 + x2^2 - 10*(cos(2*pi*x1) + cos(2*pi*x2))

frame = data.frame(x1,x2,func_value)

sort_frame <- frame[order(func_value),]

final_answer = sort_frame[1,]

end.time <- Sys.time()
time.taken <- end.time - start.time

time.taken

Time difference of 0.01810408 secs

Part 2: Random Search Without Replacement:

Since I do not know how to write the code for the traditional way to do Random Search Without Replacement (i.e. generate random value, see if this generated value has already occurred : if not then evaluate function at this value, else generate new value until a unseen value is generated) - since I want to evaluate the function at 100 values, I decided to generate 200 values in hopes that at least 100 values in these 200 values will be unique. Then, I repeated the above procedure:

start.time <- Sys.time()

x1 = as.integer(rnorm(200,5,5))
x2 = as.integer(rnorm(200,5,5))

frame = data.frame(x1,x2)
de_duplicated = frame[!(duplicated(frame) | duplicated(frame, fromLast = TRUE)), ]

#check to make sure at least 100 values in "de_duplicated"

ifelse(nrow(de_duplicated)<100, "BAD", "GOOD")

[1] "GOOD"

#only keep 100 values in de_duplicated:

de_duplicated = de_duplicated[1:100,]

func_value = 20 + de_duplicated$x1^2 + de_duplicated$x2^2 - 10*(cos(2*pi*de_duplicated$x1) + cos(2*pi*de_duplicated$x2))

final_frame = data.frame(de_duplicated$x1,de_duplicated$x2,func_value)

sort_frame <- final_frame[order(func_value),]

final_answer = sort_frame[1,]

end.time <- Sys.time()
time.taken <- end.time - start.time

time.taken

Time difference of 0.03689885 secs

My Question: Since I am interested in comparing the times for Part 1 and Part 2 - I don't think it is fair, since I have written the code for Part 2 in a very "clumsy" way.

If you were to write the code for Part 2 in a more "traditional" way, i.e. with "while loops" - would this reduce the time required for Part 2? Can someone please show me how to do this - is writing Random Search Without Replacement (i.e. Part 2) using "while loops" in fact the traditional way to do this?

Thanks!




Aucun commentaire:

Enregistrer un commentaire