King-Piece-Square-Tables

Discussion of chess software programming and technical issues.

Moderator: Ras

User avatar
AAce3
Posts: 80
Joined: Fri Jul 29, 2022 1:30 am
Full name: Aaron Li

King-Piece-Square-Tables

Post by AAce3 »

Hey all,
Just an interesting thought that I had, for classical evaluation.
Many NNUEs use halfkp as an input layer, with every input being relative to a specific king square.
I'm curious - has anyone tried "King piece square tables?" i.e. piece square tables relative to king location? I'd imagine it would improve a program's knowledge of king safety. It would have to be tuned sort of like a neural network, but it might give a small rating boost if tuned correctly.
Thoughts?
User avatar
hgm
Posts: 28353
Joined: Fri Mar 10, 2006 10:06 am
Location: Amsterdam
Full name: H G Muller

Re: King-Piece-Square-Tables

Post by hgm »

This is basically what NNUE are, right? Except that you have some 256 such PST, and a small NN to combine their scores.

This is primarily a Shogi technique, though. In Shogi a King typically gets chased all over the board. And most pieces are slow-moving, which means you might as well not have them at all if they are far away from the Kings.

In Chess the Kings usually sit statically in their fortress. King Safety is not very different when having a King on h1 as it is when that same King would be on g1. A single move would convert one position into the other. Evaluation is supposed to score strategic traits of a position, i.e. aspects that are likely to persist for a long time. If a King would be very unsafe on g1, and completely save on h1, the compromised safety on g1 would not be worth very much to the opponent, because this 'advantage' will evaporate on the next move. So it isn't worth more than a tempo.

It would make sense to have different PST depending on which direction the player castled. E.g. for King at or next to b1 or g1, or elsewere.
User avatar
AAce3
Posts: 80
Joined: Fri Jul 29, 2022 1:30 am
Full name: Aaron Li

Re: King-Piece-Square-Tables

Post by AAce3 »

Yeah, that makes a lot of sense. Perhaps I will try different piece square tables for different combinations of kingside and queenside castles.

The halfKP style neural network always seemed a little weird to me. Maybe someone will discover a different/better approach.
Witek
Posts: 87
Joined: Thu Oct 07, 2021 12:48 am
Location: Warsaw, Poland
Full name: Michal Witanowski

Re: King-Piece-Square-Tables

Post by Witek »

I just implemented this in my engine and it gave 230 (+-40) Elo improvement over regular piece square table when evaluation is purely PSQT-based (if I enabled NNUE back, it was only 10 Elo gain).

You can see how PSQT looks like here: https://github.com/Witek902/Caissa/blob ... d/PSQT.cpp

It's 2*10*32*64=40960 weights in total. x2 for middlegame/endgame. I'm normalizing king position to A-D files.

You can clearly see that PSQT learned some basic king safety: notice pawn weights for king on a1 - it tries to preserve pawns around king.
The other interesting thing is lowered queen importance when white king is approaching to 8th rank (or black to 1st rank).
Author of Caissa Chess Engine: https://github.com/Witek902/Caissa
User avatar
lithander
Posts: 915
Joined: Sun Dec 27, 2020 2:40 am
Location: Bremen, Germany
Full name: Thomas Jahn

Re: King-Piece-Square-Tables

Post by lithander »

Interesting!
Witek wrote: Sat Oct 08, 2022 1:23 am I just implemented this in my engine and it gave 230 (+-40) Elo improvement over regular piece square table when evaluation is purely PSQT-based (if I enabled NNUE back, it was only 10 Elo gain).
Both versions are tapered e.g. in both you are interpolating between a set of midgame and endgame tables?
Witek wrote: Sat Oct 08, 2022 1:23 am I'm normalizing king position to A-D files.
what does that mean? what if your king is not on one of these 4 files?
Witek wrote: Sat Oct 08, 2022 1:23 am It's 2*10*32*64=40960 weights in total. x2 for middlegame/endgame. I'm normalizing king position to A-D files.
That's a lot of weights! How many positions do you use for tuning the PSTs?
Minimal Chess (simple, open source, C#) - Youtube & Github
Leorik (competitive, in active development, C#) - Github & Lichess
Witek
Posts: 87
Joined: Thu Oct 07, 2021 12:48 am
Location: Warsaw, Poland
Full name: Michal Witanowski

Re: King-Piece-Square-Tables

Post by Witek »

lithander wrote: Sat Oct 08, 2022 1:35 am Both versions are tapered e.g. in both you are interpolating between a set of midgame and endgame tables?
Yes.
lithander wrote: Sat Oct 08, 2022 1:35 am Witek wrote: ↑
Sat Oct 08, 2022 1:23 am
I'm normalizing king position to A-D files.

what does that mean? what if your king is not on one of these 4 files?
If the white king is on rank E-H, I flip the board horizontally so when reading PSQT values, the pieces are always relative to the king being on A-D. Same for black king (independently). I think the same thing is done in Stockfish's NNUE. I could have full 2x10x64x64 table, but it's 2x bigger and I noticed that values in "king on a1" vs. "king on h1" were very similar (just flipped horizontally).

lithander wrote: Sat Oct 08, 2022 1:35 am How many positions do you use for tuning the PSTs?
80 million. Training takes couple minutes. The problem is when king is on further ranks, because those positions are much rare. I think "white king on a8" positions are 100x less frequent than "white king on g1" in my data set, so the training takes much longer for the former...
Author of Caissa Chess Engine: https://github.com/Witek902/Caissa
User avatar
AAce3
Posts: 80
Joined: Fri Jul 29, 2022 1:30 am
Full name: Aaron Li

Re: King-Piece-Square-Tables

Post by AAce3 »

Witek wrote: Sat Oct 08, 2022 1:43 am
lithander wrote: Sat Oct 08, 2022 1:35 am Both versions are tapered e.g. in both you are interpolating between a set of midgame and endgame tables?
Yes.
lithander wrote: Sat Oct 08, 2022 1:35 am Witek wrote: ↑
Sat Oct 08, 2022 1:23 am
I'm normalizing king position to A-D files.

what does that mean? what if your king is not on one of these 4 files?
If the white king is on rank E-H, I flip the board horizontally so when reading PSQT values, the pieces are always relative to the king being on A-D. Same for black king (independently). I think the same thing is done in Stockfish's NNUE. I could have full 2x10x64x64 table, but it's 2x bigger and I noticed that values in "king on a1" vs. "king on h1" were very similar (just flipped horizontally).

lithander wrote: Sat Oct 08, 2022 1:35 am How many positions do you use for tuning the PSTs?
80 million. Training takes couple minutes. The problem is when king is on further ranks, because those positions are much rare. I think "white king on a8" positions are 100x less frequent than "white king on g1" in my data set, so the training takes much longer for the former...
Perhaps you could maybe generalize any square on the opponent's half of the board?
User avatar
j.t.
Posts: 263
Joined: Wed Jun 16, 2021 2:08 am
Location: Berlin
Full name: Jost Triller

Re: King-Piece-Square-Tables

Post by j.t. »

AAce3 wrote: Wed Aug 24, 2022 2:10 am I'm curious - has anyone tried "King piece square tables"?
Yes, in Nalwald. When I remove them (i.e. use normal piece square tables), Nalwald performs ~100 Elo worse. At the time of implementing it for the first time, it might have been worth less Elo. Maybe ~50. Wrote about it a bit more here.

Currently, I use 4 million positions for training.