(3) 更为标准的创建Gym环境的方式(一个PIP包的形式)——学习笔记

  1. 搭建框架,首先确定文件结构:
custom-gym/
|-- README.md
|-- setup.py
|-- custom_gym/
|  |-- __init__.py
|  |-- envs/
|  |  |-- __init__.py
|  |  |-- custom_env.py
|  |  |-- custom_env_extend.py

一级目录为custom-gym. 你可以取任何不冲突的名字。包含setup.py(安装), README.md(说明书)custom_gym文件夹(环境主体)。

二级目录为custom_gym.包含init.py(初始化)envs文件夹(环境脚本)

三级目录为envs.包含init.py(初始化)和两个测试环境脚本custom_env.pycustom_env_extend.py(当然你也可以只测试一个或者多加几个试试)。

  1. 配置给个文件
    (1) custom-gym/README.md 可以空着,等写好坏境再写描述。
    (2) custom-gym/setup.py 应该包含:
from setuptools import setup

setup(name='custom_gym',        # Secondary directory
    version='0.1',
    install_requires=['gym']    # And any other dependencies needs
)

(3) custom-gym/custom_gym/init.py 应该包含:

from gym.envs.registration import register

register(
    id='custom_env-v0',                                   # Format should be xxx-v0, xxx-v1....
    entry_point='custom_gym.envs:CustomEnv',              # Expalined in envs/__init__.py
)
register(
    id='custom_env_extend-v0',
    entry_point='custom_gym.envs:CustomEnvExtend',
)

(4) custom-gym/custom_gym/envs/init.py 应该包含:

from custom_gym.envs.custom_env import CustomEnv
from custom_gym.envs.custom_env import CustomEnvExtend

(5) custom-gym/custom_gym/envs/custom_env.py 应该长这个样子:

import gym

class CustomEnv(gym.Env):
    def __init__(self):
        print('CustomEnv Environment initialized')
    def step(self):
        print('CustomEnv Step successful!')
    def reset(self):
        print('CustomEnv Environment reset')
# 这里省去了render,close等模块

(6) 同理,custom-gym/custom_gym/envs/custom_env_extend.py 应该长这个样子:

import gym

class CustomEnvExtend(gym.Env):
    def __init__(self):
        print('CustomEnvExtend Environment initialized')
    def step(self):
        print('CustomEnvExtend Step successful!')
    def reset(self):
        print('CustomEnvExtend Environment reset')
  1. 安装与测试
    打开Anaconda Prompt, 切换目录到一级目录custom-gym,就是包含setup.py这个文件夹。然后运行:pip install -e .
    接下来就可以愉快的测试了:
import gym
import custom_gym

env = gym.make('custom_env-v0')
env.step()
env.reset()

env2 = gym.make('custom_env_extend-v0')
env2.step()
env2.reset()

参考:https://www.zhihu.com/collection/581976137

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值