-
Notifications
You must be signed in to change notification settings - Fork 8
/
generate_game_data.py
329 lines (261 loc) · 8.53 KB
/
generate_game_data.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
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
from export_database import export_database
from make_deck import MakeDeckRandom, MakeDeckPytorch
import read_game_data as read_game_data
import get_action_weights as get_action_weights
import torch
#The Deck name and location
AI1Deck = 'SnakeEyes'
AI2Deck = 'Tenpai'
# deck1 = os.getcwd() +'/edopro_bin/deck/AI_SnakeEyes.ydk'
# deck2 = os.getcwd() +'/edopro_bin/deck/AI_Tenpai.ydk'
reset = False
generations = 1
totalGames = 3
parallelGames = 3
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 resetDB():
dbfile = './cardData.cdb'
con = sqlite3.connect(dbfile)
cur = con.cursor()
sql_delete_query = """DELETE from GameStats"""
cur.execute(sql_delete_query)
sql_delete_query = """DELETE from GameTable"""
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 getPool():
dbfile = './cardData.cdb'
con = sqlite3.connect(dbfile)
cur = con.cursor()
cur.execute('SELECT MAX(Pool) FROM GameTable')
result = cur.fetchone()
con.close()
result = result[0]
if result == None:
result = 0
return result
def parseArg():
global totalGames, reset
reset = len(sys.argv)>1 and ("--reset" in sys.argv or "-r" in sys.argv)
if "--games" in sys.argv:
totalGames = int(sys.argv[sys.argv.index("--games")+1])
def runAi(Deck = "AIBase",
DeckFile = "",
Name = "Random1",
Hand = 0,
TotalGames = 1,
IsFirst = True,
Id = 0,
Port = 7911
):
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=" + "AIBase",#+Deck,
"DeckFile="+DeckFile,
"Name="+str(Name),
"Hand="+str(Hand),
"TotalGames="+str(TotalGames),
"IsFirst="+str(IsFirst),
"Id="+str(Id),
"Port="+str(Port),
],
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 main_game_runner(pool, totalGames, Name1, Name2, Deck1, Deck2, DeckFile1, DeckFile2, port):
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(Name1) + ":" + Deck1)
p1 = runAi( Deck = Deck1,
DeckFile = DeckFile1,
Name = Name1,
TotalGames = totalGames,
Id = pool,
Port = port,
)
time.sleep(1)
print(" runningAi2 "+ str(Name2) + ":" + Deck2)
p2 = runAi(Deck = Deck2,
DeckFile = DeckFile2,
Name = Name2,
TotalGames = totalGames,
Id = pool,
Port = port,
)
#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 totalGames
start = time.time()
parseArg()
pool = getPool()
proc = multiprocessing.Process(target=get_action_weights.run_server, args=())
proc.start()
decks1 = ["AI_SnakeEyes", "AI_Tenpai", "AI_Yubel", "AI_FireKing"] #["Labrynth", "Labrynth2",
# decks2 = decks1
decks2 = ["AI_Labrynth"]
#decks1 = ["SnakeEyes", "Tenpai", "Labrynth", "Labrynth2", "Branded", "Runick", "Runick2", "Runick3", "Yubel" ]
#decks2 = ["SnakeEyes", "Tenpai", "Labrynth", "Labrynth2", "Branded", "Runick", "Runick2", "Runick3", "Yubel" ]
if reset:
resetDB()
resetYgoPro()
#MakeDeckRandom()
for g in range(generations):
print("running generation " + str(g))
jobs = []
pairs = []
deck1index = 0
deck2index = 0
while deck2index < len(decks2):
while len(jobs) < parallelGames:
if deck2index >= len(decks2):
break
name1 = decks1[deck1index]
name2 = decks2[deck2index]
if not (deck1index > 0 and name2 in decks1[:deck1index - 1] and deck1 == deck2):
deck1 = os.getcwd() +'/edopro_bin/deck/' + name1 + '.ydk'
deck2 = os.getcwd() +'/edopro_bin/deck/' + name2 + '.ydk'
print("running game:" + str(name1) + "vs" + str(name2) + ": Total games " + str(totalGames))
bot1 = name1.rstrip(string.digits)
bot2 = name2.rstrip(string.digits)
port = 7911 + len(jobs)
p = multiprocessing.Process(target=main_game_runner, args=(pool, totalGames, str(name1), str(name2), bot1, bot2, deck1, deck2, port))
#psutil.Process(p.pid).nice(psutil.BELOW_NORMAL_PRIORITY_CLASS)
jobs.append(p)
p.start()
deck1index += 1
if deck1index >= len(decks1):
deck1index = 0
deck2index += 1
for job in jobs:
job.join()
jobs.clear()
proc.terminate() # sends a SIGTERM
print("done cycle")
export_database()
end = time.time()
print("Total Time Past:" + str(datetime.timedelta(seconds=int(end - start))))
print("Total Average Game Time:"+str(datetime.timedelta(seconds=int((end - start)/(len(decks1) * len(deck2) * totalGames)))))
#MakeDeckRandom()
#MakeDeckPytorch(5)
if __name__ == "__main__":
torch.multiprocessing.set_start_method('spawn')
main()