|
| 1 | +""" |
| 2 | +The ``SE2Metric`` of a pose difference (x, y, theta) and its weight on the |
| 3 | +angle, in graphs: edge costs, ``closest``, the A* heuristic, copying and |
| 4 | +pickling. |
| 5 | +
|
| 6 | +Expectations are worked out by hand. |
| 7 | +""" |
| 8 | + |
| 9 | +import copy |
| 10 | +import pickle # nosec B403 - only used to round-trip objects made in this file |
| 11 | +import unittest |
| 12 | + |
| 13 | +import numpy as np |
| 14 | + |
| 15 | +from pgraph import DGraph, SE2Metric, UGraph |
| 16 | + |
| 17 | + |
| 18 | +class TestMetric(unittest.TestCase): |
| 19 | + def test_default_weight_is_the_string_se2(self): |
| 20 | + v = np.r_[3.0, 4.0, 1.0] |
| 21 | + self.assertEqual(SE2Metric()(v), UGraph(metric="SE2").metric(v)) |
| 22 | + self.assertAlmostEqual(SE2Metric()(v), np.sqrt(26)) # 9 + 16 + 1 |
| 23 | + |
| 24 | + def test_weight_scales_the_angle_only(self): |
| 25 | + v = np.r_[3.0, 4.0, np.pi / 2] |
| 26 | + self.assertAlmostEqual(SE2Metric(1)(v), np.sqrt(25 + (np.pi / 2) ** 2)) |
| 27 | + self.assertAlmostEqual(SE2Metric(2)(v), np.sqrt(25 + np.pi**2)) |
| 28 | + self.assertAlmostEqual(SE2Metric(0.5)(v), np.sqrt(25 + (np.pi / 4) ** 2)) |
| 29 | + self.assertAlmostEqual(SE2Metric(0)(v), 5.0) |
| 30 | + |
| 31 | + def test_angle_is_wrapped_before_weighting(self): |
| 32 | + for turns in (-2, -1, 0, 1, 2): |
| 33 | + with self.subTest(turns=turns): |
| 34 | + v = np.r_[0.0, 0.0, 0.5 + turns * 2 * np.pi] |
| 35 | + self.assertAlmostEqual(SE2Metric(3)(v), 1.5) |
| 36 | + # just past pi is a turn the other way |
| 37 | + self.assertAlmostEqual(SE2Metric(1)(np.r_[0.0, 0.0, np.pi + 0.1]), np.pi - 0.1) |
| 38 | + |
| 39 | + def test_a_pure_turn_is_worth_the_weight_times_the_angle(self): |
| 40 | + self.assertAlmostEqual(SE2Metric(0.25)(np.r_[0.0, 0.0, 2.0]), 0.5) |
| 41 | + |
| 42 | + def test_the_argument_is_not_changed(self): |
| 43 | + v = np.r_[1.0, 2.0, 7.0] # needs wrapping |
| 44 | + before = v.copy() |
| 45 | + SE2Metric(0.5)(v) |
| 46 | + np.testing.assert_array_equal(v, before) |
| 47 | + |
| 48 | + def test_a_list_is_accepted(self): |
| 49 | + self.assertAlmostEqual(SE2Metric(2)([0, 0, 1]), 2.0) |
| 50 | + |
| 51 | + def test_needs_three_elements(self): |
| 52 | + for v in ([1.0, 2.0], [1.0, 2.0, 3.0, 4.0]): |
| 53 | + with self.subTest(length=len(v)): |
| 54 | + with self.assertRaises(ValueError): |
| 55 | + SE2Metric()(np.array(v)) |
| 56 | + |
| 57 | + def test_bad_weights(self): |
| 58 | + for weight in (-1, np.inf, np.nan): |
| 59 | + with self.subTest(weight=weight): |
| 60 | + with self.assertRaises(ValueError): |
| 61 | + SE2Metric(weight) |
| 62 | + |
| 63 | + def test_repr_equality_and_hash(self): |
| 64 | + self.assertEqual(repr(SE2Metric(0.5)), "SE2Metric(weight=0.5)") |
| 65 | + self.assertEqual(SE2Metric(0.5), SE2Metric(0.5)) |
| 66 | + self.assertNotEqual(SE2Metric(0.5), SE2Metric(2)) |
| 67 | + self.assertEqual(len({SE2Metric(0.5), SE2Metric(0.5), SE2Metric(2)}), 2) |
| 68 | + self.assertEqual(SE2Metric(), UGraph(metric="SE2").metric) |
| 69 | + |
| 70 | + |
| 71 | +class TestInGraphs(unittest.TestCase): |
| 72 | + cls = UGraph |
| 73 | + |
| 74 | + def test_edge_cost_uses_the_weight(self): |
| 75 | + g = self.cls(dim=3, metric=SE2Metric(weight=2)) |
| 76 | + a = g.add_vertex([0.0, 0.0, 0.0]) |
| 77 | + b = g.add_vertex([0.0, 0.0, 0.5]) |
| 78 | + self.assertAlmostEqual(g.add_edge(a, b).cost, 1.0) # 2 * 0.5 |
| 79 | + |
| 80 | + def test_closest_depends_on_the_weight(self): |
| 81 | + # p is 1 away in the plane and no turn; q is at the same place but |
| 82 | + # turned by 1 rad: nearer than p only if turning is cheap enough |
| 83 | + def nearest(weight): |
| 84 | + g = self.cls(dim=3, metric=SE2Metric(weight=weight)) |
| 85 | + g.add_vertex([2.0, 0.0, 0.0], name="p") # 2 away, no turn |
| 86 | + g.add_vertex([0.0, 0.0, 1.5], name="q") # no distance, 1.5 rad |
| 87 | + return g.closest([0.0, 0.0, 0.0]) |
| 88 | + |
| 89 | + v, d = nearest(1.0) # q is 1.5 away, p is 2 away |
| 90 | + self.assertEqual((v.name, round(d, 6)), ("q", 1.5)) |
| 91 | + v, d = nearest(2.0) # q is 3 away, p is 2 away |
| 92 | + self.assertEqual((v.name, round(d, 6)), ("p", 2.0)) |
| 93 | + v, d = nearest(0.0) # q is 0 away |
| 94 | + self.assertEqual((v.name, round(d, 6)), ("q", 0.0)) |
| 95 | + |
| 96 | + def test_metric_can_be_set_after(self): |
| 97 | + g = self.cls(dim=3) |
| 98 | + g.metric = SE2Metric(weight=3) |
| 99 | + self.assertAlmostEqual(g.metric(np.r_[0.0, 0.0, 1.0]), 3.0) |
| 100 | + |
| 101 | + def test_astar_with_the_weighted_metric_is_optimal(self): |
| 102 | + # heuristic defaults to the metric, which makes the edge costs and the |
| 103 | + # heuristic consistent; compare with the uniform cost search |
| 104 | + rng = np.random.default_rng(5) |
| 105 | + for weight in (0.0, 0.3, 1.0, 4.0): |
| 106 | + with self.subTest(weight=weight): |
| 107 | + g = self.cls(dim=3, metric=SE2Metric(weight=weight), heuristic=SE2Metric(weight=weight)) |
| 108 | + vs = [g.add_vertex([*rng.uniform(0, 10, 2), rng.uniform(-3, 3)]) for _ in range(30)] |
| 109 | + for i in range(30): |
| 110 | + for j in rng.choice(30, 3, replace=False): |
| 111 | + if i != j: |
| 112 | + g.add_edge(vs[i], vs[int(j)]) |
| 113 | + for s, t in [(0, 29), (3, 17), (10, 25)]: |
| 114 | + ucs = g.path_UCS(vs[s], vs[t]) |
| 115 | + astar = g.path_Astar(vs[s], vs[t]) |
| 116 | + self.assertEqual(ucs is None, astar is None) |
| 117 | + if ucs is not None: |
| 118 | + self.assertAlmostEqual(astar[1], ucs[1]) |
| 119 | + |
| 120 | + def test_pickle_and_copy_keep_the_weight(self): |
| 121 | + g = self.cls(dim=3, metric=SE2Metric(weight=0.25), heuristic=SE2Metric(weight=0)) |
| 122 | + g.add_vertex([0.0, 0.0, 0.0]) |
| 123 | + for h in (pickle.loads(pickle.dumps(g)), g.copy(), copy.deepcopy(g)): # nosec B301 |
| 124 | + self.assertEqual(h.metric, SE2Metric(0.25)) |
| 125 | + self.assertEqual(h.heuristic, SE2Metric(0)) |
| 126 | + h = pickle.loads(pickle.dumps(self.cls(metric="SE2"))) # nosec B301 |
| 127 | + self.assertEqual(h.metric, SE2Metric()) |
| 128 | + |
| 129 | + def test_the_named_metric_is_still_pickled_by_name(self): |
| 130 | + g = self.cls(metric="SE2") |
| 131 | + self.assertIs(pickle.loads(pickle.dumps(g)).metric, g.metric) # nosec B301 |
| 132 | + |
| 133 | + |
| 134 | +class TestInDirectedGraphs(TestInGraphs): |
| 135 | + cls = DGraph |
| 136 | + |
| 137 | + |
| 138 | +if __name__ == "__main__": |
| 139 | + unittest.main() |
0 commit comments