-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_bot.py
37 lines (30 loc) · 1.03 KB
/
run_bot.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
from keras.models import Sequential
from keras.layers.convolutional import Convolution2D
from keras.layers.core import Dense, Flatten
from keras.optimizers import RMSprop
from snake_game import snake_game
from agent import Agent
def main():
game_width = 12
game_height = 9
nb_frames = 4
actions = ((-1, 0), (1, 0), (0, -1), (0, 1), (0, 0))
# Recipe of deep reinforcement learning model
model = Sequential()
model.add(Convolution2D(
16,
nb_row=3,
nb_col=3,
activation='relu',
input_shape=(nb_frames, game_height, game_width)))
model.add(Convolution2D(32, nb_row=3, nb_col=3, activation='relu'))
model.add(Flatten())
model.add(Dense(256, activation='relu'))
model.add(Dense(len(actions)))
model.compile(RMSprop(), 'MSE')
agent = Agent(
model, nb_frames, snake_game, actions, size=(game_width, game_height))
agent.train(nb_epochs=10000, batch_size=64, gamma=0.8, save_model=True)
agent.play(nb_rounds=10)
if __name__ == '__main__':
main()