Example code for talk at GECCO 2017: Simulation-based Test Functions for Optimization Algorithms

You can find the corresponding article in the publications section of this website, and the slides in the presentation section.

The R-code below can be used to reproduce the simple example illustrated in the slides and in the article.


# Introductory comments:
#
# To run this script, you need to install R, the language for statistical computing.
# https://cran.r-project.org/
# For ease of use, you may consider RStudio IDE, but this is not required.
# https://www.rstudio.com/
# For a tutorial/introduction to R, see e.g.:
# https://cran.r-project.org/doc/manuals/r-release/R-intro.html

# Note: the following line loads the package CEGO
# If it is not installed, please do so first, using: 
# install.packages("CEGO")
# Please note, that you need the most recent version (2.2.0) 
# of the package CEGO.
require(CEGO)

nsim <- 10 # number of simulations / realizations
seed <- 12121212 # RNG seed
set.seed(seed)
m <- 100 # number of samples to be simulated at

# objective  function:
fun <- function(x){
  exp(-20* x) + sin(6*x^2) + x
}
# "vectorize" target
f <- function(x){sapply(x,fun)}

# distance function (for model/kernel)
dF <- function(x,y)(sum((x-y)^2))

# plot parameters
par(mfrow=c(3,1),mar=c(2.3,2.5,0.2,0.2),mgp=c(1.4,0.5,0))

# create test samples for plots
xtest <- as.list(seq(from=-0,by=0.005,to=1))
plot(xtest,f(xtest),type="l",lty=2,xlab="x",
				ylab="Estimation",ylim=c(-0.5,1.7))

# evaluation samples (training data)
xb <- c(0.1,0.4,0.54,0.6,0.8,0.99)
yb <- f(xb)

# create support samples for simulation
x <- as.list(sort(c(runif(m-length(xb)),unlist(xb))))

# fit the model	
fit <- modelKriging(xb,yb,
		control=list(distanceFunction=dF,
                algThetaControl=
                list(method="NLOPT_GN_DIRECT_L",
                     funEvals=100),useLambda=F))
fit

# predicted obj. function values
ypred <- predict(fit,as.list(xtest))$y
lines(unlist(xtest),ypred)
points(unlist(xb),yb,pch=19)

##############################	
# create test functions with non conditional sim.
##############################

fun <- createSimulatedTestFunction(x,fit,nsim,F,seed=1)

ynew <- NULL
for(i in 1:nsim)
  ynew <- cbind(ynew,fun[[i]](xtest))

rangeY <- range(ynew)

plot(unlist(xtest),ynew[,1],type="l",
     xlab="x",ylab="Non-conditional",ylim=c(-0.5,1.7))
for(i in 2:nsim){
  lines(unlist(xtest),ynew[,i],col=i,type="l")
}

##############################	
# create test functions with conditional sim.
##############################

fun <- createSimulatedTestFunction(x,fit,nsim,T,seed=1)

ynew <- NULL
for(i in 1:nsim)
  ynew <- cbind(ynew,fun[[i]](xtest))

rangeY <- range(ynew)

plot(unlist(xtest),ynew[,1],type="l",
     xlab="x",ylab="Conditional",ylim=c(-0.5,1.7))
for(i in 2:nsim){
  lines(unlist(xtest),ynew[,i],col=i,type="l")
}
points(unlist(xb),yb,pch=19)