-
Notifications
You must be signed in to change notification settings - Fork 8
/
train_ai.py
438 lines (364 loc) · 12 KB
/
train_ai.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
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
import datetime
import glob
import math
import os
import shutil
import sqlite3
import string
import subprocess
import sys
from threading import Thread
import time
import random
import typing
from sys import platform
from pathlib import Path
import psutil
# import tensorflow as tf
# from keras.models import Sequential
# from keras.layers import Dense, Flatten
# from keras.models import load_model
import multiprocessing
import read_game_data as read_game_data
import get_action_weights as get_action_weights
import torch
#The Deck name and location
AI1Deck = 'Random1'
AI2Deck = 'Random2'#'Swordsoul'#
AIMaster = 'Swordsoul'#'Master'#
deck1 = 'AI_Swordsoul.ydk'#'AI_Random1.ydk'
deck2 = 'AI_Swordsoul.ydk'#'AI_Random2.ydk'
totalGames = 1000 # Games before any of the below
cycles = 1 # Trains decks
generations = 1 # Shuffles Decks
parallelGames = 1
rolloutCount = 1
isFirst = True
isTraining = True
winThresh = 100
pastWinLim = 5
idNumber = 1
reset = False
swap = False
def isrespondingPID(PID):
#if platform == "linux" or platform == "linux2":
return True
#https://stackoverflow.com/questions/16580285/how-to-tell-if-process-is-responding-in-python-on-windows
os.system('tasklist /FI "PID eq %d" /FI "STATUS eq running" > tmp.txt' % PID)
tmp = open('tmp.txt', 'r')
a = tmp.readlines()
tmp.close()
try:
if int(a[-1].split()[1]) == PID:
return True
else:
return False
except:
return False
def deleteData():
folder = './data'
for filename in os.listdir(folder):
file_path = os.path.join(folder, filename)
try:
if os.path.isfile(file_path) or os.path.islink(file_path):
os.unlink(file_path)
elif os.path.isdir(file_path):
shutil.rmtree(file_path)
except Exception as e:
print('Failed to delete %s. Reason: %s' % (file_path, e))
def resetDB():
deleteData()
dbfile = './cardData.cdb'
con = sqlite3.connect(dbfile)
cur = con.cursor()
sql_delete_query = """DELETE from L_ActionList"""
cur.execute(sql_delete_query)
sql_delete_query = """DELETE from L_CompareTo"""
cur.execute(sql_delete_query)
sql_delete_query = """DELETE from L_PlayRecord"""
cur.execute(sql_delete_query)
sql_delete_query = """DELETE from L_FieldState"""
cur.execute(sql_delete_query)
sql_delete_query = """DELETE from L_ActionState"""
cur.execute(sql_delete_query)
sql_delete_query = """DELETE from L_Weights"""
cur.execute(sql_delete_query)
sql_delete_query = """DELETE from L_GameResult"""
cur.execute(sql_delete_query)
con.commit()
con.close()
def resetMCTS():
dbfile = './cardData.cdb'
con = sqlite3.connect(dbfile)
cur = con.cursor()
sql_delete_query = """DELETE from MCST"""
cur.execute(sql_delete_query)
con.commit()
con.close()
def softResetDB():
dbfile = './cardData.cdb'
con = sqlite3.connect(dbfile)
cur = con.cursor()
sql_delete_query = """DELETE from L_ActionList"""
cur.execute(sql_delete_query)
sql_delete_query = """DELETE from L_CompareTo"""
cur.execute(sql_delete_query)
sql_delete_query = """DELETE from L_PlayRecord"""
cur.execute(sql_delete_query)
sql_delete_query = """DELETE from L_FieldState"""
cur.execute(sql_delete_query)
sql_delete_query = """DELETE from L_ActionState"""
cur.execute(sql_delete_query)
sql_delete_query = """DELETE from L_Weights"""
cur.execute(sql_delete_query)
sql_delete_query = """DELETE from L_GameResult"""
cur.execute(sql_delete_query)
con.commit()
con.close()
def resetYgoPro():
print("deleting old deck files from ygopro")
files = glob.glob(os.getcwd() +"/ProjectIgnis/deck/*")
for f in files:
os.remove(f)
print("deleting old replays from ygopro")
files = glob.glob(os.getcwd() +"/ProjectIgnis/replay/*")
for f in files:
os.remove(f)
def parseArg():
global reset, isTraining, repeatFor, matches, totalGames, isFirst, swap
reset = len(sys.argv)>1 and ("--reset" in sys.argv or "-r" in sys.argv)
swap = len(sys.argv)>1 and ("--swap" in sys.argv or "-s" in sys.argv)
isTraining = len(sys.argv)>1 and ("--training" in sys.argv or "-t" in sys.argv)
isFirst = len(sys.argv)>1 and ("--first" in sys.argv or "-f" in sys.argv)
if "--repeat" in sys.argv:
repeatFor = int(sys.argv[sys.argv.index("--repeat")+1])
if "--matches" in sys.argv:
matches = int(sys.argv[sys.argv.index("--matches")+1])
if "--games" in sys.argv:
totalGames = int(sys.argv[sys.argv.index("--games")+1])
def runAi(Deck = "Random1",
Name = "Random1",
Hand = 0,
TotalGames = 1,
RolloutCount = 0,
IsFirst = True,
IsTraining = True,
ShouldUpdate = True,
WinsThreshold = 50,
PastWinsLimit = 20,
Id = 0,
Port = 7911,
IsMCTS= False
):
currentdir = os.getcwd()
os.chdir(os.getcwd()+'/WindBot-Ignite-master/bin/Debug')
file_name = "WindBot.exe"
if platform == "linux" or platform == "linux2":
file_name = os.getcwd() + "/WindBot.exe"
p = subprocess.Popen([file_name,"Deck="+Deck,
"Name="+str(Name),
"Hand="+str(Hand),
"IsTraining="+str(IsTraining),
"ShouldUpdate="+str(ShouldUpdate),
"TotalGames="+str(TotalGames),
"RolloutCount="+str(RolloutCount),
"IsFirst="+str(IsFirst),
"WinsThreshold="+str(WinsThreshold),
"PastWinsLimit="+str(PastWinsLimit),
"Id="+str(Id),
"Port="+str(Port),
"IsMCTS="+str(IsMCTS)
],
stdout=subprocess.DEVNULL
)
os.chdir(currentdir)
return p
def shuffle_deck(deck_name):
filePath = os.getcwd() + '/WindBot-Ignite-master/bin/Debug/Decks/'+ deck_name
f = open(filePath,"r")
main = []
extra = []
side = []
part = 0
for line in f.readlines():
if "#extra" in line:
part = 1
continue
elif "!side" in line:
part = 2
continue
elif "#main" in line:
part = 0
continue
elif "#" in line:
continue
if part == 0:
main.append(line.strip())
elif part == 1:
extra.append(line.strip())
else:
side.append(line.strip())
random.shuffle(main)
random.shuffle(extra)
random.shuffle(side)
f.close()
f = open(filePath, "w")
f.write("#created by deck_maker_ai\n")
f.write("#main\n")
for i in main:
f.write(i +'\n')
f.write("#extra\n")
for i in extra:
f.write(i +'\n')
f.write("!side\n")
for i in side:
f.write(i +'\n')
f.close()
def swap_decks(deck1, deck2, name1, name2):
if deck1 == deck2:
return
path = os.getcwd() + '/WindBot-Ignite-master/bin/Debug/Decks/'
File1 = path + deck1
File2 = path + deck2
os.rename(File2, path + 'temp')
os.rename(File1, File2)
os.rename(path + 'temp', File1)
def setup():
global AI2Deck, AI2Deck, isTraining, totalGames
if reset:
if isTraining:
resetDB()
shuffle_deck(deck1)
shuffle_deck(deck2)
# src_dir=os.getcwd() + '/WindBot-Ignite-master/bin/Debug/Decks/' + deck1
# dst_dir=os.getcwd() + '/WindBot-Ignite-master/bin/Debug/Decks/' + deck2
# shutil.copy(src_dir,dst_dir)
resetYgoPro()
if not isTraining:
AI2Deck = AIMaster
def main_game_runner(isTraining, totalGames, Id1, Id2, Deck1, Deck2, port, isFirst):
start = time.time()
#subprocess.Popen - does not wait to finish
#subprocess.run - waits to finish
file_path = os.getcwd() + "/edopro_bin/ygoprodll.exe"
if platform == "linux" or platform == "linux2":
file_path = str(Path(__file__).resolve().parent.parent) + "/ProjectIgnisLinux/ygopro"
g = subprocess.Popen([file_path, "-p", str(port)], stdout=subprocess.DEVNULL)
while(g.poll() == None and not isrespondingPID(g.pid)):
time.sleep(1)
time.sleep(8)
print(" runningAi1 " + str(Id1) + ":" + Deck1)
p1 = runAi( Deck = Deck1,
Name = Id1,
Hand = 2,
TotalGames = totalGames,
RolloutCount = rolloutCount,
IsFirst = (isFirst),
IsTraining = isTraining,
ShouldUpdate= True,#isTraining,
WinsThreshold = winThresh,
PastWinsLimit = pastWinLim,
Id = Id1,
Port = port,
IsMCTS=isTraining#False#
)
time.sleep(1)
print(" runningAi2 "+ str(Id2) + ":" + Deck2)
p2 = runAi(Deck = Deck2,
Name = Id2,
Hand = 3,
TotalGames = totalGames,
RolloutCount = rolloutCount,
IsFirst = (not isFirst),
IsTraining = isTraining,
ShouldUpdate= False,#True,#isTraining,#
WinsThreshold = winThresh,
PastWinsLimit = pastWinLim,
Id = Id2,
Port = port,
IsMCTS=False
)
#psutil.Process(g.pid).nice(psutil.BELOW_NORMAL_PRIORITY_CLASS)
#psutil.Process(p1.pid).nice(psutil.BELOW_NORMAL_PRIORITY_CLASS)
#psutil.Process(p2.pid).nice(psutil.BELOW_NORMAL_PRIORITY_CLASS)
if (p1.poll() == None or p2.poll() == None):
time.sleep(1)
if (not (p1.poll() == None or p2.poll() == None)):
print(" WARNING! ai is not running")
timer = 0
timeout = 30 * 60 # Length of run
# print(p1.pid)
# print(p2.pid)
#make sure the game does not run longer than needed
#ends the ygopro program as soon as the ais are done. Ais play faster than what you see.
while (p1.poll() == None or p2.poll() == None):
continue
if platform == "linux" or platform == "linux2":
os.system("kill -9 " + str(g.pid))
else:
os.system(" TASKKILL /F /IM ygoprodll.exe")
end = time.time()
print("Time Past:" + str(datetime.timedelta(seconds=int(end - start))))
print("Average Game Time:"+str(datetime.timedelta(seconds=int((end - start)/(totalGames)))))
def main():
global reset, totalGames, generations, cycles, swap, isFirst, deck1, deck2
parseArg()
setup()
# if not reset:
# read_game_data.createBetterDataset()
# softResetDB()
for g in range(generations):
if reset:
shuffle_deck(deck1)
shuffle_deck(deck2)
for c in range(cycles):
read_game_data.read_data()
get_action_weights.fetchDatabaseData()
get_action_weights.load_data()
if reset or swap:
resetMCTS()
#if swap:
#swap_decks(deck1, deck2)
#isFirst = not isFirst
#resetDB()
proc = multiprocessing.Process(target=get_action_weights.run_server, args=())
proc.start()
jobs = []
pairs = []
bots = list(range(parallelGames * 2))
if (isTraining and AI2Deck != AIMaster) and False:
while bots:
r1 = bots.pop(random.randrange(0, len(bots)))
r2 = bots.pop(random.randrange(0, len(bots)))
pairs.append((r1, r2))
else:
while bots:
r1 = bots.pop(0)
r2 = bots.pop(0)
if swap and c % 2 == 1:
pairs.append((r2, r1))
else:
pairs.append((r1, r2))
for i in range(len(pairs)):
print("generation " + str(g) + " cycle: " + str(c) +" running game " + str(i) + ":" + str(pairs[i][0]) + "vs" + str(pairs[i][1]) + ": Total games " + str(totalGames))
port = 7911 + i
p = multiprocessing.Process(target=main_game_runner, args=(isTraining, totalGames, str(pairs[i][0]), str(pairs[i][1]), AI1Deck, AI2Deck, port, isFirst))
#psutil.Process(p.pid).nice(psutil.BELOW_NORMAL_PRIORITY_CLASS)
jobs.append(p)
p.start()
for job in jobs:
job.join()
proc.terminate() # sends a SIGTERM
print("done cycle")
# Swap the decks
if swap:
isFirst = not isFirst
if g <= generations - 1:
limit = parallelGames * cycles
read_game_data.read_data()
read_game_data.createBetterDataset()
#softResetDB()
read_game_data.read_data()
if __name__ == "__main__":
torch.multiprocessing.set_start_method('spawn')
main()