-
Notifications
You must be signed in to change notification settings - Fork 0
/
FlappyBirdGA.py
248 lines (210 loc) · 8.19 KB
/
FlappyBirdGA.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
237
238
239
240
241
242
243
244
245
246
247
248
import pygame
import pygad
import pygad.gann
import pygad.nn
import random
import math
import numpy
MAP_WIDTH = 500
MAP_HEIGHT = 512
FPS = 60
DARK_GROUND = (124, 115, 46)
class Bird():
def __init__(self):
self.x = 80
self.y = 250
self.width = 34
self.height = 24
self.alive = True
self.gravity = 0
self.velocity = 0.3
self.jump = -6
def flap(self):
self.gravity = self.jump
def update(self):
self.gravity += self.velocity
self.y += self.gravity
def is_dead(self, pipes):
if self.y >= MAP_HEIGHT or self.y + self.height <= 0:
return True
for pipe in pipes:
if not (
self.x > pipe.x+pipe.width or
self.x+self.width < pipe.x or
self.y > pipe.y+pipe.height or
self.y + self.height < pipe.y
):
return True
return False
class Pipe():
def __init__(self, x=0, y=0, height=40):
self.x = x
self.y = y
self.width = 40
self.height = height
self.speed = 3
def update(self):
self.x -= self.speed
def is_out(self):
if self.x+self.width <= 0:
return True
return False
class Game():
def __init__(self):
self.pipes = []
self.birds = []
self.score = 0
self.width = MAP_WIDTH
self.height = MAP_HEIGHT
self.spawn_interval = 90
self.interval = 0
self.Neuro = Neuroevolution()
self.gen = []
self.alives = 0
self.generation = 0
self.background_speed = 0.5
self.background_x = 0
self.max_score = 0
def start(self):
self.interval = 0
self.score = 0
self.pipes = []
self.birds = []
self.Neuro.score_map.clear()
self.gen = self.Neuro.ga_instance.population.copy()
for i in self.gen:
b = Bird()
self.birds.append(b)
self.generation += 1
self.alives = len(self.birds)
def update(self):
next_holl = 0
if self.alives > 0:
for i in range(0, len(self.pipes), 2):
if self.pipes[i].x + self.pipes[i].width > self.birds[0].x:
next_holl = self.pipes[i].height/self.height
break
for bird in self.birds:
if bird.alive is True:
idx = self.birds.index(bird)
solution = self.gen[idx]
inputs = numpy.array(
[bird.y/bird.height, next_holl]).reshape(1, -1)
pred = self.Neuro.predict(inputs, idx)
if (pred[0] > 0.5):
bird.flap()
bird.update()
if bird.is_dead(self.pipes) is True:
bird.alive = False
self.alives -= 1
# self.Neuro.score_map[list2hash(solution)] = self.score
self.Neuro.score_map[idx] = self.score
if self.is_end() is True:
self.Neuro.ga_instance.run()
self.start()
for i in range(len(self.pipes)-1, -1, -1):
pipe = self.pipes[i]
pipe.update()
if pipe.is_out() is True:
del self.pipes[i]
if self.interval == 0:
delta_board = 50
pipe_hole = 120
hole_position = round(
random.random()*(self.height-delta_board*2-pipe_hole))+delta_board
self.pipes.append(Pipe(x=self.width, y=0, height=hole_position))
self.pipes.append(
Pipe(x=self.width, y=hole_position+pipe_hole, height=self.height))
self.interval += 1
if self.interval == self.spawn_interval:
self.interval = 0
self.score += 1
self.max_score = max(self.score, self.max_score)
def is_end(self):
if self.alives == 0:
return True
else:
return False
def display(self):
global GAME_SPRITES, SCREEN, arial18
background = GAME_SPRITES['background']
pipetop = pygame.transform.rotate(GAME_SPRITES['pipe'], 180)
pipebottom = GAME_SPRITES['pipe']
bird_raw = GAME_SPRITES['bird']
for i in range(math.ceil(self.width/background.get_width())+1):
SCREEN.blit(background, (
i*background.get_width()-math.floor(self.background_x % background.get_width()), 0))
for pipe in self.pipes:
idx = self.pipes.index(pipe)
if idx % 2 == 0:
SCREEN.blit(pipetop, pygame.Rect(
pipe.x, pipe.y+pipe.height-pipetop.get_height(), pipe.width, pipetop.get_height()))
else:
SCREEN.blit(pipebottom, pygame.Rect(
pipe.x, pipe.y, pipe.width, pipebottom.get_height()))
for bird in self.birds:
if bird.alive is True:
bird_ro = pygame.transform.rotate(
bird_raw, math.pi/2*bird.gravity/20)
SCREEN.blit(bird_ro, (bird.x, bird.y))
text = f"Generation: {self.generation}\nMax Score:{self.max_score}\nScore:{self.score}\nAlive:{self.alives}/{self.Neuro.ga_instance.pop_size[0]}".split(
'\n')
for i, t in enumerate(text):
text = arial18.render(t, True, DARK_GROUND)
textX = text.get_rect().width
textY = text.get_rect().height
SCREEN.blit(text, (self.width/2-textX/2, (i*textY)))
class Neuroevolution():
def __init__(self):
self.GANN_instance = pygad.gann.GANN(num_solutions=50,
num_neurons_input=2,
num_neurons_hidden_layers=[2],
num_neurons_output=1,
hidden_activations=["sigmoid"],
output_activation="sigmoid")
self.population_vectors = pygad.gann.population_as_vectors(
population_networks=self.GANN_instance.population_networks)
self.initial_population = self.population_vectors.copy()
self.ga_instance = pygad.GA(num_generations=1,
num_parents_mating=10,
initial_population=self.initial_population,
sol_per_pop=50,
keep_elitism=5,
fitness_func=self.fitness_func,
mutation_type="random",
init_range_low=-2,
init_range_high=5,
keep_parents=2,
on_generation=self.callback_generation
)
self.score_map = {}
def fitness_func(self, ga_instance, solution, sol_idx):
return self.score_map[sol_idx]
def predict(self, data_inputs, idx):
return pygad.nn.predict(last_layer=self.GANN_instance.population_networks[idx], data_inputs=data_inputs, problem_type="regression")
def callback_generation(self, ga_instance):
population_matrices = pygad.gann.population_as_matrices(
population_networks=self.GANN_instance.population_networks, population_vectors=ga_instance.population)
self.GANN_instance.update_population_trained_weights(
population_trained_weights=population_matrices)
pygame.init()
SCREEN = pygame.display.set_mode((MAP_WIDTH, MAP_HEIGHT))
pygame.display.set_caption("Flappy Bird GA")
clock = pygame.time.Clock()
arial18 = pygame.font.SysFont('arial', 18, False, False)
GAME_SPRITES = {}
GAME_SPRITES['pipe'] = pygame.image.load("./assets/pipe.png").convert_alpha()
GAME_SPRITES['background'] = pygame.image.load(
"./assets/background.png").convert_alpha()
GAME_SPRITES['bird'] = pygame.image.load("./assets/bird.png").convert_alpha()
game = Game()
game.start()
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
game.update()
game.display()
pygame.display.flip()
clock.tick(FPS)