2017-09-20 310 views
2

想象一下xy坐标的小数据集。这些点由一个名为indexR的变量组成,共有3组。所有的xy坐标都是相同的单位。数据看起来大致像这样:找到多个点之间的最短距离

# A tibble: 61 x 3 
    indexR  x  y 
    <dbl> <dbl> <dbl> 
1  1 837 924 
2  1 464 661 
3  1 838 132 
4  1 245 882 
5  1 1161 604 
6  1 1185 504 
7  1 853 870 
8  1 1048 859 
9  1 1044 514 
10  1 141 938 
# ... with 51 more rows 

的目标是确定哪些3点,从每个组,是彼此最接近,在最小化所选择点之间的成对距离的总和的感觉。

我试图通过考虑欧几里德距离,如下所示。 (幸得@Mouad_S,在这个线程,和https://gis.stackexchange.com/questions/233373/distance-between-coordinates-in-r

#dput provided at bottom of this post 
> df$dummy = 1 
> df %>% 
+ full_join(df, c("dummy" = "dummy")) %>% 
+ full_join(df, c("dummy" = "dummy")) %>% 
+ filter(indexR.x != indexR.y & indexR.x != indexR & indexR.y != indexR) %>% 
+ mutate(dist = 
+   ((.$x - .$x.x)^2 + (.$y- .$y.x)^2)^.5 + 
+   ((.$x - .$x.y)^2 + (.$y- .$y.y)^2)^.5 + 
+   ((.$x.x - .$x.y)^2 + (.$y.x- .$y.y)^2)^.5, 
+   dist = round(dist, digits = 0)) %>% 
+ arrange(dist) %>% 
+ filter(dist == min(dist)) 
# A tibble: 6 x 11 
    indexR.x x.x y.x dummy indexR.y x.y y.y indexR  x  y dist 
    <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> 
1  1 638 324  1  2 592 250  3 442 513 664 
2  1 638 324  1  3 442 513  2 592 250 664 
3  2 592 250  1  1 638 324  3 442 513 664 
4  2 592 250  1  3 442 513  1 638 324 664 
5  3 442 513  1  1 638 324  2 592 250 664 
6  3 442 513  1  2 592 250  1 638 324 664 

由此我们可以识别最接近的三个点一起(最小距离隔开;放大在下面的图)。然而,当扩展这个指数R有4,5 ... n个组时,挑战就来了。问题在于找到一个更实用或最优化的方法来进行此计算。

enter image description here

structure(list(indexR = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 
2, 2, 2, 2, 2, 3, 3), x = c(836.65, 464.43, 838.12, 244.68, 1160.86, 
1184.52, 853.4, 1047.96, 1044.2, 141.06, 561.01, 1110.74, 123.4, 
1087.24, 827.83, 100.86, 140.07, 306.5, 267.83, 1118.61, 155.04, 
299.52, 543.5, 782.25, 737.1, 1132.14, 659.48, 871.78, 1035.33, 
867.81, 192.94, 1167.8, 1099.59, 1097.3, 1089.78, 1166.59, 703.33, 
671.64, 346.49, 440.89, 126.38, 638.24, 972.32, 1066.8, 775.68, 
591.86, 818.75, 953.63, 1104.98, 1050.47, 722.43, 1022.17, 986.38, 
1133.01, 914.27, 725.15, 1151.52, 786.08, 1024.83, 246.52, 441.53 
), y = c(923.68, 660.97, 131.61, 882.23, 604.09, 504.05, 870.35, 
858.51, 513.5, 937.7, 838.47, 482.69, 473.48, 171.78, 774.99, 
792.46, 251.26, 757.95, 317.71, 401.93, 326.32, 725.89, 98.43, 
414.01, 510.16, 973.61, 445.33, 504.54, 669.87, 598.75, 225.27, 
789.45, 135.31, 935.51, 270.38, 241.19, 595.05, 401.25, 160.98, 
778.86, 192.17, 323.76, 361.08, 444.92, 354, 249.57, 301.64, 
375.75, 440.03, 428.79, 276.5, 408.84, 381.14, 459.14, 370.26, 
304.05, 439.14, 339.91, 435.85, 759.42, 513.37)), class = c("tbl_df", 
"tbl", "data.frame"), row.names = c(NA, -61L), .Names = c("indexR", 
"x", "y")) 

