我正在尝试在Python中构建一个破折号应用程序来模拟Q-Learning问题。在实现算法之前,我只是专注于使表工作随机递增值并在每个增量之间等待1秒。
Q是一个熊猫数据帧:
table = ff.create_table(Q, height_constant=20)
table.layout.width=300
def update_Q(Q):
for i in range(len(Q)):
for j in range(1, len(Q.columns)):
Q.iloc[i,j] += np.random.choice([0,1,2])
print(Q)
return Q
我能够使用print语句,控制台上表的值确实得到了更新。
但是,在浏览器中它只是第一次更新,但它仍然是静态的。这是代码:
# Browser visualization
app.layout = html.Div([
html.H1(children='Frozen Lake: Q-Learning Demo'),
dcc.Graph(id='table', figure=table),
dcc.Interval(
id='time',
interval=1*1000, # in milliseconds
n_intervals=0)
]
)
@app.callback(Output(component_id = 'table', component_property='figure'),
[Input(component_id = 'time', component_property='n_intervals')])
def update_table(n):
# Update values
new_table = ff.create_table(update_Q(Q))
time.sleep(1)
return new_table
if __name__ == '__main__':
app.run_server()
我错过了什么?
解决了。没有什么能像早晨的咖啡; )
最好将表的创建包装到一个函数中,并在每个时间间隔为每次更新调用它。此外,以前的语法不会在创建的第一个表中定义样式。
# Helper functions to draw and update values of the table
def draw_Table(Q):
table = ff.create_table(Q, index=True, height_constant=20)
table.layout.width=300
return table
def update_Q(Q):
for i in range(len(Q)):
for j in range(1, len(Q.columns)):
Q.iloc[i,j] += np.random.choice([0,1,2])
return Q
然后,
@app.callback(Output(component_id = 'table', component_property='figure'),
[Input(component_id = 'time', component_property='n_intervals')])
def update_table(n):
# Update values
new_table = draw_Table(update_Q(Q))
time.sleep(1)
return new_table
希望它可以帮到某人!