Approaches to king safety?

Discussion of chess software programming and technical issues.

Moderators: bob, hgm, Harvey Williamson

Forum rules
This textbox is used to restore diagrams posted with the [d] tag before the upgrade.
mike_bike_kite
Posts: 98
Joined: Mon Jul 25, 2011 10:18 pm
Location: London
Contact:

Re: Approaches to king safety?

Post by mike_bike_kite » Fri Jan 20, 2012 2:45 pm

I'll try out all these ideas guys - at least the ones I'm capable of implementing. I'm currently building up my "test harness" so it plays any changes on both sides before reporting a result (I used to do this manually). This might catch a few of the problems I've been having. At the moment I can't rely on catching king attacks purely in the normal search because I'm not searching deep enough - null moves, transposition tables and extending on check will probably help a little here.

I'll continue with the king safety, try the extend on check, add null move, then transposition tables. Then I keep my fingers crossed and see how she does against folks. I'm getting there slowly.

It's certainly been very interesting to read your input.

jdart
Posts: 3842
Joined: Fri Mar 10, 2006 4:23 am
Location: http://www.arasanchess.org

Re: Approaches to king safety?

Post by jdart » Fri Jan 20, 2012 4:03 pm

I disagree with this. If you do not penalize when the King region is attacked by enemy pieces, you will fall victim to attacks that gradually pressure the King until something "gives". Human players are pretty good at this. Also you will not evaluate sacrifices such as Bxh6 correctly, because generally breaking the pawn shelter is not enough compensation unless there is a followup attack.

User avatar
Don
Posts: 5106
Joined: Tue Apr 29, 2008 2:27 pm

Re: Approaches to king safety?

Post by Don » Fri Jan 20, 2012 4:17 pm

mike_bike_kite wrote:My little program ( Fun Chess ) plays OK at the moment but looses against many 1900+ players when they sacrifice material and go for it's king. The program happily believes it's doing fine until it finally realises that there's just no escape from mate.

I've tried evaluating the king safety bu just looking whether it's castled, the nearby pawn structure and how squares around the king are attacked or defended but this doesn't seem to be enough as the human player sacrificed a rook and a knight to eventually get mate.
Welcome to the black art of king safety evaluation!

I believe I have a few options:
  • Raise the scoring on the existing safety routines. The problem here is that this will make the other evaluations seem insignificant ie why bother getting a protected passed pawn if it can add another minor piece to it's impregnable castle fortress.

    I then looked at gathering safety info while doing the search ie if the opponents queen is moved near our king then score that badly. If the queen is taken then score that well. Checks bad. Mates worse etc etc. I tried putting in what I thought was moderate evaluation scores but found these scores were upsetting the whole scoring system.

    Currently thinking about just counting the number of checks or mates that are found in any given search. If it exceeds a certain number then use the king safety evaluation function.
Essentially I'm trying anything that comes into my head. Any suggestions?

PS I'm hoping that when I have a better king safety evaluation then I might break the 2000 ELO barrier. Other options I've thought about adding include null moves, fractional extensions, hash positions and lazy evaluations. Any idea which of these might give most bang for the buck (or most ELO for hours coding)?
There are many things you can do, but one important principle which was being done even 30 years ago is that the king safety evaluation should be progressive, not linear. Allocate so many points to each "thing" that you measure and then score based on a non-linear function of the number of points you collect. An example of this might be to use the square of the number of points as the amount of weight to assign to king safety i.e. 0, 1, 4, 9, 16, 25, 36 etc.

So if you notice 2 things, you might get 4 points but if there are 3 things your score jumps to 9 points. The above "square table" is just an example, we are actually more severe than that with Komodo but it's usually good to start conservatively and experiment.

What might you get points for? It's typical to give points for an attack right next to the king and Komodo gives different numbers of points for different pieces. A bishop attacking g7 next to the king probably doesn't deserve the same number of points as a queen attacking g7.

Your idea of giving points for having a check possible is a good one too. In Komodo each different piece gets a different number of points for the possibility to attack. We also have a point table for which square of the board the king is on so that a king in the center of the board gets a few points but near the corners no points. Add all the points together and use this to index into the progressive table and of course scale it appropriately for you evaluation function.