回答

1

一种可能性是将每个组中最近的元素标识为混合整数程序。我们可以定义决策变量y_i是否选择每个点i,以及x_ {ij}是否选择了点i和j(x_ {ij} = y_iy_j)。我们需要从每个组中选择一个元素。

实际上,您可以使用lpSolve包(或其他R优化包之一)实现此混合整数程序。

opt.closest <- function(df) { 
    # Compute every pair of indices 
    library(dplyr) 
    pairs <- as.data.frame(t(combn(nrow(df), 2))) %>% 
    mutate(G1=df$indexR[V1], G2=df$indexR[V2]) %>% 
    filter(G1 != G2) %>% 
    mutate(dist = sqrt((df$x[V1]-df$x[V2])^2+(df$y[V1]-df$y[V2])^2)) 

    # Compute a few convenience values 
    n <- nrow(df) 
    nP <- nrow(pairs) 
    groups <- sort(unique(df$indexR)) 
    nG <- length(groups) 
    gpairs <- combn(groups, 2) 
    nGP <- ncol(gpairs) 

    # Solve the optimization problem 
    obj <- c(pairs$dist, rep(0, n)) 
    constr <- rbind(cbind(diag(nP), -outer(pairs$V1, seq_len(n), "==")), 
        cbind(diag(nP), -outer(pairs$V2, seq_len(n), "==")), 
        cbind(diag(nP), -outer(pairs$V1, seq_len(n), "==") - outer(pairs$V2, seq_len(n), "==")), 
        cbind(matrix(0, nG, nP), outer(groups, df$indexR, "==")), 
        cbind((outer(gpairs[1,], pairs$G1, "==") & 
         outer(gpairs[2,], pairs$G2, "==")) | 
         (outer(gpairs[2,], pairs$G1, "==") & 
         outer(gpairs[1,], pairs$G2, "==")), matrix(0, nGP, n))) 
    dir <- rep(c("<=", ">=", "="), c(2*nP, nP, nG+nGP)) 
    rhs <- rep(c(0, -1, 1), c(2*nP, nP, nG+nGP)) 
    library(lpSolve) 
    mod <- lp("min", obj, constr, dir, rhs, all.bin=TRUE) 
    which(tail(mod$solution, n) == 1) 
} 

这可以计算最接近的3个点,从每个集群,在您的示例数据集:

df[opt.closest(df),] 
# A tibble: 3 x 3 
# indexR  x  y 
# <dbl> <dbl> <dbl> 
# 1  1 638.24 323.76 
# 2  2 591.86 249.57 
# 3  3 441.53 513.37 

它也可以计算与更多的积分和组数据集最佳的解决方案。以下是运行时用于与每个7组,100和200点的数据集:

make.dataset <- function(n, nG) { 
    set.seed(144) 
    data.frame(indexR = sample(seq_len(nG), n, replace=T), x = rnorm(n), y=rnorm(n)) 
} 
df100 <- make.dataset(100, 7) 
system.time(opt.closest(df100)) 
# user system elapsed 
# 11.536 2.656 15.407 
df200 <- make.dataset(200, 7) 
system.time(opt.closest(df200)) 
# user system elapsed 
# 187.363 86.454 323.167 

这远远瞬时 - 它需要15秒的100点,7组数据集和323秒钟的200点,7组数据集。尽管如此,它比遍历100点数据集中的所有9200万个7元组或130点数据集中的所有138亿7元组要快得多。您可以使用Rglpk软件包中的求解器来设置运行时限制,以获得在此限制内获得的最佳解决方案。

