/
ChessStateTree.py
262 lines (235 loc) · 8.6 KB
/
ChessStateTree.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
#See also https://colab.research.google.com/drive/1c7717_Mq0fPQNYHNbAvPiEI55pDCyCtR
from ChessState import ChessState
import chess
from math import inf
import pickle
from numpy import argmax
#from anytree import Node, RenderTree
class ChessStateTree:
def __init__(self, initState, movesToMate):
self.headState = ChessState(initState)
self.depth = movesToMate + 1
self.lowestRow = []
self.lowestRow.append(self.headState)
self.nodesExplored = 0
def createTree(self):
for i in range(1, self.depth):
print(i)
temp = []
# Generates the moves for White
for state in self.lowestRow:
state.generateNextMoves()
for child in state.children:
temp.append(child)
self.lowestRow = temp
temp = []
# Generates the moves for Black, if it's not at the end of the tree
if i != self.depth-1:
print("black " + str(i))
for state in self.lowestRow:
state.generateNextMoves()
for child in state.children:
temp.append(child)
self.lowestRow = temp
def scoreTree(self, scorer):
for state in self.lowestRow:
state.score(scorer)
def printLeaves(self):
for state in self.lowestRow:
print(state.value)
def getSuccessors(self, node):
assert node is not None
return node.children
def isTerminal(self, node):
assert node is not None
return len(node.children) == 0
def getUtility(self, node):
assert node is not None
return node.value
def alpha_beta_search(self):
node = self.headState
best_val = -inf
beta = inf
successors = self.getSuccessors(node)
best_state = None
self.nodesExplored += 1
for state in successors:
value = self.min_value(state, best_val, beta)
if value > best_val:
best_val = value
best_state = state
print("AlphaBeta: Utility Value of Root Node: = " + str(best_val))
print("AlphaBeta: Best State is: " + str(best_state.state.move_stack))
return best_state, best_val
def max_value(self, node, alpha, beta):
#print("AlphaBeta-->MAX: Visited Node :: " + str(node.state.peek()))
self.nodesExplored += 1
if self.isTerminal(node):
return self.getUtility(node)
value = -inf
successors = self.getSuccessors(node)
for state in successors:
value = max(value, self.min_value(state, alpha, beta))
if value >= beta:
return value
alpha = max(alpha, value)
return value
def min_value(self, node, alpha, beta):
#print("AlphaBeta-->MIN: Visited Node :: " + str(node.state.peek()))
self.nodesExplored += 1
if self.isTerminal(node):
return self.getUtility(node)
value = inf
successors = self.getSuccessors(node)
for state in successors:
value = min(value, self.max_value(state, alpha, beta))
if value <= alpha:
return value
beta = min(beta, value)
return value
def easyScorer(board):
if board.result() == '1-0':
return inf
if board.result() == '0-1' or board.result() == '1/2-1/2':
return -inf
if board.result() == '*':
return 0
def pieceScorer(board):
if board.result() == '1-0':
return inf
if board.result() == '0-1' or board.result() == '1/2-1/2':
return -inf
if board.result() == '*':
fen = board.fen()
fen = fen.split()
fen = fen[0]
blackScore = 0
whiteScore = 0
for char in fen:
if char == 'p':
blackScore += 1
elif char == 'n':
blackScore += 3
elif char == 'b':
blackScore += 3
elif char == 'r':
blackScore += 5
elif char == 'q':
blackScore += 9
elif char == 'P':
whiteScore += 1
elif char == 'N':
whiteScore += 3
elif char == 'B':
whiteScore += 3
elif char == 'R':
whiteScore += 5
elif char == 'Q':
whiteScore += 9
return whiteScore - blackScore
def readfile(filename, count=10000):
i = 1
boardNames = []
solutions = []
with open(filename) as f:
for line in f:
if i % 5 == 2:
boardNames.append(line)
elif i % 5 == 3:
solutions.append(line)
i += 1
if i >= count*5:
break
return boardNames, solutions
def getAllTrees(filename='2movestomate.txt'):
#boardNames, solutions = readfile(filename)
scrubbedFilename = filename.split(".")
scrubbedFilename = scrubbedFilename[0]
i = 1
matchesEasy = 0
successesEasy = 0
nodesEasy = 0
matchesPiece = 0
successesPiece = 0
nodesPiece = 0
totalBoards = 0
skip = False
with open(filename) as f:
for line in f:
print(line + " " + str(i))
if i % 5 == 2:
skip = False
boardName = line
board = chess.Board(boardName)
boardSplit = boardName.split(" ")
if boardSplit[1] == 'b':
skip = True
elif i % 5 == 3 and not skip:
totalBoards += 1
solutions = line.split(" ")
solution = solutions[1]
solution = board.parse_san(solution)
cst = ChessStateTree(board, 2)
cst.createTree()
cst.scoreTree(easyScorer)
best_state, best_val = cst.alpha_beta_search()
if solution == best_state.state.move_stack[0]:
matchesEasy += 1
if best_val == inf:
successesEasy += 1
matchPercent = matchesEasy/totalBoards
print("Percent Match: " + str(matchPercent))
percentSuccess = successesEasy/totalBoards
print("Percent Success: " + str(percentSuccess))
nodesEasy += cst.nodesExplored
averageNodes = nodesEasy/totalBoards
print("Average Nodes: " + str(averageNodes))
with open('easyScorer' + scrubbedFilename + '.pkl', 'wb') as f:
print("dumpin time")
pickle.dump([matchPercent, percentSuccess, averageNodes, totalBoards], f)
cst.scoreTree(pieceScorer)
best_state, best_val = cst.alpha_beta_search()
if solution == best_state.state.move_stack[0]:
matchesPiece += 1
if best_val == inf:
successesPiece += 1
matchPercent = matchesPiece / totalBoards
print("Percent Match: " + str(matchPercent))
percentSuccess = successesPiece / totalBoards
print("Percent Success: " + str(percentSuccess))
nodesPiece += cst.nodesExplored
averageNodes = nodesPiece / totalBoards
print("Average Nodes: " + str(averageNodes))
with open('pieceScorer' + scrubbedFilename + '.pkl', 'wb') as f:
print("dumpin time")
pickle.dump([matchPercent, percentSuccess, averageNodes, totalBoards], f)
i += 1
# for board in boardName:
# cst = ChessStateTree(board, 2)
# cst.createTree()
# cst.scoreTree(easyScorer)
# cst.alpha_beta_search()
# i += 1
if __name__ == '__main__':
PKLFILE = False
# boardName = "r2qkb1r/pp2nppp/3p4/2pNN1B1/2BnP3/3P4/PPP2PPP/R2bK2R w KQkq - 1 0"
boardName = "6qk/8/5P1p/8/8/6QP/5PP1/4R1K1 w KQkq - 1 0"
#boardName = "7p/8/8/8/8/8/8/P7"
board = chess.Board(boardName)
if not PKLFILE:
cst = ChessStateTree(board, 2)
cst.createTree()
cst.scoreTree(pieceScorer)
# with open('cst2movesToMate.pkl', 'wb') as f:
# print("dumpin time")
# pickle.dump([cst, boardName], f)
# cst.scoreTree(easyScorer)
# with open('cst2movesToMate.pkl', 'wb') as f:
# print("dumpin time")
# pickle.dump([cst, boardName], f)
else:
pickle_in = open('cst2movesToMate.pkl', "rb")
asdf = pickle.load(pickle_in)
cst = asdf[0]
asdf = None
cst.alpha_beta_search()