如何在OpenAI中创建一个新的健身房环境?

99

我有一个任务,要创建一个使用机器学习玩视频游戏的AI代理。我想使用OpenAI Gym创建一个新的环境,因为我不想使用现有的环境。如何创建新的、定制的环境?

此外,是否有其他方法可以开始开发制作AI代理来玩特定的视频游戏,而不需要OpenAI Gym的帮助呢?

2个回答

146

可以查看我的banana-gym,这是一个非常小的环境。

创建新的环境

请查看该存储库的主页:

https://github.com/openai/gym/blob/master/docs/creating_environments.md

步骤如下:

  1. 创建一个具有PIP包结构的新存储库

应该看起来像这样:

gym-foo/
  README.md
  setup.py
  gym_foo/
    __init__.py
    envs/
      __init__.py
      foo_env.py
      foo_extrahard_env.py

请查看上面的链接以获取其内容。未在其中提到的详细信息,特别是在foo_env.py中某些函数应该具有什么样的形式。查看示例和gym.openai.com/docs/可以帮助理解。以下是一个示例:

class FooEnv(gym.Env):
    metadata = {'render.modes': ['human']}

    def __init__(self):
        pass

    def _step(self, action):
        """

        Parameters
        ----------
        action :

        Returns
        -------
        ob, reward, episode_over, info : tuple
            ob (object) :
                an environment-specific object representing your observation of
                the environment.
            reward (float) :
                amount of reward achieved by the previous action. The scale
                varies between environments, but the goal is always to increase
                your total reward.
            episode_over (bool) :
                whether it's time to reset the environment again. Most (but not
                all) tasks are divided up into well-defined episodes, and done
                being True indicates the episode has terminated. (For example,
                perhaps the pole tipped too far, or you lost your last life.)
            info (dict) :
                 diagnostic information useful for debugging. It can sometimes
                 be useful for learning (for example, it might contain the raw
                 probabilities behind the environment's last state change).
                 However, official evaluations of your agent are not allowed to
                 use this for learning.
        """
        self._take_action(action)
        self.status = self.env.step()
        reward = self._get_reward()
        ob = self.env.getState()
        episode_over = self.status != hfo_py.IN_GAME
        return ob, reward, episode_over, {}

    def _reset(self):
        pass

    def _render(self, mode='human', close=False):
        pass

    def _take_action(self, action):
        pass

    def _get_reward(self):
        """ Reward is given for XY. """
        if self.status == FOOBAR:
            return 1
        elif self.status == ABC:
            return self.somestate ** 2
        else:
            return 0

利用你的环境

import gym
import gym_foo
env = gym.make('MyEnv-v0')

示例

  1. https://github.com/openai/gym-soccer
  2. https://github.com/openai/gym-wikinav
  3. https://github.com/alibaba/gym-starcraft
  4. https://github.com/endgameinc/gym-malware
  5. https://github.com/hackthemarket/gym-trading
  6. https://github.com/tambetm/gym-minecraft
  7. https://github.com/ppaquette/gym-doom
  8. https://github.com/ppaquette/gym-super-mario
  9. https://github.com/tuzzer/gym-maze

1
我得到了一个丑陋的“gym_foo已导入但未使用”的警告。我该如何摆脱它? - hipoglucido
@hipoglucido 要消除“gym_foo已导入但未使用”的警告,您需要告诉您的编辑器忽略此导入。通常可以通过import gym_foo # noqa来实现。 - Martin Thoma
12
我认为应该大声声明,你只需要派生类的权限,而不需要这些内容。如果你不是通过 gym 生态系统进行区分,那么创建一个包真的没有什么意义。 - mathtick
在按照上述步骤后出现"gym_foo"导入错误时,执行pip install -e .命令可以解决问题。@hipoglucido - praneeth

20

这是有可能的。在文档页面的末尾附近,他们也这样说了。

https://gym.openai.com/docs

至于如何实现它,您应该查看现有环境的源代码以获取灵感。它们可以在github上找到:

https://github.com/openai/gym#installation

他们大多数的环境并不是从头开始实现的,而是创建了一个包装器,围绕现有的环境,并给出了一个对强化学习方便的接口。

如果您想自己制作,应该沿着这个方向去尝试将已有的东西适应gym接口。虽然这可能非常耗时。

还有另一种选项可能符合您的目的。那就是OpenAI的Universe。

https://universe.openai.com/

它可以与网站集成,以便您在kongregate游戏中训练模型,例如。但Universe不像Gym那么容易使用。

如果您是初学者,我的建议是您从标准环境的基本实现开始。在解决了这些基础问题之后,再逐步增加...


如果我想为非数字活动(如井字游戏或魔方)创建一个环境,其中可能的状态是有限且可以明确定义的,该怎么办?我只需生成包含所有可能状态的列表吗?模拟如何找出给定状态的有效目标状态? - Hendrik

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接