+0

这对于实际数据集非常有效。我可以将组数最多扩展到8个,没有任何问题。我可以用Rglpk稍微提高速度,但最好这是8秒钟的事情,所以并不是真的有必要。此后,行数开始呈指数增长,无论如何都不再可行。 我也很想感谢您提前给予的建议和指导。干杯! – Visser

0

您可以使用交叉连接一起把所有的点的组合,计算所有的三个点之间的总距离,然后取最小的那个。

df$id <- row.names(df) # to create ID's for the points 

df2 <- merge(df, df, by = NULL) # the first cross join 

df3 <- merge(df2, df, by = NULL) # the second cross join 



# eliminating rows where the points are of the same indexR 

df3 <- df3[df3$indexR.x != df3$indexR.y & df3$indexR.x != df3$indexR 
      & df3$indexR.y != df3$indexR,] 


## calculating the total distance 

df3$total_distance <- ((df3$x - df3$x.x)^2 + (df3$y- df3$y.x)^2)^.5 + 
    ((df3$x - df3$x.y)^2 + (df3$y- df3$y.y)^2)^.5 + 
    ((df3$x.x - df3$x.y)^2 + (df3$y.x- df3$y.y)^2)^.5 

## minimum distance 

df3[which.min(df3$total_distance),] 

indexR.x x.x y.x id.x indexR.y x.y y.y id.y indexR  x  y id total_distance 
155367  3 441.53 513.37 61  2 591.86 249.57 46  1 638.24 323.76 42  664.3373 
+1

即使有一个中等规模的数据集(1000点),横三路连接就生成l个十亿行,其中你可能无法舒适地存储在内存中。 – josliber

+2

另外,如果你有4组,你需要一个4路交叉连接。使用1000点的数据集时,4路交叉连接将为1万亿行,这绝对不能存储在内存中。 – josliber

+0

@josliber这是我所关心的问题。因为实际上,我有7个组(真的13,但我不得不减少它,因为它疯了)。 3只是为了可重复性和简单的解释;它感觉更像是一个xyz 3D问题,并且更容易可视化 – Visser

1

您不能列举所有可能的解决方案,而且我也没有看到任何明显的捷径。

所以,我想你将不得不做一个分支和约束优化的方法。

首先猜测一个相当好的解决方案。就像最近的两个不同标签的点一样。然后添加最近的不同标签,直到您覆盖所有标签。

现在做一些简单的优化:对于每个标签,尝试是否有某个点可以用来代替当前点来改善结果。当你找不到任何进一步的改进时停下来。

对于这个初始猜测,计算距离。这会给你一个上限,这可以让你尽早停止搜索。您还可以计算一个下限,即所有最佳双标签解决方案的总和。

现在您可以尝试删除点,其中每个标签的最近邻居+所有其他标签的下界已经比您的初始解决方案更差。这将有希望消除很多点。

然后,您可以开始枚举解决方案(可能首先从最小的标签开始),但是只要当前解决方案+剩余下限大于您最知名的解决方案(分支和界限)就停止递归。

你也可以尝试排序点,例如通过与其余标签的最小距离,希望快速找到更好的边界。

我肯定不会选择R实现这个...

+1

这个分支定界的一个选择是使用整数规划解算器 - 我肯定会采取这种方法,而不是滚动我自己的分支定界算法!你可以在R中很容易地做到这一点 - 我在我的解决方案中使用了'lpSolve',但还有很多其他选项。 – josliber

+0

谢谢Anony-Mousse,这是我将来可以看到的东西。但是现在,这篇文章的一部分代码集中在R的简单性和可用性(针对非编码器)。我已经使用了分支和界限算法来解决这个问题,让我能够像现在这样得到我的“最终名单”,所以转移到另一个平台/语言的部分可能稍后可行。谢谢 – Visser

相关问题