If you want to get quick results, I would use a table similar to the above and just count the attacks around the king, that is up to 8 squares. If you try to throw in everything but the kitchen sink you will fail, build it up slowly and prove to yourself with many test games that you made an improvement and that each step is bug free. If that doesn't work you are probably doing something wrong. You can start by giving each piece 2 points and experiment with giving the queen and knight slightly more, perhaps 3 or 4. Don't get fancy, make it work first then experiment. If you get that working add points for the king not being on ideal squares. We have a dangerPoint table which ranges from zero (for a good square) all the way up to 8 for not being close to the king. It should get pretty bad pretty quickly, a king on f3 is not much better than a king on e4 and once the king is beyond the 3rd rank we don't make much of a distinction between squares.

Our table is such that our king safety can get ridiculously high. I made Komodo show me positions with very high king safety scores and found positions where it was SEVERAL pawns up or down - I thought that might be a bug but when viewing many of these positions I could not disagree a single time, the king was basically busted and Komodo knew it. Now imagine a program with no king safety not being able to distinguish that and you see how important it is.

By the way, your king safety scores should be almost zero in the endings and very high when all the pieces are on the board. Komodo uses the current fad of having 2 evaluation functions and interpolating them. In king a pawn endings king safety is turned off and if there are couple of minor pieces it still has almost no effect.

Komodo's king safety is adequate, but not superb. We have tended to focus on positional play but you have no chance without adequate king safety.

jdart
Posts: 3842
Joined: Fri Mar 10, 2006 4:23 am
Location: http://www.arasanchess.org

Re: Approaches to king safety?

Post by jdart » Fri Jan 20, 2012 4:56 pm

All this is good advice. You are welcome to look at how I do it (arasanchess.org). I use a nonlinear eval based on king attacks with a bonus if the pawn structure is also compromised.

