-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathget_data.py
135 lines (113 loc) · 2.77 KB
/
get_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
import numpy as np
import chess
import chess.pgn
import random
import pickle
from util import *
DATA = "data/ccrl.pgn"
BATCH_SIZE = 25000
TOTAL_BATCHES = 24
TWOTIMES_BATCH_SIZE = 50000
'''
BATCH_SIZE = 2
TOTAL_BATCHES = 24
TWOTIMES_BATCH_SIZE = 4
'''
SPG = 6
def getGood(node):
total = 0
good = []
while not node.is_end():
next_node = node.variation(0)
x = (node.board().san(next_node.move))
total = total + 1
if 'x' not in x:
if total > 6:
good.append(total)
node = next_node
return good
def traverse(node, moves, arr, curIndex):
total = 1
arrSize = len(arr)
board = chess.Board()
while not node.is_end():
move = node.main_line().next()
board.push(chess.Move.from_uci(str(move)))
if total in moves:
arr[curIndex] = beautifyFEN(board.fen())
curIndex = curIndex + 1
if curIndex == arrSize:
return curIndex
moves.remove(total)
if not moves:
return curIndex
next_node = node.variation(0)
node = next_node
total = total + 1
print('bug, shouldnt have reached here')
return curIndex
def addGameData(game, arr, curIndex):
goodmoves = getGood(game)
picked = []
for i in range(SPG):
if not goodmoves:
break
nu = random.choice(goodmoves)
goodmoves.remove(nu)
picked.append(nu)
if not picked:
return curIndex
curIndex = traverse(game, picked, arr, curIndex)
return curIndex
def iterateOverFile():
whiteWins = np.zeros((BATCH_SIZE, 65))
blackWins = np.zeros((BATCH_SIZE, 65))
total = 0
runningTotal = 0
doneBatches = 0
whiteIndex = 0
blackIndex = 0
pgn = open(DATA)
while doneBatches < TOTAL_BATCHES:
g = chess.pgn.read_game(pgn)
if not g:
break
if g.headers["Result"] == "1-0" and whiteIndex < BATCH_SIZE:
whiteIndex = addGameData(g, whiteWins, whiteIndex)
elif g.headers["Result"] == "0-1" and blackIndex < BATCH_SIZE:
blackIndex = addGameData(g, blackWins, blackIndex)
total = whiteIndex + blackIndex
if total % 500 == 0:
print(doneBatches*BATCH_SIZE + total/2)
if (total > 0 and total % TWOTIMES_BATCH_SIZE == 0):
name = 'volume' + str(doneBatches) + '.p'
print(name)
amount = BATCH_SIZE
saveP(addLabels((whiteWins, blackWins)), name)
doneBatches = doneBatches + 1
total = 0
whiteIndex = 0
blackIndex = 0
def addLabels(arrs):
x_labels = np.zeros((BATCH_SIZE,2))
x = np.zeros((BATCH_SIZE,2,65))
w = arrs[0]
b = arrs[1]
np.random.shuffle(w)
np.random.shuffle(b)
for i in range(BATCH_SIZE):
cur = [w[i],b[i]]
label = [1,0]
if random.random() > 0.5:
# cur = np.concatenate((b[i],w[i]), axis=0)
cur = [b[i],w[i]]
label = [0,1]
x[i] = cur
x_labels[i] = label
return (x, x_labels)
def saveP(arr, name):
full_data = {"x": arr[0], "x_labels": arr[1]}
f = open('pGames/' + name, "wb")
pickle.dump(full_data, f)
f.close()
iterateOverFile()