-
Notifications
You must be signed in to change notification settings - Fork 2
/
pacman_util.py
145 lines (114 loc) · 4.79 KB
/
pacman_util.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import numpy as np
from pacman import Directions
# Used code from
# DQN code implemented by
# https://github.com/tychovdo/PacmanDQN
def getOneHot(actions, batch_size):
""" Create list of vectors with 1 values at index of action in list """
# actions_onehot = np.zeros((self.params['batch_size'], 4))
actions_one_hot = np.zeros((batch_size, 4))
for i in range(len(actions)):
actions_one_hot[i][int(actions[i])] = 1
return actions_one_hot
def mergeStateMatrices(state_matrices):
""" Merge state matrices to one state tensor """
stateMatrices = np.swapaxes(state_matrices, 0, 2)
total = np.zeros((7, 7))
for i in range(len(stateMatrices)):
total += (i + 1) * stateMatrices[i] / 6
return total
def getStateMatrices(state, width, height):
""" Return wall, ghosts, food, capsules matrices """
def getWallMatrix(state):
""" Return matrix with wall coordinates set to 1 """
width, height = state.data.layout.width, state.data.layout.height
grid = state.data.layout.walls
matrix = np.zeros((height, width), dtype=np.int8)
for i in range(grid.height):
for j in range(grid.width):
# Put cell vertically reversed in matrix
cell = 1 if grid[j][i] else 0
matrix[-1 - i][j] = cell
return matrix
def getPacmanMatrix(state):
""" Return matrix with pacman coordinates set to 1 """
width, height = state.data.layout.width, state.data.layout.height
matrix = np.zeros((height, width), dtype=np.int8)
for agentState in state.data.agentStates:
if agentState.isPacman:
pos = agentState.configuration.getPosition()
cell = 1
matrix[-1 - int(pos[1])][int(pos[0])] = cell
return matrix
def getGhostMatrix(state):
""" Return matrix with ghost coordinates set to 1 """
width, height = state.data.layout.width, state.data.layout.height
matrix = np.zeros((height, width), dtype=np.int8)
for agentState in state.data.agentStates:
if not agentState.isPacman:
if not agentState.scaredTimer > 0:
pos = agentState.configuration.getPosition()
cell = 1
matrix[-1 - int(pos[1])][int(pos[0])] = cell
return matrix
def getScaredGhostMatrix(state):
""" Return matrix with ghost coordinates set to 1 """
width, height = state.data.layout.width, state.data.layout.height
matrix = np.zeros((height, width), dtype=np.int8)
for agentState in state.data.agentStates:
if not agentState.isPacman:
if agentState.scaredTimer > 0:
pos = agentState.configuration.getPosition()
cell = 1
matrix[-1 - int(pos[1])][int(pos[0])] = cell
return matrix
def getFoodMatrix(state):
""" Return matrix with food coordinates set to 1 """
width, height = state.data.layout.width, state.data.layout.height
grid = state.data.food
matrix = np.zeros((height, width), dtype=np.int8)
for i in range(grid.height):
for j in range(grid.width):
# Put cell vertically reversed in matrix
cell = 1 if grid[j][i] else 0
matrix[-1 - i][j] = cell
return matrix
def getCapsulesMatrix(state):
""" Return matrix with capsule coordinates set to 1 """
width, height = state.data.layout.width, state.data.layout.height
capsules = state.data.layout.capsules
matrix = np.zeros((height, width), dtype=np.int8)
for i in capsules:
# Insert capsule cells vertically reversed into matrix
matrix[-1 - i[1], i[0]] = 1
return matrix
# Create observation matrix as a combination of
# wall, pacman, ghost, food and capsule matrices
# width, height = state.data.layout.width, state.data.layout.height
observation = np.zeros((6, height, width))
observation[0] = getWallMatrix(state)
observation[1] = getPacmanMatrix(state)
observation[2] = getGhostMatrix(state)
observation[3] = getScaredGhostMatrix(state)
observation[4] = getFoodMatrix(state)
observation[5] = getCapsulesMatrix(state)
# observation = np.swapaxes(observation, 0, 2)
return observation
def get_value(direction):
if direction == Directions.NORTH:
return 0.
elif direction == Directions.EAST:
return 1.
elif direction == Directions.SOUTH:
return 2.
else:
return 3.
def get_direction(value):
if value == 0.:
return Directions.NORTH
elif value == 1.:
return Directions.EAST
elif value == 2.:
return Directions.SOUTH
else:
return Directions.WEST