Crafty has an eval that combines attacks near the king with distance of pieces from the king (tropism). Scorpio (http://sites.google.com/site/dshawul/home) does something similar and like Komodo it can generate pretty high scores.

mike_bike_kite
Posts: 98
Joined: Mon Jul 25, 2011 10:18 pm
Location: London
Contact:

Re: Approaches to king safety?

Post by mike_bike_kite » Fri Jan 20, 2012 6:07 pm

micron wrote:
... most bang for the buck (or most ELO for hours coding)?
On that criterion it's hard to beat the simplest check extension

Code: Select all

if ( InCheck() ) depth++;
which is worth 35 Elo in my engine.
This seemed the easiest possible change to make but I didn't get a good result with my engine. I tried it in my new testing harness where it just plays 10 games for each side and reports back. It lost nearly all the games with the new code! I'm guessing it's spending too much time in extended searches but obviously the new testing code could also have bugs. Anyway, just thought I'd mention my results :o

User avatar
Kempelen
Posts: 620
Joined: Fri Feb 08, 2008 9:44 am
Location: Madrid - Spain
Contact:

Re: Approaches to king safety?

Post by Kempelen » Fri Jan 20, 2012 6:07 pm

Don wrote:
mike_bike_kite wrote:My little program ( Fun Chess ) plays OK at the moment but looses against many 1900+ players when they sacrifice material and go for it's king. The program happily believes it's doing fine until it finally realises that there's just no escape from mate.

I've tried evaluating the king safety bu just looking whether it's castled, the nearby pawn structure and how squares around the king are attacked or defended but this doesn't seem to be enough as the human player sacrificed a rook and a knight to eventually get mate.
Welcome to the black art of king safety evaluation!

I believe I have a few options:
  • Raise the scoring on the existing safety routines. The problem here is that this will make the other evaluations seem insignificant ie why bother getting a protected passed pawn if it can add another minor piece to it's impregnable castle fortress.

    I then looked at gathering safety info while doing the search ie if the opponents queen is moved near our king then score that badly. If the queen is taken then score that well. Checks bad. Mates worse etc etc. I tried putting in what I thought was moderate evaluation scores but found these scores were upsetting the whole scoring system.

    Currently thinking about just counting the number of checks or mates that are found in any given search. If it exceeds a certain number then use the king safety evaluation function.
Essentially I'm trying anything that comes into my head. Any suggestions?

PS I'm hoping that when I have a better king safety evaluation then I might break the 2000 ELO barrier. Other options I've thought about adding include null moves, fractional extensions, hash positions and lazy evaluations. Any idea which of these might give most bang for the buck (or most ELO for hours coding)?
There are many things you can do, but one important principle which was being done even 30 years ago is that the king safety evaluation should be progressive, not linear. Allocate so many points to each "thing" that you measure and then score based on a non-linear function of the number of points you collect. An example of this might be to use the square of the number of points as the amount of weight to assign to king safety i.e. 0, 1, 4, 9, 16, 25, 36 etc.

So if you notice 2 things, you might get 4 points but if there are 3 things your score jumps to 9 points. The above "square table" is just an example, we are actually more severe than that with Komodo but it's usually good to start conservatively and experiment.

What might you get points for? It's typical to give points for an attack right next to the king and Komodo gives different numbers of points for different pieces. A bishop attacking g7 next to the king probably doesn't deserve the same number of points as a queen attacking g7.

Your idea of giving points for having a check possible is a good one too. In Komodo each different piece gets a different number of points for the possibility to attack. We also have a point table for which square of the board the king is on so that a king in the center of the board gets a few points but near the corners no points. Add all the points together and use this to index into the progressive table and of course scale it appropriately for you evaluation function.

If you want to get quick results, I would use a table similar to the above and just count the attacks around the king, that is up to 8 squares. If you try to throw in everything but the kitchen sink you will fail, build it up slowly and prove to yourself with many test games that you made an improvement and that each step is bug free. If that doesn't work you are probably doing something wrong. You can start by giving each piece 2 points and experiment with giving the queen and knight slightly more, perhaps 3 or 4. Don't get fancy, make it work first then experiment. If you get that working add points for the king not being on ideal squares. We have a dangerPoint table which ranges from zero (for a good square) all the way up to 8 for not being close to the king. It should get pretty bad pretty quickly, a king on f3 is not much better than a king on e4 and once the king is beyond the 3rd rank we don't make much of a distinction between squares.

Our table is such that our king safety can get ridiculously high. I made Komodo show me positions with very high king safety scores and found positions where it was SEVERAL pawns up or down - I thought that might be a bug but when viewing many of these positions I could not disagree a single time, the king was basically busted and Komodo knew it. Now imagine a program with no king safety not being able to distinguish that and you see how important it is.

By the way, your king safety scores should be almost zero in the endings and very high when all the pieces are on the board. Komodo uses the current fad of having 2 evaluation functions and interpolating them. In king a pawn endings king safety is turned off and if there are couple of minor pieces it still has almost no effect.

Komodo's king safety is adequate, but not superb. We have tended to focus on positional play but you have no chance without adequate king safety.
Hi Don

How do you usually test king safety? I have seen opinions that this must not be tested under fast time setups, althougt I don't see exactly why not.
Fermin Serrano
Author of 'Rodin' engine
http://sites.google.com/site/clonfsp/

User avatar
Don
Posts: 5106
Joined: Tue Apr 29, 2008 2:27 pm

Re: Approaches to king safety?

Post by Don » Fri Jan 20, 2012 7:10 pm

Kempelen wrote: Hi Don

How do you usually test king safety? I have seen opinions that this must not be tested under fast time setups, althougt I don't see exactly why not.
King safety is one of those evaluations improvements that scale funny. Many evaluation terms are more or less ok to test at any depth or time control and you will get reasonable consistency but king safety may even hurt your program if you (for example) run 3 ply searches. My theory on this is that by nature it's really difficult to do anything more than give a very crude estimate of king safety which needs to be tempered a bit with some depth. But that is just a theory, I don't pretend to fully understand it.

If you are in the early and crude stages of development with king safety I would test at any level that is at least 6 or 7 ply to be safe. It will probably help even at lower depths but I know that when we first started getting serious about king safety with Doch it weakened the program at very low depths but kicked in with a vengeance at higher depths. You may not have the same experience with this that we did.

We don't rely much on fixed depth testing, although we used to much more. I cannot stress this enough, but running 10 games matches is worthless, you have to run thousands of games. 2 or 3 hundred games will reveal that you have made an improvement if the improvement is MAJOR, but if it's less then 60 ELO there will still be a great deal of uncertainty.

King safety is a MAJOR scalability boost. Before we went to work on King Safety I was chasing ELO points trying to first catch Spike and then hopefully later Glaurung and thought I had clearly passed them at one point. Fast testing showing a clear superiority. However Doch fell flat once the level increased and it was not even a close call at higher levels where Doch fell far behind. We wasted several months trying to figure out what the "scalability bug" was, trying everything we possibly could to no avail and this was a very discouraging time for us. I think I rewrote the search several times :-)

