gatagat / lap

Linear Assignment Problem solver (LAPJV/LAPMOD).
BSD 2-Clause "Simplified" License
211 stars 66 forks source link

Non-square matrix with infinite costs in lapjv #20

Open jvlmdr opened 4 years ago

jvlmdr commented 4 years ago

Hey, thanks for lapjv and lapmod, they are awesome.

I think I found a small bug. Consider the cost matrix

[[inf,  11,   8],
 [  8, inf,   7]]

The minimum cost is 8 + 8 = 16. However, the result I get from lapjv is 18 (test case below).

I guess the problem arises in the extension since inf + 1 => inf.

[[inf,  11,   8, inf, inf],
 [  8, inf,   7, inf, inf],
 [inf, inf, inf,   0,   0],
 [inf, inf, inf,   0,   0],
 [inf, inf, inf,   0,   0]]

The minimum cost of this matrix is inf since we can select at most 1 finite value from each of the first 2 rows, 1 finite value from each of the last 2 columns and then we must select an inf edge to complete the matching. I suppose the problem is that all matchings in the extended problem have the same sum (i.e. inf).

Here is a test case:

def test_lapjv_extension_with_inf():
    cost = np.asarray([[np.inf, 11, 8], [8, np.inf, 7]], dtype=float)
    ret = lapjv(cost, extend_cost=True)
    assert ret[0] == 16.0
    np.testing.assert_equal(ret[1], [2, 0])
    np.testing.assert_equal(ret[2], [1, -1, 0])