【问题标题】:determining whether two convex hulls overlap确定两个凸包是否重叠
【发布时间】:2019-12-19 20:50:57
【问题描述】:

我正在尝试找到一种有效的算法来确定两个凸包是否相交。外壳由 N 维空间中的数据点组成,其中 N 为 3 到 10 左右。使用来自 scipy 的 linprog 建议了一种优雅的算法here,但是您必须遍历一个外壳中的所有点,事实证明该算法对于低维度非常慢(我尝试过,一位受访者也尝试过)。在我看来,该算法可以概括为回答我在这里发布的问题,我发现我认为是一个解决方案here。作者说,一般线性规划问题采用 Ax + tp >= 1 的形式,其中 A 矩阵包含both 外壳,t 是某个常数 >= 0,并且 p = [1,1,1,1...1](相当于找到 A 的解x > 0 对于某些 x)。由于我是 linprog() 的新手,我不清楚它是否可以处理这种形式的问题。如果论文第1页定义了A_ub,那么b_ub是什么?

【问题讨论】:

    标签: linear-programming convex-hull cvxpy scipy-optimize


    【解决方案1】:

    this website 上有一个很好的解释如何解决这个问题,使用 R 中的算法。我原来的帖子提到了 scipy.optimize.linprog 库,但事实证明这不够健壮。我发现cvxpy库中的SCS算法效果很好,基于此我想出了以下python代码:

    import numpy as np
    import cvxpy as cvxpy
    
    # Determine feasibility of Ax <= b
    # cloud1 and cloud2 should be numpy.ndarrays
    def clouds_overlap(cloud1, cloud2):
        # build the A matrix
        cloud12 = np.vstack((-cloud1, cloud2))
        vec_ones = np.r_[np.ones((len(cloud1),1)), -np.ones((len(cloud2),1))]
        A = np.r_['1', cloud12, vec_ones]
    
        # make b vector
        ntot = len(cloud1) + len(cloud2)
        b = -np.ones(ntot)
    
        # define the x variable and the equation to be solved
        x = cvxpy.Variable(A.shape[1])
        constraints = [A*x <= b]
    
        # since we're only determining feasibility there is no minimization
        # so just set the objective function to a constant
        obj = cvxpy.Minimize(0)
    
        # SCS was the most accurate/robust of the non-commercial solvers
        # for my application
        problem = cvxpy.Problem(obj, constraints)
        problem.solve(solver=cvxpy.SCS)
    
        # Any 'inaccurate' status indicates ambiguity, so you can
        # return True or False as you please
        if problem.status == 'infeasible' or problem.status.endswith('inaccurate'):
            return True
        else:
            return False
    
    cube = np.array([[1,1,1],[1,1,-1],[1,-1,1],[1,-1,-1],[-1,1,1],[-1,1,-1],[-1,-1,1],[-1,-1,-1]])
    inside = np.array([[0.49,0.0,0.0]])
    outside = np.array([[1.01,0,0]])
    
    print("Clouds overlap?", clouds_overlap(cube, inside))
    print("Clouds overlap?", clouds_overlap(cube, outside))
    
    # Clouds overlap? True
    # Clouds overlap? False
    

    数值不稳定区域是指两朵云刚刚接触,或者任意接近接触,因此无法明确判断它们是否重叠。这是您将看到此算法报告“不准确”状态的情况之一。在我的代码中,我选择考虑重叠的这种情况,但由于它是模棱两可的,你可以自己决定做什么。

    【讨论】:

      猜你喜欢
      • 2010-09-23
      • 2017-06-28
      • 2016-02-14
      • 1970-01-01
      • 2010-10-19
      • 2021-08-18
      • 1970-01-01
      • 1970-01-01
      • 2012-11-03
      相关资源
      最近更新 更多