-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathengine.py
180 lines (143 loc) · 5.11 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
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
import sys
import chess
import keras
from keras.callbacks import ModelCheckpoint
import os
from ocean_eater_network import create_model
from decision_tree import make_probabilistic_decision
# There's a lot of code that is repeated and could be better
# implemented as functions, but I don't have a firm grasp of
# scope so I left it like this.
# log file:
f = open('uci_log.txt', 'a')
f.write('\n\n\nStarting new log!\n\n')
f.flush()
# For logging purposes
def print_log(str):
f.write(str + '\n')
f.flush()
print(str)
def log(str):
f.write('COMMAND: ' + str + '\n')
f.flush()
ENGINE_NAME = 'Ocean Eater 0.1'
AUTHOR_NAME = 'Ryan Peterson and Joshua Marsh'
# TODO: currently this doesn't actually change search cap
DEFAULT_STATE_CAP = 1000
MIN_STATE_CAP = 50
MAX_STATE_CAP = 10000000
MODEL_CHECKPOINTER = ModelCheckpoint("GameHistories/model.h5", monitor='loss')
if os.path.exists(MODEL_CHECKPOINTER.filepath):
print("Using checkpoint model.")
model = keras.models.load_model(MODEL_CHECKPOINTER.filepath)
else:
model = create_model()
# initialize to empty string
# wait for uci command to be provided
while input().strip() != 'uci':
pass # do nothing
# Now, identify
print_log('id name ' + ENGINE_NAME)
print_log('id author ' + AUTHOR_NAME)
# Specify options
# State Cap is number of states
print_log('option name state_cap type spin default ' + str(DEFAULT_STATE_CAP) +
' min ' + str(MIN_STATE_CAP) +
' max ' + str(MAX_STATE_CAP))
# Done specifying options
print_log('uciok')
# get next command
command = input().strip()
log(command)
state_cap = DEFAULT_STATE_CAP
ready = False
while not ready:
if command.startswith('quit'):
sys.exit()
elif command.startswith('setoption'):
# check what the 3rd word is
if command.split()[2].strip() == 'state_cap':
# 4th word is the value
state_cap = int(command.split()[3].strip())
elif command.startswith('isready'):
ready = True
print_log('readyok')
command = input().strip()
log(command)
have_board = False
board = chess.Board()
while not have_board:
if command.startswith('quit'):
sys.exit()
elif command.startswith('setoption'):
# check what the 3rd word is
if command.split()[2].strip() == 'state_cap':
# 4th word is the value
state_cap = int(command.split()[4].strip())
elif command.startswith('ucinewgame'):
pass
elif command.startswith('position'):
# if second word is startpos
if command.split()[1].strip() == 'startpos':
have_board = True
board.reset()
# if it has a list of moves
if 'moves' in command:
moves = command.split(' moves')[1]
# apply moves to board
for move in moves.split():
board.push(chess.Move.from_uci(move))
# set from fen, command will be 'position fen <FEN> moves <MOVES>
elif command.split()[1].strip() == 'fen':
# get everything after 'fen' and before 'moves'
fen = command[13:].split(' moves')[0]
have_board = True
board.set_fen(fen) # set starting position
# if it has a list of moves
if 'moves' in command:
moves = command.split(' moves')[1]
# apply moves to board
for move in moves.split():
board.push(chess.Move.from_uci(move))
command = input().strip()
log(command)
while True:
if command.startswith('quit'):
sys.exit()
elif command.startswith('setoption'):
# check what the 3rd word is
if command.split()[2].strip() == 'state_cap':
# 4th word is the value
state_cap = int(command.split()[4].strip())
elif command.startswith('ucinewgame'):
pass
elif command.startswith('isready'):
print_log('readyok')
elif command.startswith('position'):
# if second word is startpos
if command.split()[1].strip() == 'startpos':
have_board = True
board.reset()
# if it has a list of moves
if 'moves' in command:
moves = command.split(' moves')[1]
# apply moves to board
for move in moves.split():
board.push(chess.Move.from_uci(move))
# set from fen, command will be 'position fen <FEN> moves <MOVES>
elif command.split()[1].strip() == 'fen':
# get everything after 'fen' and before 'moves'
fen = command[13:].split(' moves')[0]
have_board = True
board.set_fen(fen) # set starting position
# if it has a list of moves
if 'moves' in command:
moves = command.split(' moves')[1]
# apply moves to board
for move in moves.split():
board.push(chess.Move.from_uci(move))
elif command.startswith('go'):
move = chess.Move.uci(make_probabilistic_decision(model, board))
print_log('bestmove ' + move)
command = input().strip()
log(command)