File size: 3,080 Bytes
e5bdb21
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Board and move encoding shared by data preparation, training and the engine.



Everything is seen from the side to move: if Black is to move, ranks are mirrored and colours swapped, so the

network always plays "up the board".

Policy index: from * 64 + to for ordinary moves and queen promotions (4096 entries), then 72 entries for

under-promotions: 4096 + piece (N=0, B=1, R=2) * 24 + from_file * 3 + (to_file - from_file + 1).

"""
POLICY_SIZE = 4096 + 72
POLICY_TEMP_CP = 60.0

PIECES = {"P": 1, "N": 2, "B": 3, "R": 4, "Q": 5, "K": 6}
FILES = "abcdefgh"
UNDER = {"n": 0, "b": 1, "r": 2}


def encode_fen(fen):
    parts = fen.split(" ")
    placement, side, castling, ep = parts[0], parts[1], parts[2], parts[3]
    white = side == "w"
    board = [0] * 64
    rank = 7
    file = 0
    for ch in placement:
        if ch == "/":
            rank -= 1
            file = 0
        elif ch.isdigit():
            file += int(ch)
        else:
            own = ch.isupper() == white
            code = PIECES[ch.upper()] + (0 if own else 6)
            r = rank if white else 7 - rank
            board[r * 8 + file] = code
            file += 1
    if white:
        bits = ("K" in castling) | ("Q" in castling) << 1 | ("k" in castling) << 2 | ("q" in castling) << 3
    else:
        bits = ("k" in castling) | ("q" in castling) << 1 | ("K" in castling) << 2 | ("Q" in castling) << 3
    ep_file = 0 if ep == "-" else FILES.index(ep[0]) + 1
    return board, [bits, ep_file]


def square(name, white):
    f = FILES.index(name[0])
    r = int(name[1]) - 1
    if not white:
        r = 7 - r
    return r * 8 + f


def move_index(uci, white):
    fr = square(uci[0:2], white)
    to = square(uci[2:4], white)
    if len(uci) == 5 and uci[4] in UNDER:
        return 4096 + UNDER[uci[4]] * 24 + (fr % 8) * 3 + (to % 8 - fr % 8 + 1)
    return fr * 64 + to


def move_index_chess(move, white):
    """Same index for a python-chess Move (squares a1 = 0)."""
    fr, to = move.from_square, move.to_square
    if not white:
        fr ^= 56
        to ^= 56
    if move.promotion and move.promotion != 5:  # 5 = queen
        return 4096 + (move.promotion - 2) * 24 + (fr % 8) * 3 + (to % 8 - fr % 8 + 1)
    return fr * 64 + to


def encode_board(b):
    """Same result as encode_fen(b.fen()) for a python-chess Board, without building the FEN string."""
    white = b.turn
    board = [0] * 64
    for sq, piece in b.piece_map().items():
        code = piece.piece_type + (0 if piece.color == white else 6)
        r = sq >> 3 if white else 7 - (sq >> 3)
        board[r * 8 + (sq & 7)] = code
    own, opp = (True, False) if white else (False, True)
    bits = (int(b.has_kingside_castling_rights(own)) | int(b.has_queenside_castling_rights(own)) << 1
            | int(b.has_kingside_castling_rights(opp)) << 2 | int(b.has_queenside_castling_rights(opp)) << 3)
    ep_file = (b.ep_square & 7) + 1 if b.ep_square is not None and b.has_legal_en_passant() else 0
    return board, [bits, ep_file]