[Tutor] TypeError: unhashable type: 'pygame.math.Vector2'

Cravan savageapple850 at gmail.com
Thu Jun 25 10:56:12 EDT 2020


Hi all,

                I recently embarked on learning Q-learning in python, but got an error that I’ve no idea how to fix. So here’s my code 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 part in my main .py file:

`````

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 when I run the second file I encounter an error:

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'



More information about the Tutor mailing list