class FlightTaskStub(FlightTask): """ A minimal Task for testing. """ test_property1 = prp.BoundedProperty('test_property1', 'dummy property for testing', -1, 1) test_property2 = prp.BoundedProperty('test_property2', 'dummy property for testing', -1, 1) def __init__(self, *_): self.state_variables = (self.test_property1, self.test_property2) self.action_variables = (prp.aileron_cmd, prp.elevator_cmd) super().__init__(AssessorStub()) def _is_terminal(self, _: Tuple[float, ...], __: float) -> bool: return False def get_initial_conditions(self): return self.base_initial_conditions def get_state(self, value1: float, value2: float): """ Returns a State of this class' test properties populated with input values """ return self.State(value1, value2) def get_props_to_output(self) -> Tuple: return prp.u_fps, prp.altitude_sl_ft, prp.heading_deg @staticmethod def get_dummy_state_class_and_properties(length: int): dummy_properties = tuple( prp.Property('test_prop' + str(i), '') for i in range(length)) DummyState = collections.namedtuple( 'DummyState', [prop.name for prop in dummy_properties]) return DummyState, dummy_properties @staticmethod def get_dummy_state_and_properties( values: Iterable[float] ) -> Tuple[NamedTuple, Tuple[prp.Property, ...]]: """ given a collection of floats, creates dummy Properties for each value and inits a State """ values_safe = tuple(values) DummyState, props = FlightTaskStub.get_dummy_state_class_and_properties( len(values_safe)) return DummyState(*values_safe), props def _reward_terminal_override(self, reward: rewards.Reward, sim: Simulation): return False
def test_initialise_conditions_basic_config(self): plane = aircraft.f15 # manually reset JSBSim instance with new initial conditions if self.sim: self.sim.close() sim_frequency = 2 self.sim = Simulation(sim_frequency_hz=sim_frequency, aircraft=plane, init_conditions=None) self.assertEqual(self.sim.get_loaded_model_name(), plane.jsbsim_id, msg='JSBSim did not load expected aircraft model: ' + self.sim.get_loaded_model_name()) # check that properties are as we expected them to be expected_values = { prp.initial_u_fps: 328.0, prp.initial_v_fps: 0.0, prp.initial_w_fps: 0.0, prp.u_fps: 328.0, prp.v_fps: 0.0, prp.w_fps: 0.0, prp.BoundedProperty('simulation/dt', '', None, None): 1 / sim_frequency } for prop, expected in expected_values.items(): actual = self.sim[prop] self.assertAlmostEqual(expected, actual)
def test_asl_agl_elevations_equal(self): # we want the height above sea level to equal ground elevation at all times self.setUp(agent_interaction_freq=1) for i in range(25): self.env.step(action=self.env.action_space.sample()) alt_sl = self.env.sim[prp.altitude_sl_ft] alt_gl = self.env.sim[prp.BoundedProperty('position/h-agl-ft', '', 0, 0)] self.assertAlmostEqual(alt_sl, alt_gl)
def test_get_bad_property(self): self.setUp() bad_prop = prp.BoundedProperty("bad_prop_name", "", 0, 0) with self.assertRaises(KeyError): _ = self.sim[bad_prop]