python-chess icon indicating copy to clipboard operation
python-chess copied to clipboard

Efficient way to get new transposition key

Open Leon0402 opened this issue 3 years ago • 1 comments

Hi,

I currently use _transposition_key() for my chess engine rather than a zorbrist hash or anything like that because the representation is unique and calculated anyway by push / pop. But with further enhancements I more often need to get the hash of the some move, so I always have to do:

        board.push(move)
        cache_key = board._transposition_key()
        board.pop()

and push is somewhat the bottleneck here. With a zobrist hash this would be much more efficient obviously. I wonder though: Is there any quicker way to calculate the transposition key without doing the (full) push / pop. The board doesn't need to be in a usable state, I just need the cache key.

I also just use regular chess, no chess960. I know there is plenty of stuff I don't need in the push method like everything related to chess960, crazyhouse and other variants, some internals like the clock and that stuff. But I'm not looking too much into micro optimizations.

Maybe it's also possible to simplify the transposition key, but keeping it still unique?

Thanks for your help!

Leon0402 avatar Jun 02 '22 11:06 Leon0402

Could you use hash((*board._transposition_key(), move.uci())?

jacksonthall22 avatar Jul 18 '22 01:07 jacksonthall22

Could you use hash((*board._transposition_key(), move.uci())?

Depending on the use-case yes, but it might miss transpositions.

There's no built-in faster solution. It would be to possible to look at the internals of push() and write a new def partial_push(board, move), but internals are subject to change across python-chess versions.

niklasf avatar Jul 10 '23 18:07 niklasf