示例#1
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.map_name, 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()
示例#2
0
    def test_valid_feature_specification_is_parsed(self):
        agent_interface_format = features.parse_agent_interface_format(
            feature_screen=32, feature_minimap=(24, 24))

        self.assertEqual(agent_interface_format.feature_dimensions.screen,
                         point.Point(32, 32))

        self.assertEqual(agent_interface_format.feature_dimensions.minimap,
                         point.Point(24, 24))
示例#3
0
    def test_valid_action_space_is_parsed(self, action_space):
        agent_interface_format = features.parse_agent_interface_format(
            feature_screen=32,
            feature_minimap=(24, 24),
            rgb_screen=64,
            rgb_minimap=(48, 48),
            action_space=action_space)

        self.assertEqual(agent_interface_format.action_space,
                         actions.ActionSpace[action_space])
 def interface(self):
     agent_interface_format = features.parse_agent_interface_format(
         feature_screen=FLAGS.feature_screen_size,
         feature_minimap=FLAGS.feature_minimap_size,
         rgb_screen=FLAGS.rgb_screen_size,
         rgb_minimap=FLAGS.rgb_minimap_size,
         action_space=FLAGS.action_space,
         use_feature_units=FLAGS.use_feature_units)
     return sc2_env.SC2Env._get_interface(agent_interface_format,
                                          require_raw=False)
示例#5
0
 def _thunk():
     agent_interface = features.parse_agent_interface_format(
         feature_screen=64, feature_minimap=64)
     env = sc2_env.SC2Env(
         map_name=map_name,
         step_mul=step_mul,
         agent_interface_format=agent_interface,
         # screen_size_px=(screen_size, screen_size),
         # minimap_size_px=(minimap_size, minimap_size),
         visualize=False)
     return env
示例#6
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()
示例#7
0
    def start(self):
        # importing here to lazy-load
        from pysc2.env import sc2_env

        self._env = sc2_env.SC2Env(
            map_name=self.id,
            visualize=self.render,
            agent_interface_format=[features.parse_agent_interface_format(
                feature_screen=self.spatial_dim,
                feature_minimap=self.spatial_dim,
                rgb_screen=None,
                rgb_minimap=None
            )],
            step_mul=self.step_mul,)
示例#8
0
    def start(self):
        # importing here to lazy-load
        from pysc2.env import sc2_env

        self._env = sc2_env.SC2Env(
            map_name=self.map_name,
            visualize=self.render,
            agent_interface_format=[features.parse_agent_interface_format(
                feature_screen=self.spatial_dim,
                feature_minimap=self.spatial_dim,
                rgb_screen=None,
                rgb_minimap=None
            )],
            step_mul=self.step_mul,)
示例#9
0
def sc2_feature_env(env_id, seed, replay_dir, render):
    agent_interface_format = parse_agent_interface_format(
        feature_screen=84,
        feature_minimap=84,
        action_space='FEATURES'
    )
    env = SC2Env(
        map_name=env_id,
        step_mul=8,
        game_steps_per_episode=0,
        discount=0.99,
        agent_interface_format=agent_interface_format,
        random_seed=seed,
        save_replay_episodes=1 if replay_dir is not None else 0,
        replay_dir=replay_dir,
        visualize=render
    )
    env = AdeptSC2Env(env)
    return env
示例#10
0
    def start(self):
        # importing here to lazy-load
        from pysc2.env import sc2_env

        # fail-safe if executed not as absl app
        if not flags.FLAGS.is_parsed():
            flags.FLAGS(sys.argv)

        self._env = sc2_env.SC2Env(
            map_name=self.id,
            visualize=self.render,
            agent_interface_format=[
                features.parse_agent_interface_format(
                    feature_screen=self.spatial_dim,
                    feature_minimap=self.spatial_dim,
                    rgb_screen=None,
                    rgb_minimap=None)
            ],
            step_mul=self.step_mul,
        )
示例#11
0
    def start(self):
        # importing here to lazy-load

        # fail-safe if executed not as absl app
        if not flags.FLAGS.is_parsed():
            flags.FLAGS([""])
        #            flags.FLAGS(sys.argv)
        try:
            self._env = sc2_env.SC2Env(
                map_name=self.id,
                visualize=self.render,
                agent_interface_format=[features.parse_agent_interface_format(
                    feature_screen=self._screen_dim,
                    feature_minimap=self._minimap_dim,
                    rgb_screen=None,
                    rgb_minimap=None
                )],
                step_mul=self.step_mul,)
                # game_steps_per_episode=10
        except protocol.ConnectionError:
            self.start()
    def __init__(self):
        """Constructs the game controller object.

        Args:
          config: Interface configuration options.
        """
        agent_interface_format = features.parse_agent_interface_format(
            feature_screen=FLAGS.feature_screen_size,
            feature_minimap=FLAGS.feature_minimap_size,
            rgb_screen=FLAGS.rgb_screen_size,
            rgb_minimap=FLAGS.rgb_minimap_size,
            action_space=FLAGS.action_space,
            use_feature_units=FLAGS.use_feature_units)
        self._interface = sc2_env.SC2Env._get_interface(agent_interface_format,
                                                        require_raw=False)
        self._player_id = 1
        self._sc2_proc = None
        self._controller = None
        self._replay_data = None

        self._initialize()
示例#13
0
    episode_summary.value.add(simple_value=stats.episode_lengths[i_episode],
                              node_name="episode_length",
                              tag="episode_length")
    q_estimator.summary_writer.add_summary(episode_summary, total_t)
    q_estimator.summary_writer.flush()

    yield total_t, plotting.EpisodeStats(
        episode_lengths=stats.episode_lengths[:i_episode + 1],
        episode_rewards=stats.episode_rewards[:i_episode + 1])

    return stats


