"""Tests for told2_logic.py: the model-boundary reparametrisation of the replica-free told-gear runner.""" import unittest import numpy as np import told2_logic as T2 KNEE = 4 class Boundary(unittest.TestCase): def test_factor_one_is_the_exact_identity_and_new_arrays(self): s = T2.scale(6, KNEE, 1.0) u = np.array([0.3, -1.0, 1.0, -0.7, 0.123456789, 0.0]) m = T2.to_model(u, s) self.assertTrue(np.array_equal(m, u)) self.assertIsNot(m, u) back = T2.to_command(m, s, KNEE, 1.0) self.assertTrue(np.array_equal(back, u)) self.assertIsNot(back, m) def test_model_sees_half_the_executed_knee_at_both_ends_and_inside(self): s = T2.scale(6, KNEE, T2.HALF) for u_k, m_k in ((1.0, 0.5), (-1.0, -0.5), (0.6, 0.3), (0.0, 0.0)): u = np.array([0.1, -0.2, 0.3, -0.4, u_k, 0.6]) m = T2.to_model(u, s) self.assertAlmostEqual(m[KNEE], m_k) self.assertEqual(list(np.delete(m, KNEE)), [0.1, -0.2, 0.3, -0.4, 0.6]) self.assertEqual(u[KNEE], u_k) # the input is never modified def test_policy_proposal_maps_back_and_is_clipped_to_the_command_range(self): s = T2.scale(6, KNEE, T2.HALF) for m_k, u_k in ((0.25, 0.5), (0.5, 1.0), (0.8, 1.0), (-0.9, -1.0)): m = np.array([0.1, 0.2, -0.3, 0.4, m_k, -0.5]) u = T2.to_command(m, s, KNEE, T2.HALF) self.assertAlmostEqual(u[KNEE], u_k) self.assertEqual(list(np.delete(u, KNEE)), [0.1, 0.2, -0.3, 0.4, -0.5]) self.assertEqual(m[KNEE], m_k) def test_one_conversion_round_trip_inside_the_box(self): s = T2.scale(6, KNEE, T2.HALF) u = np.array([0.0, 0.0, 0.0, 0.0, 0.7, 0.0]) self.assertAlmostEqual(T2.to_command(T2.to_model(u, s), s, KNEE, T2.HALF)[KNEE], 0.7) self.assertLessEqual(abs(T2.to_model(np.array([0, 0, 0, 0, 1.0, 0]), s)[KNEE]), T2.HALF) def test_factor_one_never_clips(self): s = T2.scale(6, KNEE, 1.0) m = np.array([0.0, 0.0, 0.0, 0.0, 1.0000001, 0.0]) self.assertEqual(T2.to_command(m, s, KNEE, 1.0)[KNEE], 1.0000001) def test_factor_two_clips_only_the_knee(self): s = T2.scale(6, KNEE, T2.HALF) m = np.array([1.2, 0.0, 0.0, 0.0, 0.9, 0.0]) u = T2.to_command(m, s, KNEE, T2.HALF) self.assertEqual((u[0], u[KNEE]), (1.2, 1.0)) def test_told_arm_admission(self): self.assertTrue(T2.told_arm_fits(30)) self.assertTrue(T2.told_arm_fits(43)) self.assertFalse(T2.told_arm_fits(44)) with self.assertRaises(ValueError): T2.told_arm_fits(float("nan")) def test_action_repeat_consistency(self): self.assertTrue(T2.action_repeat_consistent([[0.1, 0.2], [0.1, 0.2], [0.3, 0.4], [0.3, 0.4]])) self.assertFalse(T2.action_repeat_consistent([[0.1, 0.2], [0.1, 0.25]])) self.assertFalse(T2.action_repeat_consistent([[0.1, 0.2]])) def test_bad_scale_is_refused(self): for half in (0, 1.5, float("nan")): with self.assertRaises(ValueError): T2.scale(6, KNEE, half) def test_progress_lines(self): self.assertEqual((T2.progress_total(True), T2.progress_total(False)), (21, 11)) if __name__ == "__main__": unittest.main()