def test_observe_first_state_returns_valid_state(self): sim = SimStub.make_valid_state_stub(self.task) first_state = self.task.observe_first_state(sim) self.assertEqual(len(first_state), len(self.task.state_variables)) self.assertIsInstance(first_state, tuple)
def test_observe_first_state_creates_target_heading_in_expected_range(self): sim = SimStub.make_valid_state_stub(self.task) _ = self.task.observe_first_state(sim) desired_heading = sim[HeadingControlTask.target_track_deg] self.assertGreaterEqual(desired_heading, 0) self.assertLessEqual(desired_heading, 360)
def test_task_first_observation_inputs_controls(self): dummy_sim = SimStub.make_valid_state_stub(self.task) _ = self.task.observe_first_state(dummy_sim) # check engine as expected self.assertAlmostEqual(self.task.THROTTLE_CMD, dummy_sim[prp.throttle_cmd]) self.assertAlmostEqual(self.task.MIXTURE_CMD, dummy_sim[prp.mixture_cmd]) self.assertAlmostEqual(1.0, dummy_sim[prp.engine_running])
def setUp(self): self.task = self.make_task() sim = SimStub.make_valid_state_stub(self.task) _ = self.task.observe_first_state( sim) # causes task to init new-episode attributes self.dummy_action = np.asarray( [0 for _ in range(len(self.task.action_variables))])
def test_observe_first_state_changes_target_heading(self): sim = SimStub.make_valid_state_stub(self.task) _ = self.task.observe_first_state(sim) desired_heading = sim[HeadingControlTask.target_track_deg] _ = self.task.observe_first_state(sim) new_desired_heading = sim[HeadingControlTask.target_track_deg] self.assertNotEqual(desired_heading, new_desired_heading)
def test_task_step_returns_terminal_time_equals_max(self): sim = SimStub.make_valid_state_stub(self.task) _ = self.task.observe_first_state(sim) terminal_time = self.default_episode_time_s sim[prp.sim_time_s] = terminal_time steps = 1 _, _, is_terminal, _ = self.task.task_step(sim, self.dummy_action, steps) self.assertTrue(is_terminal)
def test_task_step_returns_reward_in_info(self): sim = SimStub.make_valid_state_stub(self.task) steps = 1 _ = self.task.observe_first_state(sim) _, reward_scalar, _, info = self.task.task_step(sim, self.dummy_action, steps) reward_object = info['reward'] self.assertIsInstance(reward_object, rewards.Reward) self.assertAlmostEqual(reward_object.agent_reward(), reward_scalar)
def test_observe_first_state_correct_track_error(self): self.setUp() sim = SimStub.make_valid_state_stub(self.task) _ = self.task.observe_first_state(sim) track_deg = prp.Vector2.from_sim(sim).heading_deg() target_track_deg = sim[self.task.target_track_deg] error_deg = track_deg - target_track_deg expected_acute_error_deg = utils.reduce_reflex_angle_deg(error_deg) actual_error_deg = sim[self.task.track_error_deg] self.assertAlmostEqual(expected_acute_error_deg, actual_error_deg)
def get_initial_state_sim(self, task=None) -> SimStub: if task is None: task = self.task sim = SimStub.make_valid_state_stub(task) # set properties to reasonable initial episode values sim[prp.sim_time_s] = 0.0 sim[prp.dist_travel_m] = 0.0 sim[prp.heading_deg] = task.INITIAL_HEADING_DEG sim[prp.altitude_sl_ft] = task.get_initial_conditions()[prp.initial_altitude_ft] sim[prp.roll_rad] = 0 return sim
def test_task_step_correct_return_types(self): sim = SimStub.make_valid_state_stub(self.task) steps = 1 _ = self.task.observe_first_state(sim) state, reward, is_terminal, info = self.task.task_step(sim, self.dummy_action, steps) self.assertIsInstance(state, tuple) self.assertEqual(len(state), len(self.task.state_variables)) self.assertIsInstance(reward, float) self.assertIsInstance(is_terminal, bool) self.assertIsInstance(info, dict)
def get_perfect_state_sim(self, task=None, time_terminal=True) -> SimStub: if task is None: task = self.task sim = SimStub.make_valid_state_stub(task) task.observe_first_state(sim) perfect_track = sim[task.target_track_deg] perfect_altitude = task._get_target_altitude() perfect_roll = 0.0 perfect_sideslip = 0.0 self.modify_sim_to_state_(sim, task, steps_terminal=time_terminal, track_deg=perfect_track, altitude_ft=perfect_altitude, roll_rad=perfect_roll, sideslip_deg=perfect_sideslip) return sim
def get_initial_sim_with_state(self, task: HeadingControlTask = None, time_terminal=False, track_deg=None, altitude_ft=None, roll_rad=0.0) -> SimStub: if task is None: task = self.task sim = SimStub.make_valid_state_stub(task) task.observe_first_state(sim) if track_deg is None: # get the target value and use that (i.e. perfect state) track_deg = sim[task.target_track_deg] perfect_initial_sim = self.modify_sim_to_state_(sim, task, time_terminal, track_deg, altitude_ft, roll_rad) # the task set its last_state attr when it observed the first state - reset this # based on the perfect sim values we just set. Ugh. perfect_state = task.State(*(sim[prop] for prop in task.state_variables)) task.last_state = perfect_state return perfect_initial_sim