desire.R

devtools::load_all()
lg$set_threshold("warn")

done = function(msg) {
  BBmisc::catf("%s %s", crayon::green$bold("DONE:"), msg)
}

failed = function(msg) {
  BBmisc::catf("%s %s", crayon::red$bold("FAILED:"), msg)
}

## EXAMPLE
## ===

dim = 100L
dim2 = dim
mu = 100L
lambda = 100L

# dim = 5L
# dim2 = 30L
# mu = 5L
# lambda = 2L
max.iter = ceiling(3 * dim2 * log(dim2))

# fitness.fn = smoof::makeDTLZ2Function(dimensions = 2L, n.objectives = 2L)
# fitness.fn2 = smoof::makeDTLZ1Function(dimensions = 2L, n.objectives = 2L)

# lower = getLowerBoxConstraints(fitness.fn)
# upper = getUpperBoxConstraints(fitness.fn)

# control = EAControl$new(fitness.fn, genotype = "real")$
#   register("generate", EAGeneratorReal(n = mu, d = 2L, lower = lower, upper = upper))$
#   register("mutate", EAMutatorPolynomial(lower = lower, upper = upper))$
#   #register("recombine", EARecombinatorSBX(lower = lower, upper = upper))$
#   register("selectForMating", EASelectorRandom())$
#   register("selectForSurvival", EASelectorNondominatedSortingII())

# stats = EAStatistics$new()$
#   register("fitness", group = "y", funs = list("min", "max", "sd"), obj = 1L)#$
#   #register("inds", group = "xlength", funs = c("min", "mean", "sd"), extractor = function(x) sapply(x, length))

# myEA = EA$new(control, pm = 1L, mu = mu, lambda = lambda, stats = stats, algo.name = "myEA", strategy = "plus", terminators = list(EATerminatorIterations(max.iter)))

# st = system.time({
# res = myEA$run(1L) # run x-times in parallel
# })
# print(plotLogbook(res$getLogbook(), y = "y.min"))
# print(plotScatter2d(res$getXY()))
# stop("test")

# #FIXME: now we pass the algorithms with fitness function attached!
# # - Add possibility to pass fitness functions
# # - Add possibility to pass parameters to fitness functions

# myEA2 = myEA$clone(deep = TRUE)
# myEA2$algo.name = "myEA-2"

# future::plan(future::multicore)
# collections = benchmark(algos = list(myEA, myEA2), funs = list(fitness.fn, fitness.fn2), repls = 2L)
# pl = plotScatter2d(collections, shape = "algorithm")
# print(pl)

# stop()

fitness.fn = function(x) {
  #Sys.sleep(0.05)
  sum(x)
}

# control object
control = EAControl$new(fitness.fn, n.objectives = 1L, minimize = TRUE, genotype = "binary")$
  register("generate", EAGeneratorBinary(n = mu, d = dim))$
  #register("mutate", EAMutatorKBitflip(k = 1))$
  register("mutate", EAMutatorBitflip(p = 1/dim))$
  #register("recombine", EARecombinatorOnePointCrossover$new())$
  #register("mutate", EAMutatorKBitflip$new(list(k = 5L)))$
  #register("mutate", mutIndependentBitflip, p = 0.9)$
  register("selectForMating", EASelectorTournament(k = 3L))$
  register("selectForSurvival", EASelectorGreedy())

control$check()

# statistics
stats = EAStatistics$new()$
  register("fitness", group = "y", funs = list("min", "max", "sd"))#$
  #register("inds", group = "age", funs = list("min", "max"), extractor = function(x) sapply(x, function(xx) attr(xx, "age")))
  #register("inds", group = "xlength", funs = c("min", "mean", "sd"), extractor = function(x) sapply(x, length))

myEA = EA$new(control, pm = 1, mu = mu, lambda = lambda, stats = stats, algo.name = "myEA", strategy = "plus", terminators = list(EATerminatorIterations(max.iter)))

# library("future.apply")
# future::plan(multicore)
st = system.time({
res = myEA$run(1L) # run x-times in parallel
})
print(plotLogbook(res$getLogbook(), y = "y.min"))

stop()

print(plotLogbook(res$getLogbook(), y = "y.min"))

# Return EA R6 object with control parameters set approriately
algorithm = EAGet(preset = "nsgaii", fitness.fun, genotype = "real",
  mu = 10, lambda = 10L,
  mutator = list(EAMyMutator$new(k = 10L), EAMySecondMutator$new(p = .03), probs = c(0.5, 0.3)),
  terminators = list(EATerminator$new(max.iter = 100L)))

res = algorithm$run(4L)
jakobbossek/ecr3 documentation built on Nov. 14, 2019, 7:47 p.m.