-
Notifications
You must be signed in to change notification settings - Fork 0
/
snaketrainer_ga.py
executable file
·282 lines (230 loc) · 9.41 KB
/
snaketrainer_ga.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
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
from art import *
import os
import time
import curses
import math
import argparse as ap
from Resources.snek import Snake
from Resources.field import Field
import Resources.fruits as frt
import importlib
from random import randrange
from NN import ga, nn
#Parse flags and arguments
parser = ap.ArgumentParser(description='Train snaek. Majeek!')
parser.add_argument("map", choices=['duel', 'survival', 'classic'], help="Map to play on")
parser.add_argument("-p", "--players", type=int, default=2, help="Population number")
parser.add_argument("-r", "--refresh", type=float, default=0.0, required=False, help="This should be non zero if you wan see how they move")
parser.add_argument("-g", "--generations", type=int, default=0, required=False, help="How many generations to run")
parser.add_argument("--manual-inputs", action="store_true", help="Setup inputs manually for each snake")
parser.add_argument("--manual-aesthetics", action="store_true", help="Setup aesthetics manually for each snake")
parser.add_argument("--collision", action="store_true", help="Do they bump into eachother?")
parser.add_argument("--max-moves", type=int, default=50, required=False, help="How many times are they allowed to run in circles?")
args = parser.parse_args()
stdscr = curses.initscr()
curses.start_color()
curses.init_pair(1, curses.COLOR_GREEN, curses.COLOR_BLACK)
curses.init_pair(2, curses.COLOR_RED, curses.COLOR_BLACK)
curses.init_pair(9, curses.COLOR_GREEN, curses.COLOR_RED)
curses.init_pair(10, curses.COLOR_GREEN, curses.COLOR_WHITE)
curses.init_pair(11, curses.COLOR_RED, curses.COLOR_WHITE)
curses.init_pair(12, curses.COLOR_GREEN, curses.COLOR_WHITE)
curses.init_pair(13, curses.COLOR_CYAN, curses.COLOR_WHITE)
def endCurse():
curses.nocbreak()
curses.endwin()
display_font = 'cjk'
m = __import__("Maps." + args.map)
m = getattr(m, args.map)
gen = 1
while gen <= args.generations or args.generations == 0:
map = getattr(m, args.map.capitalize())()
def setupName(i):
return "Player " + str(i+1)
def setupControls(name):
controls = []
stdscr.addstr("\nSetting up controls for u, " + name + "...")
stdscr.addstr("\nPwease hit UP key... ")
controls.append(stdscr.getkey())
stdscr.addstr("\nNow LEFT... ")
controls.append(stdscr.getkey())
stdscr.addstr("\nDOWN even... ")
controls.append(stdscr.getkey())
stdscr.addstr("\nRIGHT if I may... ")
controls.append(stdscr.getkey())
stdscr.addstr("I very graciously thenk\n\n\n")
return controls
def setupAesthetics(name):
aesthetics = []
stdscr.addstr("\nNow let's get funkeh with appearance, if you may >//< (for " + name + ")")
stdscr.addstr("\nSelect string... ")
curses.noecho()
x = stdscr.getkey()
aesthetics.append(x)
stdscr.addstr(text2art(x, font = display_font))
curses.echo()
stdscr.addstr("\nNow the color... ")
aesthetics.append(int(stdscr.getkey()))
return aesthetics
#Setup snakes controls and appearance
snakes = []
defaultControls = ['w', 'a', 's', 'd']
defaultAesthetics = ['o', 1]
for i in range(args.players):
controls = None
aesthetics = None
name = setupName(i)
#There is surely a better way to do this
if args.manual_inputs or len(defaultControls) <= 0:
controls = setupControls(name)
else:
controls = defaultControls
if args.manual_aesthetics or len(defaultAesthetics) <= 0:
aesthetics = setupAesthetics(name)
else:
aesthetics = defaultAesthetics
snake = Snake(map.getRandomSpawnLocation(i), 2)
snake.name = name
snake.setControls(controls[0], controls[1], controls[2], controls[3])
snake.setAesthetics(99 + i, aesthetics[0], aesthetics[1])
snakes.append(snake)
stdscr.clear()
stdscr.refresh()
field = map.field
obstacles = map.obstacles
fruitSpawners = map.fruitSpawners
inputs = []
map.refreshRate = args.refresh
def handleInput(s, input):
boxLeft = s.registerInput(input)
if isinstance(boxLeft, list):
field.clearTempNumAt(boxLeft)
handlePosition(s)
if not s.dead:
for x in s.body:
field.addTempNumAt(x, s.number, s.string, s.color)
def handlePosition(s):
global fruitSpawner
global field
if not (s.head[0] in range(field.height) and s.head[1] in range(field.width)):
s.dead = True
return
numAtHead = field.getNumAt(s.head)
if numAtHead == field.blank_number:
return
for f in fruitSpawners:
if isInRange(s.head, f.spawnStart, f.spawnEnd):
for fruit in f.spawnedFruits:
if s.head == fruit.start:
fruit.doMagic(s, snakes, map)
f.spawnedFruits.remove(fruit)
field.shapes.remove(fruit)
return
for o in obstacles:
if numAtHead == o.number:
s.dead = True
if args.collision:
for snake in snakes:
if s.head == snake.head:
(s if s.length < snake.length else snake).dead = True
elif numAtHead == snake.number:
s.dead = True
def drawField(stdscr):
for x in range(field.height):
for y in range(field.width):
stdscr.addstr(text2art(field.drawnMap[x][y][0], font=display_font), curses.color_pair(field.drawnMap[x][y][1]))
stdscr.addstr('\n')
def displayScore(stdscr):
stdscr.addstr("\nScore:\n")
for s in snakes:
if not s.name:
stdscr.addstr("Player " + str(snakes.index(s) + 1) + ': ')
else:
stdscr.addstr(s.name + ': ')
stdscr.addstr(str(s.getScore()) + '\n')
def isInRange(pos, start, end):
return (pos[0] >= start[0] and pos[0] <= end[0] and pos[1] >= start[1] and pos[1] <= end[1])
def positionToKey(pos, snake):
if pos == 0:
return snake.up
elif pos == 1:
return snake.left
elif pos == 2:
return snake.down
elif pos == 3:
return snake.right
def takeSecond(elem):
return elem[1]
#Beware the MAIN
def main(stdscr):
global inputs
global field
global fruitSpawners
try:
debugTimerStart = time.time()
pop = []
lastWinners = []
popDead = 0
deadSnakes = []
moves = 0
rankings = []
if os.path.exists("NN/" + args.map + ".save"):
lastWinners = nn.restore("NN/" + args.map + ".save")
for x in lastWinners:
winner = nn.ANN(1, [1], 1, nn.sigmoid)
winner.init_network_from_list(x)
pop.append(winner)
for i in range(args.players - len(lastWinners)):
pop.append(nn.ANN(121, [100, 100], 4, nn.sigmoid))
#Big loop
while not popDead == args.players and moves < args.max_moves:
debugTimerEnd = time.time()
#Clean and draw field thingy
stdscr.clear()
drawField(stdscr)
displayScore(stdscr)
stdscr.addstr("Loop lag: " + str(debugTimerEnd - debugTimerStart - map.refreshRate))
stdscr.addstr("\nGeneration: " + str(gen))
stdscr.refresh()
debugTimerStart = time.time()
#Get AI output
inputs = []
for x in range(len(snakes)):
if not snakes[x] in deadSnakes:
#Get proximity
gannInputs = field.getProximity(snakes[x], 5, args.collision)
outputAsKey = positionToKey(pop[x].get_highest_output(gannInputs), snakes[x])
#Handle inputs
handleInput(snakes[x], outputAsKey)
#Update for map related thingz
map.update()
for x in range(len(snakes)):
if not snakes[x] in deadSnakes:
snakes[x].additionalScore += 0.5
if snakes[x].dead:
rankings.append([pop[x], snakes[x].getScore()])
deadSnakes.append(snakes[x])
popDead += 1
field.refresh()
moves += 1
time.sleep(args.refresh)
#Sort rankings by snake score
rankings.sort(key=takeSecond)
#Save the best n (20% of pop) in a separate file
n = math.floor(0.2 * args.players) if math.floor(0.2 * args.players) > 4 else 4
n = (n + 1) if n%2 == 1 else n
theQuirkiest = []
for winner in rankings[-n:]:
theQuirkiest.append(winner[0].nn)
children = []
i = 0
while i < len(theQuirkiest):
children.extend(ga.breedTwo(theQuirkiest[i], theQuirkiest[i+1]))
i += 2
nn.store(children, "NN/" + args.map + ".save")
time.sleep(2 * args.refresh)
except KeyboardInterrupt:
endCurse()
os._exit(0)
curses.wrapper(main)
gen += 1