-
Notifications
You must be signed in to change notification settings - Fork 0
/
runSavedBot.py
74 lines (65 loc) · 2.27 KB
/
runSavedBot.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
import tensorflow as tf
import chess
import utils
def evaluate(board):
#print(vectorize(board.fen()).shape)
evaluation = model.predict_step(utils.vectorize(board.fen()).reshape((1,832)))
#print(evaluation)
return round(float(evaluation), 4)
def find_best_move(board, depth, maximizing_player):
if maximizing_player:
best_score = float('-inf')
best_move = None
for move in board.legal_moves:
board.push(move)
score = minimax(board, depth - 1, float('-inf'), float('inf'), False)
print(move, score)
if score > best_score:
best_score = score
best_move = move
board.pop()
else:
best_score = float('inf')
best_move = None
for move in board.legal_moves:
board.push(move)
score = minimax(board, depth - 1, float('-inf'), float('inf'), True)
print(move, score)
if score < best_score:
best_score = score
best_move = move
board.pop()
return best_move
def minimax(board, depth, alpha, beta, maximizing_player):
if board.is_checkmate():
return -10000 if maximizing_player else 10000
if board.is_game_over(claim_draw=True):
return 9000 if maximizing_player else -9000
if depth == 0:
return evaluate(board)
if maximizing_player:
best_score = float('-inf')
for move in board.legal_moves:
board.push(move)
score = minimax(board, depth-1, alpha, beta, False)
score += depth
board.pop()
best_score = max(score, best_score)
alpha = max(alpha, best_score)
if alpha >= beta:
break
return best_score
else:
best_score = float('inf')
for move in board.legal_moves:
board.push(move)
score = minimax(board, depth-1, alpha, beta, True)
score -= depth
board.pop()
best_score = min(score, best_score)
beta = min(beta, best_score)
if alpha >= beta:
break
return best_score
model = tf.keras.models.load_model('corrected_12mil_25epoch_64batch_0.0001learnRate')
print('loading model')