class for agent code
class RandomAgent:
"""
This taxi driver selects actions randomly.
You better not get into this taxi!
"""
def __init__(self, env):
self.env = env
def get_action(self, state) -> int:
"""
We have `state` as an input to keep
a consistent API for all our agents, but it
is not used.
i.e. The agent does not consider the state of
the environment when deciding what to do next.
This is why we call it "random".
"""
return self.env.action_space.sample()
agent = RandomAgent(env)
Q-learning train agent code
import random
from tqdm import tqdm
# exploration vs exploitation prob
epsilon = 0.1
n_episodes = 10000
# For plotting metrics
timesteps_per_episode = []
penalties_per_episode = []
for i in tqdm(range(0, n_episodes)):
state = env.reset()
epochs, penalties, reward, = 0, 0, 0
done = False
while not done:
if random.uniform(0, 1) < epsilon:
# Explore action space
action = env.action_space.sample()
else:
# Exploit learned values
action = agent.get_action(state)
next_state, reward, done, info = env.step(action)
agent.update_parameters(state, action, reward, next_state)
if reward == -10:
penalties += 1
state = next_state
epochs += 1
timesteps_per_episode.append(epochs)
penalties_per_episode.append(penalties)
Error
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
C:\Users\ASUS10~1\AppData\Local\Temp/ipykernel_27412/680295490.py in <module>
30 next_state, reward, done, info = env.step(action)
31
---> 32 agent.update_parameters(state, action, reward, next_state)
33
34 if reward == -10:
AttributeError: 'RandomAgent' object has no attribute 'update_parameters'
I am not sure why this error occurred when my class RandomAgent is declared and works fine but this specific code is causing errors. If i remove the code, the agent does not update and the code doesnt function as it is intended to.
Link for website where i got my code from : https://hackernoon.com/reinforcement-learning-part-2-the-q-learning-algorithm