0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

強化学習37 Atariのwrapperで自動スタートを作る

Posted at

中学生から大学生までのAI初学者を対象にしています。

OpenAIのATARIを使おうとすると、ゲームの特殊性をカバーするためのwrapperが欲しくなります。
 例えば、ブロック崩しでは、ライフポイントが5に初期設定されていますが、最初とライフポイントが減った時点(失敗したとき)にfireボタンを押さないとゲームが動きません。
 これを超えないと、機械学習が全く進まないというアホみたいなことになります。個人的には時間の無駄です。
 そもそもは、FireResetEnvというwrapperが準備されていますが、ブロック崩しでは、ライフが減るごとには動かないので、1度失敗すると、ずっとそのままになります。
 そこで、下のようなラッパーを書いてみました。

class FireResetEnvAuto(gym.Wrapper):
    def __init__(self, env):
        """Take action on reset for envs that are fixed until firing."""
        gym.Wrapper.__init__(self, env)
        assert env.unwrapped.get_action_meanings()[1] == 'FIRE'
        assert len(env.unwrapped.get_action_meanings()) >= 3
        self.lives = 0

    def reset(self, **kwargs):
        self.env.reset(**kwargs)
        self.lives = self.env.unwrapped.ale.lives()
        obs, _, done, info = self.env.step(1)
        if done or info.get('needs_reset', False):
            self.env.reset(**kwargs)
        obs, _, done, info = self.env.step(2)
        if done or info.get('needs_reset', False):
            self.env.reset(**kwargs)
        return obs

    def step(self, ac):
        lives = self.env.unwrapped.ale.lives()
        if lives<self.lives:
            self.lives=lives
            return self.env.step(1)
        return self.env.step(ac)

殆どは、chainerrl/wrapper/atari_wrapper.py
でちょっとだけ付け足しただけです。
これが、chokozainerrlです。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?