简体   繁体   English

如何根据 R 中的时间矩阵对点进行排序?

[英]How to sort points based on a time matrix in R?

I have a time matrix in R which I computed with the help of osrm package.我在 osrm package 的帮助下计算了osrm中的时间矩阵。 I want to sort the points based on the neighboring points.我想根据相邻点对点进行排序。 The sample data:样本数据:

name <- LETTERS[1:10]
lat  <- c(22.57, 22.69, 22.72, 22.50, 22.66, 22.19, 22.60, 22.27, 22.31, 22.15)
lon  <- c(88.69, 88.84, 88.77, 88.85, 88.63, 88.91, 88.54, 88.62, 88.78, 88.66)
demand <- c(30, 70, 75, 100, 45, 60, 135, 65, 55, 50)

df<-data.frame(name, lon, lat, demand)

computing the time matrix计算时间矩阵

library(osrm)


time<- osrmTable(df[,c('name', 'lon', 'lat')])
time_matrix<- time$durations

Now, I want a data frame something like this, based on the time matrix above.现在,我想要一个类似这样的数据框,基于上面的时间矩阵。

From To Time Demand
A    G  30.1 135
G    E  33.9 45
E    C  30.3 75

I can find the nearest point but I need to check whether the nearest point is included in the From column.我可以找到最近的点,但我需要检查最近的点是否包含在 From 列中。 If it has been then the 2nd nearest point will be used and so on.如果是,则将使用第二个最近的点,依此类推。 Like G's nearest point is A here, but as it has been included already, so it will be E (the 2nd nearest point).就像这里 G 的最近点是 A,但由于它已经包含在内,所以它将是 E(第二个最近点)。 Similarly, it will go on until all the points have been included in the table.同样,它将 go 上,直到所有点都包含在表中。

Any idea how to do it?知道怎么做吗? Any help would mean a lot!任何帮助都意义重大!

The solution depends on the starting point (that we can assume to be the first point in the data) and how the following point is selected.解决方案取决于起点(我们可以假设它是数据中的第一个点)以及如何选择下一个点

Continuous path连续路径

The following point is the nearest neighbor:以下点是最近邻:

diag(time_matrix) <- NA

nearestpoints <- data.frame(matrix(ncol = 4, nrow = 0))
colnames(nearestpoints) <- c("From", "To", "Time", "Demand")

inputrowindex=1
outputrowindex=1
visitedpoints <- c(rownames(time_matrix)[1]) #The visited points are the 'To' points

while(length(setdiff(rownames(time_matrix), visitedpoints)) > 0){
  nearest <- which.min(time_matrix[inputrowindex,])
  if(length(nearest)==0) break
  
  nearestpoints[outputrowindex, 1] <- rownames(time_matrix)[inputrowindex]
  nearestpoints[outputrowindex, 2] <- names(nearest)
  nearestpoints[outputrowindex, 3] <- time_matrix[inputrowindex, nearest]
  nearestpoints[outputrowindex, 4] <- df[nearest, 4]
  
  time_matrix[inputrowindex,] <- NA
  time_matrix[,inputrowindex] <- NA
  
  visitedpoints <- c(visitedpoints, names(nearest))
  
  inputrowindex = as.numeric(nearest) #Next point is the nearest
  outputrowindex = outputrowindex + 1
}

Which gives:这使:

head(nearestpoints)
#  From To Time Demand
#1    A  G 30.1    135
#2    G  E 33.7     45
#3    E  C 30.3     75
#4    C  B 11.4     70
#5    B  D 35.2    100
#6    D  I 56.5     55

Data ordered path数据有序路径

The following point is the next one in the data:以下是数据中的下一点:

diag(time_matrix) <- NA

nearestpoints <- data.frame(matrix(ncol = 4, nrow = 0))
colnames(nearestpoints) <- c("From", "To", "Time", "Demand")

inputrowindex=1
outputrowindex=1

visitedpoints <- c() #The visited points are the 'From' points

while(length(setdiff(rownames(time_matrix), visitedpoints)) > 0){
  nearest <- which.min(time_matrix[inputrowindex,])
  if(length(nearest)==0) break
  
  nearestpoints[outputrowindex, 1] <- rownames(time_matrix)[inputrowindex]
  nearestpoints[outputrowindex, 2] <- names(nearest)
  nearestpoints[outputrowindex, 3] <- time_matrix[inputrowindex, nearest]
  nearestpoints[outputrowindex, 4] <- df[nearest, 4]
  
  time_matrix[inputrowindex,] <- NA
  time_matrix[,inputrowindex] <- NA
  
  visitedpoints <- c(visitedpoints,  rownames(time_matrix)[inputrowindex])
  
  inputrowindex = inputrowindex + 1 #Next point in the data
  outputrowindex = outputrowindex + 1
}

Which gives:这使:

head(nearestpoints)
#  From To  Time Demand
#1    A  G  30.1    135
#2    B  C  11.4     75
#3    C  E  30.3     45
#4    D  I  56.5     55
#5    E  G  33.9    135
#6    F  H 118.1     65

Raw data:原始数据:

name <- LETTERS[1:10]
lat  <- c(22.57, 22.69, 22.72, 22.50, 22.66, 22.19, 22.60, 22.27, 22.31, 22.15)
lon  <- c(88.69, 88.84, 88.77, 88.85, 88.63, 88.91, 88.54, 88.62, 88.78, 88.66)
demand <- c(30, 70, 75, 100, 45, 60, 135, 65, 55, 50)
df <- data.frame(name, lat, lon, demand)

library(osrm)
time <- osrmTable(df[,c('name', 'lon', 'lat')])
time_matrix <- time$durations

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM