Download encoding.py from Convergent-ch/Arkonium-0.9: direct link, hf CLI and curl.
- Browser
- Download file 2.23 kB
-
https://huggingface.co/Convergent-ch/Arkonium-0.9/resolve/main/encoding.py
- Command line
-
hf download hf://Convergent-ch/Arkonium-0.9/encoding.py
-
curl -L -o encoding.py https://huggingface.co/Convergent-ch/Arkonium-0.9/resolve/main/encoding.py
2.23 kB
| """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 | |