reinforcement_learning_basic_book
reinforcement_learning_basic_book copied to clipboard
运行1-gym_developing的maze_game.py报错
报错信息如下: line 57, in _trans_make self.t.loc[s, a] = tuple(n_s) 中间是嵌套的报错信息 最后到了 raise KeyError(f"None of [{key}] are in the [{axis_name}]") KeyError: "None of [Int64Index([0, 0], dtype='int64')] are in the [index]" 代码完全没改过,请问是为什么。
我遇到了相同的问题,主要是pandas的int64index数学类型没法用于索引行数,bug出在 self.t.loc[s,a] = tuple(s)这句上。
问题已经解决,主要出在loc[element1, element2]上,element1需要整形或者标签(字符型),而源代码定义的是一个元组,因此在用loc进行切片时会出现index数据不匹配的错误。部分代码修改为: (1)在self.observation_space = [tuple(s) for s in np.argwhere(self.map == 0)]下增加一行: self.observation_space_str = [str(tuple(s)) for s in np.argwhere(self.map == 0)] (2)dataframe修改为self.t = pd.DataFrame(data=None, index=self.observation_space_str, columns=self.action_space) (3) 在n_s = np.array(s) + np.array([-1, 0])下增加: row = str(s) #当前元组的二维数组转换为标签字符格式 if (np.all(n_s >= [0, 0])) and (np.all(n_s <= [4, 4])) and not self.map[n_s[0], n_s[1]]: self.t.loc[row, a] = str(tuple(n_s)) #赋值为下一个状态的标签字符格式 else: self.t.loc[row, a] = row (4)PS:源代码的render有一些错误,需要修改后才能显示迷宫图案。 def render(self, mode='human', close=False): if close: if self.viewer is not None: self.viewer.close() self.viewer = None return unit = 50 screen_width = 6* unit screen_height = 6 * unit
if self.viewer is None:
from gym.envs.classic_control import rendering
self.viewer = rendering.Viewer(screen_width, screen_height)
#创建网格
for c in range(6):
self.line = rendering.Line((unit/2, (c+1/2)*unit), (screen_width-unit/2, (c+1/2)*unit))
self.line.set_color(0, 0, 0)
self.viewer.add_geom(self.line)
for r in range(6):
self.line = rendering.Line(((r + 1/2)*unit, unit/2), ((r + 1/2)*unit, screen_height - unit/2))
self.line.set_color(0, 0, 0)
self.viewer.add_geom(self.line)
# 创建墙壁
for x, y in self.walls:
self.block = rendering.make_polygon(
v=[((x+1/2) * unit, (y+1/2)* unit),
((x + 3/2) * unit, (y+1/2) * unit),
((x + 3/2) * unit, (y + 3/2) * unit),
((x + 1/2)* unit, (y + 3/2) * unit)])
self.block.set_color(0, 0, 0)
self.viewer.add_geom(self.block)
# 创建机器人
self.robot = rendering.make_circle(20)
self.robotrans = rendering.Transform()
self.robot.add_attr(self.robotrans)
self.robot.set_color(0.8, 0.6, 0.4)
self.viewer.add_geom(self.robot)
# 创建出口
self.exit = rendering.make_circle(20)
self.exitrans = rendering.Transform(translation=(5*unit, 3*unit))
self.exit.add_attr(self.exitrans)
self.exit.set_color(0, 1, 0)
self.viewer.add_geom(self.exit)
if self.__state is None:
return None
self.robotrans.set_translation(self.__state[0] * unit + unit , self.__state[1] * unit + unit )
return self.viewer.render(return_rgb_array=(mode == 'rgb_array'))
@lotharelvin