Skip to content

Instantly share code, notes, and snippets.

@SigurdJanson
Created May 4, 2020 19:18
Show Gist options
  • Save SigurdJanson/b7f1ee43cf4c3d5788aa26b4c828ebb4 to your computer and use it in GitHub Desktop.
Save SigurdJanson/b7f1ee43cf4c3d5788aa26b4c828ebb4 to your computer and use it in GitHub Desktop.
Testing Results of dfoptim::nmkb (version 2018.2-1)
library(dfoptim)
fopt <- "dfoptim"
# Choose objective function ----
rosbkext <- function(x) {
# Extended Rosenbrock function
n <- length(x)
sum (100*(x[1:(n-1)]^2 - x[2:n])^2 + (x[1:(n-1)] - 1)^2)
}
# moved rastrigin by 1 so that the global optimum is at rep(1, 6)
rastrigin <- function(x) 10*length(x) + sum((x-1)^2 - 10*cos(1*pi*(x-1)))
# EDIT HERE:
f <- rastrigin
fs <- "rastrigin"
# Activate output ----
con <- file(paste0("./test/", fs, "_", fopt, ".txt"))
sink(con, append=TRUE)
sink(con, append=TRUE, type="message")
# Run! ----
df <- data.frame()
np <- 6
loops <- 5E3
print(paste(fs, fopt, sep = "-"))
print(date())
for (box in c(0, 2, 4, 12, 24, 32, 64, 128)) {
#set.seed(123)
for(trial in 1:loops) {
p0 <- rnorm(np)
p0[p0 > +2] <- +2 - 1E-8
p0[p0 < 0] <- 0 + 1E-8
ctrl <- list(maxfeval = 5E4)
if(box == 0) {
o <- nmk(fn = f, par = p0, control = ctrl)
} else {
tryCatch( {
o <- nmkb(fn = f, par = p0, lower = 0, upper = +box, control = ctrl)
}, error = function(e) {
o <<- list(convergence = 666, value = NA, par = rep(0, np), message = e$message)
#print(paste("Crash:", e$message))
},
warning = function(e) print(paste("Warn:", e$message))
)
}
df <- rbind(df, list(box,
ifelse(o$convergence == 0, o$value < 1e-6, NA),
o$convergence,
ifelse(box == 0, FALSE, (any(o$par < 0) | any(o$par > box)))
))
}
}
# Wrap up ----
names(df) <- c("Box", "GlobalOpt", "ExitCode", "OOB")
df$Box <- as.factor(df$Box)
result <- table(df[1:2], useNA = "ifany")
colnames(result) <- c("Local", "Global", "NotConverged")
oob <- table(df[c("Box", "OOB")], dnn = c("OOB"))
if(ncol(oob) == 1) colnames(oob) <- "InBounds" else colnames(oob) <- c("InBounds", "OoB")
result <- cbind( result, oob )
crash <- table(df[c("Box", "ExitCode")], dnn = c("Error"))
x <- matrix(c("0", "Success", "1", "FEval", "2", "Stagnation", "666", "Error"), byrow = TRUE, ncol=2)
colnames(crash) <- x[match(colnames(crash), x[,1]),2]
result <- cbind( result, crash )
if (any(df$OOB >= .Machine$double.eps)) print("Out of bounds detected")
save(df, result, file=paste0("./test/", fs, "_", fopt, ".RData"))
warnings()
# Close output ----
sink()
sink(type="message")
Result of 5000 runs of the Nelder-Mead implementation in dfoptim using a slightly modified rastrigin function.
Box Local Global NotConverged InBounds Success Stagnation Error
0 3020 1980 0 5000 5000 0 0
2 3440 54 1506 5000 3494 1049 457
4 3643 18 1339 5000 3661 983 356
12 4036 45 919 5000 4081 913 6
24 4180 44 776 5000 4224 772 4
32 4263 37 700 5000 4300 594 106
64 4515 51 434 5000 4566 429 5
128 4650 50 300 5000 4700 293 7
All cells show counts. The "Box" column shows the size of the box constraint `x in (0, box)`.
* Local = result of optimisation was a local minimum
* Global = result was the global minimum
* NotConverged = algorithm did not converge at all
* InBounds = Results were inside the specified box constraint
* Success = algorithm terminated with success (exit code 0)
* Stagnation = reason for termination is "stagnation" (exit code 2)
* Error = function crashed with an error message "system is exactly singular"
Result of 5000 runs of the Nelder-Mead implementation in dfoptim using a rosenbrock function.
Box Local Global NotConverged InBounds Success Stagnation Error
0 332 4668 0 5000 5000 0 0
2 4569 40 391 5000 4609 362 29
4 4781 50 169 5000 4831 169 0
12 4913 32 55 5000 4945 54 1
24 4959 23 18 5000 4982 18 0
32 4965 26 9 5000 4991 9 0
64 4977 21 2 5000 4998 2 0
128 4982 18 0 5000 5000 0 0
All cells show counts. The "Box" column shows the size of the box constraint `x in (0, box)`.
* Local = result of optimisation was a local minimum
* Global = result was the global minimum
* NotConverged = algorithm did not converge at all
* InBounds = Results were inside the specified box constraint
* Success = algorithm terminated with success (exit code 0)
* Stagnation = reason for termination is "stagnation" (exit code 2)
* Error = function crashed with an error message "system is exactly singular"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment