我已经按照OpenAI Gym框架创建了一个自定义环境;包含step
,reset
,action
和reward
函数。我的目标是在此自定义环境上运行OpenAI基准。但是在此之前,必须在OpenAI Gym上注册环境。我想知道如何在OpenAI Gym上注册自定义环境?另外,我是否应该修改OpenAI基准代码以纳入此标准?
答案 0 :(得分:3)
您不需要修改基线存储库。
这是一个最小的示例。假设您拥有npm-cache
,并具有所有必需的功能(node_modules\.staging\material-design-icons-7d5a1f73\action\drawable-xxhdpi
,myenv.py
,...)。类环境的名称为step
,您想将其添加到reset
文件夹中。你必须
MyEnv
文件放在classic_control
添加到myenv.py
(位于同一文件夹中)
gym/gym/envs/classic_control
通过添加
在__init__.py
中注册环境
from gym.envs.classic_control.myenv import MyEnv
注册时,您还可以添加gym/gym/envs/__init__.py
和gym.envs.register(
id='MyEnv-v0',
entry_point='gym.envs.classic_control:MyEnv',
max_episode_steps=1000,
)
(如果您的班级需要一些参数)。
您也可以直接在要运行的脚本(TRPO,PPO或其他任何一种)中注册环境,而不用在reward_threshold
中进行操作。
编辑
这是创建LQR环境的最小示例。
将下面的代码保存在kwargs
中,并将其放置在Gym的 classic_control 文件夹中。
gym/gym/envs/__init__.py
将lqr_env.py
添加到import gym
from gym import spaces
from gym.utils import seeding
import numpy as np
class LqrEnv(gym.Env):
def __init__(self, size, init_state, state_bound):
self.init_state = init_state
self.size = size
self.action_space = spaces.Box(low=-state_bound, high=state_bound, shape=(size,))
self.observation_space = spaces.Box(low=-state_bound, high=state_bound, shape=(size,))
self._seed()
def _seed(self, seed=None):
self.np_random, seed = seeding.np_random(seed)
return [seed]
def _step(self,u):
costs = np.sum(u**2) + np.sum(self.state**2)
self.state = np.clip(self.state + u, self.observation_space.low, self.observation_space.high)
return self._get_obs(), -costs, False, {}
def _reset(self):
high = self.init_state*np.ones((self.size,))
self.state = self.np_random.uniform(low=-high, high=high)
self.last_u = None
return self._get_obs()
def _get_obs(self):
return self.state
(也在 classic_control 中)。
在脚本中,创建环境时,请执行
from gym.envs.classic_control.lqr_env import LqrEnv
答案 1 :(得分:0)