-
Notifications
You must be signed in to change notification settings - Fork 130
/
mini_pacman.py
165 lines (127 loc) · 5.25 KB
/
mini_pacman.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# Simple pacman to avoid falling bombs
# Original author: Jin Kim (golbin) https://github.com/golbin/TensorFlow-Tutorials
import numpy as np
import random
import matplotlib.pyplot as plt
import matplotlib.patches as patches
class Gym:
def __init__(self, screen_width=6, screen_height=10, show_game=True):
self.screen_width = screen_width
self.screen_height = screen_height
self.road_width = (screen_width // 2)
self.road_left = self.road_width // 2 + 1
self.road_right = self.road_left + self.road_width - 1
self.car = {"col": 0, "row": 2}
self.block = [
{"col": 0, "row": 0, "speed": 1},
{"col": 0, "row": 0, "speed": 2},
]
self.total_reward = 0.
self.current_reward = 0.
self.total_game = 0
self.show_game = show_game
if show_game:
self.fig, self.axis = self.prepare_display()
def prepare_display(self):
fig, axis = plt.subplots(figsize=(4, 6))
fig.set_size_inches(4, 6)
fig.canvas.mpl_connect('close_event', exit)
plt.axis((0, self.screen_width, 0, self.screen_height))
plt.tick_params(top='off', right='off',
left='off', labelleft='off',
bottom='off', labelbottom='off')
plt.draw()
plt.ion()
plt.show()
return fig, axis
def action_space_sample(self):
return random.randint(0, 2)
def get_state(self):
state = np.zeros((self.screen_width, self.screen_height))
state[self.car["col"], self.car["row"]] = 1
if self.block[0]["row"] < self.screen_height:
state[self.block[0]["col"], self.block[0]["row"]] = 1
if self.block[1]["row"] < self.screen_height:
state[self.block[1]["col"], self.block[1]["row"]] = 1
return state.reshape((-1, self.screen_width * self.screen_height))
def draw_screen(self):
title = " Avg. Reward: %d Reward: %d Total Game: %d" % (
self.total_reward / self.total_game,
self.current_reward,
self.total_game)
self.axis.clear()
self.axis.set_title(title, fontsize=12)
road = patches.Rectangle((self.road_left - 1, 0), self.road_width + 1,
self.screen_height, linewidth=0, facecolor="#333333")
if self._is_gameover():
car_color = "#FF0000"
else:
car_color = "#00FF00"
car = patches.Wedge((self.car["col"] - 0.5, self.car["row"] - 0.5),
0.5, 20, 340, linewidth=0, facecolor=car_color)
block1 = patches.Circle((self.block[0][
"col"] - 0.5, self.block[0]["row"]), 0.5, linewidth=0, facecolor="#0099FF")
block2 = patches.Circle((self.block[1][
"col"] - 0.5, self.block[1]["row"]), 0.5, linewidth=0, facecolor="#EB70AA")
self.axis.add_patch(road)
self.axis.add_patch(car)
self.axis.add_patch(block1)
self.axis.add_patch(block2)
self.fig.canvas.draw()
plt.pause(0.0001)
def reset(self):
self.current_reward = 0
self.total_game += 1
self.car["col"] = int(self.screen_width / 2)
self.block[0]["col"] = random.randrange(
self.road_left, self.road_right + 1)
self.block[0]["row"] = 0
self.block[1]["col"] = random.randrange(
self.road_left, self.road_right + 1)
self.block[1]["row"] = 0
self.update_block()
return self.get_state()
def update_car(self, move):
self.car["col"] = max(self.road_left, self.car["col"] + move)
self.car["col"] = min(self.car["col"], self.road_right)
def update_block(self):
reward = 0
if self.block[0]["row"] > 0:
self.block[0]["row"] -= self.block[0]["speed"]
else:
self.block[0]["col"] = random.randrange(
self.road_left, self.road_right + 1)
self.block[0]["row"] = self.screen_height
reward += 1
if self.block[1]["row"] > 0:
self.block[1]["row"] -= self.block[1]["speed"]
else:
self.block[1]["col"] = random.randrange(
self.road_left, self.road_right + 1)
self.block[1]["row"] = self.screen_height
reward += 1
return reward
def _is_gameover(self):
if ((self.car["col"] == self.block[0]["col"] and
self.car["row"] == self.block[0]["row"]) or
(self.car["col"] == self.block[1]["col"] and
self.car["row"] == self.block[1]["row"])):
self.total_reward += self.current_reward
return True
else:
return False
def step(self, action):
# action: 0: left, 1: stay, 2: right
self.update_car(action - 1)
escape_reward = self.update_block()
stable_reward = 1. / self.screen_height if action == 1 else 0
gameover = self._is_gameover()
stat = self.get_state()
if gameover:
reward = -2
else:
reward = escape_reward + stable_reward
self.current_reward += reward
if self.show_game:
self.draw_screen()
return stat, reward, gameover, None