-
Notifications
You must be signed in to change notification settings - Fork 13
/
engine.py
139 lines (111 loc) · 3.46 KB
/
engine.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
import search
import chess
import chess.pgn
import sys
import traceback
CACHE_SIZE = 200000
MINTIME = 0.1
TIMEDIV = 25.0
NODES = 800
C = 3.0
logfile = open("a0lite.log", "w")
LOG = True
def log(msg):
if LOG:
logfile.write(str(msg))
logfile.write("\n")
logfile.flush()
def send(str):
log(">{}".format(str))
sys.stdout.write(str)
sys.stdout.write("\n")
sys.stdout.flush()
def process_position(tokens):
board = chess.Board()
offset = 0
if tokens[1] == 'startpos':
offset = 2
elif tokens[1] == 'fen':
fen = " ".join(tokens[2:8])
board = chess.Board(fen=fen)
offset = 8
if offset >= len(tokens):
return board
if tokens[offset] == 'moves':
for i in range(offset+1, len(tokens)):
board.push_uci(tokens[i])
# deal with cutechess bug where a drawn positions is passed in
if board.can_claim_draw():
board.clear_stack()
return board
def load_network():
log("Loading network")
#net = search.EPDLRUNet(search.BadGyalNet(cuda=True), CACHE_SIZE)
net = search.EPDLRUNet(search.MeanGirlNet(cuda=False), CACHE_SIZE)
return net
def main():
send("A0 Lite")
board = chess.Board()
nn = None
while True:
line = sys.stdin.readline()
line = line.rstrip()
log("<{}".format(line))
tokens = line.split()
if len(tokens) == 0:
continue
if tokens[0] == "uci":
send('id name A0 Lite')
send('id author Dietrich Kappe')
send('uciok')
elif tokens[0] == "quit":
exit(0)
elif tokens[0] == "isready":
if nn == None:
nn = load_network()
send("readyok")
elif tokens[0] == "ucinewgame":
board = chess.Board()
elif tokens[0] == 'position':
board = process_position(tokens)
elif tokens[0] == 'go':
my_nodes = NODES
my_time = None
if (len(tokens) == 3) and (tokens[1] == 'nodes'):
my_nodes = int(tokens[2])
if (len(tokens) == 3) and (tokens[1] == 'movetime'):
my_time = int(tokens[2])/1000.0
if my_time < MINTIME:
my_time = MINTIME
if (len(tokens) == 9) and (tokens[1] == 'wtime'):
wtime = int(tokens[2])
btime = int(tokens[4])
winc = int(tokens[6])
binc = int(tokens[8])
if (wtime > 5*winc):
wtime += 5*winc
else:
wtime += winc
if (btime > 5*binc):
btime += 5*binc
else:
btime += binc
if board.turn:
my_time = wtime/(TIMEDIV*1000.0)
else:
my_time = btime/(TIMEDIV*1000.0)
if my_time < MINTIME:
my_time = MINTIME
if nn == None:
nn = load_network()
if my_time != None:
best, score = search.UCT_search(board, 1000000, net=nn, C=C, max_time=my_time, send=send)
else:
best, score = search.UCT_search(board, my_nodes, net=nn, C=C, send=send)
send("bestmove {}".format(best))
try:
main()
except:
exc_type, exc_value, exc_tb = sys.exc_info()
log(traceback.format_exception(exc_type, exc_value, exc_tb))
logfile.close()