|
通过安装gym与retro搭建强化学习的实验环境
gym下载:https://gitee.com/dfy888/gym
retro下载:https://gitee.com/dfy888/retro
先下载zip源码包 通过源码编译的方式安装
系统:Ubuntu18.04
隔离工具:conda
IDE工具:Pycharm
python版本:3.7
安装步骤如下:
1、准备工作(虚拟环境创建与work目录)- conda create -n py3_gym python=3.7
- conda activate py3_gym
- python3 get-pip.py
- pip --version
- pip3 --version
- cd & mkdir py3_gym_works
- 将gym源码包解压到 py3_gym_works目录下
复制代码
2、打开pycharm 创建项目 关联虚拟环境与py3_gym_works目录
3、开始安装gym
- cd gym
- pip3 install -e '.[all]'
- 会报错:
- Building wheel for mujoco-py (setup.py) ... error
- ERROR: Command errored out with exit status 1:
- 原因:无法编译mujoco 其需要依赖其他的包
- 我们这里暂时不需要用到mujoco 故将
- setup.py文件中屏蔽mujoco相关的代码
- # 'mujoco': ['mujoco_py>=1.50, <2.1', 'imageio'],
- # 'robotics': ['mujoco_py>=1.50, <2.1', 'imageio'],
- 再次运行:
- pip3 install -e '.[all]' 提示成功
- pip3 list 可以看到安装的gym
复制代码
4、测试gym是否安装成功,跑下面的代码,显示如下图所示- # -*- coding: utf-8 -*-
- __author__ = u'东方耀 微信:dfy_88888'
- __date__ = '2019/8/18 上午08:43'
- __product__ = 'PyCharm'
- __filename__ = 'demo_gym'
- import gym
- env = gym.make('CartPole-v0')
- for i_episode in range(10):
- # Reset the environment's state. Returns observation.
- observation = env.reset()
- print('i_episode回合数:', i_episode)
- for t in range(100):
- # Render one frame of the environment.
- env.render() # 更新动画
- action = env.action_space.sample()
- # Step the environment by one timestep. Returns observation, reward, done, info.
- observation, reward, done, info = env.step(action) # 推进一步
- if done:
- env.reset()
- continue
复制代码
5、开始编译安装gym-retro,如下图所示
- cd retro
- pip3 install cmake
- pip3 install -e .
- sudo apt-get install capnproto libcapnp-dev libqt5opengl5-dev qtbase5-dev
- cmake . -DBUILD_UI=ON -UPYLIB_DIRECTORY
- make -j$(grep -c ^processor /proc/cpuinfo)
- ./gym-retro-integration
复制代码
6、代码测试retro是否成功,如下图所示
- # -*- coding: utf-8 -*-
- __author__ = u'东方耀 微信:dfy_88888'
- __date__ = '2019/8/18 上午09:15'
- __product__ = 'PyCharm'
- __filename__ = 'retro_demo'
- import retro
- def main():
- # Create a Gym Environment
- env = retro.make(game='Airstriker-Genesis')
- # reset(self): Reset the environment's state. Returns observation.
- obs = env.reset()
- while True:
- # Step the environment by one timestep. Returns observation, reward, done, info.
- obs, rew, done, info = env.step(env.action_space.sample())
- # render(self, mode='human'): Render one frame of the environment.
- # The default mode will do something human friendly, such as pop up a window.
- env.render()
- if done:
- obs = env.reset()
- env.close()
- if __name__ == "__main__":
- main()
复制代码
7、
8、
|
|