简体   繁体   English

如何实现使用图像作为观察的 2D OpenAI-Gym 环境?

[英]How to implement a 2D OpenAI-Gym environment that uses images as observations?

I need to create a 2D environment with a basic model of a robot arm and a target point.我需要创建一个带有机械臂基本模型和目标点的 2D 环境。 I want the arm to reach the target through a series of discrete actions (eg go right, left, up and down) and I need the observation space to be an RGB image of the screen that I will then use as input to DQN.我希望手臂通过一系列离散动作(例如向右、向左、向上和向下)到达目标,并且我需要观察空间是屏幕的 RGB 图像,然后我将其用作 DQN 的输入。

My problem is that I don't understand how can I use the image observation to move the robot and to get the positions of its arm and of the target (eg to create a reward function based on the their distance).我的问题是我不明白如何使用图像观察来移动机器人并获得其手臂和目标的位置(例如,根据它们的距离创建奖励函数)。

It looks like the only Gym environments I can take inspiration from are the Atari ones, but I cannot find specific codes of the single games probably because they are embedded in their ROMs.看起来我能从中获得灵感的唯一 Gym 环境是 Atari 环境,但我找不到单个游戏的特定代码,可能是因为它们嵌入在它们的 ROM 中。

So, if I want to follow the examples of Atari environments in Gym and use atari_env.py, does it mean that I also need to create a game and its ROM and then integrate it in Stella?那么,如果我想按照 Gym 中 Atari 环境的示例并使用 atari_env.py,是否意味着我还需要创建一个游戏及其 ROM,然后将其集成到 Stella 中? Or is there another way?或者还有其他方法吗? In general, are there other types of environments that use images as observations from which I can get inspiration?一般来说,是否有其他类型的环境使用图像作为观察,我可以从中获得灵感?

Many thanks非常感谢

You can create a custom Gym environment by simply implementing a class with the appropriate methods:您可以通过使用适当的方法简单地实现一个类来创建自定义 Gym 环境:

import gym
from gym.spaces import Discrete, Box

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

  def __init__(self, arg1, arg2, ...):
    super().__init__()
    # The action and observation spaces need to be gym.spaces objects:
    self.action_space = Discrete(4)  # up, left, right, down
    # Here's an observation space for 200 wide x 100 high RGB image inputs:
    self.observation_space = Box(
        low=0, high=255, shape=(100, 200, 3), dtype=np.uint8)

  def step(self, action):
    # Execute one time step in the environment
    ...

  def reset(self):
    # Reset the state of the environment
    ...

  def render(self, mode='human', close=False):
    if mode == 'human':
        # render to screen
        ...
    elif mode == 'rgb_array':
        # render to a NumPy 100x200x3 array
        ...
        return array
    else:
        # raise an error, unsupported mode

No need to implement your own Atari ROM or anything like that.无需实现您自己的 Atari ROM 或类似的东西。 It can be pure Python (+ NumPy arrays).它可以是纯 Python(+ NumPy 数组)。 To produce NumPy arrays representing images, there are many options.要生成表示图像的 NumPy 数组,有很多选项。 For example, you could use Matplotlib, PIL, or other libraries.例如,您可以使用 Matplotlib、PIL 或其他库。 Or, if the images are very simple, you could even create the NumPy arrays manually.或者,如果图像非常简单,您甚至可以手动创建 NumPy 数组。 For example, the following code creates a random 200x100 RGB image:例如,以下代码创建一个随机的 200x100 RGB 图像:

import numpy as np

random_image = np.random.rand(100, 200, 3)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM