[Tutor] TypeError: unhashable type: 'pygame.math.Vector2'
Cravan
savageapple850 at gmail.com
Fri Jun 26 10:25:32 EDT 2020
Sadly, an error pops up when I change the code.
Here's my edited code for the class file:
````
class Zomb(pg.sprite.Sprite):
def __init__(self, game, x, y):
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()
#image = image provided in game
self.x = x
self.y = y
self.pos = (x, y)
#this is each state
#possible states
for a in range(int(GRIDWIDTH)):
for b in range(int(GRIDHEIGHT)):
self.stateSpace = [(a,b)]
self.actionSpace = {'U': - 1, 'D': 1,
'L': - 1, 'R': 1}
#corresponding adjustments
self.possibleActions = ['U', 'D', 'L', 'R']
#list of possible actions
self.vel = 0
self.acc = 0
self.rect.center = vec(self.x, self.y)
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.x, self.y
if self.actionSpace[action] == 'U':
self.y = self.y + 1
elif self.actionSpace[action] == 'D':
self.y = self.y - 1
elif self.actionSpace[action] == 'R':
self.x = self.x + 1
elif self.actionSpace[action] == 'L':
self.x = self.x - 1
resultingState = (self.x, self.y)
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.rotate = (self.game.player.pos - self.rect.center).angle_to(vec(1, 0))
self.image = self.game.zomb_img
self.rect = self.image.get_rect()
self.acc = vec(ZOMB_SPEED, 0).rotate(-self.rotate)
````
And here's my edited main implementation now in the update() function:
```
ALPHA = 0.1
GAMMA = 1.0
EPS = 1.0
Q = {}
for zomb in self.zombs:
print(zomb.stateSpace)
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 + 1)
epRewards = 0
rand = np.random.random()
for zomb in self.zombs:
observation = zomb.pos
#state
action = maxAction(Q, observation, zomb.possibleActions) if rand < (1-EPS) \
else zomb.actionSpaceSample()
#choosing the best action possible
observationnew, reward, done, info = zomb.step(action)
epRewards += reward
for zomb in self.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
```
However, this error pops up:
#######
Traceback (most recent call last):
File "maze.py", line 181, in <module>
g.run()
File "maze.py", line 53, in run
self.update()
File "maze.py", line 109, in update
action_ = maxAction(Q, observationnew, possible_actions)
File "maze.py", line 177, in maxAction
values = np.array([Q[state,a] for a in actions])
File "maze.py", line 177, in <listcomp>
values = np.array([Q[state,a] for a in actions])
KeyError: ((6, 26), 'U')
######
Apologies for the (late) and long email here, would appreciate if someone could offer some help.
Thanks,
Cravan
On 26/6/20, 12:18 AM, "Tutor on behalf of Mats Wichmann" <tutor-bounces+savageapple850=gmail.com at python.org on behalf of mats at wichmann.us> wrote:
On 6/25/20 10:06 AM, Mats Wichmann wrote:
sigh... think one thing, write another:
> Your Q is a dictionary;
>
> Q = {}
>
> and dicts specifically have the restriction that the type be hashable
... that the _key_ be hashable ...
_______________________________________________
Tutor maillist - Tutor at python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
More information about the Tutor
mailing list