Advertisement
Guest User

Untitled

a guest
Sep 16th, 2019
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 4.33 KB | None | 0 0
  1. # We depend on GenSA, as we want to run one of the algorithms from the paper.
  2.  
  3. if (!require(GenSA)) install.packages("GenSA")
  4. library(GenSA)
  5.  
  6. # This is a port of fitness.m / initialize.m, taken from http://staff.itee.uq.edu.au/marcusg/msg.html
  7. # Note that both files are a part of this function:
  8. # - initialize.m comes before the function to be returned,
  9. # - fitness.m comes in function(x) { ... }.
  10.  
  11. # Please also note that fitness is not signature-compatible with the Matlab version,
  12. # e.g. it accepts exactly one individual unlike Matlab which takes a vector of individuals,
  13. # the lower-upper arguments come in this order, and ratio control is more refined.
  14.  
  15. # Of course we don't use global variables anymore.
  16.  
  17. GaussianLandscapeGenerator <- function(dimension, nGaussian, lower, upper, globalValue, ratioMin, ratioMax, optVariance) {
  18.     if (dimension <= 1) {
  19.         stop(paste("Illegal value for argument 'dimension': shall be > 1, found", dimension))
  20.     }
  21.     if (nGaussian <= 0) {
  22.         stop(paste("Illegal value for argument 'nGaussian': shall be > 0, found", nGaussian))
  23.     }
  24.     if ((length(upper) != dimension) || (length(lower) != dimension)) {
  25.         stop(paste("'dimension' and dimensions of 'lower' and 'upper' do not match:", dimension, length(lower), length(upper)))
  26.     }
  27.     if (sum(upper <= lower) > 0) {
  28.         lower_txt <- paste(lower, collapse=" ")
  29.         upper_txt <- paste(upper, collapse=" ")
  30.         stop(paste("Illegal value for arguments 'upper' and 'lower': they shall be componentwise '>', found", upper_txt, "and", lower_txt))
  31.     }
  32.     if (globalValue <= 0) {
  33.         stop(paste("Illegal value for argument 'globalValue': shall be > 0, found", globalValue))
  34.     }
  35.     if (ratioMin <= 0 || ratioMin > ratioMax || ratioMax >= 1) {
  36.         stop(paste("Illegal value for arguments 'ratioMin', 'ratioMax': shall be 0 < ratioMin <= ratioMax < 1, found", ratioMin, ratioMax))
  37.     }
  38.  
  39.     rotation <- array(dim=c(dimension,dimension,nGaussian))
  40.     for (i in 1:nGaussian) {
  41.         vi <- diag(nrow = dimension)
  42.         for (j in 1:(dimension-1)) {
  43.             for (k in (j+1):dimension) {
  44.                 r <- diag(nrow = dimension)
  45.                 alpha <- runif(1) * pi / 2 - pi / 4
  46.                 r[j, j] <- cos(alpha)
  47.                 r[j, k] <- sin(alpha)
  48.                 r[k, j] <- -sin(alpha)
  49.                 r[k, k] <- cos(alpha)
  50.                 vi <- vi %*% r
  51.             }
  52.         }
  53.         rotation[,,i] <- vi
  54.     }
  55.  
  56.     varianceRange <- (upper - lower) / 20
  57.     variance <- matrix(runif(nGaussian * dimension), nrow = nGaussian, ncol = dimension) * varianceRange + 0.05
  58.     meanVector <- matrix(runif(nGaussian * dimension), nrow = nGaussian, ncol = dimension) * (upper - lower) + lower
  59.  
  60.     optimumValue <- runif(nGaussian) * globalValue * (ratioMax - ratioMin) + ratioMin
  61.  
  62.     variance[1,] <- optVariance
  63.     optimumValue[1] <- globalValue
  64.     meanVector[1,] <- meanVector[2,] + 1e-3
  65.  
  66.     covMatrixInv <- array(dim=c(dimension,dimension,nGaussian))
  67.     for (i in 1:nGaussian) {
  68.         covMatrix <- diag(variance[i,])
  69.         covMatrixInv[,,i] <- solve(t(rotation[,,i]) %*% covMatrix %*% rotation[,,i])
  70.     }
  71.  
  72.     list(fun = function(x) {
  73.         tmp <- array(0, dim = nGaussian)
  74.         for (i in 1:nGaussian) {
  75.             newx <- x - meanVector[i,]
  76.             z <- (newx %*% covMatrixInv[,,i]) * newx
  77.             tmp[i] <- t(sum(z))
  78.         }
  79.         -max(exp(-0.5 * tmp / dimension) * optimumValue)
  80.     }, optimum = meanVector[1,])
  81. }
  82.  
  83. dimension <- 2
  84.  
  85. lower <- sapply(1:dimension, function(i) {0})
  86. upper <- sapply(1:dimension, function(i) {1})
  87.  
  88. runOne <- function(test, name) {
  89.     function(i) {
  90.         result <- GenSA(fn = test, lower = lower, upper = upper)
  91.         message(paste("  ", name, "run", i, ":", result$value))
  92.         result$value
  93.     }
  94. }
  95.  
  96. measurements <- 1:6
  97.  
  98. for (attempt in 1:10) {
  99.     test <- GaussianLandscapeGenerator(dimension, 10, lower, upper, 1, 0.5, 0.5 + 1e-9, 1e-7)
  100.     message(paste("Attempt", attempt))
  101.     lhsI <- sapply(measurements, function(i) {1})
  102.     rhsI <- sapply(measurements, function(i) {2})
  103.     lhsV <- sapply(1:6, runOne(test$fun, "GenSA v1"))
  104.     rhsV <- sapply(1:6, runOne(test$fun, "GenSA v2"))
  105.     df <- data.frame(algo <- c(lhsI, rhsI), value <- c(lhsV, rhsV))
  106.     print(summary(aov(value ~ algo, data = df)))
  107.     message("")
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement