King safety

Discussion of chess software programming and technical issues.

Moderator: Ras

User avatar
algerbrex
Posts: 608
Joined: Sun May 30, 2021 5:03 am
Location: United States
Full name: Christian Dean

King safety

Post by algerbrex »

Since Blunder currently has material, PST, and mobility evaluation, it seems like I good idea to begin working on king safety. And although I've only gotten Elo loses so far, I've still made good progress since my first attempt a couple of months ago.

originally had pawn shields and semi-open adjacent files to the king evaluation terms, but I've removed them so I can focus on getting the basics to work first, before adding them back in.

The approach I'm currently using is a very simplistic one and seems pretty standard:

While I'm calculating mobility, I check to see if the piece (knight, bishop, rook, or queen) is attacking the enemy king's "zone" (here I defined zone as being all the squares the king can currently move to, plus three more facing the enemy side). If it is, I multiply how many squares the piece is attacking by a constant bonus depending on if it's a major (2 points), rook (3 points), or queen (5 points), and add the result to a running sum of points for each side. Once I've finished evaluating mobility, I use the total points gained by each side to index a non-linear king safety table and use the resulting score to penalize the other side. I also make sure to ignore the king's safety score if there are fewer than two attackers of the attacking side that has no queen.

From what I've read this approach is pretty standard and common, so it seems like the basics of my implementation seemed right. Most of the time I've been spending has just been on tuning the piece point value and the non-linear table. The current approach I used to generate the table was a parabola, whose values were scaled down and converted to centipawn units. I've also tried running just the safety table values through my tuner, but the results produced were weird and irregular, so I scraped them for now as well.

Any advice from others who have experience with implementing king safety would be appreciated. Otherwise, I'll continue tinkering with things myself, as it seems like I'm close to getting it all to work, based on some games I've seen the dev version of Blunder play.
Mike Sherwin
Posts: 965
Joined: Fri Aug 21, 2020 1:25 am
Location: Planet Earth, Sol system
Full name: Michael J Sherwin

Re: King safety

Post by Mike Sherwin »

Something that I have thought about but have not tried yet is creating ratios for each root move. There would be a ratio for white and a ratio for black. The ratios for each side would be the number of checkmates divided by the number of moves of that side. The idea then would be to multiply each ratio by some constant and then do (assuming wtm) score = score + whiteBonus - blackBonus. Thus not relying on rules of thumb and instead using internal to the search experience. It is just a thought.
jdart
Posts: 4409
Joined: Fri Mar 10, 2006 5:23 am
Location: http://www.arasanchess.org

Re: King safety

Post by jdart »

It is important to consider "stacked" attackers (for example Bishop behind Queen that attacks a King square).

Also I found that directly tuning a scaling table generated noisy results because some table entries were sparsely represented in the position tuning set. So Arasan uses a sigmoid function whose parameters are tuned directly. The function is used to populate a table that translates values derived from attacks + other factors into a king safety score.

You also want to make sure damaging the King pawn cover or moving the King off ranks 1 or 2 is penalized sufficiently.

The value of having a decent king safety vs. no king safety should be considerable in terms of ELO, especially against computer opponents that do have good king safety scoring.
User avatar
algerbrex
Posts: 608
Joined: Sun May 30, 2021 5:03 am
Location: United States
Full name: Christian Dean

Re: King safety

Post by algerbrex »

Mike Sherwin wrote: Fri Nov 12, 2021 3:11 am Something that I have thought about but have not tried yet is creating ratios for each root move. There would be a ratio for white and a ratio for black. The ratios for each side would be the number of checkmates divided by the number of moves of that side. The idea then would be to multiply each ratio by some constant and then do (assuming wtm) score = score + whiteBonus - blackBonus. Thus not relying on rules of thumb and instead using internal to the search experience. It is just a thought.
I remember come across your idea in another king safety approach and I actually implemented it as it seemed relatively simple and my search is fail soft.

I had to play with a formula I liked for scoring the root move, but it actually played decently well in some positions. It just didn’t generalize very well in Elo testing, but with some tuning it might be workable.
User avatar
Brunetti
Posts: 424
Joined: Tue Dec 08, 2009 1:37 pm
Location: Milan, Italy
Full name: Alex Brunetti

Re: King safety

Post by Brunetti »

algerbrex wrote: Fri Nov 12, 2021 2:48 am I check to see if the piece (knight, bishop, rook, or queen) is attacking the enemy king's "zone" (here I defined zone as being all the squares the king can currently move to, plus three more facing the enemy side). If it is, I multiply how many squares the piece is attacking by a constant bonus depending on if it's a major (2 points), rook (3 points), or queen (5 points), and add the result to a running sum of points for each side.
I would also take into account the defenders. Consider these two positions with the same attackers, they hardly should have the same safety score:


[fen]2B5/kpp2rqr/pbbp4/8/8/B7/RP3PPP/QRNn2K1 w - - 0 1[/fen]




[fen]8/kppp1r1r/pbb3q1/4B3/8/1P1n4/R1R2PPP/3N1QKB w - - 0 1[/fen]


Alex
User avatar
algerbrex
Posts: 608
Joined: Sun May 30, 2021 5:03 am
Location: United States
Full name: Christian Dean

Re: King safety

Post by algerbrex »

algerbrex wrote: Fri Nov 12, 2021 2:48 am
... if it's a major (2 points), rook (3 points), or queen (5 points), and ...
Edit: Small typo. major should be minor
User avatar
algerbrex
Posts: 608
Joined: Sun May 30, 2021 5:03 am
Location: United States
Full name: Christian Dean

Re: King safety

Post by algerbrex »

Brunetti wrote: Fri Nov 12, 2021 5:14 am
algerbrex wrote: Fri Nov 12, 2021 2:48 am I check to see if the piece (knight, bishop, rook, or queen) is attacking the enemy king's "zone" (here I defined zone as being all the squares the king can currently move to, plus three more facing the enemy side). If it is, I multiply how many squares the piece is attacking by a constant bonus depending on if it's a major (2 points), rook (3 points), or queen (5 points), and add the result to a running sum of points for each side.
I would also take into account the defenders. Consider these two positions with the same attackers, they hardly should have the same safety score:


[fen]2B5/kpp2rqr/pbbp4/8/8/B7/RP3PPP/QRNn2K1 w - - 0 1[/fen]




[fen]8/kppp1r1r/pbb3q1/4B3/8/1P1n4/R1R2PPP/3N1QKB w - - 0 1[/fen]


Alex
Good point. I wasn't considering attackers since I had mostly been going by the CPW, but I'll implement that too as well.
User avatar
algerbrex
Posts: 608
Joined: Sun May 30, 2021 5:03 am
Location: United States
Full name: Christian Dean

Re: King safety

Post by algerbrex »

jdart wrote: Fri Nov 12, 2021 3:48 am It is important to consider "stacked" attackers (for example Bishop behind Queen that attacks a King square).

Also I found that directly tuning a scaling table generated noisy results because some table entries were sparsely represented in the position tuning set. So Arasan uses a sigmoid function whose parameters are tuned directly. The function is used to populate a table that translates values derived from attacks + other factors into a king safety score.

You also want to make sure damaging the King pawn cover or moving the King off ranks 1 or 2 is penalized sufficiently.

The value of having a decent king safety vs. no king safety should be considerable in terms of ELO, especially against computer opponents that do have good king safety scoring.
Also another quick question. I'm considering just adding king safety eval to the middlegame scores for black in white (in the context of tapered eval). Since my understanding is that king safety doesn't matter nearly as much in the endgame, and it wouldn't really be useful considering it there.
Mike Sherwin
Posts: 965
Joined: Fri Aug 21, 2020 1:25 am
Location: Planet Earth, Sol system
Full name: Michael J Sherwin

Re: King safety

Post by Mike Sherwin »

algerbrex wrote: Fri Nov 12, 2021 4:57 am
Mike Sherwin wrote: Fri Nov 12, 2021 3:11 am Something that I have thought about but have not tried yet is creating ratios for each root move. There would be a ratio for white and a ratio for black. The ratios for each side would be the number of checkmates divided by the number of moves of that side. The idea then would be to multiply each ratio by some constant and then do (assuming wtm) score = score + whiteBonus - blackBonus. Thus not relying on rules of thumb and instead using internal to the search experience. It is just a thought.
I remember come across your idea in another king safety approach and I actually implemented it as it seemed relatively simple and my search is fail soft.

I had to play with a formula I liked for scoring the root move, but it actually played decently well in some positions. It just didn’t generalize very well in Elo testing, but with some tuning it might be workable.
Thanks for trying the idea. The idea is rather 'raw' and it does need 'cooked' a bit. I do believe that the way it was presented above is a little more advanced than what was presented before?
User avatar
algerbrex
Posts: 608
Joined: Sun May 30, 2021 5:03 am
Location: United States
Full name: Christian Dean

Re: King safety

Post by algerbrex »

Mike Sherwin wrote: Fri Nov 12, 2021 8:22 am
algerbrex wrote: Fri Nov 12, 2021 4:57 am
Mike Sherwin wrote: Fri Nov 12, 2021 3:11 am Something that I have thought about but have not tried yet is creating ratios for each root move. There would be a ratio for white and a ratio for black. The ratios for each side would be the number of checkmates divided by the number of moves of that side. The idea then would be to multiply each ratio by some constant and then do (assuming wtm) score = score + whiteBonus - blackBonus. Thus not relying on rules of thumb and instead using internal to the search experience. It is just a thought.
I remember come across your idea in another king safety approach and I actually implemented it as it seemed relatively simple and my search is fail soft.

I had to play with a formula I liked for scoring the root move, but it actually played decently well in some positions. It just didn’t generalize very well in Elo testing, but with some tuning it might be workable.
Thanks for trying the idea. The idea is rather 'raw' and it does need 'cooked' a bit. I do believe that the way it was presented above is a little more advanced than what was presented before?
Indeed I believe so, as your other post was from last year on a post by Loki's author. I may try this new idea as well and play around with it if I get to frustrated with traditional king safety techniques :)