Then for other reasons unrelated to scalability we knew that we had to put a lot of work into king safety and so for the time being put the scalability issue on hold. We spent some serious time on king safety and got one of our biggest gains ever from a single set of related changes but what is interesting is that when I went back to the scalability issue I discovered that the problem no longer existed! Inadvertently we had SOLVED the scalability problem! At this point not only had we (finally) passed the exceptionally fine Spike 1.2 program at longer time controls but had increased our lead at short time controls substantially and the lead only increased with depth.

User avatar
lucasart
Posts: 3046
Joined: Mon May 31, 2010 11:29 am
Full name: lucasart
Contact:

Re: Approaches to king safety?

Post by lucasart » Fri Jan 20, 2012 10:51 pm

Evert wrote: I would consider Ed Schröder's writeup to be essential background reading.
Thank you Evert. It's a very good page indeed.

User avatar
lucasart
Posts: 3046
Joined: Mon May 31, 2010 11:29 am
Full name: lucasart
Contact:

Re: Approaches to king safety?

Post by lucasart » Fri Jan 20, 2012 10:58 pm

hgm wrote:Well, Pawn shelter is King Safety, even when you prefer to call it differently.
I'm reading Ed Scroder's page on King Safety. Very interesting indeed. I may do some experiment with King safety...

User avatar
lucasart
Posts: 3046
Joined: Mon May 31, 2010 11:29 am
Full name: lucasart
Contact:

Re: Approaches to king safety?

Post by lucasart » Sun Jan 22, 2012 5:15 am

Don wrote: There are many things you can do, but one important principle which was being done even 30 years ago is that the king safety evaluation should be progressive, not linear. Allocate so many points to each "thing" that you measure and then score based on a non-linear function of the number of points you collect. An example of this might be to use the square of the number of points as the amount of weight to assign to king safety i.e. 0, 1, 4, 9, 16, 25, 36 etc.

So if you notice 2 things, you might get 4 points but if there are 3 things your score jumps to 9 points. The above "square table" is just an example, we are actually more severe than that with Komodo but it's usually good to start conservatively and experiment.

What might you get points for? It's typical to give points for an attack right next to the king and Komodo gives different numbers of points for different pieces. A bishop attacking g7 next to the king probably doesn't deserve the same number of points as a queen attacking g7.

Your idea of giving points for having a check possible is a good one too. In Komodo each different piece gets a different number of points for the possibility to attack. We also have a point table for which square of the board the king is on so that a king in the center of the board gets a few points but near the corners no points. Add all the points together and use this to index into the progressive table and of course scale it appropriately for you evaluation function.
Thank you Don, that's an interesting summary. I've always been too lazy to work on my eval, so it's still *really basic*. I've finally made my first attempt at King Safety:

1/ Pawn shelter (using pawn hash table)
* zone1: 3 squares in front of the king (or 2 squares when king on the edge). add a bonus of 4 centipawns (cp) for each friendly pawn there, and -4cp for each enemy pawn there
* zone2: same story but 2 ranks in front of the king i/o 1 rank. and bonus is divided by 2 (so 2 cp for friendly pawn and -2cp for enemy pawn)

This is an extremely crude method, but testing still shows that it's better than nothing. also testing seems to show that 4 cp is a good value.
What do you recommend as a first step to improve this ?

2/ King Attacks
* zone 1 = "king ring" is all the squres adjacent to the king
* zone 2 = zone 1 + sqaures where a knight would give check (ie. squares the king go jump onto it the king was a knight)
* count pieces attacking the relevant zone (zone 1 for B/R/Q and zone 2 for N), where the square(s) attacked are neither occupied by a friendly pawn (blocking our own attack) or defended by an enemy pawn.
* do a weighted cound: weighted_count is incremented by 2 for a N/B attack, by 3 for a R attack and by 5 for a Q attack
* king attack bonus = weighted_count * count.

example, you have a Knight with attacking zone 2, a bishop attacking zone 1, and a Queen attacking zone 1. Then the bonus is (2+2+5)*3 = 21 cp.

So what I'm wondering now is:
* does it make sense ? are there any simple ways to improve this ?
* intuitively would you say my weights are too small ? too big ?

I suppose I should remove the enemy pawn from the pawn shelter evaluation, and consider them in the king attack mechanism rather ?

Post Reply