File size: 8,221 Bytes
7ffaec7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
"""python3 -m unittest test_entry2_receiver: receiver R's XML, slot map, mask and the G transport with its exact inverse."""
import math
import unittest
import xml.etree.ElementTree as ET

import numpy as np

import entry2_receiver as RX


class XmlDeterminism(unittest.TestCase):
    def test_embedded_healthy_walker_matches_the_pinned_box_hash(self):
        self.assertEqual(RX.sha256_text(RX.healthy_xml()), RX.WALKER_XML_SHA256)

    def test_generated_receiver_is_the_same_string_every_time_and_matches_the_committed_hash(self):
        a, b = RX.receiver_xml(), RX.receiver_xml()
        self.assertEqual(a, b)
        self.assertEqual(RX.sha256_text(a), RX.RECEIVER_XML_SHA256)
        self.assertEqual(RX.receiver_xml_checked(), a)

    def test_edits_are_the_section_1_transformation(self):
        root = ET.fromstring(RX.receiver_xml())
        torso = root.find("./worldbody/body[@name='torso']")
        self.assertEqual(torso.get("pos"), "0 0 1.39")
        self.assertEqual(torso.find("./geom[@name='torso']").get("density"), "1500")
        for side in RX.SIDES:
            thigh = torso.find(f"./body[@name='{side}_thigh']")
            g = thigh.find(f"./geom[@name='{side}_thigh']")
            self.assertEqual((g.get("size"), g.get("pos")), ("0.05 0.175", "0 0 -0.175"))
            leg = thigh.find(f"./body[@name='{side}_leg']")
            self.assertEqual(leg.get("pos"), "0 0 -0.7")
            self.assertEqual(leg.find(f"./joint[@name='{side}_knee']").get("pos"), "0 0 0.35")
            self.assertEqual(leg.find(f"./geom[@name='{side}_leg']").get("size"), "0.04 0.35")
            self.assertIsNone(leg.find(f"./body[@name='{side}_foot']"))
        self.assertEqual(RX.actuators_in_order(RX.receiver_xml()), ["right_hip", "right_knee", "left_hip", "left_knee"])
        self.assertIsNotNone(root.find("./sensor/subtreelinvel[@name='torso_subtreelinvel']"))

    def test_derived_values_match_the_box_reading(self):
        d = RX.derived()
        self.assertAlmostEqual(d["torso_z"], 1.39, places=12)
        self.assertAlmostEqual(d["stand_height"], 1.2831, places=4)
        self.assertAlmostEqual(d["upright_height_threshold"], 0.695, places=12)
        m = d["mass_kg"]
        self.assertEqual([round(m[k], 4) for k in ("torso", "thigh", "shank", "total")], [16.0096, 3.2725, 3.7867, 30.1279])

    def test_healthy_masses_recomputed_with_the_same_formula(self):
        self.assertAlmostEqual(RX.capsule_mass(0.07, 0.3, 1000), 10.673, places=3)
        self.assertAlmostEqual(RX.capsule_mass(0.05, 0.225, 1000), 4.0579, places=4)

    def test_a_changed_source_is_refused_not_silently_edited(self):
        changed = RX.healthy_xml().replace('size="0.05 0.225"', 'size="0.05 0.2"', 1)
        with self.assertRaises(RuntimeError):
            RX.receiver_xml(changed)

    def test_tampered_embedded_healthy_text_is_refused(self):
        saved = RX.WALKER_XML
        try:
            RX.WALKER_XML = saved.replace("gear=\"100\"", "gear=\"101\"", 1)
            with self.assertRaises(RuntimeError):
                RX.healthy_xml()
        finally:
            RX.WALKER_XML = saved


class SlotMap(unittest.TestCase):
    def test_slot_map_equals_the_map_recomputed_from_names(self):
        self.assertEqual(RX.slot_map_from_names(), RX.R_OBS_TO_SLOT)

    def test_native_observation_has_18_entries_and_the_healthy_24(self):
        self.assertEqual(len(RX.observation_names(RX.receiver_xml())), RX.R_OBS_DIM)
        self.assertEqual(len(RX.observation_names(RX.healthy_xml())), RX.MT30_OBS_DIM)

    def test_active_and_inactive_slots_partition_the_24(self):
        self.assertEqual(sorted(RX.R_OBS_TO_SLOT + RX.INACTIVE_OBS_SLOTS), list(range(24)))
        healthy = RX.observation_names(RX.healthy_xml())
        self.assertEqual([healthy[i] for i in RX.INACTIVE_OBS_SLOTS],
                         ["right_foot.xx", "right_foot.xz", "left_foot.xx", "left_foot.xz", "qvel.right_ankle",
                          "qvel.left_ankle"])

    def test_obs_to_slots_places_values_and_zeroes_inactive_slots(self):
        o = np.arange(1, 19, dtype=np.float32)
        s = RX.obs_to_slots(o)
        self.assertEqual(s.shape, (24,))
        self.assertTrue(np.array_equal(s[list(RX.R_OBS_TO_SLOT)], o))
        self.assertTrue(np.all(s[list(RX.INACTIVE_OBS_SLOTS)] == 0.0))
        self.assertEqual(o[0], 1.0)

    def test_actions_read_slots_0_1_3_4_and_ignore_2_5(self):
        a = np.array([0.1, 0.2, 9.0, 0.4, 0.5, -9.0])
        self.assertEqual(RX.slots_to_action(a).tolist(), [0.1, 0.2, 0.4, 0.5])
        healthy_act = RX.actuators_in_order(RX.healthy_xml())
        self.assertEqual([healthy_act[i] for i in RX.R_ACTION_SLOTS], RX.actuators_in_order(RX.receiver_xml()))

    def test_wrong_widths_are_errors(self):
        with self.assertRaises(ValueError):
            RX.obs_to_slots(np.zeros(24))
        with self.assertRaises(ValueError):
            RX.slots_to_action(np.zeros(4))