if __name__ == '__main__':
    agent_interface_format = features.parse_agent_interface_format(
        rgb_screen=84,
        rgb_minimap=84,
    )

    env = sc2_env.SC2Env(
        map_name=
        "MoveToBeacon",  #CollectMineralShards, DefeatRoaches, DefeatZerglingsAndBanelings
        step_mul=step_mul,
        agent_interface_format=agent_interface_format)

    env = PySC2EnvWrapper(env)
    tf.reset_default_graph()

    # Where we save our checkpoints and graphs
    experiment_dir = os.path.abspath(
        "./experiments/")  #{}".format(env.spec.id))
    # Create a glboal step variable
示例#14
0
        i += 1
        steps = 0
        x = 0
        qt = None
        states = None
        stack = Stack()
        stack_score = Stack()
        total_start_time = time.time()

        with sc2_env.SC2Env(players=[sc2_env.Agent(sc2_env.Race.terran)],
                            map_name=maps.get(args.map),
                            realtime=False,
                            agent_interface_format=[
                                features.parse_agent_interface_format(
                                    feature_screen=16,
                                    feature_minimap=16,
                                    rgb_screen=None,
                                    rgb_minimap=None)
                            ],
                            visualize=args.visualize) as env:

            if Path(p / args.folder /
                    '{0}_qtable.npy'.format(args.map)).is_file() and Path(
                        p / args.folder / '{0}_states.npy'.format(
                            args.map)).is_file() and args.load_qtable:
                print('Loaded QTable and States from Files')
                qt = p / args.folder / '{0}_qtable.npy'.format(args.map)
                states = p / args.folder / '{0}_states.npy'.format(args.map)
            else:
                print('Started with fresh QTable')
示例#15
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
示例#16
0
 def test_no_arguments_raises(self):
     with self.assertRaises(ValueError):
         features.parse_agent_interface_format()
示例#17
0
 def test_invalid_minimap_combinations_raise(self, screen, minimap):
     with self.assertRaises(ValueError):
         features.parse_agent_interface_format(rgb_screen=screen,
                                               rgb_minimap=minimap)
示例#18
0
    def __init__(self, config: SC2EnvironmentConfig):
        self._aif = parse_agent_interface_format(
            feature_screen=config.screen_size,
            feature_minimap=config.minimap_size)
        self._visualize = config.visualize

        if config.function_set == 'all':
            self._func_ids = [f.id for f in FUNCTIONS]
        elif config.function_set == 'minigames':
            self._func_ids = [
                0, 1, 2, 3, 4, 6, 7, 12, 13, 42, 44, 50, 91, 183, 234, 309,
                331, 332, 333, 334, 451, 452, 490
            ]
        else:
            raise ValueError

        sc2_features = Features(agent_interface_format=self._aif)
        sc2_action_spec = sc2_features.action_spec()
        sc2_obs_spec = sc2_features.observation_spec()

        fn_args_mask = np.zeros(
            (len(self._func_ids), len(sc2_action_spec.types) + 1),
            dtype=np.bool)
        fn_args_mask[:, 0] = 1
        for i, func_id in enumerate(self._func_ids):
            used_args = [a.id + 1 for a in FUNCTIONS[func_id].args]
            fn_args_mask[i, used_args] = 1
        action_spec = [('function_id',
                        ActionSpec(0, (len(self._func_ids), ), None,
                                   fn_args_mask))]
        for t in sc2_action_spec.types:
            if t.name == 'screen' or t.name == 'screen2':
                space = 'screen'
            elif t.name == 'minimap':
                space = 'minimap'
            else:
                space = None

            action_spec.append(
                (t.name, ActionSpec(len(action_spec), t.sizes, space, None)))
        action_spec = OrderedDict(action_spec)

        def feature_spec(features):
            return [
                FeatureSpec(f.index, f.scale,
                            f.type == FeatureType.CATEGORICAL)
                for f in features
            ]

        obs_spec = OrderedDict([
            ('screen',
             ObservationSpec(0, sc2_obs_spec['feature_screen'], True,
                             feature_spec(SCREEN_FEATURES))),
            ('minimap',
             ObservationSpec(1, sc2_obs_spec['feature_minimap'], True,
                             feature_spec(MINIMAP_FEATURES))),
            ('available_actions',
             ObservationSpec(2, (len(self._func_ids), ), False, None)),
            ('player', ObservationSpec(3, sc2_obs_spec['player'], False, None))
        ])

        self.spec = EnvironmentSpec(action_spec, obs_spec)

        from pysc2.env.sc2_env import SC2Env, Agent, Race

        if not flags.FLAGS.is_parsed():
            flags.FLAGS(sys.argv)

        num_retries = 3
        while True:
            try:
                self._env = SC2Env(map_name=config.map_name,
                                   agent_interface_format=self._aif,
                                   players=[Agent(Race.protoss)],
                                   visualize=self._visualize)

                break
            except RequestError:
                num_retries -= 1
                logging.error(
                    'SC2Env creation failed, {} retries remaining'.format(
                        num_retries))
                if num_retries <= 0:
                    raise

        atexit.register(self._env.close)
示例#19
0
 def test_invalid_action_space_raises(self):
     with self.assertRaises(KeyError):
         features.parse_agent_interface_format(
             feature_screen=64,
             feature_minimap=64,
             action_space="UNKNOWN_ACTION_SPACE")