[Tutor] TypeError: unhashable type: 'pygame.math.Vector2'
Cravan
savageapple850 at gmail.com
Thu Jun 25 11:05:30 EDT 2020
Hi all,
I recently began learning Q-learning in Python but encountered an error. Here’s my code for the Agent in one of my .py files:
````
class Zomb(pg.sprite.Sprite):
def __init__(self, game, m, n):
self.groups = game.all_sprites, game.zombs
pg.sprite.Sprite.__init__(self, self.groups)
self.game = game
self.image = game.zomb_img
self.rect = self.image.get_rect()
self.pos = vec(m, n) * TILESIZE
self.m = m
self.n = n
#self.pos.x will return the x-coordinate in the grid world
self.stateSpace = [i for i in range(int(GRIDWIDTH) * int(GRIDHEIGHT))]
self.actionSpace = {'U': vec(0, -self.m), 'D': vec(0, self.m),
'L': vec(-1,0), 'R': vec(1,0)}
self.possibleActions = ['U', 'D', 'L', 'R']
self.vel = vec(0, 0)
self.acc = vec(0, 0)
self.rect.center = self.pos
self.rotate = 0
def setState(self, state):
self.pos = state
def TerminalState(self, state):
if self.game.health == 0:
return True
else:
return False
def step(self, action):
x, y = self.pos.x, self.pos.y
resultingState = self.pos + self.actionSpace[action]
self.game.reward = -1 if not self.TerminalState(resultingState) else 0
self.setState(resultingState)
return resultingState,self.game.reward,\
self.TerminalState(resultingState), None
def actionSpaceSample(self):
return np.random.choice(self.possibleActions)
def update(self):
self.image = game.zomb_img
self.rect = self.image.get_rect()
self.rect.center = self.pos
self.acc = vec(ZOMB_SPEED, 0)
self.acc += self.vel * -1
self.vel += self.acc * self.game.dt
self.pos += self.vel * self.game.dt + 0.5 * self.acc * self.game.dt ** 2
print(self.pos)
````
And here’s the processing and implementation of Q-learning in my main code:
````
g = Game()
def maxAction(Q, state, actions):
values = np.array([Q[state,a] for a in actions])
action = np.argmax(values)
return actions[action]
while True:
g.new()
ALPHA = 0.1
GAMMA = 1.0
EPS = 1.0
Q = {}
for zomb in g.zombs:
for state in zomb.stateSpace:
for action in zomb.possibleActions:
Q[state, action] = 0
numGames = 10
totalRewards = np.zeros(numGames)
for i in range(numGames):
print('starting game ', i)
done = False
g.new()
epRewards = 0
while not done:
rand = np.random.random()
for zomb in g.zombs:
observation = zomb.pos
action = maxAction(Q, observation, zomb.possibleActions) if rand < (1-EPS) \
else zomb.actionSpaceSample()
observationnew, reward, done, info = zomb.step(action)
epRewards += reward
for zomb in g.zombs:
possible_actions = zomb.possibleActions
action = maxAction(Q, observationnew, possible_actions)
Q[observation,action] = Q[observation,action] + ALPHA*(reward + \
GAMMA*Q[observationnew,action_] - Q[observation,action])
observation = observationnew
if EPS - 2 / numGames > 0:
EPS -= 2 / numGames
else:
EPS = 0
totalRewards[i] = epRewards
g.run()
````
However, this error popped out:
######
Traceback (most recent call last):
File "maze.py", line 170, in <module>
action = maxAction(Q, observationnew, possible_actions)
File "maze.py", line 136, in maxAction
values = np.array([Q[state,a] for a in actions])
File "maze.py", line 136, in <listcomp>
values = np.array([Q[state,a] for a in actions])
TypeError: unhashable type: 'pygame.math.Vector2'
######
Could someone provide me with a way to rectify this problem? (removing the vector sign in the dictionary doesn’t seem to help, another error pops up).
Thanks,
Cravan
P.S. Also erm I made a buncha mistakes along the way, would appreciate any suggestion or help. Thanks!
More information about the Tutor
mailing list