def test_01_02(self): r = np.random.RandomState() r.seed(11) for reductions in [0, 2]: for _ in range(100): c = r.randint(1, 10, (5, 5)) i, j = np.mgrid[0:5, 0:5] i = i.flatten() j = j.flatten() x, y, u, v = LAPJV.lapjv(i, j, c.flatten(), True, reductions) min_cost = np.sum(c) best = None for permutation in permutations([0, 1, 2, 3, 4]): cost = sum([c[i, permutation[i]] for i in range(5)]) if cost < min_cost: best = list(permutation) min_cost = cost result_cost = sum([c[i, x[i]] for i in range(5)]) self.assertAlmostEqual(min_cost, result_cost)
def test_01_02(self): r = np.random.RandomState() r.seed(11) for reductions in [0,2]: for _ in range(100): c = r.randint(1,10,(5,5)) i,j = np.mgrid[0:5,0:5] i = i.flatten() j = j.flatten() x,y,u,v = LAPJV.lapjv(i, j, c.flatten(), True, reductions) min_cost = np.sum(c) best = None for permutation in permutations([0,1,2,3,4]): cost = sum([c[i,permutation[i]] for i in range(5)]) if cost < min_cost: best = list(permutation) min_cost = cost result_cost = sum([c[i,x[i]] for i in range(5)]) self.assertAlmostEqual(min_cost, result_cost)
def test_01_03(self): '''Regression tests of matrices that crashed lapjv''' dd = [ np.array([[0., 0., 0.], [1., 1., 5.34621029], [1., 7., 55.], [2., 2., 2.09806089], [2., 8., 55.], [3., 3., 4.82063029], [3., 9., 55.], [4., 4., 3.99481917], [4., 10., 55.], [5., 5., 3.18959054], [5., 11., 55.], [6., 1., 55.], [6., 7., 0.], [6., 8., 0.], [6., 9., 0.], [6., 10., 0.], [6., 11., 0.], [7., 2., 55.], [7., 7., 0.], [7., 8., 0.], [7., 9., 0.], [7., 10., 0.], [7., 11., 0.], [8., 3., 55.], [8., 7., 0.], [8., 8., 0.], [8., 9., 0.], [8., 10., 0.], [8., 11., 0.], [9., 4., 55.], [9., 7., 0.], [9., 8., 0.], [9., 9., 0.], [9., 10., 0.], [9., 11., 0.], [10., 5., 55.], [10., 7., 0.], [10., 8., 0.], [10., 9., 0.], [10., 10., 0.], [10., 11., 0.], [11., 6., 55.], [11., 7., 0.], [11., 8., 0.], [11., 9., 0.], [11., 10., 0.], [11., 11., 0.]]), np.array([[0., 0., 0.], [1., 1., 1.12227977], [1., 6., 55.], [2., 2., 18.66735253], [2., 4., 16.2875504], [2., 7., 55.], [3., 5., 1.29944194], [3., 8., 55.], [4., 5., 32.61892281], [4., 9., 55.], [5., 1., 55.], [5., 6., 0.], [5., 7., 0.], [5., 8., 0.], [5., 9., 0.], [6., 2., 55.], [6., 6., 0.], [6., 7., 0.], [6., 8., 0.], [6., 9., 0.], [7., 3., 55.], [7., 6., 0.], [7., 7., 0.], [7., 8., 0.], [7., 9., 0.], [8., 4., 55.], [8., 6., 0.], [8., 7., 0.], [8., 8., 0.], [8., 9., 0.], [9., 5., 55.], [9., 6., 0.], [9., 7., 0.], [9., 8., 0.], [9., 9., 0.]]) ] expected_costs = [74.5, 1000000] for d, ec in zip(dd, expected_costs): n = np.max(d[:, 0].astype(int)) + 1 x, y = LAPJV.lapjv(d[:, 0].astype(int), d[:, 1].astype(int), d[:, 2]) c = np.ones((n, n)) * 1000000 c[d[:, 0].astype(int), d[:, 1].astype(int)] = d[:, 2] self.assertTrue(np.sum(c[np.arange(n), x]) < ec) self.assertTrue(np.sum(c[y, np.arange(n)]) < ec)
def test_01_03(self): '''Regression tests of matrices that crashed lapjv''' dd = [ np.array([[ 0. , 0. , 0. ], [ 1. , 1. , 5.34621029], [ 1. , 7. , 55. ], [ 2. , 2. , 2.09806089], [ 2. , 8. , 55. ], [ 3. , 3. , 4.82063029], [ 3. , 9. , 55. ], [ 4. , 4. , 3.99481917], [ 4. , 10. , 55. ], [ 5. , 5. , 3.18959054], [ 5. , 11. , 55. ], [ 6. , 1. , 55. ], [ 6. , 7. , 0. ], [ 6. , 8. , 0. ], [ 6. , 9. , 0. ], [ 6. , 10. , 0. ], [ 6. , 11. , 0. ], [ 7. , 2. , 55. ], [ 7. , 7. , 0. ], [ 7. , 8. , 0. ], [ 7. , 9. , 0. ], [ 7. , 10. , 0. ], [ 7. , 11. , 0. ], [ 8. , 3. , 55. ], [ 8. , 7. , 0. ], [ 8. , 8. , 0. ], [ 8. , 9. , 0. ], [ 8. , 10. , 0. ], [ 8. , 11. , 0. ], [ 9. , 4. , 55. ], [ 9. , 7. , 0. ], [ 9. , 8. , 0. ], [ 9. , 9. , 0. ], [ 9. , 10. , 0. ], [ 9. , 11. , 0. ], [ 10. , 5. , 55. ], [ 10. , 7. , 0. ], [ 10. , 8. , 0. ], [ 10. , 9. , 0. ], [ 10. , 10. , 0. ], [ 10. , 11. , 0. ], [ 11. , 6. , 55. ], [ 11. , 7. , 0. ], [ 11. , 8. , 0. ], [ 11. , 9. , 0. ], [ 11. , 10. , 0. ], [ 11. , 11. , 0. ]]), np.array([[ 0. , 0. , 0. ], [ 1. , 1. , 1.12227977], [ 1. , 6. , 55. ], [ 2. , 2. , 18.66735253], [ 2. , 4. , 16.2875504 ], [ 2. , 7. , 55. ], [ 3. , 5. , 1.29944194], [ 3. , 8. , 55. ], [ 4. , 5. , 32.61892281], [ 4. , 9. , 55. ], [ 5. , 1. , 55. ], [ 5. , 6. , 0. ], [ 5. , 7. , 0. ], [ 5. , 8. , 0. ], [ 5. , 9. , 0. ], [ 6. , 2. , 55. ], [ 6. , 6. , 0. ], [ 6. , 7. , 0. ], [ 6. , 8. , 0. ], [ 6. , 9. , 0. ], [ 7. , 3. , 55. ], [ 7. , 6. , 0. ], [ 7. , 7. , 0. ], [ 7. , 8. , 0. ], [ 7. , 9. , 0. ], [ 8. , 4. , 55. ], [ 8. , 6. , 0. ], [ 8. , 7. , 0. ], [ 8. , 8. , 0. ], [ 8. , 9. , 0. ], [ 9. , 5. , 55. ], [ 9. , 6. , 0. ], [ 9. , 7. , 0. ], [ 9. , 8. , 0. ], [ 9. , 9. , 0. ]])] expected_costs = [74.5, 1000000] for d,ec in zip(dd, expected_costs): n = np.max(d[:,0].astype(int))+1 x,y = LAPJV.lapjv(d[:,0].astype(int), d[:,1].astype(int), d[:,2]) c = np.ones((n,n)) * 1000000 c[d[:,0].astype(int), d[:,1].astype(int)] = d[:,2] self.assertTrue(np.sum(c[np.arange(n),x]) < ec) self.assertTrue(np.sum(c[y,np.arange(n)]) < ec)