[强化学习环境Gym 旧版本下载] Windows10 gym版本0.19.0 atari_py版本0.2.6 解决gym中不存在环境问题 pycharm

新版本 Gym 不支持 atari 下的环境,为方便学习运行一些代码,可进行版本回退,安装 gym版本为 0.19.0atari_py 版本为 0.2.6 。安装操作均在 pycharm 的 terminal 终端命令行上执行。不存在环境时可能出现如下类似报错:

windows gym.error.NameNotFound: Environment Pong doesn't exist. 

首先卸载已经安装过的新版本 gym

pip uninstall gym

下载旧版本的 gym 和 atari_py

pip --default-timeout=100 install gym==0.19.0 -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install --no-index -f https://github.com/Kojoley/atari-py/releases atari_py==0.2.6
pip install pyglet==1.5.27

若出现类似报错:

error: subprocess-exited-with-error

× python setup.py egg_info did not run successfully.   
│ exit code: 1   
╰─> [1 lines of output]       
error in gym setup command: 'extras_require' must be a dictionary whose values are strings or lists of strings containing valid project/version requirement  specifiers.      
 [end of output]    
note: This error originates from a subprocess, and is likely not a problem with pip. 
error: metadata-generation-failed  × Encountered error while generating package metadata. ╰─> See above for output.  
note: This is an issue with the package mentioned above, not pip. hint: See above for details.

参考:【debug日常】解决python setup.py egg_info did not run successfully. - 知乎 (zhihu.com)

利用以下命令解决:

pip install --upgrade pip setuptools==57.5.0

若安装完成后,运行出现以下类似报错:

FileNotFoundError: Could not find module 'D:\anaconda3\envs\pytorch\lib\site-packages\atari_py\ale_interface\ale_c.dll' (or one of its dependencies). Try using the full path with constructor syntax.

参考这篇文章中百度网盘中的链接,直接下载文件 ale_c.dll 复制进相应文件夹即可解决。

强化学习Gym Atari报错缺少ale_c.dll_could not find module 'ale_c.dll-CSDN博客

可成功运行 atari 下的环境  ‘Pong-v0’

  • 24
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
强化学习是一种通过智能体与环境的交互来学习最优行为的方法。Gym是一个用于开发和比较强化学习算法的工具包,它提供了一系列标准化的环境,使得研究者可以更加方便地进行算法的实现和比较。 要编写一个Gym环境,需要实现一个Python类,该类必须继承gym.Env类,并实现以下方法: 1. __init__(self): 初始化环境,包括环境状态的初始化、动作空间的定义等。 2. reset(self): 重置环境状态,返回初始状态。 3. step(self, action): 执行一个动作,返回下一个状态、奖励和是否终止的标志。 4. render(self, mode='human'): 可选方法,用于可视化环境状态。 下面是一个简单的Gym环境示例,该环境是一个简单的迷宫游戏,智能体需要找到宝藏并躲避陷阱: ```python import gym from gym import spaces import numpy as np class MazeEnv(gym.Env): def __init__(self): self.action_space = spaces.Discrete(4) # 动作空间为上下左右四个方向 self.observation_space = spaces.Box(low=0, high=255, shape=(5, 5, 3), dtype=np.uint8) # 状态空间为一个5x5的RGB图像 self.maze = np.array([ [0, 0, 0, 0, 0], [0, 1, 1, 0, 0], [0, 0, 1, 1, 0], [0, 1, 1, 0, 0], [0, 0, 0, 0, 2] ]) # 迷宫地图,0表示空地,1表示陷阱,2表示宝藏 self.agent_pos = np.array([1, 1]) # 智能体初始位置 self.reward_range = (-1, 1) # 奖励范围为[-1, 1] def reset(self): self.agent_pos = np.array([1, 1]) # 重置智能体位置 return self._get_obs() def step(self, action): if action == 0: # 上 self.agent_pos[0] = max(0, self.agent_pos[0] - 1) elif action == 1: # 下 self.agent_pos[0] = min(4, self.agent_pos[0] + 1) elif action == 2: # 左 self.agent_pos[1] = max(0, self.agent_pos[1] - 1) elif action == 3: # 右 self.agent_pos[1] = min(4, self.agent_pos[1] + 1) reward = self._get_reward() done = self._is_done() obs = self._get_obs() return obs, reward, done, {} def render(self, mode='human'): pass def _get_obs(self): obs = np.zeros((5, 5, 3), dtype=np.uint8) obs[self.agent_pos[0], self.agent_pos[1], :] = [255, 255, 255] # 智能体位置为白色 obs[self.maze == 1, 0] = 255 # 陷阱位置为红色 obs[self.maze == 2, 2] = 255 # 宝藏位置为蓝色 return obs def _get_reward(self): if self.maze[tuple(self.agent_pos)] == 1: # 踩到陷阱 return -1 elif self.maze[tuple(self.agent_pos)] == 2: # 找到宝藏 return 1 else: return 0 def _is_done(self): return self.maze[tuple(self.agent_pos)] != 0 ``` 这个环境比较简单,状态空间为一个5x5的RGB图像,动作空间为上下左右四个方向,奖励为-1(踩到陷阱)、0(未找到宝藏)和1(找到宝藏)。在实际应用,需要根据具体问题设计合适的状态空间、动作空间和奖励函数。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值