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

问题描述 投票:1回答:1

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

我浏览了很多资料,知道使用的公式是这样的。M(i, j) = 0.5(D(1,j)^2+D(i,1)^2-D(i,j)^2)*。 解题 请在此输入链接描述. 我数学不好,我只想实现到它。

首先,我试着去理解数学的原理,这是我的解决方案。请在这里输入链接描述.

然后,我想用python把这个算法植入到下面的例子中。 这是我的矩阵,它代表了每个巴士站的不同距离。我想把它转移到点的坐标上。

enter image description here

这是我的植入代码。

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]. 它们不能满足本例中的距离矩阵。请帮助我如何获得正确的点。谢谢!

python matrix geometry eigenvalue eigenvector
1个回答
1
投票

构造与距离矩阵相关联的点积的格拉姆矩阵,并将其进一步因式化,一般来说是一种很好的方法,也可以推断出距离矩阵的坐标实现的维度。然而,如果在你的情况下,实现是平面的(二维),那么我认为(可以说)更容易(也可能更快),只是更几何化地接近它(同样,你应该确定距离矩阵是针对二维的点)。

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 y_coord_of_point(D, j):
    return math.sqrt( D[0,j]**2 - P[j,0]**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,0] = x_coord_of_point(D, 2)
    P[2,1] = y_coord_of_point(D, 2) 
    for j in range(3,n):
        P[j,0] = x_coord_of_point(D, j)
        P[j,1] = y_coord_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_2 = 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[j0, :] 不合 P[1,0]. 这第一个矢量的作用 P[j0,:] 与初始向量不对齐 P[1,:] 允许你拥有一个有用的 if 的条款 function vector(D). 我没有把它包括在内,以免掩盖了代码的概念。

© www.soinside.com 2019 - 2024. All rights reserved.