devtools::load_all("..")
source("defs.R")
source("../R/operator.EAMutatorKBitflip.R")
EA1p1BaseR = function(fun, mu, lambda, max.iter) {
#BBmisc::catf("Starting 0th generation")
X = sample(c(0L, 1L), size = DIM * MU, replace = TRUE)
X = matrix(X, ncol = MU)
FX = apply(X, 2L, fun)
XY = matrix(0, nrow = DIM, ncol = MU + LAMBDA)
#mut = EAMutatorKBitflip$new(list(k = 3L))$apply
mut = function(ind, k = 3L) {
#k = #BBmisc::coalesce(k, self$params$k)
n = length(ind)
#checkmate::assertInt(k, lower = 1L, upper = n)
mut.idx = sample(seq_len(n), size = k, replace = FALSE)
ind[mut.idx] = 1 - ind[mut.idx]
return(ind)
}
#BBmisc::catf("Starting first generation ...")
for (iter in seq_len(MAX.ITER)) {
# generate offspring
#BBmisc::catf("Building offspring")
POOL = sample(1:MU, size = LAMBDA, replace = TRUE)
Y = X[, POOL, drop = FALSE]
Y = apply(Y, 2L, mut)
FY = apply(Y, 2L, fun)
# print(FY)
# stop()
XY[1:DIM, 1:MU] = X
XY[1:DIM, (MU+1):(MU+LAMBDA)] = Y
FXY = c(FX, FY)
#BBmisc::catf("Survival selection")
do.survive = order(FXY, decreasing = FALSE)[1:MU]
X = XY[, do.survive, drop = FALSE]
FX = FXY[do.survive]
}
return(list(y.x.best = min(FX), x.best = X[, which.min(FX), drop = TRUE]))
}
st = system.time({
res = EA1p1BaseR(fun, MU, LAMBDA, MAX.ITER)
})
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.