选择mlr遗传算法的初始种群

时间:2019-05-17 09:47:45

标签: r genetic-algorithm mlr

我想从mlr包中使用makeFeatSelControlGA()函数选择用于选择变量的遗传算法的初始种群。 可以吗?


编辑

由于未实现,为此,我对 selectFeaturesGA 函数进行了肮脏的编辑。这是代码,如果可以帮助任何人。

library(R.utils)
reassignInPackage("selectFeaturesGA","mlr",function (learner, task, resampling, measures, bit.names, bits.to.features, 
                                                     control, opt.path, show.info) 
{
  states=get("states",envir=.GlobalEnv)
  mu = control$extra.args$mu
  lambda = control$extra.args$lambda
  yname = opt.path$y.names[1]
  minimize = opt.path$minimize[1]

  if (!length(states))
  {
    for (i in seq_len(mu)) {
      while (TRUE) {
        states[[i]] = rbinom(length(bit.names), 1, 0.5)
        if (is.na(control$max.features) || sum(states[[i]] <= 
                                               control$max.features)) 
          break
      }
    }
  }

  evalOptimizationStatesFeatSel(learner, task, resampling, 
                                measures, bits.to.features, control, opt.path, show.info, 
                                states, 0L, NA_integer_)
  pop.inds = seq_len(mu)
  for (i in seq_len(control$maxit)) {
    pop.df = as.data.frame(opt.path)[pop.inds, , drop = FALSE]
    pop.featmat = as.matrix(pop.df[, bit.names, drop = FALSE])
    mode(pop.featmat) = "integer"
    pop.y = pop.df[, yname]
    kids.list = replicate(lambda, generateKid(pop.featmat, 
                                              control), simplify = FALSE)
    kids.evals = evalOptimizationStatesFeatSel(learner, task, 
                                               resampling, measures, bits.to.features, control, 
                                               opt.path, show.info, states = kids.list, i, as.integer(NA))
    kids.y = extractSubList(kids.evals, c("y", yname))
    oplen = getOptPathLength(opt.path)
    kids.inds = seq(oplen - lambda + 1, oplen)
    if (control$extra.args$comma) {
      setOptPathElEOL(opt.path, pop.inds, i - 1)
      pool.inds = kids.inds
      pool.y = kids.y
    }
    else {
      pool.inds = c(pop.inds, kids.inds)
      pool.y = c(pop.y, kids.y)
    }
    pop.inds = pool.inds[order(pool.y, decreasing = !minimize)[seq_len(mu)]]
    setOptPathElEOL(opt.path, setdiff(pool.inds, pop.inds), 
                    i)
  }
  makeFeatSelResultFromOptPath(learner, measures, resampling, 
                               control, opt.path)
})

在全球环境中,您需要添加一个名为 states 的列表,其长度等于 mu

1 个答案:

答案 0 :(得分:1)

mlr当前不支持此功能,因此您必须修改源代码才能实现。