0

I've 2 matrices both with Nx2 elements. Any value is a float with 8-10 decimals and they represent respectively 'x' and 'y' of a point.

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. 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.

I created a NxN matrix where I computed the distance from any point of first array to any point of the second array via

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;

enter image description here

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). So if m[i][j] is an element of the matrix NxN, I found those constraints

enter image description here

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.

JackLametta
  • 400
  • 1
  • 2
  • 21

2 Answers2

1

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.

Scipy has a simple assignment solver (link). This is not a very fast implementation however: a good LP solver is faster (link).

Erwin Kalvelagen
  • 15,677
  • 2
  • 14
  • 39
0

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

There will be a 1 to 1 correspondence between p1 & nearest ie p1[0] maps to nearest[0] etc..

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)
DrBwts
  • 3,470
  • 6
  • 38
  • 62