-
Notifications
You must be signed in to change notification settings - Fork 8
/
parallel_mcts_test.py
51 lines (42 loc) · 1.33 KB
/
parallel_mcts_test.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
from multiprocessing import Pool
import chess.pgn
import numpy as np
from config import Config
from game.chess_env import ChessEnv
from game.stockfish import Stockfish
def softmax(x):
"""Compute softmax values for each sets of scores in x."""
e_x = np.exp(x - np.max(x))
return e_x / e_x.sum()
def evaluate_state(board):
# print(fen)
env = ChessEnv(board=board)
# env.step(move)
game_over, score = env.is_game_over()
if game_over:
return score
value = env.stockfish.stockfish_eval(env.board, timeout=100)
return value
def value_policy(board: chess.Board):
env = ChessEnv(board)
game_over, score = env.is_game_over()
if game_over:
return score, []
stockfish = Stockfish()
value = stockfish.stockfish_eval(env.board, timeout=100)
next_states = []
for move in env.board.legal_moves:
board_copy = env.board.copy()
board_copy.push(move)
next_states.append(board_copy)
p = Pool()
actions_value = p.map(evaluate_state, next_states)
p.close()
p.join()
policy = softmax(actions_value)
index_list = [Config.MOVETOINDEX[move.uci()] for move in env.board.legal_moves]
map = np.zeros((5120,))
for index, pi in zip(index_list, policy):
map[index] = pi
assert policy.sum() > 0.999
return value, map, env.board