def test_distance_unit_diff_identically_weighted(self): n = 4 domains = {0: [0, 1], 1: [2, 3]} space.init(n, domains) dom = {0: 1, 1: 1} dim = {0: {0: 0.5, 1: 0.5}, 1: {2: 0.5, 3: 0.5}} w = Weights(dom, dim) x = [1, 2, 3, 4] y = [2, 3, 2, 3] # distance of 1 wrt each coordinate self.assertEqual(space.distance(x, y, w), 2.0) self.assertEqual(space.distance(x, y, w), space.distance(y, x, w))
def test_distance_illegal_point(self): n = 4 domains = {0: [0, 1], 1: [2, 3]} space.init(n, domains) dom = {0: 1, 1: 1} dim = {0: {0: 0.5, 1: 0.5}, 1: {2: 0.5, 3: 0.5}} w = Weights(dom, dim) x = [1, 2, 3, 4] y = [5, 6, 7] with self.assertRaises(Exception): space.distance(x, y, w)
def test_distance_other_diff_identically_weighted(self): n = 4 domains = {0: [0, 1], 1: [2, 3]} space.init(n, domains) dom = {0: 1, 1: 1} dim = {0: {0: 0.5, 1: 0.5}, 1: {2: 0.5, 3: 0.5}} w = Weights(dom, dim) x = [1, 2, 3, 4] y = [2, 0, 2, 2] # difference: 1 2 1 2 self.assertEqual(space.distance(x, y, w), sqrt(0.5 * 1 + 0.5 * 4) + sqrt(0.5 * 1 + 0.5 * 4)) self.assertEqual(space.distance(x, y, w), space.distance(y, x, w))
def test_distance_other_diff_differently_weighted(self): n = 4 domains = {0: [0, 1], 1: [2, 3]} space.init(n, domains) dom = {0: 2, 1: 1} dim = {0: {0: 1, 1: 1}, 1: {2: 3, 3: 2.0}} w = Weights(dom, dim) x = [1, 2, 3, 4] y = [2, 0, 2, 2] # difference: 1 2 1 2 self.assertEqual(space.distance(x, y, w), (4.0 / 3) * sqrt(0.5 * 1 + 0.5 * 4) + (2.0 / 3) * sqrt(0.6 * 1 + 0.4 * 4)) self.assertEqual(space.distance(x, y, w), space.distance(y, x, w))