-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathChessEngine.py
executable file
·278 lines (254 loc) · 8.08 KB
/
ChessEngine.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
#! /bin/python3
import chess
import chess.engine
import chess.svg
import cv2
from cairosvg import svg2png
from multiprocessing import Pool
import os
import math
A = float(input("A (coeff. of material) = "))
B = float(input("B (coeff. of central control) = "))
C = float(input("C (coeff. of king safety) = "))
D = float(input("D (coeff. of mobility) = "))
n = int(input("Number of games = "))
depth = int(input("depth (no. of turns you want algorithm to think furthur) = "))
man = bool(input("MANUAL (leave it blank if you want automation) = "))
def dispBoard(board):
# Function to display the board
f = open("pic.svg", "w")
a = chess.svg.board(board, flipped=man)
f.write(a)
f.close()
svg2png(url="./pic.svg", write_to="./pic.png")
image = cv2.imread("./pic.png")
cv2.imshow("image window", image)
if cv2.waitKey(1) & 0xFF == ord("q"):
cv2.destroyAllWindows()
def material(board):
# Assign value to the pieces
value = {
"p": -1,
"b": -3,
"n": -3,
"r": -5,
"q": -9,
"P": 1,
"B": 3,
"N": 3,
"R": 5,
"Q": 9,
}
temp = board.fen()
ret = 0
for c in temp:
try:
ret += value[c]
except:
continue
return ret
def cent_cont(board):
# Evaluate the control of the center
value = {
chess.E5: 2,
chess.E4: 2,
chess.D5: 2,
chess.D4: 2,
chess.F6: 1,
chess.E6: 1,
chess.D6: 1,
chess.C6: 1,
chess.F3: 1,
chess.E3: 1,
chess.D3: 1,
chess.C3: 1,
chess.F5: 1,
chess.F4: 1,
chess.C5: 1,
chess.C4: 1,
}
ret = 0
for square in value.keys():
whiteAttackers = len(board.attackers(chess.WHITE, square)) # Number of white pieces controlling the centre.
blackAttackers = len(board.attackers(chess.BLACK, square))# Number of black pieces controlling the centre.
ret += (whiteAttackers - blackAttackers) * value[square]
return ret
def kingSafety(board):
surrounding = [[7,8,9],[-1,0,1],[-9, -8, -7]]
white_king_index = board.king(chess.WHITE)
black_king_index = board.king(chess.BLACK)
ret = 0
for i in range(0,3):
for j in range(0,3):
try:
blackAttackers = len(board.attackers(chess.BLACK, white_king_index + surrounding[i][j])) # Number of attackers near white king
whiteAttackers = len(board.attackers(chess.WHITE, black_king_index + surrounding[i][j]))# Number of attackers near black king
ret += (whiteAttackers - blackAttackers)
except IndexError:
continue
return ret
def mobiltity(board):
temp = [i for i in board.legal_moves]
return len(temp)
def ev_func(board):
if(board.is_checkmate()):
outcome = board.outcome()
if(outcome.winner == chess.WHITE):
return 1
else:
return -1
# The evaluation function J(x)
return math.tanh(A * material(board) + B * cent_cont(board) + C * kingSafety(board) + D * mobiltity(board))
def sum_from(arr, t): # This is our $\Delta t$
n = len(arr)
sum = 0
lamb = 0.7
for i in range(t, n):
sum += (lamb ** (i - t)) * arr[i]
return sum
def grad(board):
# Gradient of J(x)
return [material(board), cent_cont(board), kingSafety(board), mobiltity(board)]
def generateReward(board, move, count = 1):
# Assigns reward for each move based on depth
brd = board.copy()
if(count == depth):
brd.push_san(str(move))
if(brd.is_checkmate()):
return ev_func(brd)
temp = []
print("Black loop 1")
for BlackMove in brd.legal_moves:
brd.push_san(str(BlackMove))
temp.append(ev_func(brd))
brd.pop()
brd.pop()
#print(temp)
return min(temp)
else:
brd.push_san(str(move))
if(brd.is_checkmate()):
return ev_func(brd)
black_eval = []
for BlackMove in brd.legal_moves:
brd.push_san(str(BlackMove))
if(brd.is_checkmate()):
black_eval.append(ev_func(brd))
continue
white_eval = []
print("-------------------------------------")
for WhiteMove in brd.legal_moves:
print("WHITE LOOP 2")
white_eval.append(generateReward(brd, WhiteMove, count + 1))
if white_eval != []:
black_eval.append(max(white_eval))
brd.pop()
brd.pop()
#print(black_eval)
return min(black_eval)
def simple_terminal_engine():
# This function allows the user to play chess with the engine
board = chess.Board()
state_list = []
brd = board.copy()
state_list.append(brd)
del(brd)
print(state_list)
if(man == False):
engine = chess.engine.SimpleEngine.popen_uci(r"stockfish")
engine.configure({"Skill Level": 0})
while True:
A = [[board ,i] for i in board.legal_moves]
rewards = []
# Making the engine think multiple moves simultaneously using multiprocessing
with Pool(os.cpu_count()) as p:
rewards = p.starmap(generateReward, A)
max_eval = max(rewards)
bestMove = A[rewards.index(max_eval)][1]
print("Engine move : ", bestMove)
board.push_san(str(bestMove))
print(board)
dispBoard(board)
if board.is_checkmate():
state_list.append("W")
break
if board.is_insufficient_material():
state_list.append("D")
break
if man:
while True:
try:
PlayerMove = str(input("Enter move : "))
board.push_san(PlayerMove)
brd = board.copy()
state_list.append(brd)
del(brd)
print(state_list)
break
except ValueError:
print("Invalid move. Please try again.")
else:
stock_move = engine.play(board, chess.engine.Limit(time=0.1))
board.push(stock_move.move)
brd = board.copy()
state_list.append(brd)
del(brd)
print(board)
dispBoard(board)
if board.is_checkmate():
state_list.append("L")
break
if board.is_insufficient_material():
state_list.append("D")
break
if(man == False):
engine.quit()
return state_list
def engine_learn(a):
# Making the engine learn from its mistakes
state_list = simple_terminal_engine()
N = len(state_list) - 1 # Actual length of the game neglecting the final element
rN = 0
if state_list[N] == "W":
rN = 1
elif state_list[N] == "L":
rN = -1
d = [] # Temporal differences are stored here
for t in range(0, N - 1):
J_t = ev_func(state_list[t])
J_tp = ev_func(state_list[t + 1])
d.append(J_tp - J_t)
J = ev_func(state_list[N - 1])
d.append(rN - J)
update = [A, B, C, D] # This will contain the corrected coefficients
for t in range(N):
temp = grad(state_list[t]) # The gradient at a particular state
delta_t = sum_from(d, t) # This is our $\Delta t$
for i in range(len(temp)):
update[i] += temp[i] * delta_t * a
return update, state_list[N]
next_game = 1
while next_game == 1:
if n == 0:
print("Learnt")
break
l, outcome = engine_learn(n)
# making sure that coefficients are never negative
for i in range(len(l)):
if(l[i] < 0):
l[i] = 0
# update of coefficients
A = l[0]
B = l[1]
C = l[2]
D = l[3]
f = open("Parameters2.txt", "a") # Putting the updated coeff. in a file
text = str(l) + ' ' + outcome + "\n"
f.write(text)
f.close()
print(l)
#next_game = int(input("Continue? (0 = No / 1 = Yes)"))
n -= 1
print(
"--------------------------------------------------------------------------------------------------------"
)