简体   繁体   English

KeyError:q 表上的 0

[英]KeyError: 0 on q-table

I am trying to create a Q-table for reinforcement learning.我正在尝试为强化学习创建一个 Q 表。 I have created the following Pandas data frame as my possible state-action pairs are kind of large and pandas DataFrame is quite readable.我创建了以下 Pandas 数据框,因为我可能的状态-动作对有点大,并且 pandas DataFrame 可读性很强。 but i cannot modify any of my values using q_table.loc[] as i get Key Error: 0 .但我无法使用q_table.loc[]修改我的任何值,因为我得到Key Error: 0 How can i find and manipulate my q_table values using state/action names?如何使用状态/动作名称查找和操作我的 q_table 值?

import numpy as np
import pandas as pd

factory_price = 10
max_margin =0.2
max_inv_cap =10
actions = []
states =[]

prices = np.ndarray.tolist(np.arange(factory_price, (factory_price * (1 + max_margin))+ 0.1, 0.25))



# Actions
for i in range(max_inv_cap):
        for ii in prices:
            for iii in prices:
                for iiii in prices:
                    if ii < iii < iiii :
                        actions.append((i,(ii,iii,iiii)))
                    else:
                        continue
                        
#states
for j in range(max_inv_cap):
    for jj in range(max_inv_cap):
        for jjj in range(max_inv_cap):
            states.append([j,jj,jjj])

zero_data = np.zeros(shape=(len(actions),len(states))) 

label_rows = pd.Series(actions)
label_cols = tuple(pd.Series(states))
q_table = pd.DataFrame(zero_data, label_rows, label_cols)

well in order to access and manipulate the q-table values i defined the following dictionaries:好吧,为了访问和操作 q-table 值,我定义了以下字典:

    states_dict = {}
    for a, b in enumerate(states):
        states_dict[b] = a

    actions_dict = {}
    for a1, b1 in enumerate(actions):
        actions_dict[b1] = a1 

now i can use q-table.iloc[actions_dict[action], states_dict[state]] to get my values.现在我可以使用 q-table.iloc[actions_dict[action], states_dict[state]] 来获取我的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM