Error: Numerical expression has 6 elements: only the first used and Warning in GA
Error: Numerical expression has 6 elements: only the first used and Warning in GA
这是我的代码,我想知道为什么会有警告消息说 "number of items to replace is not a multiple of replacement length"?
library(GA)
library(readxl)
> data1 <-
read_excel("C:/Users/nadiahalim/OneDrive/CS954/Scheduling/data1.xlsx")
> View(data1)
> data1
data1 的输出
# A tibble: 6 x 4
Jobj Pj Dj Wj
<dbl> <dbl> <dbl> <dbl>
1 1 25 61 3
2 2 7 102 7
3 3 42 86 1
4 4 36 44 3
5 5 18 150 1
6 6 29 134 4
我想对 Jobj 进行重新排序,使其给出由 Cj 表示的最短完成时间。 For循环已在我的代码中使用如下:
Cj=0
i<- sample(data1$Jobj)
for(i in 1:i){
fitness <-function(j){
for(j in data1$Jobj){
Cj[j] <- data1$Pj[j]+sum(Cj[j-1])
}
print(Cj)
}
fitness()
此代码的输出
numerical expression has 6 elements: only the first used[1] 25 32 74 110 128 157
[1] 25 32 74 110 128 157
[1] 25 32 74 110 128 157
[1] 25 32 74 110 128 157
[1] 25 32 74 110 128 157
然后,我运行求GA码:
GA <- ga(type = "permutation", fitness = fitness, lower = 1, upper = 6, maxiter= 5, run = 20, optim = TRUE)
GA 的输出如下,警告指出要替换的项目数不是替换长度的倍数
number of items to replace is not a multiple of replacement length[1] 25 32 74 110 128 157
number of items to replace is not a multiple of replacement lengthGA | iter = 3 | Mean = 25 | Best = 25
[1] 25 32 74 110 128 157
number of items to replace is not a multiple of replacement length[1] 25 32 74 110 128 157
number of items to replace is not a multiple of replacement length[1] 25 32 74 110 128 157
然后我运行为了
summary(GA)
汇总 GA 的输出
-- Genetic Algorithm -------------------
GA settings:
Type = permutation
Population size = 50
Number of generations = 5
Elitism = 2
Crossover probability = 0.8
Mutation probability = 0.1
GA results:
Iterations = 5
Fitness function value = 25
Solutions =
x1 x2 x3 x4 x5 x6
[1,] 4 1 6 3 5 2
[2,] 6 3 1 5 4 2
[3,] 6 2 3 4 1 5
[4,] 3 6 4 1 5 2
[5,] 4 6 2 1 5 3
[6,] 5 3 4 1 2 6
[7,] 2 1 6 3 5 4
[8,] 4 1 6 3 2 5
[9,] 3 6 1 2 5 4
[10,] 3 6 2 1 5 4
...
[35,] 4 5 6 2 1 3
[36,] 3 4 5 6 2 1
当我 运行 进行摘要(GA)时,它似乎有效,但我对警告感到好奇 messages.I 如果有人能帮助我解决这个问题,我将不胜感激。提前谢谢你。
您在 fitness
函数中尝试执行的操作无需 for
循环即可轻松实现。
例如,您从 for
循环获得的输出是
#[1] 25 32 74 110 128 157
并使用 cumsum
我们得到同样的结果。
cumsum(data1$Pj)
#[1] 25 32 74 110 128 157
然而,主要问题是 fitness
函数应该 return 只有一个值,而不是像 cumsum
这样的值向量。所以如果你把函数改成这样
fitness <- function(j) sum(j)
你可以做到:
fitness(data1$Pj)
#[1] 157
library(GA)
GA <- ga(type = "permutation", fitness = fitness, lower = 1, upper = 6,
maxiter= 5, run = 20, optim = TRUE)
其中 return 没有警告。
这是我的代码,我想知道为什么会有警告消息说 "number of items to replace is not a multiple of replacement length"?
library(GA)
library(readxl)
> data1 <-
read_excel("C:/Users/nadiahalim/OneDrive/CS954/Scheduling/data1.xlsx")
> View(data1)
> data1
data1 的输出
# A tibble: 6 x 4
Jobj Pj Dj Wj
<dbl> <dbl> <dbl> <dbl>
1 1 25 61 3
2 2 7 102 7
3 3 42 86 1
4 4 36 44 3
5 5 18 150 1
6 6 29 134 4
我想对 Jobj 进行重新排序,使其给出由 Cj 表示的最短完成时间。 For循环已在我的代码中使用如下:
Cj=0
i<- sample(data1$Jobj)
for(i in 1:i){
fitness <-function(j){
for(j in data1$Jobj){
Cj[j] <- data1$Pj[j]+sum(Cj[j-1])
}
print(Cj)
}
fitness()
此代码的输出
numerical expression has 6 elements: only the first used[1] 25 32 74 110 128 157
[1] 25 32 74 110 128 157
[1] 25 32 74 110 128 157
[1] 25 32 74 110 128 157
[1] 25 32 74 110 128 157
然后,我运行求GA码:
GA <- ga(type = "permutation", fitness = fitness, lower = 1, upper = 6, maxiter= 5, run = 20, optim = TRUE)
GA 的输出如下,警告指出要替换的项目数不是替换长度的倍数
number of items to replace is not a multiple of replacement length[1] 25 32 74 110 128 157
number of items to replace is not a multiple of replacement lengthGA | iter = 3 | Mean = 25 | Best = 25
[1] 25 32 74 110 128 157
number of items to replace is not a multiple of replacement length[1] 25 32 74 110 128 157
number of items to replace is not a multiple of replacement length[1] 25 32 74 110 128 157
然后我运行为了
summary(GA)
汇总 GA 的输出
-- Genetic Algorithm -------------------
GA settings:
Type = permutation
Population size = 50
Number of generations = 5
Elitism = 2
Crossover probability = 0.8
Mutation probability = 0.1
GA results:
Iterations = 5
Fitness function value = 25
Solutions =
x1 x2 x3 x4 x5 x6
[1,] 4 1 6 3 5 2
[2,] 6 3 1 5 4 2
[3,] 6 2 3 4 1 5
[4,] 3 6 4 1 5 2
[5,] 4 6 2 1 5 3
[6,] 5 3 4 1 2 6
[7,] 2 1 6 3 5 4
[8,] 4 1 6 3 2 5
[9,] 3 6 1 2 5 4
[10,] 3 6 2 1 5 4
...
[35,] 4 5 6 2 1 3
[36,] 3 4 5 6 2 1
当我 运行 进行摘要(GA)时,它似乎有效,但我对警告感到好奇 messages.I 如果有人能帮助我解决这个问题,我将不胜感激。提前谢谢你。
您在 fitness
函数中尝试执行的操作无需 for
循环即可轻松实现。
例如,您从 for
循环获得的输出是
#[1] 25 32 74 110 128 157
并使用 cumsum
我们得到同样的结果。
cumsum(data1$Pj)
#[1] 25 32 74 110 128 157
然而,主要问题是 fitness
函数应该 return 只有一个值,而不是像 cumsum
这样的值向量。所以如果你把函数改成这样
fitness <- function(j) sum(j)
你可以做到:
fitness(data1$Pj)
#[1] 157
library(GA)
GA <- ga(type = "permutation", fitness = fitness, lower = 1, upper = 6,
maxiter= 5, run = 20, optim = TRUE)
其中 return 没有警告。