Example #1
0
def act_obs_wrapper_generator():
    mock_env = mock_sc2_env.SC2TestEnv(
        map_name=0,
        agent_interface_format=[
            features.parse_agent_interface_format(feature_screen=screen_size,
                                                  feature_minimap=minimap_size)
        ])

    obs_features = {
        'screen': [
            'player_relative', 'selected', 'visibility_map',
            'unit_hit_points_ratio', 'unit_density'
        ],
        'minimap': ['player_relative', 'selected', 'visibility_map', 'camera'],
        # available actions should always be present and in first position
        'non-spatial': ['available_actions', 'player']
    }

    action_ids = [
        0, 1, 2, 3, 4, 6, 7, 12, 13, 42, 44, 50, 91, 183, 234, 309, 331, 332,
        333, 334, 451, 452, 490
    ]
    act_wrapper = ActionWrapper(16, action_ids)
    act_wrapper.make_spec(mock_env.action_spec())

    obs_wrapper = ObservationWrapper(obs_features, action_ids)
    obs_wrapper.make_spec(mock_env.observation_spec())

    return obs_wrapper, act_wrapper
Example #2
0
 def _init_env(self):
     if self.mock:
         env = mock_sc2_env.SC2TestEnv(**self.settings)
     else:
         env = sc2_env.SC2Env(**self.settings)
     self._wrapped_env = env
     return env
Example #3
0
 def test_episode(self):
     env = mock_sc2_env.SC2TestEnv(
         map_name='nonexistant map',
         agent_interface_format=features.AgentInterfaceFormat(
             feature_dimensions=features.Dimensions(screen=64, minimap=32)))
     env.episode_length = float('inf')
     self._test_episode(env)
Example #4
0
 def test_episode_length(self):
     env = mock_sc2_env.SC2TestEnv(
         map_name='nonexistant map',
         agent_interface_format=features.AgentInterfaceFormat(
             feature_dimensions=features.Dimensions(screen=64, minimap=32)))
     self.assertEqual(env.episode_length, 10)
     self._test_episode_length(env, length=10)
Example #5
0
 def test_episode(self):
   env = mock_sc2_env.SC2TestEnv(
       map_name='nonexistant map',
       feature_screen_size=64,
       feature_minimap_size=32)
   env.episode_length = float('inf')
   self._test_episode(env)
Example #6
0
 def test_episode_length(self):
   env = mock_sc2_env.SC2TestEnv(
       map_name='nonexistant map',
       feature_screen_size=64,
       feature_minimap_size=32)
   self.assertEqual(env.episode_length, 10)
   self._test_episode_length(env, length=10)
Example #7
0
    def make_specs(self):
        # importing here to lazy-load
        from pysc2.env import mock_sc2_env
        mock_env = mock_sc2_env.SC2TestEnv(
            map_name=self.id,
            agent_interface_format=[
                features.parse_agent_interface_format(
                    feature_screen=self._screen_dim,
                    feature_minimap=self._minimap_dim)
            ])
        self.act_wrapper.make_spec(mock_env.action_spec(),
                                   mock_env.observation_spec())
        self.obs_wrapper.make_spec(mock_env.observation_spec())

        self.input_tensor_specs = []
        self.action_specs = []
        self.func_masks = []

        action_steps = [-1, 0, 1]
        for action_step in action_steps:
            input_spec, action_spec, func_action_mask = self.arg_spec_generator(
                action_step)
            self.input_tensor_specs.append(input_spec)
            self.action_specs.append(action_spec)
            self.func_masks.append(func_action_mask)
        mock_env.close()

        self._time_step_spec = ts.time_step_spec(self.obs_wrapper.obs_spec)

        return
 def setUpClass(cls):
     # super(TestCompareEnvironments, cls).setUpClass()
     super().setUpClass()
     players = [
         sc2_env.Agent(race=sc2_env.Race.terran),
         sc2_env.Agent(race=sc2_env.Race.protoss),
     ]
     kwargs = {
         'map_name':
         'Flat64',
         'players':
         players,
         'agent_interface_format': [
             sc2_env.AgentInterfaceFormat(
                 feature_dimensions=sc2_env.Dimensions(screen=(32, 64),
                                                       minimap=(8, 16)),
                 rgb_dimensions=sc2_env.Dimensions(screen=(31, 63),
                                                   minimap=(7, 15)),
                 action_space=sc2_env.ActionSpace.FEATURES),
             sc2_env.AgentInterfaceFormat(
                 rgb_dimensions=sc2_env.Dimensions(screen=64, minimap=32))
         ]
     }
     cls._env = sc2_env.SC2Env(**kwargs)
     cls._mock_env = mock_sc2_env.SC2TestEnv(**kwargs)
    def test_feature_units_are_supported(self):
        env = mock_sc2_env.SC2TestEnv(map_name='nonexistant map',
                                      feature_screen_size=64,
                                      feature_minimap_size=32,
                                      use_feature_units=True)

        self.assertIn('feature_units', env.observation_spec()[0])
