-
Notifications
You must be signed in to change notification settings - Fork 9
/
game
executable file
·320 lines (276 loc) · 8.48 KB
/
game
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
#!/usr/bin/env python
from neat import nn, population
import pygame
import field
import food
import snake
import math
import sys
import pickle
blockSize = 10 # size of blocks
width = 12 # size of field width in blocks
height = 12
screenSize = (width * blockSize, height * blockSize)
speed = 1000 # milliseconds per step
bg_color = 0x00A000
snake_color = 0x0000FF
best_fitness = 0
# Initialize pygame and open a window
pygame.init()
screen = pygame.display.set_mode(screenSize)
pygame.time.set_timer(pygame.USEREVENT, speed)
scr = pygame.surfarray.pixels2d(screen)
dx = 1
dy = 0
generation_number = 0
def get_game_matrix(scr):
global bg_color
global snake_color
res_matrix = []
for i,x in enumerate(scr):
res_arr = []
if (i % blockSize == 0):
for j,y in enumerate(x):
if j % blockSize == 0:
if scr[i][j] == snake_color:
res_arr += [1]
elif scr[i][j] == bg_color:
res_arr += [0]
else:
res_arr += [2]
res_matrix += [res_arr]
#print res_matrix
return res_matrix
import pickle
def save_object(obj, filename):
with open(filename, 'wb') as output:
pickle.dump(obj, output, pickle.HIGHEST_PROTOCOL)
def load_object(filename):
with open(filename, 'rb') as f:
obj = pickle.load(f)
return obj
def positivy(x):
if x > 0:
return x
else:
return 0
def left(orientation):
(dx,dy) = orientation
if (dx , dy) == (-1,0) :
dx,dy = 0,1
elif (dx , dy) == (0,1) :
dx,dy = 1,0
elif (dx , dy) == (1,0) :
dx,dy = 0,-1
elif (dx , dy) == (0,-1) :
(dx,dy) = (-1,0)
return (dx,dy)
def right(orientation):
(dx,dy) = orientation
if (dx , dy) == (-1,0) :
(dx,dy) = (0,-1)
elif (dx , dy) == (0,-1) :
(dx,dy) = (1,0)
elif (dx , dy) == (1,0) :
(dx,dy) = (0,1)
elif (dx , dy) == (0,1) :
(dx,dy) = (-1,0)
return (dx,dy)
def get_inputs(game_matrix,position,orientation): #(dx,dy)
dx,dy = orientation
#print "orientation",dx,dy
position_x,position_y = position
#print "position",position_x,position_y
dist_straight_wall = 0
dist_straight_food = 0
dist_straight_tail = 0
dist_left_wall = 0
dist_left_food = 0
dist_left_tail = 0
dist_right_wall = 0
dist_right_food = 0
dist_right_tail = 0
#print len(game_matrix) ,"rows"
#print len(game_matrix[0]) ,"cols"
#print game_matrix
while position_x >=0 and position_x < len(game_matrix) and position_y >= 0 and position_y <len(game_matrix[0]):
dist_straight_wall += 1
position_x += dx
position_y += dy
#dist_straight_wall -= 1
position_x,position_y = position
while position_x >=0 and position_x < len(game_matrix) and position_y >= 0 and position_y <len(game_matrix[0]) and game_matrix[position_x][position_y] != 2:
dist_straight_food += 1
position_x += dx
position_y += dy
#dist_straight_food -= 1
position_x,position_y = position
while position_x >=0 and position_x < len(game_matrix) and position_y >= 0 and position_y <len(game_matrix[0]) and game_matrix[position_x][position_y] != 1 or (position_x,position_y) == position:
dist_straight_tail += 1
position_x += dx
position_y += dy
#dist_straight_tail -= 1
position_x,position_y = position
(dx,dy) = left(orientation)
while position_x >=0 and position_x < len(game_matrix) and position_y >= 0 and position_y <len(game_matrix[0]):
dist_left_wall += 1
position_x += dx
position_y += dy
#dist_left_wall -= 1
position_x,position_y = position
while position_x >=0 and position_x < len(game_matrix) and position_y >= 0 and position_y <len(game_matrix[0]) and game_matrix[position_x][position_y] != 2:
dist_left_food += 1
position_x += dx
position_y += dy
#dist_left_food -= 1
position_x,position_y = position
while position_x >=0 and position_x < len(game_matrix) and position_y >= 0 and position_y <len(game_matrix[0]) and game_matrix[position_x][position_y] != 1 or (position_x,position_y) == position:
dist_left_tail += 1
position_x += dx
position_y += dy
#dist_left_tail -= 1
position_x,position_y = position
(dx,dy) = right(orientation)
while position_x >=0 and position_x < len(game_matrix) and position_y >= 0 and position_y <len(game_matrix[0]):
dist_right_wall += 1
position_x += dx
position_y += dy
#dist_right_wall -= 1
position_x,position_y = position
while position_x >=0 and position_x < len(game_matrix) and position_y >= 0 and position_y <len(game_matrix[0]) and game_matrix[position_x][position_y] != 2:
dist_right_food += 1
position_x += dx
position_y += dy
#dist_right_food -= 1
position_x,position_y = position
while position_x >=0 and position_x < len(game_matrix) and position_y >= 0 and position_y <len(game_matrix[0]) and game_matrix[position_x][position_y] != 1 or (position_x,position_y) == position:
dist_right_tail += 1
position_x += dx
position_y += dy
return [dist_straight_wall,dist_straight_food,dist_straight_tail,dist_left_wall,dist_left_food,dist_left_tail,dist_right_wall,dist_right_food,dist_right_tail]
def eval_fitness(genomes):
global best_fitness
global screen
global width
global height
global blockSize
global scr
global generation_number
global pop
global bg_color
global snake_color
#global dx
#global dy
#global speed
genome_number = 0
for g in genomes:
net = nn.create_feed_forward_phenotype(g)
dx = 1
dy = 0
score = 0.0
speed = 200
hunger = 100
# Create the field, the snake and a bit of food
theField = field.Field(screen, width, height, blockSize,bg_color)
theFood = food.Food(theField)
theSnake = snake.Snake(theField,snake_color,5)
pygame.time.set_timer(pygame.USEREVENT, speed)
snake_head_x , snake_head_y = theSnake.body[0]
dist = math.sqrt((snake_head_x - theFood.x) ** 2 + (snake_head_y - theFood.y) ** 2)
error = 0
while True:
event = pygame.event.wait()
if event.type == pygame.QUIT: # window closed
print "Quittin"
save_object(pop,'population.dat') ## export population
pygame.quit()
sys.exit()
if event.type == pygame.USEREVENT: # timer elapsed
matrix = get_game_matrix(scr)
#print matrix
head_x,head_y = theSnake.body[0]
head_x += dx
head_y += dy
inputs = get_inputs(matrix,(head_x,head_y),(dx,dy))
previous_state = inputs[1] < inputs[0]
if inputs[4] < inputs[3] :
inputs[4] = 1
else:
inputs[4] = 0
if inputs[7] < inputs[6] :
inputs[4] = 1
else:
inputs[4] = 0
outputs = net.serial_activate(inputs)
direction = outputs.index(max(outputs))
if direction == 0: #dont turn
#print "Straight"
pass
if direction == 1: #turn left
#print "Left"
(dx , dy) = left((dx,dy))
if direction == 2: # turn right
#print "Right"
(dx , dy) = right((dx,dy))
hunger -= 1
if not theSnake.move(dx, dy) or hunger <= 0:
break
else:
inputs = get_inputs(matrix,(head_x,head_y),(dx,dy))
current_state = inputs[1] < inputs[0]
if inputs[1] <= 0:
inputs[1] = 1
if inputs[0] <= 0:
inputs[0] = 1
if inputs[2] <= 0:
inputs[2] = 1
wall,bread,tail,wall_left,bread_left,tail_left,wall_right,bread_right,tail_right = (inputs)
score += math.sqrt((theFood.x -theSnake.body[0][0]) ** 2 + (theFood.y -theSnake.body[0][1]) ** 2 )
pass
if theSnake.body[0] == (theFood.x, theFood.y):
theSnake.grow()
speed -= 5
theFood = food.Food(theField) # make a new piece of food
score += 5
hunger += 100
theField.draw()
theFood.draw()
theSnake.draw()
pygame.display.update()
if event.type == pygame.KEYDOWN: # key pressed
if event.key == pygame.K_LEFT:
dx = -1
dy = 0
elif event.key == pygame.K_RIGHT:
dx = 1
dy = 0
elif event.key == pygame.K_DOWN:
dx = 0
dy = 1
elif event.key == pygame.K_UP:
dx = 0
dy = -1
# Game over!
for i in range(0, 10):
theField.draw()
theFood.draw()
theSnake.draw(damage = (i % 2 == 0))
pygame.display.update()
#pygame.time.wait(100)
print "score ",score
score = positivy(score)+1
g.fitness = positivy((-1/(math.sqrt(score+1))) + 1)
print "Error",error
best_fitness = max(best_fitness,g.fitness)
print "Generation",generation_number, "\tGenome",genome_number,"\tFitness",g.fitness,"\tBest fitness",best_fitness
genome_number += 1
generation_number += 1
if generation_number % 20 == 0:
save_object(pop,'population.dat')
print "Exporting population" ## export population
#save_object(pop,'population.dat') ## export population
pop = population.Population('config')
if len(sys.argv) > 1:
pop = load_object(sys.argv[1])
print "Reading popolation from",sys.argv[1]
pop.run(eval_fitness, 10000)