📄 d_2.r
字号:
### 读数据
rc<-read.csv("D_data.csv")
### 将数据写成矩阵
lab1<-paste("X", 1:5, sep="")
lab2<-paste("Z", 1:73, sep="")
labT<-c("D", lab1, lab2); labT
### 写矩阵
source("write_matrix.R")
dist<-write_matrix(rc, labT)
source("write_file2.R")
write_file2(labT, dist, file="dist2.ldt")
### Floyd 算法求任意两点的最短路
source("floyd.R")
dist<-floyd(dist)
dist
str<-c(
58, 59, 61, 62, 63, 64, 65, 66, 67, 68, 72, 73,
14, 15, 16,
52,53,
41,42,
29, 31, 33,
18
)
str<-c(
62, 16, 15, 14,
67, 66, 63, 18,
69, 68, 29, 31, 33,
72, 73, 42, 41,
61, 59, 58, 52, 53
)
lab2<-paste("Z", str, sep="")
labT<-c("D", lab1, lab2); labT
#### 计算小矩阵 ####################################
source("comput_small_matrix.R")
index<-comput_small_matrix(dist, labT)
D<-dist[index, index]
D
### 写文件, 为LINGO运算使用
source("write_file2.R")
write_file2(labT, D)
### 此时用LINGO软件计算最优生成树
#### 计算给定路线长度 ####################################
str<-c(60, 59, 58, 52); labT<-c("D",paste("Z", str, sep=""), "X5")
str<-c(61, 59, 62, 16, 15, 14); labT<-c("D",paste("Z", str, sep=""), "X1")
str<-c(67, 66, 63, 18); labT<-c("D",paste("Z", str, sep=""), "X2")
str<-c(69, 68, 29, 31,33); labT<-c("D",paste("Z", str, sep=""), "X3")
str<-c(72, 73, 42, 41); labT<-c("D",paste("Z", str, sep=""), "X4")
str<-c(71, 70, 35, 34,32, 33); labT<-c("D",paste("Z", str, sep=""), "X3")
index<-comput_small_matrix(dist, labT)
D<-dist[index, index];D
n<-length(index);n
L<-0
for (i in 1:(n-1))
L<-L+D[i, i+1]
L
#### 计算小圈的TSP ####################################
str1<-c(67, 66, 63, 18);
str2<-c(27, 64, 65)
labT<-c("D",paste("Z", str1, sep=""), "X2", paste("Z", str2, sep=""))
str1<-c(61, 59, 53, 52);
str2<-c(58, 60)
labT<-c("D",paste("Z", str1, sep=""), "X5", paste("Z", str2, sep=""))
str1<-c(72, 73, 42, 41);
str2<-c(36, 71)
labT<-c("D",paste("Z", str1, sep=""), "X4", paste("Z", str2, sep=""))
str1<-c(69, 70, 35, 34, 32, 30, 31, 28);
str2<-c(33, 29, 68)
labT<-c("D",paste("Z", str1, sep=""), "X3", paste("Z", str2, sep=""))
str1<-c(62, 16, 15, 14);
str2<-c(11, 10, 9, 8)
labT<-c("D",paste("Z", str1, sep=""), "X1", paste("Z", str2, sep=""))
str<-c(19:26); labT<-c("X2",paste("Z", str, sep=""));labT
str<-c(27:31,33);labT<-c("X3",paste("Z", str, sep=""));labT
str<-c(43,40,39,38,37);labT<-c("X4",paste("Z", str, sep=""));labT
str<-c(44:51);labT<-c("X5",paste("Z", str, sep=""));labT
str<-c(54:57);labT<-c("X5",paste("Z", str, sep=""));labT
str<-c(2:7);labT<-c("X1",paste("Z", str, sep=""));labT
str<-c(12,13,1);labT<-c("X1",paste("Z", str, sep=""));labT
str<-c(17,19); labT<-c("X2",paste("Z", str, sep=""));labT
str<-c(20:26); labT<-c("X2",paste("Z", str, sep=""));labT
index<-comput_small_matrix(dist, labT)
D<-dist[index, index];D
write_file2(labT, D)
#### 调用D1_TSP
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -