-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmatsya_nnet.py
173 lines (141 loc) · 4.91 KB
/
matsya_nnet.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
import chess
import random
import math
import neural_net
import torch
class Matsya:
def __init__(self, board):
self.board = board
self.depth = 3
self.time_control = 1000
self.eval_model = neural_net.EvalNetwork()
self.eval_model.load()
def get_current_position(self):
return self.board.fen()
def make_move(self, move):
self.board.push(move)
def get_best_move(self):
best_move = chess.Move.null()
depth = 2
if self.board.turn:
best_move, _ = self.search(depth, self.board, True)
else:
best_move, _ = self.search(depth, self.board, False)
return best_move
def search(self, depth, board, maximize):
legals = board.legal_moves
bestMove = None
bestValue = -9999
if(not maximize):
bestValue = 9999
for move in legals:
board.push(move)
value = self.alphaBeta(board, depth-1, -10000, 10000, (not maximize))
board.pop()
if maximize:
if value > bestValue:
bestValue = value
bestMove = move
else:
if value < bestValue:
bestValue = value
bestMove = move
return (bestMove, bestValue)
def alphaBeta(self, board, depth, alpha, beta, maximize):
if(board.is_checkmate()):
if(board.turn == chess.WHITE):
return -100000
else:
return 100000
if depth == 0:
val = self.evaluate(board)
if val is not None:
return val
else:
return 0
legals = board.legal_moves
if(maximize):
bestVal = -9999
for move in legals:
board.push(move)
bestVal = max(bestVal, self.alphaBeta(board, depth-1, alpha, beta, (not maximize)))
board.pop()
alpha = max(alpha, bestVal)
if alpha >= beta:
return bestVal
return bestVal
else:
bestVal = 9999
for move in legals:
board.push(move)
bestVal = min(bestVal, self.alphaBeta(board, depth - 1, alpha, beta, (not maximize)))
board.pop()
beta = min(beta, bestVal)
if beta <= alpha:
return bestVal
return bestVal
'''
def search(self, board, depth, alpha, beta):
if depth == 0:
return chess.Move.null(), self.evaluate(board)
best_move = chess.Move.null()
best_score = -99999
for move in self.board.legal_moves:
board.push(move)
eval = self.search(board, depth-1, -alpha, -beta)
board.pop()
if eval[1] is not None and eval[1] > best_score:
best_move = move
best_score = eval[1]
if best_score > alpha:
alpha = best_score
if best_score >= beta:
break
return best_move, best_score
'''
def evaluate(self, board):
# first check for mate or draw
if board.is_checkmate():
if board.turn:
return -math.inf
else:
return math.inf
if board.is_stalemate() or board.is_insufficient_material():
return 0
value = self.eval_model.inference(board.fen())
return value
def uci_loop(self):
while True:
command = input().split()
if command[0] == "uci":
self.uci_protocol()
elif command[0] == "isready":
print("readyok")
elif command[0] == "ucinewgame":
self.board = chess.Board()
elif command[0] == "position":
self.handle_position_command(command)
elif command[0] == "go":
self.handle_go_command()
elif command[0] == "quit":
exit()
def uci_protocol(self):
print("id name Matsya_v1_NNet")
print("id author Paritosh Dahiya")
print("uciok")
def handle_position_command(self, command):
if command[1] == "startpos":
self.board.set_fen(chess.STARTING_FEN)
command = command[1:]
moves = command[2:]
for move in moves:
self.board.push_uci(move)
def handle_go_command(self):
best_move = self.get_best_move()
# if best_move is None or best_move == chess.Move.null():
# best_move = random.choice(list(self.board.legal_moves))
self.board.push(best_move)
print("bestmove " + str(best_move))
if __name__ == "__main__":
engine = Matsya(chess.Board())
engine.uci_loop()