-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbishopai.py
160 lines (140 loc) · 6.11 KB
/
bishopai.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
import chess
from chessUtils import ChessUtils
import numpy as np
from tree import Tree
from brain import Brain
from timer import Timer
class BishopAI:
CONCURRENT_ROLLOUT_BATCH_SIZE = 1
ROLLOUTS_PER_MOVE = 400
def __init__(self):
self.brain = Brain(load_saved=True)
self.board = chess.Board()
self.tree = Tree(root=True)
self.cache_hits = 0
self.cache_misses = 0
def catch_up(self, moves):
print(self.board)
for move in moves:
self.board.push_san(move)
if self.tree.children and move in self.tree.children:
self.tree = self.tree.children[move]
self.tree.historical = True
else:
self.tree = Tree(root=True)
print('forced to reset tree in memory')
print(self)
def add_moves(self, moves):
for move in moves:
self.board.push(move)
def remove_moves(self, moves):
for move in moves:
self.board.pop()
def get_move(self):
Timer.start('run_concurrent')
for i in range(BishopAI.ROLLOUTS_PER_MOVE):
if i % 20 == 0:
print(f"\rthinking: {i}", end='')
self.perform_concurrent_simulations(self.tree, i)
selected_move = self.tree.get_most_explored_child().move.uci()
print('\n\n' + f"{selected_move} is best move with victory odds of {round(self.tree.children[selected_move].branch_valuation * 100, 2)}%")
print(f"{self.tree.endings_found} end games explored")
print('\n' + self.tree.as_string(recurse=False, visitedOnly=True))
print(f"Cache hit rate {100*self.cache_hits/(self.cache_hits + self.cache_misses)}% ({self.cache_hits}/{self.cache_hits + self.cache_misses})")
Timer.stop('run_concurrent')
Timer.print()
# stored_fen = self.board.fen()
# self.__init__()
# self.board.set_fen(stored_fen)
# Timer.start('run_single')
# for i in range(BishopAI.ROLLOUTS_PER_MOVE):
# if i % 20 == 0:
# print(f"\rthinking: {i}", end='')
# self.perform_simulation(self.tree)
# selected_move = self.tree.get_most_explored_child().move.uci()
# print('\n' + self.tree.as_string(recurse=False, visitedOnly=True))
# Timer.stop('run_single')
# Timer.print()
return selected_move
def perform_concurrent_simulations(self, tree, sim_num):
best_leaf = tree.get_best_leaf()
if best_leaf.outcome:
best_leaf.elevate_data(1)
tree.endings_found += 1
return
move_chain = Tree.get_move_chain(tree, best_leaf)
self.add_moves(move_chain)
if self.board.is_game_over():
best_leaf.mark_as_end_state(self.board)
self.remove_moves(move_chain)
return
best_leaf_key = BrainCache.queue_for_prediction(self.board if self.board.turn else self.board.mirror())
if not BrainCache.is_cached(best_leaf_key):
self.remove_moves(move_chain)
Timer.start('searching_to_cache')
if BishopAI.CONCURRENT_ROLLOUT_BATCH_SIZE > 1:
self.cache_good_neighbors(best_leaf, BishopAI.CONCURRENT_ROLLOUT_BATCH_SIZE) # max(2, 2 ** int(5 * (( 600 - sim_num) / 600)))
Timer.stop('searching_to_cache')
Timer.start('predicting')
BrainCache.predict_on_queued(self.brain)
Timer.stop('predicting')
self.add_moves(move_chain)
self.cache_misses += 1
else:
self.cache_hits += 1
value, policy = BrainCache.get_cached(best_leaf_key)
best_leaf.spawn_children(self.board, value, policy)
self.remove_moves(move_chain)
def cache_good_neighbors(self, best_leaf, num_to_cache):
potential_neighbors = []
current_tree = self.tree
move_path = Tree.get_move_chain(self.tree, best_leaf)
for i, move in enumerate(move_path):
current_tree = current_tree.children[move.uci()]
for neighbor in current_tree.get_siblings():
if not neighbor.visited and not neighbor.cached:
neighbor.cache_value = abs(current_tree.puct - neighbor.puct)
potential_neighbors.append(neighbor)
potential_neighbors.sort(key = lambda neighbor : neighbor.cache_value, reverse=False)
for neighbor in potential_neighbors[0:num_to_cache-1]:
neighbor.cached = True
move_path = Tree.get_move_chain(self.tree, neighbor)
self.add_moves(move_path)
BrainCache.queue_for_prediction(self.board if self.board.turn else self.board.mirror())
self.remove_moves(move_path)
def perform_simulation(self, tree):
if not tree.historical:
self.board.push(tree.move)
if not tree.visited:
if self.board.is_game_over():
tree.mark_as_end_state(self.board)
else:
value, policy = self.brain.predict(self.board if self.board.turn else self.board.mirror())
tree.spawn_children(self.board, value[0][0], policy[0])
elif tree.outcome:
tree.elevate_data(1)
else:
self.perform_simulation(tree.get_best_child())
if tree.move is not None and not tree.historical:
self.board.pop()
def __str__(self):
return f"\n{self.board}\n"
class BrainCache:
cache = {}
queued = {}
def queue_for_prediction(board):
key = board.fen()
if key not in BrainCache.queued and key not in BrainCache.cache:
BrainCache.queued[key] = ChessUtils.board_to_arr(board)
return key
def predict_on_queued(brain):
if len(BrainCache.queued) == 0:
return
values, policies = brain.predict_raw(np.array(list(BrainCache.queued.values())))
for i, key in enumerate(BrainCache.queued):
BrainCache.cache[key] = (values[i][0], policies[i])
BrainCache.queued = {}
def get_cached(key):
return BrainCache.cache[key]
def is_cached(key):
return key in BrainCache.cache