r - 我如何处理'to'必须是长度为1的错误?
问题描述
我正在尝试使用 GA 算法来计算客户端之间的最短距离,
下面是我的代码
file1<-read.xlsx("distance_matrix_v1.xlsx",sheetName = "Sheet1",header = T)
str(file1)
file1$Place<-as.character(file1$Place)
newdf<- file1 %>% column_to_rownames("Place")
str(newdf)
class(newdf)
D2 <- as.matrix(newdf)
D2
#Function to calculate tour length
tourLength <- function(tour, distMatrix) {
tour <- c(tour, tour[1])
route <- embed(tour, 2)[,2:1]
sum(distMatrix[route])
}
#Firness function to be maximized
tspFitness <- function(tour, ...) 1/tourLength(tour, ...)
GA <- ga(type = "permutation", fitness = tspFitness, distMatrix = D2,
min = 1, max = attr(newdf, "Size"), popSize = 50, maxiter = 5000,
run = 500, pmutation = 0.2)
summary(GA)
这是我的矩阵
> D2
A B C D E F G H I
A 0 10 2 2 8 1 17 8 6
B 10 0 8 8 1 7 9 2 2
C 2 8 0 1 11 2 17 8 7
D 2 8 1 0 9 8 17 8 8
E 8 1 11 9 0 4 10 1 2
F 1 7 2 8 4 0 12 4 3
G 17 9 17 17 10 12 0 9 10
H 8 2 8 8 1 4 9 0 2
I 6 2 7 8 2 3 10 2 0
错误发生在这部分
GA <- ga(type = "permutation", Fitness = tspFitness, distMatrix = D2, + min = 1, max = attr(newdf, "Size"), popSize = 50, maxiter = 5000, + run = 500, pmutation = 0.2) seq.int(lower, upper) 中的错误:'to' 的长度必须为 1 另外:警告消息:1:在 ga(type = "permutation",fitness = tspFitness,distMatrix = D2,:'min' arg 已弃用。请改用 'lower'。2:在 ga(type = "permutation", Fitness = tspFitness, distMatrix = D2, : 'max' arg 已弃用。改用 'upper'。
解决方案
推荐阅读
- php - 堆栈无法转换为 int
- python - Python Postgres - 如何为单列创建数组?
- c# - C#发出对泛型方法的调用
- excel - 带有VBA的excel表头公式
- spring - 调试 jHipster 应用程序
- c++ - 对类静态 constexpr 结构的未定义引用,g++ vs clang
- winapi - 有没有办法从 Win32 应用程序检测 Windows 10 中的焦点辅助(以前称为安静时间)的变化
- eclipse - eclipse ide中的空格(>>>>>>)和空格(......)有什么区别?
- sql - SQL Server 计数记录按月和位置按 4 周分组
- c# - 在 Windows server 2016、Window 10 上构建 .NET 4.0 项目