-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathValueEvaluation.py
More file actions
316 lines (248 loc) · 11.2 KB
/
ValueEvaluation.py
File metadata and controls
316 lines (248 loc) · 11.2 KB
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
import numpy as np
from ChessEnvironment import ChessEnvironment
import torch
import copy
import ActionToArray
from DoubleHeadDataset import DoubleHeadDataset
import ChessResNet
import time
import math
import threading
def moveValueEvaluation(move, board, network):
# import the network
neuralNet = network
tempBoard = copy.deepcopy(board)
# import the game board
evalBoard = ChessEnvironment()
evalBoard.arrayBoard = tempBoard.arrayBoard
evalBoard.board = tempBoard.board
evalBoard.plies = tempBoard.plies
evalBoard.whiteCaptivePieces = tempBoard.whiteCaptivePieces
evalBoard.blackCaptivePieces = tempBoard.blackCaptivePieces
evalBoard.actuallyAPawn = tempBoard.actuallyAPawn
evalBoard.updateNumpyBoards()
# make temporary move
evalBoard.makeMove(move)
# evalBoard.printBoard()
state = evalBoard.boardToState()
nullAction = torch.from_numpy(np.zeros(1)) # this will not be used, is only a filler
testSet = DoubleHeadDataset(state, nullAction, nullAction)
generatePredic = torch.utils.data.DataLoader(dataset=testSet, batch_size=len(state), shuffle=False)
with torch.no_grad():
for images, labels1, labels2 in generatePredic:
neuralNet.eval()
output = (neuralNet(images)[1].numpy())[0][0]
# so far, output gives a winning probability from -1 to 1, 1 for white, -1 for black. We want to scale this to
# a value between 0 and 1.
output = (output/2) + 0.5
# now we have an evaluation from 0 to 1. Now we have to scale this to a probability
# for either black or white depending on who moves next.
turn = evalBoard.plies % 2
# if plies is divisible by 2, then black has just moved, which means that
# our evaluation should be for black. If plies is not, then white has just moved,
# which means that our evaluation should be for white.
if turn == 0:
output = 1-output
# now, let's return our evaluation
# print(output)
return output
def moveValueEvaluationsNew(legalMoves, board, network):
positions = np.zeros((len(legalMoves),15,8,8))
# make the input vector
for i in range(len(legalMoves)):
tempBoard = copy.deepcopy(board)
# import the game board
evalBoard = ChessEnvironment()
evalBoard.arrayBoard = tempBoard.arrayBoard
evalBoard.board = tempBoard.board
evalBoard.plies = tempBoard.plies
evalBoard.whiteCaptivePieces = tempBoard.whiteCaptivePieces
evalBoard.blackCaptivePieces = tempBoard.blackCaptivePieces
evalBoard.actuallyAPawn = tempBoard.actuallyAPawn
evalBoard.updateNumpyBoards()
evalBoard.makeMove(legalMoves[i])
evalBoard.updateNumpyBoards()
positions[i] = evalBoard.boardToState()
positions = torch.from_numpy(positions)
nullAction = torch.from_numpy(np.zeros(len(positions))) # this will not be used, is only a filler
testSet = DoubleHeadDataset(positions, nullAction, nullAction)
generatePredic = torch.utils.data.DataLoader(dataset=testSet, batch_size=128, shuffle=False)
with torch.no_grad():
for images, labels1, labels2 in generatePredic:
output = neuralNet(images)[1].detach().numpy().flatten()
# so far, output gives a winning probability from -1 to 1, 1 for white, -1 for black. We want to scale this to
# a value between 0 and 1.
output = (output / 2) + 0.5
# now we have an evaluation from 0 to 1. Now we have to scale this to a probability
# for either black or white depending on who moves next.
turn = evalBoard.plies % 2
# if plies is divisible by 2, then black has just moved, which means that
# our evaluation should be for black. If plies is not, then white has just moved,
# which means that our evaluation should be for white.
if turn == 0:
output = 1 - output
return output
def moveValueEvaluations(legalMoves, board, network):
evaluation = np.zeros(len(legalMoves))
class myThread(threading.Thread):
def __init__(self, move, board, network, index):
threading.Thread.__init__(self)
self.move = move
self.board = board
self.network = network
self.index = index
def run(self):
# import the network
neuralNet = network
tempBoard = copy.deepcopy(self.board)
# import the game board
evalBoard = ChessEnvironment()
evalBoard.arrayBoard = tempBoard.arrayBoard
evalBoard.board = tempBoard.board
evalBoard.plies = tempBoard.plies
evalBoard.whiteCaptivePieces = tempBoard.whiteCaptivePieces
evalBoard.blackCaptivePieces = tempBoard.blackCaptivePieces
evalBoard.actuallyAPawn = tempBoard.actuallyAPawn
evalBoard.updateNumpyBoards()
# make temporary move
evalBoard.makeMove(self.move)
state = torch.from_numpy(evalBoard.boardToState())
output = (neuralNet(state)[1].detach().numpy())[0][0]
# so far, output gives a winning probability from -1 to 1, 1 for white, -1 for black. We want to scale this to
# a value between 0 and 1.
output = (output / 2) + 0.5
# now we have an evaluation from 0 to 1. Now we have to scale this to a probability
# for either black or white depending on who moves next.
turn = evalBoard.plies % 2
# if plies is divisible by 2, then black has just moved, which means that
# our evaluation should be for black. If plies is not, then white has just moved,
# which means that our evaluation should be for white.
if turn == 0:
output = 1 - output
# now, let's return our evaluation
evaluation[self.index] = output
threads = []
for i in range(len(legalMoves)):
t = myThread(legalMoves[i], board, network, i)
threads.append(t)
for thread in threads:
thread.start()
for thread in threads:
thread.join()
return evaluation
def objectivePositionEval(board, network):
# import the network
neuralNet = network
tempBoard = copy.deepcopy(board)
# import the game board
evalBoard = ChessEnvironment()
evalBoard.arrayBoard = tempBoard.arrayBoard
evalBoard.board = tempBoard.board
evalBoard.plies = tempBoard.plies
evalBoard.whiteCaptivePieces = tempBoard.whiteCaptivePieces
evalBoard.blackCaptivePieces = tempBoard.blackCaptivePieces
evalBoard.actuallyAPawn = tempBoard.actuallyAPawn
evalBoard.updateNumpyBoards()
# evalBoard.printBoard()
state = evalBoard.boardToState()
nullAction = torch.from_numpy(np.zeros(1)) # this will not be used, is only a filler
testSet = DoubleHeadDataset(state, nullAction, nullAction)
generatePredic = torch.utils.data.DataLoader(dataset=testSet, batch_size=len(state), shuffle=False)
with torch.no_grad():
for images, labels1, labels2 in generatePredic:
neuralNet.eval()
output = (neuralNet(images)[1].numpy())[0][0]
# so far, output gives a winning probability from -1 to 1, 1 for white, -1 for black. We want to scale this to
# a value between 0 and 1.
output = (output/2) + 0.5
turn = evalBoard.plies % 2
output = (output*2)-1
# now, this is a probability of white winning. we need to change this to centipawns...
output = 290.680623072 * math.tan(1.548090806 * output)
if turn == 1:
output = -output
return output
def objectivePositionEvalMCTS(board, network, MCTS_WIN_RATE):
# import the network
neuralNet = network
tempBoard = copy.deepcopy(board)
# import the game board
evalBoard = ChessEnvironment()
evalBoard.arrayBoard = tempBoard.arrayBoard
evalBoard.board = tempBoard.board
evalBoard.plies = tempBoard.plies
evalBoard.whiteCaptivePieces = tempBoard.whiteCaptivePieces
evalBoard.blackCaptivePieces = tempBoard.blackCaptivePieces
evalBoard.actuallyAPawn = tempBoard.actuallyAPawn
evalBoard.updateNumpyBoards()
# evalBoard.printBoard()
state = evalBoard.boardToState()
nullAction = torch.from_numpy(np.zeros(1)) # this will not be used, is only a filler
testSet = DoubleHeadDataset(state, nullAction, nullAction)
generatePredic = torch.utils.data.DataLoader(dataset=testSet, batch_size=len(state), shuffle=False)
with torch.no_grad():
for images, labels1, labels2 in generatePredic:
neuralNet.eval()
output = (neuralNet(images)[1].numpy())[0][0]
turn = evalBoard.plies % 2
if turn == 1:
MCTS_WIN_RATE = 1 - MCTS_WIN_RATE
# so far, output gives a winning probability from -1 to 1, 1 for white, -1 for black. We want to scale this to
# a value between 0 and 1.
output = (output/2) + 0.5
output = (output+MCTS_WIN_RATE)/2
output = (output*2)-1
# now, this is a probability of white winning. we need to change this to centipawns...
output = 290.680623072 * math.tan(1.548090806 * output)
if turn == 1:
output = -output
return output
def positionEval(board, network):
# import the network
neuralNet = network
tempBoard = copy.deepcopy(board)
# import the game board
evalBoard = ChessEnvironment()
evalBoard.arrayBoard = tempBoard.arrayBoard
evalBoard.board = tempBoard.board
evalBoard.plies = tempBoard.plies
evalBoard.whiteCaptivePieces = tempBoard.whiteCaptivePieces
evalBoard.blackCaptivePieces = tempBoard.blackCaptivePieces
evalBoard.actuallyAPawn = tempBoard.actuallyAPawn
evalBoard.updateNumpyBoards()
# evalBoard.printBoard()
state = evalBoard.boardToState()
nullAction = torch.from_numpy(np.zeros(1)) # this will not be used, is only a filler
testSet = DoubleHeadDataset(state, nullAction, nullAction)
generatePredic = torch.utils.data.DataLoader(dataset=testSet, batch_size=len(state), shuffle=False)
with torch.no_grad():
for images, labels1, labels2 in generatePredic:
neuralNet.eval()
output = (neuralNet(images)[1].numpy())[0][0]
# so far, output gives a winning probability from -1 to 1, 1 for white, -1 for black. We want to scale this to
# a value between 0 and 1.
output = (output/2) + 0.5
# now we have an evaluation from 0 to 1. Now we have to scale this to a probability
# for either black or white depending on who moves next.
turn = evalBoard.plies % 2
# if plies is not divisible by 2, then it is black to move.
if turn == 1:
output = 1-output
# now, let's return our evaluation
# print(output)
return output
testing = False
if testing:
neuralNet = ChessResNet.ResNetDoubleHead()
neuralNet.load_state_dict(torch.load('New Networks/(MCTS)(6X256|4|8)(V4)(DESKTOP)64fish.pt'))
neuralNet.double()
neuralNet.eval()
board = ChessEnvironment()
start = time.time()
print(moveValueEvaluations(ActionToArray.legalMovesForState(board.arrayBoard, board.board), board, neuralNet))
end = time.time()
print("time taken:", end-start)
start = time.time()
print(moveValueEvaluationsNew(ActionToArray.legalMovesForState(board.arrayBoard, board.board), board, neuralNet))
end = time.time()
print("time taken:", end-start)