Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从R中的GPS坐标列表创建最短路径_R_Graph_Routes_Coordinates_Distance - Fatal编程技术网

从R中的GPS坐标列表创建最短路径

从R中的GPS坐标列表创建最短路径,r,graph,routes,coordinates,distance,R,Graph,Routes,Coordinates,Distance,我有一个GPS坐标列表作为2列(横向和纵向)数据帧 我想找到包含所有GPS坐标的最短路径,并按照最短路径的顺序用GPS坐标重新排序数据帧 路线将是非周期性的,因此从最后一个坐标到第一个坐标的距离是不相关的,因为它不需要回环 出于我在这里的目的,如果列表是基于2D空间中的点距离排序的,那么就可以简化算法 我看了一下igraph软件包,寻找可能使用的函数,但看不清它的头绪。我愿意接受任何最容易完成工作的事情。我是个笨蛋。谢谢。学习如何制作可复制的示例: 对于您的问题: > #grab some

我有一个GPS坐标列表作为2列(横向和纵向)数据帧

我想找到包含所有GPS坐标的最短路径,并按照最短路径的顺序用GPS坐标重新排序数据帧

路线将是非周期性的,因此从最后一个坐标到第一个坐标的距离是不相关的,因为它不需要回环

出于我在这里的目的,如果列表是基于2D空间中的点距离排序的,那么就可以简化算法


我看了一下igraph软件包,寻找可能使用的函数,但看不清它的头绪。我愿意接受任何最容易完成工作的事情。我是个笨蛋。谢谢。

学习如何制作可复制的示例:

对于您的问题:

> #grab some US city lat and long. I downloaded uscities.csv data from from simplemaps.com .  after downloading to your local folder.
> cities <- read.csv(".../uscities.csv") #where ever the file on your local machine
> #select 8
> cities <- cities[1:8,c("city","lat","lng")]
> cities
          city     lat       lng
1     New York 40.6943  -73.9249
2  Los Angeles 34.1139 -118.4068
3      Chicago 41.8373  -87.6862
4        Miami 25.7839  -80.2102
5       Dallas 32.7936  -96.7662
6 Philadelphia 40.0077  -75.1339
7      Houston 29.7863  -95.3889
8      Atlanta 33.7627  -84.4224
> 
> #create a distance matrix.  I used this answer https://stackoverflow.com/questions/45784094/geosphere-dplyr-create-matrix-of-distance-between-coordinates
> library(geosphere)
> # create a distance matrix
> m <- distm(cities[3:2], cities[3:2], fun = distVincentyEllipsoid)
> 
> # replace the diagonal with 0
> diag(m) <- 0
> 
> # bind the distance matrix to the dataframe
> cities = cbind.data.frame(cities, m)
> 
> #solve for shortest path 
> library(TSP)
> 
> tsp = TSP(m)
> tour = solve_TSP(tsp)
> 
> #tour length 
> tour_length(tour)
[1] 10220935
> 
> #permutation vector for shortest tour
> perm_vec = as.integer(tour)
> 
> #re-arrange cities data frame in order of city tour
> cities <- cities[perm_vec,]
> cities
          city     lat       lng         1       2         3         4         5         6         7         8
4        Miami 25.7839  -80.2102 1753118.6 3777773 1908488.7       0.0 1783521.9 1646629.9 1558870.8  973458.3
8      Atlanta 33.7627  -84.4224 1206599.8 3127449  940984.3  973458.3 1154246.7 1078687.4 1127668.8       0.0
7      Houston 29.7863  -95.3889 2288557.3 2223344 1505897.4 1558870.8  358282.5 2163138.9       0.0 1127668.8
5       Dallas 32.7936  -96.7662 2211900.8 2013528 1284933.7 1783521.9       0.0 2092251.3  358282.5 1154246.7
2  Los Angeles 34.1139 -118.4068 3962368.2       0 2814608.6 3777772.5 2013528.3 3865420.3 2223343.6 3127449.0
3      Chicago 41.8373  -87.6862 1158846.6 2814609       0.0 1908488.7 1284933.7 1075634.3 1505897.4  940984.3
1     New York 40.6943  -73.9249       0.0 3962368 1158846.6 1753118.6 2211900.8  127912.5 2288557.3 1206599.8
6 Philadelphia 40.0077  -75.1339  127912.5 3865420 1075634.3 1646629.9 2092251.3       0.0 2163138.9 1078687.4
#抓取一些美国城市的长线和长线。我从simplemaps.com下载了uscities.csv数据。下载到本地文件夹后。
>城市#选择8个
>城市
城市液化天然气
1纽约40.6943-73.9249
2洛杉矶34.1139-118.4068
3芝加哥41.8373-87.6862
4迈阿密25.7839-80.2102
5达拉斯32.7936-96.7662
6费城40.0077-75.1339
7休斯顿29.7863-95.3889
8亚特兰大33.7627-84.4224
> 
>#创建一个距离矩阵。我用了这个答案https://stackoverflow.com/questions/45784094/geosphere-dplyr-create-matrix-of-distance-between-coordinates
>图书馆(地球圈)
>#创建距离矩阵
>m
>#将对角线替换为0
>诊断(m)
>#将距离矩阵绑定到数据帧
>cities=cbind.data.frame(城市,m)
> 
>#求解最短路径
>图书馆(TSP)
> 
>tsp=tsp(m)
>tour=求解TSP(TSP)
> 
>#行程长度
>行程长度(行程)
[1] 10220935
> 
>#最短行程的置换向量
>perm_vec=as.integer(教程)
> 
>#按城市游览顺序重新排列城市数据框
>城市
城市lat液化天然气1 2 3 4 5 6 7 8
4迈阿密25.7839-80.2102 1753118.6 3777773 1908488.7 0.0 1783521.9 1646629.9 1558870.8 973458.3
8亚特兰大33.7627-84.4224 1206599.8 3127449 940984.3 973458.3 1154246.7 1078687.4 1127668.8 0.0
7休斯顿29.7863-95.3889 2288557.3 2223344 1505897.4 1558870.8 358282.5 2163138.9 0.0 1127668.8
5达拉斯32.7936-96.7662 2211900.8 2013528 1284933.7 1783521.9 0.0 2092251.3 358282.5 1154246.7
2洛杉矶34.1139-118.4068 3962368.2 0 2814608.6 3777772.5 2013528.3 3865420.3 2223343.6 3127449.0
3芝加哥41.8373-87.6862 1158846.6 2814609 0.0 1908488.7 1284933.7 1075634.3 1505897.4 940984.3
1纽约40.6943-73.9249 0.0 3962368 1158846.6 1753118.6 2211900.8 127912.5 2288557.3 1206599.8
6费城40.0077-75.1339 127912.5 3865420 1075634.3 1646629.9 2092251.3 0.0 2163138.9 1078687.4

Eric给出了一个很好的回答,他只忽略了询问者关于路线将是非循环路线的陈述,而
solve\u TSP()
搜索返回出发城市的循环路线。但这没什么大不了的;显示了将最短哈密顿路径问题(即,寻找最佳线性阶)转化为 借助
insert\u dummy()
的最短哈密顿循环问题:

我们可以看到,
tour_length(tour)
比周期性旅行的长度要小,减少的距离最大(Eric的例子中是洛杉矶到芝加哥)

可能也会引起兴趣

tsp = TSP(m)
tsp = insert_dummy(tsp) # add a dummy city to find a short Hamiltonian path
tour = solve_TSP(tsp)
path = cut_tour(tour, "dummy")  # cut the tour to get the path
print(cities[path,1:3]) # view the cities sorted along the path