如何实现基于gram-matrix从python距离矩阵中查找点的坐标?

How to implement finding the coordinates of points from distance matrix in python based on gram-matrix?

我想研究公交车站优化问题。但是,我现在陷入了如何将距离矩阵转换为点的实际坐标。

我浏览了很多资源,知道使用公式:M(i, j) = 0.5(D(1, j)^2 + D(i, 1)^2 - D(i, j)^2)* 求解问题enter link description here。我不擅长数学,我只想实现它。

首先,我试图理解数学原理,这是我的解决方案。 enter link description here.

然后,我想在下面的示例中使用 python 来实现算法。这是我的矩阵,代表每个公交车站的不同距离。我要转成点坐标

这是我的实现代码:

import csv
import numpy as np
import math

class csv_util():

    def generate_coordinate_point(self):
        '''transfer the distance matrix to the real coordinate points'''
        sqrt_result = 2*math.sqrt(2)

        matrix = np.array([[0,2,2,sqrt_result],[2,0,sqrt_result,2],[2,sqrt_result,0,2],[sqrt_result,2,2,0]])

        gram_matrix = self.calculate_gram_matrix(matrix)

        a, b = np.linalg.eig(gram_matrix)

        #b = b.astype(np.int16)
        a = a.astype(np.int16)


        eigen_vector = format(b)

        length = a.size
        tmp_matrix = np.zeros(length * length)
        random_point_matrix = tmp_matrix.reshape(length, length)

        for item1 in range(length):
            random_point_matrix[item1][item1] = a[item1]

        print("the eigen-value is: " + format(random_point_matrix))
        print("the eigen-vector is: " + eigen_vector)

        new_matrix = (np.sqrt(random_point_matrix))*b

        print("the coordinate points: "+format(new_matrix))



    def calculate_gram_matrix(self,matrix):
        '''get the gram matrix for transfer to the coordinate points'''

        length = matrix[0].size
        tmp_matrix = np.zeros(length*length)
        gram_matrix = tmp_matrix.reshape(length,length)

        for item1 in range(length):
            for item2 in range(length):
                gram_matrix[item1][item2] = (math.pow(matrix[0][item2],2)+math.pow(matrix[0][item1],2)-math.pow(matrix[item1][item2],2))/2
                if gram_matrix[item1][item2]<0.1 and gram_matrix[item1][item2]>-0.1:
                    gram_matrix[item1][item2] = 0

        return gram_matrix

但是,最终矩阵的结果不正确。结果是这样的:

the eigen-value is: [[12.  0.  0.  0.]
 [ 0.  0.  0.  0.]
 [ 0.  0.  4.  0.]
 [ 0.  0.  0.  0.]]
-------------
the eigen-vector is: [[ 0.00000000e+00  0.00000000e+00  0.00000000e+00  1.00000000e+00]
 [ 4.08248290e-01 -5.77350269e-01 -7.07106781e-01  0.00000000e+00]
 [ 4.08248290e-01 -5.77350269e-01  7.07106781e-01  0.00000000e+00]
 [ 8.16496581e-01  5.77350269e-01  1.57009246e-16  0.00000000e+00]]
-------------
the coordinate points: [[ 0.          0.          0.          0.        ]
 [ 0.         -0.         -0.          0.        ]
 [ 0.         -0.          1.41421356  0.        ]
 [ 0.          0.          0.          0.        ]]

最后的点是这样的:[0,0],[-0.0,-0.0],[-0.0,1.414421],[0.0,0.0]。他们不能满足于这个例子中的距离矩阵。请帮助我如何获得正确的分数。谢谢!

与距离矩阵相关联的点积的格拉姆矩阵的构造及其进一步分解通常是一个很好的方法,这也允许您推断距离矩阵的坐标实现的维数。但是,如果在您的情况下实现是平面的(二维的),那么我认为(可以说)更容易(并且可能更快)更几何地接近它(同样,您应该确定距离矩阵是为了二维点):

import numpy as np
import math

def x_coord_of_point(D, j):
    return ( D[0,j]**2 + D[0,1]**2 - D[1,j]**2 ) / ( 2*D[0,1] )

def coords_of_point(D, j):
    x = x_coord_of_point(D, j)
    return np.array([x, math.sqrt( D[0,j]**2 - x**2 )])
    
def calculate_positions(D):
    (m, n) = D.shape
    P = np.zeros( (n, 2) )
    tr = ( min(min(D[2,0:2]), min(D[2,3:n])) / 2)**2
    P[1,0] = D[0,1]
    P[2,:] = coords_of_point(D, 2)
    for j in range(3,n):
        P[j,:] = coords_of_point(D, j) 
        if abs( np.dot(P[j,:] - P[2,:], P[j,:] - P[2,:]) - D[2,j]**2 ) > tr:
            P[j,1] = - P[j,1]
    return P 
    
sqrt_result = 2*math.sqrt(2)
D = np.array([[0, 2, 2, sqrt_result],
              [2, 0, sqrt_result, 2], 
              [2, sqrt_result, 0, 2], 
              [sqrt_result, 2, 2, 0]])

P = calculate_positions(D)
print(P)
     

您可能希望添加一些检查和改进以确保向量 P[1,:] 和 P[2,:] 未对齐,这相当于检查

abs( P[1,0]*P[2,1] - P[1,1]*P[2,0] ) < 0.0001 (or some more appropriate threshold)

如果是,只需执行一个 while 循环,直到找到第一个未与 P[1,0] 对齐的向量 P[j0, :]。第一个向量 P[j0,:] 与初始向量 P[1,:] 不对齐的作用允许您在 function vector(D) 中有一个有用的 if 子句。我没有包含它以避免混淆代码的概念。