0

我试图最小化函数然后绘制它,但我得到负适应值,我不知道如何或为什么。我正在使用 GA 包,只要我向它发送一个适应度函数,它基本上就可以为我处理算法。(文档可以在这里找到。https://cran.r-project.org/web/packages/GA/vignettes/GA.html

# Objective function for I-beam cross section area
cross <- function(x1, x2, x3, x4) {
  2 * x2 * x4 + x3 * (x1 - 2 * x4) # Not possible to be negative
}

遗传算法的参数。

crossGA <- ga(
  type = "real-valued",
  fitness = function(x)
    -cross(x[1], x[2], x[3], x[4]),
  lower = c(10, 10, 0.9, 0.9), # Not possible to be negative
  upper = c(80, 50, 5, 5), # Not possible to be negative
  popSize = 50,
  maxiter = 100,
  run = 100,
  pcrossover = 0.75,
  pmutation = .001
)

输出。

GA | iter = 1 | Mean = -295.2791 | Best = -104.4886
GA | iter = 2 | Mean = -231.7914 | Best = -104.4886
GA | iter = 3 | Mean = -205.53407 | Best =  -82.49887
GA | iter = 4 | Mean = -199.11156 | Best =  -82.49887
GA | iter = 5 | Mean = -164.27739 | Best =  -82.49887
GA | iter = 6 | Mean = -138.23192 | Best =  -82.49887
GA | iter = 7 | Mean = -117.80066 | Best =  -72.36154
GA | iter = 8 | Mean = -107.05787 | Best =  -72.36154
GA | iter = 9 | Mean = -93.94951 | Best = -72.36154
GA | iter = 10 | Mean = -86.25630 | Best = -72.36154
4

1 回答 1

0

您将适应度函数定义为-cross(...),它否定了该值,从而使其为负数。换句话说,它看起来像是你的代码中的一个错字。

于 2019-04-17T01:27:19.407 回答