##############################################
#Example for maximization of the claw function
##############################################
claw <- function(xx) {
x <- xx[1]
y <- (0.46 * (dnorm(x, -1, 2/3) + dnorm(x, 1, 2/3)) +
(1/300) * (dnorm(x, -0.5, 0.01) + dnorm(x, -1,
0.01) + dnorm(x, -1.5, 0.01)) + (7/300) *
(dnorm(x, 0.5, 0.07) + dnorm(x, 1, 0.07) + dnorm(x,
1.5, 0.07)))
return(y)
}
#use MA-CMA-Chains
res.claw <- malschains(function(x) {-claw(x)}, lower=c(-3), upper=c(3),
maxEvals=50000, control=malschains.control(popsize=50,
istep=300, ls="cmaes", optimum=-5))
# \donttest{
#use only the CMA-ES local search
res.claw2 <- malschains(function(x) {-claw(x)}, lower=c(-3), upper=c(3), verbosity=0,
maxEvals=50000, control=malschains.control(ls="cmaes",
lsOnly=TRUE, optimum=-5))
#use only the Simplex local search
res.claw3 <- malschains(function(x) {-claw(x)}, lower=c(-3), upper=c(3), verbosity=0,
maxEvals=50000, control=malschains.control(ls="simplex",
lsOnly=TRUE, optimum=-5))
x <- seq(-3, 3,length=1000)
claw_x <- NULL
for (i in 1:length(x)) claw_x[i] <- claw(x[i])
plot(x,claw_x, type="l")
points(res.claw$sol, -res.claw$fitness, col="red")
points(res.claw2$sol, pch=3, -res.claw2$fitness, col="blue")
points(res.claw3$sol, pch=3, -res.claw3$fitness, col="green")
##############################################
#Example for the rastrigin function
##############################################
rastrigin <- function(x) {
dimension <- length(x)
res <- 0.0
for (i in 1:dimension) {
res <- res + (x[i]*x[i] - 10.0*cos(2.0*pi*x[i]) + 10.0)
}
res
}
res.rastrigin1 <- malschains(rastrigin, lower=seq(-1.0, -1.0, length=30),
upper=seq(1.0, 1.0, length=30), maxEvals=50000,
control=malschains.control(effort=0.8, alpha=0.3,
popsize=20, istep=100, ls="simplex"))
res.rastrigin2 <- malschains(rastrigin, lower=seq(-1.0, -1.0, length=30),
upper=seq(1.0, 1.0, length=30), maxEvals=50000,
initialpop = seq(0.1, 0.1, length=30),
control=malschains.control(popsize=50,
istep=300, ls="cmaes"))
res.rastrigin1
res.rastrigin2
# }
Run the code above in your browser using DataLab