pysc2简单教程-4:使用pysc2的环境并与其进行交互

2021-05-23  本文已影响0人  超级超级小天才

使用pysc2的环境并与其进行交互

关于pysc2使用的全部内容请参考【文集:pysc2的简单教程】

作为应用于强化学习的SCII环境是被定义在 pysc2.env.sc2_env 中的,(action 和 observation 空间是定义在 pysc2.lib.features 中的),环境的类即 SC2Env,继承自 pysc2.env.environment.Base 类。可以作为使用一般的gym库的强化学习的环境一样使用它

要实例化并使用一个 SC2Env,需要传入几个重要的参数,完整的参数列表如下:

_only_use_kwargs=None,
map_name=None,
battle_net_map=False,
players=None,
agent_interface_format=None,
discount=1.,
discount_zero_after_timeout=False,
visualize=False,
step_mul=None,
realtime=False,
save_replay_episodes=0,
replay_dir=None,
replay_prefix=None,
game_steps_per_episode=None,
score_index=None,
score_multiplier=None,
random_seed=None,
disable_fog=False,
ensure_available_actions=True,
version=None

重要的参数说明

一些重要的参数说明如下:

使用自己实例化的环境

定义了自己的env以及agent,就可以像使用其他gym环境一样进行交互了,一个很好的官方提供的例子就是这个 pysc2/env/run_loop.py 文件下的 run_loop 函数,它接收一个/两个 agent(s) 实例和一个 env 实例,运行若干帧/代:

def run_loop(agents, env, max_frames=0, max_episodes=0):
    """A run loop to have agents and an environment interact."""
    total_frames = 0
    total_episodes = 0
    start_time = time.time()

    observation_spec = env.observation_spec()
    action_spec = env.action_spec()
    for agent, obs_spec, act_spec in zip(agents, observation_spec, action_spec):
        agent.setup(obs_spec, act_spec)

    try:
        while not max_episodes or total_episodes < max_episodes:
            total_episodes += 1
            timesteps = env.reset()
            for a in agents:
                a.reset()
            while True:
                total_frames += 1
                actions = [agent.step(timestep) for agent, timestep in zip(agents, timesteps)]
                if max_frames and total_frames >= max_frames:
                    return
                if timesteps[0].last():
                    break
                timesteps = env.step(actions)
    except KeyboardInterrupt:
        pass
    finally:
        elapsed_time = time.time() - start_time
        print("Took %.3f seconds for %s steps: %.3f fps" % (elapsed_time, total_frames, total_frames / elapsed_time))

最后再来看一下官方提供的,实例化一个 SC2Env 实例,一个 Agent 实例,然后使用上述的 run_loop 函数进行测试的例子,若干个例子可以在 pysc2/tests/easy_scripted_test.py 中找到:

def test_move_to_beacon(self):
    with sc2_env.SC2Env(
            map_name="MoveToBeacon",
            players=[sc2_env.Agent(sc2_env.Race.terran)],
            agent_interface_format=sc2_env.AgentInterfaceFormat(
                feature_dimensions=sc2_env.Dimensions(
                    screen=84,
                    minimap=64)),
            step_mul=self.step_mul,
            game_steps_per_episode=self.steps * self.step_mul) as env:
        agent = scripted_agent.MoveToBeacon()
        run_loop.run_loop([agent], env, self.steps)

这里使用了 with as 的形式,我们需要创建长久的交互式直接使用 evn = SC2Env(…) 即可。

启动SCII程序

启动真正的SCII程序需要使用 absl.app 下的 run 函数,然后传入一个运行我们上述代码的主要函数作为参数,比如我们设置了一个主函数来设置env、agent等,以及完成了基本的循环条件,把这些都封装在了一个名为 main 的函数中,最后,需要使用 absl.app.run(main) 来启动真正的 SCII 程序。一个完整的例子如下:

from pysc2.env import run_loop, sc2_env
from pysc2.agents import random_agent
from absl import app


def main(args):
    agent = random_agent.RandomAgent()

    with sc2_env.SC2Env(map_name="MoveToBeacon", players=[sc2_env.Agent(sc2_env.Race.terran)],
                        agent_interface_format=sc2_env.AgentInterfaceFormat(
                            feature_dimensions=sc2_env.Dimensions(screen=84, minimap=64)), step_mul=16,
                        game_steps_per_episode=200 * 16, visualize=True) as env:
        run_loop.run_loop([agent], env, 200)


if __name__ == "__main__":
    app.run(main)

几个非常需要注意的点:

重要参考官方文档:https://github.com/deepmind/pysc2/blob/master/docs/environment.md#rl-environment
参考一篇blog:https://itnext.io/build-a-zerg-bot-with-pysc2-2-0-295375d2f58e

上一篇 下一篇

猜你喜欢

热点阅读