【问题标题】:The point that minimizes the sum of euclidean distances to a set of n points最小化到一组 n 点的欧几里得距离之和的点
【发布时间】:2019-07-30 17:40:04
【问题描述】:

我在二维平面上有一组点W={(x1, y1), (x2, y2),..., (xn, yn)}。你能找到一种算法,将这些点作为输入并返回二维平面上的点(x, y),它与W 中的点的距离之和最小吗?换句话说,如果

di = Euclidean_distance((x, y), (xi, yi))

我想最小化:

d1 + d2 + ... + dn

【问题讨论】:

  • 冒着让自己尴尬的风险,因为一些未曾考虑过的猜测:这不只是重心吗?
  • 您使用了哪些搜索词未能找到已知的解决方案?
  • 您正在寻找geometric median
  • @Marco13:重心使平方距离的总和最小化。

标签: algorithm mathematical-optimization computational-geometry convex-optimization


【解决方案1】:

问题

您正在寻找geometric median

简单的解决方案

这个问题没有封闭形式的解决方案,因此使用迭代或概率方法。找到这一点的最简单方法可能是使用 Weiszfeld 算法:

我们可以在 Python 中如下实现:

import numpy as np
from numpy.linalg import norm as npnorm
c_pt_old = np.random.rand(2)
c_pt_new = np.array([0,0])

while npnorm(c_pt_old-c_pt_new)>1e-6:
    num   = 0
    denom = 0
    for i in range(POINT_NUM):
        dist   = npnorm(c_pt_new-pts[i,:])
        num   += pts[i,:]/dist
        denom += 1/dist
    c_pt_old = c_pt_new
    c_pt_new = num/denom

print(c_pt_new)

Weiszfeld 的算法有可能不会收敛,因此最好从不同的起点运行几次。

通用解决方案

您也可以使用second-order cone programming (SOCP) 找到它。除了解决您的特定问题外,此通用公式还允许您轻松添加约束和权重,例如每个数据点位置的可变不确定性。

为此,您需要创建一些指标变量来表示建议的中心点和数据点之间的距离。

然后,您可以最小化指标变量的总和。结果如下

import cvxpy as cp
import numpy as np
import matplotlib.pyplot as plt

#Generate random test data
POINT_NUM = 100
pts       = np.random.rand(POINT_NUM,2)

c_pt      = cp.Variable(2)           #The center point we wish to locate
distances = cp.Variable(POINT_NUM)   #Distance from the center point to each data point

#Generate constraints. These are used to hold distances.
constraints = []                     
for i in range(POINT_NUM):
    constraints.append( cp.norm(c_pt-pts[i,:])<=distances[i] ) 

objective = cp.Minimize(cp.sum(distances))

problem = cp.Problem(objective,constraints)

optimal_value = problem.solve()

print("Optimal value = {0}".format(optimal_value))
print("Optimal location = {0}".format(c_pt.value))

plt.scatter(x=pts[:,0], y=pts[:,1], s=1)
plt.scatter(c_pt.value[0], c_pt.value[1], s=10)
plt.show()

SOCP 在number of solvers 中可用,包括 CPLEX、Elemental、ECOS、ECOS_BB、GUROBI、MOSEK、CVXOPT 和 SCS。

我已经测试过,这两种方法在公差范围内给出了相同的答案。

魏斯菲尔德,E.(1937 年)。 “Sur le point pour lequel la somme des distances de n points donnes est minimum”。东北数学杂志。 43:355–386。

【讨论】:

    【解决方案2】:

    第三种方法是使用紧凑的非线性规划公式。一个无约束的 NLP 模型将是:

      min sum(i,  ||x-p(i)|| )
    

    这只有 2 个变量(x 的坐标)。

    有一个很好的初始点可用。让p(i,c) 成为数据点的坐标。那么平均值就是

      m(c) = sum(i, p(i,c)) / n
    

    其中n 是数据点的数量。这个点通常非常接近x 的最优值。所以我们可以使用m 作为x 的一个很好的初始点。

    一些有限的实验表明,这种方法比大型n 的锥形编程公式要快得多。

    详情见Yet Another Math Programming Consultant - Finding the Central Point in a Point Cloud blog post

    【讨论】:

    • 尽量避免在链接中放置关键信息。 (不过很棒的博客!)
    • 请注意,无约束版本具有非平滑目标,因为范数可微分为零。这可能会也可能不会导致问题。
    猜你喜欢
    • 1970-01-01
    • 2021-01-30
    • 2011-01-29
    • 2019-02-07
    • 2021-10-11
    • 2021-05-14
    • 2013-02-12
    • 1970-01-01
    • 2021-10-01
    相关资源
    最近更新 更多