0

I'm trying to solve a reinforcement learning problem with a simple q learning algorithme. I must add a new state to my table if the agent don't already know it. The probleme I have is that when I learn the new state, I have the folowing error: 'None of [x] are in the [index]'

I did a little test code to understand what's happening and solve the problems when I encounter one but I don't find solution to this one.

Here's my code:

import pandas as pd import numpy as np import random actions = [0, 1] obs = (0) q_table = pd.DataFrame(columns=actions) def check_state_exist(state): global q_table if state not in q_table.index: # append new state to q table q_table = q_table.append( pd.Series( [0]*len(actions), index=q_table.columns, name=state, ) ) def choose_action(state): global actions check_state_exist(state) # action selection if np.random.uniform() < 0.9: # choose best action state_action = q_table.loc[state, :] state_action = state_action.reindex(np.random.permutation(state_action.index)) # some actions have same value action = np.argmax(state_action) else: # choose random action action = np.random.choice(actions) return action for i in range(50): rand = random.randrange(100) if rand == 1: obs = 'rare' else: obs = [rand, random.randrange(10)] obs = tuple(obs) choose_action(obs) 

'None of [(56, 5)] are in the [index]'

print(q_table) 0 1 (56, 5) 0 0 

Thanks for your help

2 Answers 2

1

When you use a tuple as index, pandas will automatically behave like multi-indexing. If you want to index with (56,5) in a solo-level-index, you might want to try something like q_table.loc[[(56,5)]] (not sure if it works though). Otherwise, as it makes sense in Q-learning, you can try using a multi-index.

Sign up to request clarification or add additional context in comments.

Comments

0

Well, after some hours of test, I finally solve the problem. Instead of using a tuple, I used a string. I gess it's because .loc was trying to find the number of the index and not the name of the index.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.