def test_comparison(): continuous_spaces = [make_box([-1, 2.3], [45, 4.3]), make_box([-10], [45])] space = SimpleHybrid(continuous_spaces) continuous_spaces2 = [ make_box([-1, 54], [45, 4.3]), make_box([-10], [445]) ] space2 = SimpleHybrid(continuous_spaces2) assert space == space assert space != "hi" assert space != space2 assert issubclass(SimpleHybrid, HybridBase)
def test_invalid_arguments(): with pytest.raises(AssertionError): SimpleHybrid("string") with pytest.raises(AssertionError): SimpleHybrid(1.343) with pytest.raises(ValueError): SimpleHybrid([]) with pytest.raises(AssertionError): SimpleHybrid([1, 2]) with pytest.raises(AssertionError): continuous_spaces = [ make_box([-1, 2.3], [45, 4.3]), make_box([-10], [45]), make_box([50, 34, 0], [100, 120, 2]), ] SimpleHybrid([Discrete(10)] + continuous_spaces)
def test_repr_does_not_throw_error(): from hmlf.spaces import Box continuous_spaces = [make_box([-1, 2.3], [45, 4.3]), make_box([-10], [45])] space = SimpleHybrid(continuous_spaces) represensation_string = repr(space) represensation_string = represensation_string.replace(", float32", "") eval(represensation_string) assert "SimpleHybrid" in represensation_string
def test_dimensions(): space = SimpleHybrid( [make_box(shape=(1, )), make_box(shape=(3, )), make_box(shape=(2, ))]) assert space.n_discrete_options == 3 assert space.get_n_discrete_spaces() == 1 assert space.get_n_discrete_options() == 3 assert isinstance(space._get_continuous_spaces(), list) assert len(space._get_continuous_spaces()) == 3 assert space.get_n_continuous_spaces() == 3 assert space.get_n_continuous_options() == (1 + 3 + 2) assert space._get_dimensions_of_continuous_spaces() == [1, 3, 2] assert np.array_equal(space.split_indices, [1, 4]) assert space.get_dimension() == (1 + 1 + 3 + 2)
def test_build_action(): continuous_spaces = [make_box([-1, 2.3], [45, 4.3]), make_box([-10], [45])] space = SimpleHybrid(continuous_spaces) discrete = np.array([2, 0, 1]) parameters = np.array([ [0, 3, 2], [-10, 3, 63.1], [0, 3, 50], ]) action = space.build_action(discrete, parameters) assert isinstance(action, list) assert len(action) == 3 assert len(action[0]) == 3 assert isinstance(action[0], tuple) assert action[0][0] == 2 assert action[1][0] == 0 assert action[2][0] == 1 assert np.allclose(action[0][1], [0.0, 3.0]) assert np.allclose(action[0][2], [2]) assert np.allclose(action[1][1], [-1, 3]) assert np.allclose(action[1][2], [45])
def test_low_high_concatination(): continuous_spaces = [ make_box([-1, 2.3], [45, 4.3]), make_box([-10], [45]), make_box([50, 34, 0], [100, 120, 2]), ] space = SimpleHybrid(continuous_spaces) print(space.continuous_low) print(space.continuous_high) assert np.allclose(space.continuous_low, [-1.0, 2.3, -10.0, 50.0, 34.0, 0.0]) assert np.allclose(space.continuous_high, [45.0, 4.3, 45.0, 100.0, 120.0, 2.0])
def __init__(self): self.max_move = np.float32(0.2) self.max_jump = 0.05 self.n_obstacles = 3 self.goal_position = 0.95 self.obstacle_thickness = 0.01 self.jump_threshold = 0.1 self.max_timesteps = 30 self.goal_threshold = 0.05 self.action_space = SimpleHybrid([ Box(-self.max_move, self.max_move, (1, )), Box(np.float32(0), np.float32(1), (1, )) ]) self.observation_space = Box(low=np.zeros(7, dtype=np.float32), high=np.ones(7, dtype=np.float32)) self.position = 0 self.time = 0 self.obstacle_position = np.zeros(self.n_obstacles) self.obstacle_target_height = np.zeros(self.n_obstacles)
def test_init_simple_hybrid(spaces): hybrid_space = SimpleHybrid(spaces) assert hybrid_space.get_n_discrete_options() == len(spaces) sample = hybrid_space.sample() # check datatypes assert type(sample) == tuple assert type(sample[0]) == int for s in sample[1:]: assert type(s) == np.ndarray # check that sample and fomat_actions work consistently and correctly sample_list = [hybrid_space.sample(), hybrid_space.sample()] sample_array = np.vstack([np.hstack(sample_list[0]), np.hstack(sample_list[1])]) new_sample_list = hybrid_space.format_action(sample_array) for sample, new_sample in zip(sample_list, new_sample_list): for action, new_action in zip(sample, new_sample): assert np.sum(np.abs(action - new_action)) < 1e-6
def __init__( self, observation_space: Space, action_space: SimpleHybrid, lr_schedule_q: Schedule, lr_schedule_parameter: Schedule, net_arch_q: Optional[List[int]] = None, net_arch_parameter: Optional[List[int]] = None, activation_fn: Type[nn.Module] = nn.ReLU, features_extractor_class: Type[ BaseFeaturesExtractor] = FlattenExtractor, features_extractor_kwargs: Optional[Dict[str, Any]] = None, normalize_images: bool = True, optimizer_class: Type[th.optim.Optimizer] = th.optim.Adam, optimizer_kwargs: Optional[Dict[str, Any]] = None, ): super(MPDQNPolicy, self).__init__( observation_space, # :Space, action_space, # :SimpleHybrid, lr_schedule_q, # :Schedule, lr_schedule_parameter, # :Schedule, net_arch_q, # :Optional[List[int]] = None, net_arch_parameter, # :Optional[List[int]] = None, activation_fn, # :Type[nn.Module] = nn.ReLU, features_extractor_class, # :Type[BaseFeaturesExtractor] = FlattenExtractor, features_extractor_kwargs, # :Optional[Dict[str, Any]] = None, normalize_images, # :bool = True, optimizer_class, # :Type[th.optim.Optimizer] = th.optim.Adam, optimizer_kwargs, # :Optional[Dict[str, Any]] = None, ) # For formatting inside forward Q self.discrete_action_size = self.action_space_q.n self.state_size = observation_space.shape[0] self.offsets = np.cumsum( action_space._get_dimensions_of_continuous_spaces()) self.offsets = np.insert(self.offsets, 0, 0)
def test_input_tuple(spaces): space_list = SimpleHybrid(spaces) space_tuple = SimpleHybrid(tuple(spaces)) assert space_list == space_tuple
def _build_action_space(self) -> SimpleHybrid: parameter_spaces = self._build_parameter_spaces() spaces = parameter_spaces return SimpleHybrid(spaces)