niklasf / python-chess

A chess library for Python, with move generation and validation, PGN parsing and writing, Polyglot opening book reading, Gaviota tablebase probing, Syzygy tablebase probing, and UCI/XBoard engine communication
https://python-chess.readthedocs.io/en/latest/
GNU General Public License v3.0
2.46k stars 531 forks source link

Weight value for polyglot opening books goes up to 65520 instead of 65535 #748

Closed AttackingOrDefending closed 3 years ago

AttackingOrDefending commented 3 years ago

The weight value in polyglot opening books is uint16 (0 to 65535) but the maximum weight value I got when reading a polyglot opening book is 65520. Why doesn't the weight value go up to 65535?

niklasf commented 3 years ago

I see no reason why 65535 wouldn't work. Are you sure that the book really contains an entry of that magnitude? If so, can you please share the book?

AttackingOrDefending commented 3 years ago

I tested the books I made using Scid vs. PC. When I put 100 weight on e4 and opened the book using python-chess it had a weight of 65520. Then, I put 65535 weight on e4 but it still had a weight of 65520. I also tested Perfect 2021 (from https://sites.google.com/site/computerschess/perfect-2021-books) but the max weight a move had was still 65520. I used

with chess.polyglot.open_reader('engines/book.bin') as reader:
    board = chess.Board('rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1')
    min_weight = 0
    move = reader.find_all(board, minimum_weight=min_weight)
    print(list(move))

for my tests.

niklasf commented 3 years ago

Ok, so I downloaded Perfect2021.bin from that source and did a hexdump, grepping for the starting position:

❯ hexdump Perfect2021.bin | grep "3b46 1896 9116 9cfc"
0005ef0 3b46 1896 9116 9cfc 1c03 f0ff 0000 0000
0005f00 3b46 1896 9116 9cfc db02 9099 0000 0000
0005f10 3b46 1896 9116 9cfc 9a02 3033 0000 0000
0005f20 3b46 1896 9116 9cfc 9501 3033 0000 0000

It has a weight column of f0ff, which is 65520 in big endian. So that's really the value that's stored in the file. Why that is would be a question for the creator of the file or Scid vs. PC.