-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
236 lines (190 loc) · 8.95 KB
/
main.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
import argparse
import pygame
import game
import random
import time
import neat
import os
import math
import visualize
from graph_reporter import GraphReporter
import numpy as np
BLOCK_SPACING = 10
BLOCK_WIDTH = 80
SETTINGS = {}
SETTINGS['WINDOW_WIDTH'] = (BLOCK_SPACING + BLOCK_WIDTH) * 4 + BLOCK_SPACING + 500
SETTINGS['WINDOW_HEIGTH'] = (BLOCK_SPACING + BLOCK_WIDTH) * 4 + BLOCK_SPACING
SETTINGS['GENERATIONS'] = 100
SETTINGS['DEFAULT_STEP_DELAY'] = 1
SETTINGS['MAX_INVALID_MOVE_IN_A_ROW'] = 10
SETTINGS['CURRENT_GEN'] = 0
SETTINGS['NB_GAME_IN_GEN'] = 5
SETTINGS['PENALTY_FOR_INVALID_MOVE'] = 2
COLORS = {}
COLORS["GREY"] = (77, 77, 77)
def get_config_file_path():
return os.path.join(os.path.dirname(__file__), 'neat-config')
def load_config():
return neat.Config(neat.DefaultGenome, neat.DefaultReproduction,
neat.DefaultSpeciesSet, neat.DefaultStagnation,
get_config_file_path())
def eval_genomes(genomes, config):
SETTINGS['CURRENT_GEN'] += 1
for genome_id, genome in genomes:
genome.fitness = 0
net = neat.nn.FeedForwardNetwork.create(genome, config)
# net = neat.nn.RecurrentNetwork.create(genome, config)
num_games = SETTINGS['NB_GAME_IN_GEN']
fitnesses = np.zeros(shape=(num_games,))
for i in range(num_games):
game_grid = game.GameGrid()
invalid_moves_in_a_row = 0
while not game_grid.is_game_over() and invalid_moves_in_a_row < SETTINGS['MAX_INVALID_MOVE_IN_A_ROW']:
inputs = [invalid_moves_in_a_row]
for row in game_grid.get_elements():
for val in row:
inputs.append(val)
move_one_hot = net.activate(inputs)
move = game.MoveDirection(move_one_hot.index(max(move_one_hot)))
valid_move = game_grid.do_move(move)
# TODO Make legit fitness
if valid_move:
fitnesses[i] = game_grid.score
invalid_moves_in_a_row = 0
else:
invalid_moves_in_a_row += 1
game_grid.score -= (SETTINGS['PENALTY_FOR_INVALID_MOVE'] * ( SETTINGS['CURRENT_GEN'] / SETTINGS['GENERATIONS'])) * invalid_moves_in_a_row
fitnesses[i] = game_grid.score
genome.fitness = np.mean(fitnesses)
def run(args):
# Create the population, which is the top-level object for a NEAT run.
p = neat.Population(load_config())
if args.load is not None:
p = neat.checkpoint.Checkpointer.restore_checkpoint(args.load)
# Add a stdout reporter to show progress in the terminal.
p.add_reporter(neat.StdOutReporter(True))
stats = neat.StatisticsReporter()
p.add_reporter(stats)
graph_reporter = GraphReporter(stats)
p.add_reporter(graph_reporter)
saver = neat.checkpoint.Checkpointer(generation_interval=100, time_interval_seconds=None, filename_prefix=args.save)
p.add_reporter(saver)
# Run for at least 1 generations.
generations_to_run = max(SETTINGS['GENERATIONS'] - p.generation, 1)
winner = p.run(eval_genomes, generations_to_run)
if args.save is not None:
saver.save_checkpoint(p.config, p.population, p.species, p.generation)
graph_reporter.close()
print('\nBest genome:\n{!s}'.format(winner))
render_game_with_NN(winner)
visualize.plot_stats(stats, ylog=False, view=True)
visualize.plot_species(stats, view=True)
def render_game_with_NN(nn_param ):
pygame.init()
clock = pygame.time.Clock()
font = pygame.font.Font("fonts/ClearSans-Bold.ttf", 32)
step_delay = SETTINGS['DEFAULT_STEP_DELAY']
def update_fps():
fps = str(int(clock.get_fps()))
fps_text = font.render(fps, 1, pygame.Color("coral"))
return fps_text
window = pygame.display.set_mode((SETTINGS['WINDOW_WIDTH'], SETTINGS['WINDOW_HEIGTH']))
pygame.display.set_caption("2048 NEAT Game")
game_grid = game.GameGrid()
running = True
nn = neat.nn.FeedForwardNetwork.create(nn_param, load_config())
invalid_moves_in_a_row = 0
while not game_grid.is_game_over() and invalid_moves_in_a_row < SETTINGS['MAX_INVALID_MOVE_IN_A_ROW']:
inputs = [invalid_moves_in_a_row]
for row in game_grid.get_elements():
for val in row:
inputs.append(val)
move_one_hot = nn.activate(inputs)
move = game.MoveDirection(move_one_hot.index(max(move_one_hot)))
valid_move = game_grid.do_move(move)
if valid_move:
invalid_moves_in_a_row = 0
else:
invalid_moves_in_a_row += 1
render_game_grid(window, font, game_grid,
{"nb_invalid_move": invalid_moves_in_a_row,
"step_delay": step_delay})
window.blit(update_fps(), (10, 0))
clock.tick(60)
pygame.display.flip()
if game_grid.is_game_over() and invalid_moves_in_a_row < 10:
break
for evt in pygame.event.get():
if evt.type == pygame.QUIT:
raise SystemExit(0)
elif evt.type == pygame.KEYDOWN and evt.key == pygame.K_SPACE:
step_delay -= 0.2 if step_delay - 0.2 > 0 else 0
time.sleep(step_delay)
def render_game_grid(window, font, grid: game.GameGrid, data: {} = None):
window.fill((187, 173, 160))
elements = grid.get_elements()
window.blit(pygame.font.SysFont("arial", 32).render("Generation size: " + str(SETTINGS['GENERATIONS']), 1, COLORS["GREY"]), ( 375, 8))
window.blit(pygame.font.SysFont("arial", 32).render("Score: " + str(grid.score), 1, COLORS["GREY"]), ( 375, 40))
if (data is not None):
window.blit(pygame.font.SysFont("arial", 32).render("Nb invalid move: " + str(data['nb_invalid_move']), 1, COLORS["GREY"]), ( 375, 72))
window.blit(pygame.font.SysFont("arial", 32).render("Step delay: " + str(float("{:.2f}".format(data['step_delay']))), 1, COLORS["GREY"]), ( 375, 104))
for y, row in enumerate(elements):
for x, col in enumerate(row):
rect_x = x * (BLOCK_SPACING + BLOCK_WIDTH) + BLOCK_SPACING
rect_y = y * (BLOCK_SPACING + BLOCK_WIDTH) + BLOCK_SPACING
pygame.draw.rect(window, game.get_color_for_number(col),
pygame.Rect(rect_x,
rect_y,
BLOCK_WIDTH, BLOCK_WIDTH))
# Don't render text for 0
if col == 0:
continue
text = font.render(str(col), True, (119, 110, 101) if col <= 4 else (255, 255, 255))
window.blit(text, (rect_x + (float(BLOCK_WIDTH) / 2) - float(text.get_width()) / 2,
rect_y + (float(BLOCK_WIDTH) / 2) - float(text.get_height()) / 2))
def play():
pygame.init()
clock = pygame.time.Clock()
font = pygame.font.Font("fonts/ClearSans-Bold.ttf", 32)
def update_fps():
fps = str(int(clock.get_fps()))
fps_text = font.render(fps, 1, pygame.Color("coral"))
return fps_text
window = pygame.display.set_mode(
((BLOCK_SPACING + BLOCK_WIDTH) * 4 + BLOCK_SPACING, (BLOCK_SPACING + BLOCK_WIDTH) * 4 + BLOCK_SPACING))
pygame.display.set_caption("2048 NEAT Game")
game_grid = game.GameGrid()
running = True
window.fill((187, 173, 160)) # TODO: Move this in the loop if we do move animations for the numbers
while running:
for evt in pygame.event.get():
if evt.type == pygame.QUIT:
running = False
if evt.type == pygame.KEYDOWN and evt.key == pygame.K_q:
running = False
if evt.type == pygame.KEYDOWN and (evt.key == pygame.K_a or evt.key == pygame.K_LEFT):
game_grid.do_move(game.MoveDirection.LEFT)
if evt.type == pygame.KEYDOWN and (evt.key == pygame.K_d or evt.key == pygame.K_RIGHT):
game_grid.do_move(game.MoveDirection.RIGHT)
if evt.type == pygame.KEYDOWN and (evt.key == pygame.K_w or evt.key == pygame.K_UP):
game_grid.do_move(game.MoveDirection.UP)
if evt.type == pygame.KEYDOWN and (evt.key == pygame.K_s or evt.key == pygame.K_DOWN):
game_grid.do_move(game.MoveDirection.DOWN)
render_game_grid(window, font, game_grid)
window.blit(update_fps(), (10, 0))
clock.tick(60)
pygame.display.flip()
if game_grid.is_game_over():
time.sleep(5)
raise SystemExit(0)
# pygame.display.update()
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='NEAT that play 2048.')
parser.add_argument('-p', '--play', action='store_true', help='Play the NEAT game yourself')
parser.add_argument('-l', '--load', help='Load a checkpoint to resume the simulation')
parser.add_argument('-s', '--save', help='Load a checkpoint to resume the simulation')
args = parser.parse_args()
if args.play:
play()
else:
run(args)