Example #10
0
    def test_feature_units_are_supported(self):
        env = mock_sc2_env.SC2TestEnv(
            map_name='nonexistant map',
            agent_interface_format=features.AgentInterfaceFormat(
                feature_dimensions=features.Dimensions(screen=64, minimap=32),
                use_feature_units=True))

        self.assertIn('feature_units', env.observation_spec()[0])
Example #11
0
 def make_specs(self):
     # importing here to lazy-load
     from pysc2.env import mock_sc2_env
     mock_env = mock_sc2_env.SC2TestEnv(map_name=self.id, agent_interface_format=[
         features.parse_agent_interface_format(feature_screen=self.spatial_dim, feature_minimap=self.spatial_dim)])
     self.act_wrapper.make_spec(mock_env.action_spec())
     self.obs_wrapper.make_spec(mock_env.observation_spec())
     mock_env.close()
Example #12
0
 def test_screen_minimap_size(self):
     env = mock_sc2_env.SC2TestEnv(
         map_name='nonexistant map',
         agent_interface_format=features.AgentInterfaceFormat(
             feature_dimensions=features.Dimensions(screen=(84, 87),
                                                    minimap=(64, 67))))
     timestep = env.reset()
     self.assertLen(timestep, 1)
     self.assert_spec(timestep[0].observation['feature_screen'],
                      [len(features.SCREEN_FEATURES), 87, 84], np.int32)
     self.assert_spec(timestep[0].observation['feature_minimap'],
                      [len(features.MINIMAP_FEATURES), 67, 64], np.int32)
 def test_screen_minimap_size(self):
     env = mock_sc2_env.SC2TestEnv(map_name='nonexistant map',
                                   feature_screen_width=84,
                                   feature_screen_height=87,
                                   feature_minimap_width=64,
                                   feature_minimap_height=67)
     timestep = env.reset()
     self.assertLen(timestep, 1)
     self.assert_spec(timestep[0].observation['feature_screen'],
                      [len(features.SCREEN_FEATURES), 87, 84], np.int32)
     self.assert_spec(timestep[0].observation['feature_minimap'],
                      [len(features.MINIMAP_FEATURES), 67, 64], np.int32)
 def setUpClass(cls):
     players = [
         sc2_env.Agent(race=sc2_env.Race.terran),
         sc2_env.Bot(race=sc2_env.Race.protoss,
                     difficulty=sc2_env.Difficulty.very_easy),
     ]
     kwargs = {
         'map_name': 'Flat64',
         'players': players,
         'feature_screen_height': 64,
         'feature_screen_width': 32,
         'feature_minimap_height': 16,
         'feature_minimap_width': 8,
         'rgb_screen_height': 63,
         'rgb_screen_width': 31,
         'rgb_minimap_height': 15,
         'rgb_minimap_width': 7,
         'action_space': sc2_env.ActionSpace.FEATURES,
     }
     cls._env = sc2_env.SC2Env(**kwargs)
     cls._mock_env = mock_sc2_env.SC2TestEnv(**kwargs)
Example #15
0
    def _make_obs_space(self):
        # Note: might be out of spec easily, e.g. high is set to 1000, I do not know what the actual high can be
        box_lower_bound = 0
        box_higher_bound = 1000

        # mocking the sc2_env since we are making the observation space before the env is even created,
        # -> therefore we mock it with the same settings
        mock_env = mock_sc2_env.SC2TestEnv(**self._env_settings)
        timesteps = mock_env.reset()
        ts = timesteps[0]

        general_info = self.encode_general_info(player_info=ts.observation['player'],
                                                available_actions=ts.observation['available_actions'],
                                                last_actions=ts.observation['last_actions'])
        general_space = spaces.MultiBinary(general_info.size)

        encoded_screen = self.encode_screen(ts.observation['feature_screen'])
        screen_space = spaces.Box(low=box_lower_bound, high=box_higher_bound, shape=encoded_screen.shape,
                                  dtype=np.float32)

        encoded_minimap = self.encode_minimap(ts.observation['feature_minimap'])
        minimap_space = spaces.Box(low=box_lower_bound, high=box_higher_bound, shape=encoded_minimap.shape,
                                   dtype=np.float32)

        encoded_entities = self.encode_feature_units(ts.observation['feature_units'])
        entities_space = spaces.Box(low=box_lower_bound, high=box_higher_bound, shape=encoded_entities.shape,
                                    dtype=np.float32)

        space = spaces.Dict(
            {'info': general_space,
             'screen': screen_space,
             'minimap': minimap_space,
             'entities': entities_space
             })

        mock_env.close()  # might do nothing
        return space