class Mask(unittest.TestCase):
    def test_mask_row_is_the_non_prefix_row_with_sum_four(self):
        self.assertEqual(RX.MASK_ROW, (1.0, 1.0, 0.0, 1.0, 1.0, 0.0))
        self.assertEqual(sum(RX.MASK_ROW), 4.0)
        self.assertEqual([i for i, m in enumerate(RX.MASK_ROW) if m], list(RX.R_ACTION_SLOTS))
        prefix = tuple(1.0 if i < 4 else 0.0 for i in range(6))
        self.assertNotEqual(RX.MASK_ROW, prefix)

    def test_index_30_copies_walker_walk_and_hopper_mask_is_not_used(self):
        self.assertEqual((RX.R_TASK_INDEX, RX.R_EMBEDDING_SOURCE_ROW), (30, 1))
        self.assertNotEqual(RX.HOPPER_NATIVE_MASK, RX.MASK_ROW)
        self.assertEqual(RX.S_EMBEDDING_ROWS, {"walker-walk": 1, "walker-stand": 0, "hopper-hop": 18})


class Transport(unittest.TestCase):
    def setUp(self):
        self.rng = np.random.default_rng(7)

    def test_g_is_the_pinned_draw_and_a_signed_permutation(self):
        g = RX.g_checked()
        self.assertEqual(sorted(g["P_o"]), list(range(18)))
        self.assertEqual(sorted(g["P_a"]), list(range(4)))
        self.assertTrue(set(g["s_o"]) <= {-1, 1} and set(g["s_a"]) <= {-1, 1})
        self.assertEqual(len(RX.g_sha256()), 64)

    def test_observation_transport_then_inverse_is_bitwise_identity(self):
        for dtype in (np.float32, np.float64):
            o = self.rng.standard_normal((5, 18)).astype(dtype)
            back = RX.g_obs_inverse(RX.g_obs(o))
            self.assertTrue(np.array_equal(back, o))
            self.assertEqual(back.dtype, o.dtype)
            self.assertTrue(np.array_equal(RX.g_obs(RX.g_obs_inverse(o)), o))

    def test_action_transport_then_inverse_is_bitwise_identity(self):
        m = self.rng.uniform(-1, 1, (7, 4)).astype(np.float32)
        self.assertTrue(np.array_equal(RX.g_action_inverse(RX.g_action(m)), m))
        self.assertTrue(np.array_equal(RX.g_action(RX.g_action_inverse(m)), m))

    def test_g_moves_coordinates_and_keeps_magnitudes(self):
        o = np.arange(1.0, 19.0)
        y = RX.g_obs(o)
        self.assertFalse(np.array_equal(y, o))
        self.assertEqual(sorted(np.abs(y).tolist()), o.tolist())
        g = RX.G_PINNED
        for i in range(18):
            self.assertEqual(y[i], g["s_o"][i] * o[g["P_o"][i]])

    def test_g_paths_keep_inactive_slots_zero_and_mask_unchanged(self):
        o = self.rng.standard_normal(18)
        s = RX.g_obs_slots(o)
        self.assertTrue(np.all(s[list(RX.INACTIVE_OBS_SLOTS)] == 0.0))
        self.assertTrue(np.array_equal(RX.g_obs_inverse(s[list(RX.R_OBS_TO_SLOT)]), o))
        a6 = np.array([0.3, -0.2, 0.9, 0.7, -0.6, -0.9])
        u = RX.g_slots_to_action(a6)
        self.assertTrue(np.array_equal(RX.g_action_inverse(u), RX.slots_to_action(a6)))

    def test_inputs_are_not_modified(self):
        o = self.rng.standard_normal(18)
        keep = o.copy()
        RX.g_obs(o)
        RX.g_obs_inverse(o)
        self.assertTrue(np.array_equal(o, keep))

    def test_a_different_numpy_draw_would_be_refused(self):
        saved = RX.G_PINNED
        try:
            RX.G_PINNED = dict(saved, s_a=[1, 1, 1, 1])
            with self.assertRaises(RuntimeError):
                RX.g_checked()
        finally:
            RX.G_PINNED = saved


if __name__ == "__main__":
    unittest.main()