您可以尝试使用GA 包:
在手册(第 5 页)中,有一个示例。
ga(type = c("binary", "real-valued", "permutation"),
fitness, ...,
min, max, nBits,
population = gaControl(type)$population,
selection = gaControl(type)$selection,
crossover = gaControl(type)$crossover,
mutation = gaControl(type)$mutation,
popSize = 50,
pcrossover = 0.8,
pmutation = 0.1,
elitism = base::max(1, round(popSize*0.05)),
updatePop = FALSE,
postFitness = NULL,
maxiter = 100,
run = maxiter,
maxFitness = Inf,
names = NULL,
suggestions = NULL,
optim = FALSE,
optimArgs = list(method = "L-BFGS-B",
poptim = 0.05,
pressel = 0.5,
control = list(fnscale = -1, maxit = 100)),
keepBest = FALSE,
parallel = FALSE,
monitor = if(interactive())
{ if(is.RStudio()) gaMonitor else gaMonitor2 }
else FALSE,
seed = NULL)
例如,填充、选择、交叉、变异和监控算子分配新功能。在我的研究中,我使用了自己的突变和监控功能。例如;
myga <- ga(type = "binary",
fitness, ...,
min, max, nBits,
mutation = myMutationFunction
popSize = 50,
pcrossover = 0.8,
pmutation = 0.1,
maxiter = 100,
run = maxiter,
monitor = myMonitorFunction
myMutationFunction <- function (x) {
#...
}
myMonitorFunction <- function (x) {
#...
}
因此,您只需定义自己的函数并将函数名称赋予 ga 函数。为了便于参考,您可以查看默认功能。您可以在默认函数中看到必要的参数和返回值。