Example #16
0
def _create_sa_space(obs_features, action_set, agent_interface_format,
                     action_spatial_dim):
    # (adapted from 'reaver/envs/sc2/env.py')
    obs_features = OBS_FEATURES[obs_features]
    action_ids = ACTION_SET[action_set]
    obs_features = obs_features or reaver.envs.sc2.MINIMAL_FEATURES.copy()
    action_ids = action_ids or reaver.envs.sc2.MINIMAL_ACTION_SET[:]

    obs_spatial_dim = agent_interface_format.feature_dimensions.screen[0]
    action_spatial_dim = action_spatial_dim or obs_spatial_dim
    act_wrapper = ActionWrapper(action_spatial_dim,
                                action_ids,
                                obs_spatial_dim=obs_spatial_dim)
    obs_wrapper = ObservationWrapper(obs_features, action_ids)

    mock_env = mock_sc2_env.SC2TestEnv(
        agent_interface_format=[agent_interface_format])

    act_wrapper.make_spec(mock_env.action_spec())
    obs_wrapper.make_spec(mock_env.observation_spec())
    mock_env.close()

    return ReaverStateActionSpace(agent_interface_format, obs_spatial_dim,
                                  action_spatial_dim, obs_wrapper, act_wrapper)
Example #17
0
def mixed_q_netwwork_test():

    mock_env = mock_sc2_env.SC2TestEnv(
        map_name=0,
        agent_interface_format=[
            features.parse_agent_interface_format(feature_screen=16,
                                                  feature_minimap=16)
        ])

    obs_features = {
        'screen': [
            'player_relative', 'selected', 'visibility_map',
            'unit_hit_points_ratio', 'unit_density'
        ],
        'minimap': ['player_relative', 'selected', 'visibility_map', 'camera'],
        # available actions should always be present and in first position
        'non-spatial': ['available_actions', 'player']
    }

    action_ids = [
        0, 1, 2, 3, 4, 6, 7, 12, 13, 42, 44, 50, 91, 183, 234, 309, 331, 332,
        333, 334, 451, 452, 490
    ]
    act_wrapper = ActionWrapper(16, action_ids)
    act_wrapper.make_spec(mock_env.action_spec())

    obs_wrapper = ObservationWrapper(obs_features, action_ids)
    obs_wrapper.make_spec(mock_env.observation_spec())

    action_step = 1
    action_specs = \
        extract_action_spec(action_step, act_wrapper._merged_spatial_action_spec, act_wrapper._merged_structured_action_spec)
    pevious_action_specs = \
        extract_action_spec(action_step-1, act_wrapper._merged_spatial_action_spec, act_wrapper._merged_structured_action_spec)
    input_tensor_specs = obs_wrapper.obs_spec

    with tf.compat.v1.variable_scope('step2',
                                     reuse=tf.compat.v1.AUTO_REUSE) as scope:
        mixed_q_network = MixedQNetwork(input_tensor_specs, action_specs,
                                        pevious_action_specs)

    screen_image = tf.ones((1, 16, 16, len(input_tensor_specs['screen'])),
                           tf.float32)
    minimap_image = tf.ones((1, 24, 24, len(input_tensor_specs['minimap'])),
                            tf.float32)

    observation = dict()
    observation['screen'] = tf.unstack(screen_image,
                                       len(input_tensor_specs['screen']),
                                       axis=-1)
    observation['minimap'] = tf.unstack(minimap_image,
                                        len(input_tensor_specs['minimap']),
                                        axis=-1)

    for i in range(len(input_tensor_specs['screen'])):
        observation['screen'][i] = tf.cast(
            observation['screen'][i], input_tensor_specs['screen'][i].dtype)

    for i in range(len(input_tensor_specs['minimap'])):
        observation['minimap'][i] = tf.cast(
            observation['minimap'][i], input_tensor_specs['minimap'][i].dtype)

    previous_action = tf.ones((1), tf.int32)
    output = mixed_q_network(observation)

    return