简体   繁体   English

Python-最接近的最小值

[英]Python - Closest Minimum

I've 2 matrices both with Nx2 elements. 我有两个矩阵都带有Nx2元素。 Any value is a float with 8-10 decimals and they represent respectively 'x' and 'y' of a point. 任何值都是带有8-10个小数的浮点数,它们分别表示一个点的“ x”和“ y”。

For any element couple (x, y) (x is in the first column, while y is in the second one) in the first array, I'd need to find the closest point in the second one. 对于第一个数组中的任何元素对(x,y)(x在第一列中,而y在第二列中),我需要在第二个数组中找到最接近的点。 At any loop, once found, I need to remove that value from the second array. 在任何循环中,一旦找到,我就需要从第二个数组中删除该值。

Finally, my main objective would be to have the optimal solution so that there's a one-to-one mapping between any element of the first array with only one element of the second array, so that the closest value clause is met. 最后,我的主要目标是获得最佳解决方案,以使第一个数组的任何元素与第二个数组的一个元素之间只有一个一对一的映射,从而满足最接近的value子句。

I created a NxN matrix where I computed the distance from any point of first array to any point of the second array via 我创建了一个NxN矩阵,其中我计算了从第一个数组的任何点到第二个数组的任何点的距离

scipy.spatial.distance.cdist scipy.spatial.distance.cdist

Code: 码:

def find_nearest(X_start, X_end):
    mat = scipy.spatial.distance.cdist(X_start, X_end, metric='euclidean')
    new_df = pd.DataFrame(mat)
    return new_df;

在此处输入图片说明

The next step is to couple a starting point with a ending point and there should NOT be any intersection, that is a mapping one-to-one. 下一步是将起点与终点耦合,并且不应有任何交点,即一对一映射。

I thought to do it via a Integer programming (using this ). 我想通过Integer编程(使用this )来做到这一点 So if m[i][j] is an element of the matrix NxN, I found those constraints 因此,如果m [i] [j]是矩阵NxN的元素,我发现这些约束

在此处输入图片说明

The problem is that I don't know how to write the objective function and so I'm note sure if I need to add any other constraint related to it. 问题是我不知道如何编写目标函数,因此请注意是否需要添加与其相关的任何其他约束。

Do you think this is a good path to follow? 您认为这是遵循的好方法吗? Last question seems to be not appreciated since I did not expose what I already did. 由于我没有公开自己已经做过的事情,所以最后一个问题似乎没有得到赞赏。

So here it is. 就是这样

This is called an assignment problem . 这称为分配问题

   min sum((i,j), dist[i,j]*x[i,j])
   subject to
       sum(i, x[i,j]) = 1 for all j
       sum(j, x[i,j]) = 1 for all i
       x[i,j] in {0,1}

where 哪里

 i = 1..n is an element of the first matrix
 j = 1..n is an element of the second matrix
 dist[i,j] is a distance matrix

These problems can be solved with specialized solvers or it can be formulated as an LP (linear programming) problem. 这些问题可以用专门的求解器解决,也可以表述为LP(线性编程)问题。

Scipy has a simple assignment solver ( link ). Scipy有一个简单的分配求解器( link )。 This is not a very fast implementation however: a good LP solver is faster ( link ). 但是,这不是一个非常快的实现:一个好的LP解算器更快( link )。

OK I think this is what you are asking. 好的,我想这就是您要问的。 The following code will go through each coordinate in p1 & calculate the distances with every coordinate in p2 (the closest_node function was from here ) then return the closest coorinate to the nearest array & the corresponding element is deleted from p2 以下代码将遍历p1每个坐标,并计算p2每个坐标的距离( closest_node函数来自于此 ),然后将最接近的坐标返回到nearest数组,并从p2删除相应的元素

There will be a 1 to 1 correspondence between p1 & nearest ie p1[0] maps to nearest[0] etc.. p1nearest之间存在1到1的对应关系,即p1[0]映射到nearest[0]等。

import numpy as np

def closest_node(node, nodes):
    dist_2 = np.sum((nodes - node)**2, axis=1)
    return np.argmin(dist_2)

p1 = np.random.rand(10, 2)
p2 = np.random.rand(10, 2)
nearest = []

for coord in p1:
    near = closest_node(coord, p2)
    nearest.append(p2[near])
    p2 = np.delete(p2, near, 0)

nearest = np.array(nearest)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM