-
Notifications
You must be signed in to change notification settings - Fork 1
/
CreateFullDatabase.py
154 lines (131 loc) · 4.48 KB
/
CreateFullDatabase.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
"""
Creates full database of human data with three arrays:
Inputs: 15x8x8 images of the chessboard
Value Output: -1 to 1 evaluation of board (-1 is a win for black, 1 is a win for white)
Policy Output: Probabilities from 0 to 1 of making a move.
"""
import chess.variant
import torch
import numpy as np
import torch.nn as nn
import torch.utils.data as data_utils
from ChessEnvironment import ChessEnvironment
import ActionToArray
import pathlib
import h5py
import json
pgnGames = list(pathlib.Path('stockfishdatabase').glob('*.pgn'))
listOfMoves = []
listOfResults = []
for g in range(2): #len(pgnGames)):
pgn = open(pgnGames[g])
listOfMoves = []
for k in range(20): # 190,000 assures all games are looked at.
try:
game = chess.pgn.read_game(pgn)
# used for lichess database
#whiteElo = int(game.headers["WhiteElo"])
#blackElo = int(game.headers["BlackElo"])
# used for stockfish database
whiteElo = 4000
blackElo = 4000
result = str(game.headers["Result"])
if result == "1-0":
result = 1
elif result == "0-1":
result = -1
else:
result = 0
benchmark = 2000
if whiteElo >= benchmark and blackElo >= benchmark:
print("Index: ", k)
#print(whiteElo)
#print(blackElo)
board = game.board()
singleGame = []
for move in game.mainline_moves():
board.push_uci(move.uci())
singleGame.append(move.uci())
listOfMoves.append(singleGame)
listOfResults.append(result)
print(pgnGames[g])
except:
print("", end="")
inList = []
outList = []
actionList = []
actionMagList = []
for j in range(len(listOfMoves)):
board = ChessEnvironment()
for i in range(len(listOfMoves[j])):
state = ActionToArray.boardToBinaryArray(board.boardToState())
value = listOfResults[j]
action = ActionToArray.moveArray(listOfMoves[j][i], board.arrayBoard)
if i%2 == 0:
if value == 1:
mag = 1
elif value == 0:
mag = 0.5
else:
mag = 0.2
else:
if value == -1:
mag = 1
elif value == 0:
mag = 0.5
else:
mag = 0.2
if board.board.legal_moves.count() != len(ActionToArray.legalMovesForState(board.arrayBoard, board.board)):
print("ERROR!")
# make move
board.makeMove(listOfMoves[j][i])
# add to database
inList.append(state)
outList.append(value)
actionList.append(np.argmax(action))
actionMagList.append(mag)
print(board.board)
board.gameResult()
print(board.gameStatus)
print(len(inList))
print(len(outList))
print(len(actionList))
print(len(actionMagList))
print(str(int(j + 1)), "out of ", len(listOfMoves), "parsed.")
# all games are parsed, now convert list into array for outputs
inputs = np.zeros((len(inList), 15))
valueOutputs = np.zeros(len(outList))
policyOutputs = np.zeros(len(actionList))
policyMagOutputs = np.zeros(len(actionMagList))
i = 0
while len(outList) > 0:
inputs[i] = inList[len(inList)-1]
valueOutputs[i] = outList[len(outList)-1]
policyOutputs[i] = actionList[len(actionList)-1]
policyMagOutputs[i] = actionMagList[len(actionMagList)-1]
outList.pop()
actionList.pop()
inList.pop()
actionMagList.pop()
i += 1
#print(inputs)
print(valueOutputs)
#print(policyOutputs)
print(policyMagOutputs)
print(inputs.shape)
print(valueOutputs.shape)
print(policyOutputs.shape)
print(policyMagOutputs.shape)
# save outputs!
saveName = 'Training Data/StockfishOutputs3.h5'
with h5py.File(saveName, 'w') as hf:
hf.create_dataset("Policy Outputs", data=policyOutputs, compression='gzip', compression_opts=5)
hf.create_dataset("Policy Magnitude Outputs", data=policyMagOutputs, compression='gzip', compression_opts=5)
hf.create_dataset("Value Outputs", data=valueOutputs, compression='gzip', compression_opts=5)
saveName = 'Training Data/StockfishInputs3[binaryConverted].h5'
with h5py.File(saveName, 'w') as hf:
#dtype = h5py.special_dtype(vlen=str)
hf.create_dataset("Inputs", data=inputs, compression='gzip', compression_opts=9) #dtype=dtype,
inputs = []
valueOutputs = []
policyOutputs = []