- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
目前正在尝试在我使用 turtle 图形创建的环境中实现 Q 表算法。当我尝试运行使用 Q 学习的算法时,我收到一条错误消息:
File "<ipython-input-1-cf5669494f75>", line 304, in <module>
rl()
File "<ipython-input-1-cf5669494f75>", line 282, in rl
A = choose_action(S, q_table)
File "<ipython-input-1-cf5669494f75>", line 162, in choose_action
state_actions = q_table.iloc[state, :]
File "/Users/himansuodedra/anaconda3/lib/python3.6/site-packages/pandas/core/indexing.py", line 1367, in __getitem__
return self._getitem_tuple(key)
File "/Users/himansuodedra/anaconda3/lib/python3.6/site-packages/pandas/core/indexing.py", line 1737, in _getitem_tuple
self._has_valid_tuple(tup)
File "/Users/himansuodedra/anaconda3/lib/python3.6/site-packages/pandas/core/indexing.py", line 204, in _has_valid_tuple
if not self._has_valid_type(k, i):
File "/Users/himansuodedra/anaconda3/lib/python3.6/site-packages/pandas/core/indexing.py", line 1674, in _has_valid_type
return self._is_valid_list_like(key, axis)
File "/Users/himansuodedra/anaconda3/lib/python3.6/site-packages/pandas/core/indexing.py", line 1723, in _is_valid_list_like
raise IndexingError('Too many indexers')
IndexingError: Too many indexers
我似乎无法查明问题所在。我的逻辑看起来不错。我也能够在脚本卡住之后构建环境,我被迫终止它。任何帮助都会很棒。代码如下:
"""
Reinforcement Learning using table lookup Q-learning method.
An agent "Blue circle" is positioned in a grid and must make its way to the
green square. This is the end goal. Each time the agent should improve its
strategy to reach the final Square. There are two traps the red and the wall
which will reset the agent.
"""
import turtle
import pandas as pd
import numpy as np
import time
np.random.seed(2)
""" Setting Parameters """
#N_STATES = 12 # the size of the 2D world
ACTIONS = ['left', 'right', 'down','up'] # available actions
EPSILON = 0.9 # greedy police (randomness factor)
ALPHA = 0.1 # learning rate
GAMMA = 0.9 # discount factor
MAX_EPISODES = 13 # maximum episodes
FRESH_TIME = 0.3 # fresh time for one move
def isGoal():
if player.xcor() == -25 and player.ycor() == 225:
player.goto(-175,125)
status_func(1)
S_ = 'terminal'
R = 1
interaction = 'Episode %s: total_steps = %s' %(episode+1, step_counter)
print('\r{}'.format(interaction), end='')
time.sleep(2)
print('\r', end='')
return S_, R
else:
pass
def isFire():
if player.xcor() == -25 and player.ycor() == 175:
player.goto(-175,125)
status_func(3)
S_ = 'terminal'
R = -1
interaction = 'Episode %s: total_steps = %s' %(episode+1, step_counter)
print('\r{}'.format(interaction), end='')
time.sleep(2)
print('\r', end='')
return S_, R
else:
pass
def isWall():
if player.xcor() == -125 and player.ycor() == 175:
player.goto(-175,125)
status_func(2)
S_ = 'terminal'
R = -1
interaction = 'Episode %s: total_steps = %s' %(episode+1, step_counter)
print('\r{}'.format(interaction), end='')
time.sleep(2)
print('\r', end='')
return S_, R
else:
pass
""" Player Movement """
playerspeed = 50
""" Create the token object """
player = turtle.Turtle()
player.color("blue")
player.shape("circle")
player.penup()
player.speed(0)
player.setposition(-175,125)
player.setheading(90)
#Move the player left and right
def move_left():
x = player.xcor()
x -= playerspeed
if x < -175:
x = -175
player.setx(x)
isGoal()
isFire()
isWall()
S_ = player.pos()
R = 0
def move_right():
x = player.xcor()
x += playerspeed
if x > -25:
x = -25
player.setx(x)
isGoal()
isFire()
isWall()
S_ = player.pos()
R = 0
def move_up():
y = player.ycor()
y += playerspeed
if y > 225:
y = 225
player.sety(y)
isGoal()
isFire()
isWall()
S_ = player.pos()
R = 0
def move_down():
y = player.ycor()
y -= playerspeed
if y < 125:
y = 125
player.sety(y)
isGoal()
isFire()
isWall()
S_ = player.pos()
R = 0
#Create Keyboard Bindings
turtle.listen()
turtle.onkey(move_left, "Left")
turtle.onkey(move_right, "Right")
turtle.onkey(move_up, "Up")
turtle.onkey(move_down, "Down")
def build_q_table(n_states, actions):
table = pd.DataFrame(
np.zeros((n_states, len(actions))), # q_table initial values
columns=actions, # actions's name
)
# print(table) # show table
return table
def choose_action(state, q_table):
# This is how to choose an action
state_actions = q_table.iloc[state, :]
# act non-greedy or state-action have no value
if (np.random.uniform() > EPSILON) or ((state_actions == 0).all()):
action_name = np.random.choice(ACTIONS)
else: # act greedy
# replace argmax to idxmax as argmax means a different function
action_name = state_actions.idxmax()
return action_name
def get_env_feedback(S, A):
if A == 'right':
move_right()
elif A == 'left':
move_left()
elif A == 'up':
move_up()
else: #down
move_down()
return S_, R
def update_env(S, episode, step_counter):
wn = turtle.Screen()
wn.bgcolor("white")
wn.title("test")
""" Create the Grid """
greg = turtle.Turtle()
greg.speed(0)
def create_square(size,color="black"):
greg.color(color)
greg.pd()
for i in range(4):
greg.fd(size)
greg.lt(90)
greg.pu()
greg.fd(size)
def row(size,color="black"):
for i in range(4):
create_square(size)
def board(size,color="black"):
greg.pu()
greg.goto(-(size*4),(size*4))
for i in range(3):
row(size)
greg.bk(size*4)
greg.rt(90)
greg.fd(size)
greg.lt(90)
def color_square(start_pos,distance_sq, sq_width, color):
greg.pu()
greg.goto(start_pos)
greg.fd(distance_sq)
greg.color(color)
greg.begin_fill()
for i in range(4):
greg.fd(sq_width)
greg.lt(90)
greg.end_fill()
greg.pu()
def initiate_grid():
board(50)
color_square((-200,200),150, 50,color="green")
color_square((-200,150),50, 50,color="black")
color_square((-200,150),150, 50,color="red")
greg.hideturtle()
initiate_grid()
""" Create the token object """
player = turtle.Turtle()
player.color("blue")
player.shape("circle")
player.penup()
player.speed(0)
player.setposition(S)
player.setheading(90)
def rl():
possible_states = {0:(-175,125),
1:(-175,175),
2:(-175,225),
3:(-125,125),
4:(-125,175),
5:(-125,225),
6:(-75,125),
7:(-75,175),
8:(-75,225),
9:(-25,125),
10:(-25,175),
11:(-25,225)}
inv_possible_states = {v:k for k,v in possible_states.items()}
#build the qtable
q_table = build_q_table(len(possible_states),ACTIONS)
for episode in range(MAX_EPISODES):
step_counter = 0
which_state = 0
S = possible_states[which_state]
is_terminated = False
update_env(S,episode,step_counter)
while not is_terminated:
A = choose_action(S, q_table)
# take action & get next state and reward
S_, R = get_env_feedback(S, A)
q_predict = q_table.loc[S, A]
if S_ != 'terminal':
S_ = inv_possible_states[S_]
# next state is not terminal
q_target = R + GAMMA * q_table.iloc[S_, :].max()
else:
q_target = R # next state is terminal
is_terminated = True # terminate this episode
q_table.loc[S, A] += ALPHA * (q_target - q_predict) # update
S = S_ # move to next state
update_env(S, episode, step_counter+1)
step_counter += 1
return q_table
rl()
最佳答案
简短回答:您将屏幕坐标与环境的 12 种状态混淆了
长答案:当调用 A = choose_action(S, q_table)
并执行 choose_action
方法时,您会遇到问题在该方法中使用以下代码行:
state_actions = q_table.iloc[state, :]
错误 IndexingError: Too many indexers
试图告诉您您尝试访问的值在 q_table
上不存在。
如果您要打印出传递给 choose_action
函数的 state
变量,您将得到:
(-175, 125)
但这没有意义。如果在错误发生之前打印整个 Q 表,您将看到以下值:
left right down up
0 0.0 0.0 0.0 0.0
1 0.0 0.0 0.0 0.0
2 0.0 0.0 0.0 0.0
3 0.0 0.0 0.0 0.0
4 0.0 0.0 0.0 0.0
5 0.0 0.0 0.0 0.0
6 0.0 0.0 0.0 0.0
7 0.0 0.0 0.0 0.0
8 0.0 0.0 0.0 0.0
9 0.0 0.0 0.0 0.0
10 0.0 0.0 0.0 0.0
11 0.0 0.0 0.0 0.0
这些值全为零,因为您还没有学到任何东西。但是当 state
等于 (-175, 125)
时,您的代码试图访问 q_table.iloc[state, :]
。 这没有任何意义!
您传递给 choose_action
方法的值应该对应于环境中的十二种状态之一,在 q_table
中由 0 到的整数表示11.
问题似乎是由这一行引起的:
S = possible_states[which_state]
☝️ rl
方法中的那行代码将 S
的值更改为 (-175, 125)
。如果 S
应该表示代理所处的环境状态,那么 S
应该始终是 0 到 11(含)之间的整数。
您需要确保正确地将 turtle-graphics
正在绘制的屏幕位置与代理正在探索的 12 种环境状态分开。 turtle-graphics
不知道如何绘制环境状态,因为它们存储在 q_table
中,而 q_table
不知道哪些状态环境中的坐标与 turtle-graphics
用来绘制正方形的坐标相关联。
关于python - 使用 turtle 图形的强化学习算法不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50392231/
我正在处理一组标记为 160 个组的 173k 点。我想通过合并最接近的(到 9 或 10 个组)来减少组/集群的数量。我搜索过 sklearn 或类似的库,但没有成功。 我猜它只是通过 knn 聚类
我有一个扁平数字列表,这些数字逻辑上以 3 为一组,其中每个三元组是 (number, __ignored, flag[0 or 1]),例如: [7,56,1, 8,0,0, 2,0,0, 6,1,
我正在使用 pipenv 来管理我的包。我想编写一个 python 脚本来调用另一个使用不同虚拟环境(VE)的 python 脚本。 如何运行使用 VE1 的 python 脚本 1 并调用另一个 p
假设我有一个文件 script.py 位于 path = "foo/bar/script.py"。我正在寻找一种在 Python 中通过函数 execute_script() 从我的主要 Python
这听起来像是谜语或笑话,但实际上我还没有找到这个问题的答案。 问题到底是什么? 我想运行 2 个脚本。在第一个脚本中,我调用另一个脚本,但我希望它们继续并行,而不是在两个单独的线程中。主要是我不希望第
我有一个带有 python 2.5.5 的软件。我想发送一个命令,该命令将在 python 2.7.5 中启动一个脚本,然后继续执行该脚本。 我试过用 #!python2.7.5 和http://re
我在 python 命令行(使用 python 2.7)中,并尝试运行 Python 脚本。我的操作系统是 Windows 7。我已将我的目录设置为包含我所有脚本的文件夹,使用: os.chdir("
剧透:部分解决(见最后)。 以下是使用 Python 嵌入的代码示例: #include int main(int argc, char** argv) { Py_SetPythonHome
假设我有以下列表,对应于及时的股票价格: prices = [1, 3, 7, 10, 9, 8, 5, 3, 6, 8, 12, 9, 6, 10, 13, 8, 4, 11] 我想确定以下总体上最
所以我试图在选择某个单选按钮时更改此框架的背景。 我的框架位于一个类中,并且单选按钮的功能位于该类之外。 (这样我就可以在所有其他框架上调用它们。) 问题是每当我选择单选按钮时都会出现以下错误: co
我正在尝试将字符串与 python 中的正则表达式进行比较,如下所示, #!/usr/bin/env python3 import re str1 = "Expecting property name
考虑以下原型(prototype) Boost.Python 模块,该模块从单独的 C++ 头文件中引入类“D”。 /* file: a/b.cpp */ BOOST_PYTHON_MODULE(c)
如何编写一个程序来“识别函数调用的行号?” python 检查模块提供了定位行号的选项,但是, def di(): return inspect.currentframe().f_back.f_l
我已经使用 macports 安装了 Python 2.7,并且由于我的 $PATH 变量,这就是我输入 $ python 时得到的变量。然而,virtualenv 默认使用 Python 2.6,除
我只想问如何加快 python 上的 re.search 速度。 我有一个很长的字符串行,长度为 176861(即带有一些符号的字母数字字符),我使用此函数测试了该行以进行研究: def getExe
list1= [u'%app%%General%%Council%', u'%people%', u'%people%%Regional%%Council%%Mandate%', u'%ppp%%Ge
这个问题在这里已经有了答案: Is it Pythonic to use list comprehensions for just side effects? (7 个答案) 关闭 4 个月前。 告
我想用 Python 将两个列表组合成一个列表,方法如下: a = [1,1,1,2,2,2,3,3,3,3] b= ["Sun", "is", "bright", "June","and" ,"Ju
我正在运行带有最新 Boost 发行版 (1.55.0) 的 Mac OS X 10.8.4 (Darwin 12.4.0)。我正在按照说明 here构建包含在我的发行版中的教程 Boost-Pyth
学习 Python,我正在尝试制作一个没有任何第 3 方库的网络抓取工具,这样过程对我来说并没有简化,而且我知道我在做什么。我浏览了一些在线资源,但所有这些都让我对某些事情感到困惑。 html 看起来
我是一名优秀的程序员,十分优秀!