Approaches to king safety?

Discussion of chess software programming and technical issues.

Moderators: hgm, Rebel, chrisw

User avatar
Evert
Posts: 2929
Joined: Sat Jan 22, 2011 12:42 am
Location: NL

Re: Approaches to king safety?

Post by Evert »

Uri Blass wrote: I do not agree because there are cases when there is an attack but the attack is not dangerous because you have a defence.
But you detect that and give a smaller (or no) bonus for the attack, right?
I think that it may be better to have in the evaluation:
1)bonus for attacking squares near the king.
2)bigger bonus for attacking squares near the king that are not defended
But that was the suggestion, right?
You score defence by adjusting the score for the enemy attack for the presence of defenders. That doesn't directly score defence, but in effect it does score defence in response to a threat, which is what you want.
Uri Blass
Posts: 10281
Joined: Thu Mar 09, 2006 12:37 am
Location: Tel-Aviv Israel

Re: Approaches to king safety?

Post by Uri Blass »

Evert wrote:
Uri Blass wrote: I do not agree because there are cases when there is an attack but the attack is not dangerous because you have a defence.
But you detect that and give a smaller (or no) bonus for the attack, right?
I think that it may be better to have in the evaluation:
1)bonus for attacking squares near the king.
2)bigger bonus for attacking squares near the king that are not defended
But that was the suggestion, right?
You score defence by adjusting the score for the enemy attack for the presence of defenders. That doesn't directly score defence, but in effect it does score defence in response to a threat, which is what you want.
Yes
I agree that it is better not to evaluate defence if there is no threat(I do not need to defend my king if the opponent does not attack it).
User avatar
lucasart
Posts: 3232
Joined: Mon May 31, 2010 1:29 pm
Full name: lucasart

Re: Approaches to king safety?

Post by lucasart »

Don wrote:
bob wrote:
Don wrote:
lucasart wrote:
lucasart wrote:
Evert wrote: Being able to deliver check is not the only threat, taking away squares from the king is another. So I would also count knight attacks for what you call zone 1.
I do: zone1 is included in zone2.

By the way, I have the result of the test, and King Attacks alone give me a schocking +70 elo in self-play :shock:

And that was not even 10 lines of code !!

I guess HGM was right, King Safety is very important indeed
So king attacks are all good and amazing, but I also need to consider the defense side of things. I'll think about that... :roll:
We have found that attacks are much more important than defenses. In fact we have had a difficult time making defense bonuses work for us even though logically it should. I am sure we could improve on this substantially or may be missing some important principle.

The reason may be that it's difficult to get a lot of pieces attacking the squares around the king if you are defending well and so it may be highly redundant. Also, if you have a bunch of pieces defending your king (and are giving bonuses for that) then they are probably not being used elsewhere and of course they would not be attacking the enemy king either.

Like anything else the principle should be that they defend the king only when that is the best thing to do :-) Not arbitrarily. But the trick is in properly identifying those cases.
My take is "evaluating tit for tat" is VERY difficult. If you are not carneful, you get a half-assed defense, and a half-assed attack, and lose. One could r own probably do either quite well. Lots of defense, no offense, or vice-eversa. But doing both is a zero-sum issue. From experience.
I think so too. If you focus on attacks the search is going to understand what a defense is, i.e. any move which prevents an attack.
Yes, it makes sense intuitively from a chess perspective too. Having a destroyed pawn shelter and being under heavy attack, the right way to defend is rarely to control the squares around your king with your own pieces. If anything you'e just getting your own pieces cramped on top of getting your king pressured...

As they say, "in chess, the best defense is attack"

Generally I would think the right way to defend such a position is by exchanging pieces for attacking enemy pieces. That would be detected by the king attack bonus (enemy bonus reduces). And to move your king into another pawn shelter somewhere else, if possible

By the way, I had a look at Stockfish's code. Of course it's extremely complicated and far more refined than my crude king attack bonus mechanism, but amongst other things it introduces the concept of safe contact checks. That's interesting and certainly makes sense from a chess perspective. I wonder if that feature is worth much elo
User avatar
lucasart
Posts: 3232
Joined: Mon May 31, 2010 1:29 pm
Full name: lucasart

Re: Approaches to king safety?

Post by lucasart »

Evert wrote:
lucasart wrote: Here';s something even more shocking: +70 elo in self play, but against a varied population of engines, no improvement...
It pays to look at games that are lost, sometimes these provide an indication of what is missing.
It's quite possible that something else is missing that makes king safety not essential, which means that adding that missing feature could improve strength against the gauntlet a lot more than in self-play.
Yes, definitely. A good example is your program Jazz 501. DoubleCheck scores +100 elo on my rating list, with a wide range of opponents, but against Jazz it scores badly, probably +20 elo no more. And when I look at the games, Jazz wins a lot of very long games, by endgame manouevers, whilst DoubleCheck wins very short games that are brutally tactical. So some search code may not be right in Jazz, and DoubleCheck is certainly missing a lot of chess knowledge that Jazz has...
But I really don't know where to start. My eval has the following features:
* material (including bishop pair) + piece on square tables
* mobility: linear and simple, but works well, and probably the best general purpose eval term IMO
* pawn structures, including passed pawns, and pawn shelters (some basic king safety)
* king attacks (very basic code explained in my previous post)
* rook and queens on 7th rank. this is not in the piece on square table, as I have a precondition (a la Fruit) to do it: only when opponent has pawns on 7hth rank or opponent king is on 8th rank)
User avatar
OliverUwira
Posts: 170
Joined: Mon Sep 13, 2010 9:57 am
Location: Frankfurt am Main

Re: Approaches to king safety?

Post by OliverUwira »

Don wrote: We have found that attacks are much more important than defenses. In fact we have had a difficult time making defense bonuses work for us even though logically it should. I am sure we could improve on this substantially or may be missing some important principle.

The reason may be that it's difficult to get a lot of pieces attacking the squares around the king if you are defending well and so it may be highly redundant. Also, if you have a bunch of pieces defending your king (and are giving bonuses for that) then they are probably not being used elsewhere and of course they would not be attacking the enemy king either.
Sorry, if I just pop into this discussion without reading the thread completely, but you've made a very interesting observation regarding defence being correlated to other strategic concepts.

To begin with, let's consider two pieces advice postulated by two titans:
  • The king is well capable of defending himself (Steinitz)
  • Strive to employ as few pieces as necessary for defensive tasks (Lasker)
Those are not direct quotes, but recited from memory. Steinitz got extreme on his quote, which lead to him playing gambits like 1. e4 e5 2. Nc3 Nc6 3. f4 exf4 4. d4?! Qh4+ 5. Ke2. On the other hand, we have often seen that a king might well be able to do a runner, e.g. in some instances of the Greek Gift bishop sacrifice at h7.

Linked with this capability of running is Lasker's quote, who didn't like to spend unneccesary resources on defence. So if you pile up defenders of a quite healthy pawn shelter, you end up wasting resources for active operations.

For a healthy pawn shelter, an attacker needs far more firepower because he has to a) smash open the pawn shelter b) keep the king there and c) still have enough attackers left to overcome the defenders. Jacob Aagaard has christened this the "Three Piece Rule" - in absence of defenders, you need three pieces to give mate: One to sacrifice, one to assist and one to actually deliver mate.

Another correlation of defence might be found with regard to mobility. In a cramped position with a closed center, it is very difficult to manoehuvre pieces across to aid the king. Centralisation (also correlated to mobility) should be the equivalent with regard to open centers. You won't find an attack with two bishops at b2 and d3 aided by queen and rook on the kingside _if_ black has good centralisation - because then Black easily be able to prevent White from setting up an attack that scores high king safety.

I'm aware that all the above is rather generalized, but it compares well with your findings that the number of defenders is not very important most of the time as defenders can often be brought back in time. All this of course only applies to positions with a healthy shelter.[/list]
User avatar
lucasart
Posts: 3232
Joined: Mon May 31, 2010 1:29 pm
Full name: lucasart

Re: Approaches to king safety?

Post by lucasart »

Uri Blass wrote: 2)bigger bonus for attacking squares near the king that are not defended(you do not count defence by the king that you have by definition and the best defender should be minor pieces like knight and bishop(note that the queen is a bad defender because king and queen that defend a square are not enough to deefend from attack of 2 minor pieces)

In this case the defence evaluation is not redundant.
You're probably right about that. Stockfish does what you describe, with the concept of safe contact checks. So it's a way of valuing defense through the scoring of the attack itself
User avatar
Don
Posts: 5106
Joined: Tue Apr 29, 2008 4:27 pm

Re: Approaches to king safety?

Post by Don »

Uri Blass wrote:
Don wrote:
bob wrote:
Don wrote:
lucasart wrote:
lucasart wrote:
Evert wrote: Being able to deliver check is not the only threat, taking away squares from the king is another. So I would also count knight attacks for what you call zone 1.
I do: zone1 is included in zone2.

By the way, I have the result of the test, and King Attacks alone give me a schocking +70 elo in self-play :shock:

And that was not even 10 lines of code !!

I guess HGM was right, King Safety is very important indeed
So king attacks are all good and amazing, but I also need to consider the defense side of things. I'll think about that... :roll:
We have found that attacks are much more important than defenses. In fact we have had a difficult time making defense bonuses work for us even though logically it should. I am sure we could improve on this substantially or may be missing some important principle.

The reason may be that it's difficult to get a lot of pieces attacking the squares around the king if you are defending well and so it may be highly redundant. Also, if you have a bunch of pieces defending your king (and are giving bonuses for that) then they are probably not being used elsewhere and of course they would not be attacking the enemy king either.

Like anything else the principle should be that they defend the king only when that is the best thing to do :-) Not arbitrarily. But the trick is in properly identifying those cases.
My take is "evaluating tit for tat" is VERY difficult. If you are not careful, you get a half-assed defense, and a half-assed attack, and lose. One could probably do either quite well. Lots of defense, no offense, or vice-versa. But doing both is a zero-sum issue. From experience.
I think so too. If you focus on attacks the search is going to understand what a defense is, i.e. any move which prevents an attack.
I do not agree because there are cases when there is an attack but the attack is not dangerous because you have a defence.

I think that it may be better to have in the evaluation:
1)bonus for attacking squares near the king.
2)bigger bonus for attacking squares near the king that are not defended(you do not count defence by the king that you have by definition and the best defender should be minor pieces like knight and bishop(note that the queen is a bad defender because king and queen that defend a square are not enough to defend from attack of 2 minor pieces)

In this case the defence evaluation is not redundant.
As I said earlier, logically it should help. I think perhaps one of the issues is that it's even harder to reliably determine how much a defense help. If there is all kinds of action around the king it's likely to be bad, not for the attacker but for the defender. Having said that I expect that there should be some consideration for good defensive configurations, I just don't know what that might be. We will probably figure it out in an improved version of Komodo.
User avatar
Evert
Posts: 2929
Joined: Sat Jan 22, 2011 12:42 am
Location: NL

Re: Approaches to king safety?

Post by Evert »

lucasart wrote: Yes, definitely. A good example is your program Jazz 501. DoubleCheck scores +100 elo on my rating list, with a wide range of opponents, but against Jazz it scores badly, probably +20 elo no more. And when I look at the games, Jazz wins a lot of very long games, by endgame manouevers, whilst DoubleCheck wins very short games that are brutally tactical. So some search code may not be right in Jazz, and DoubleCheck is certainly missing a lot of chess knowledge that Jazz has...
Quite possibly. I made a number of search improvements/fixes in Sjaak, that I should possibly pull into Jazz as well (the two programs are very similar).
I do suspect that "something" is wrong with Jazz' search, but I've gone over it several times already and can't find anything obvious (Jazz search code is also a bit more complicated than it needs to be; perhaps that is the real thing I should fix).

Interesting that you mention Jazz' performance against DoubleCheck seems better than expected; I may give it a try. My main opponents for testing have been Beowulf and OliThink, but I have some issues starting Beowulf correctly from a setup position (it seems to not recognise castling properly, for some odd reason) and I haven't been able to figure out what to do to improve against OliThink. A new opponent may well give me a better idea.
But I really don't know where to start. My eval has the following features:
* material (including bishop pair) + piece on square tables
* mobility: linear and simple, but works well, and probably the best general purpose eval term IMO
* pawn structures, including passed pawns, and pawn shelters (some basic king safety)
* king attacks (very basic code explained in my previous post)
* rook and queens on 7th rank. this is not in the piece on square table, as I have a precondition (a la Fruit) to do it: only when opponent has pawns on 7hth rank or opponent king is on 8th rank)
Jazz does not really have mobility, what passes for mobility there is just a half-hearted attempt at it. However, it does have a lot of mobility-like conditions in its (dynamic) piece square tables, so that may compensate it somewhat.
Of the things you mention, Jazz has an extra evaluation term for candidate passed pawns, which was worth a fair amount when I tested it (a while back, the details are a bit hazy now). It does not have a "rook on the 7th" bonus as such, but it does have two generalised terms for that: cutting off the enemy king across a rank, and having a rook on the same rank as the base of the enemy pawn structure. No idea how much that is worth in practice though. Whenever I add a similar bonus for the queen, it tests as worse...
One thing I did invest time in is to have special-purpose code for common endgames. It's good to know that this really does seem to pay off (and not just in my own testing). :)
kbhearn
Posts: 411
Joined: Thu Dec 30, 2010 4:48 am

Re: Approaches to king safety?

Post by kbhearn »

Was thinking about this some this morning - there's not a really a lot of good examples where your defense is ok because of a piece - theres' the N on f8 when the obvious attack is on h7, the B on f8 when the obvious attack is on g7, and the R on the 7th when your 7th rank is open or quickly openable thus allowing you to defend whatever the target along the 7th is, and the fianchettoed bishop when your opponent no longer has their bishop of this colour - these seem somewhat reliable to at least be a help not a hinderance, but when you look at even something like a N on f6 which overall may be a help defensively, it may defend h7 or block an attack on g7 but it also provides another target, an exchange on it could destroy the shelter, a pawn could drive it away, and a sacrifice line may wind up paying off because while it doesn't mate immediately there's a piece nearby to be won.

Looking at the queenside case, it's pretty much a mirror image, move your K to b1, stick a B on c1 possibly, open your 2nd rank and put a rook on it. Maybe just specific benefits for the few cases you can come up with of a 'good defender' would be appropriate.
jdart
Posts: 4366
Joined: Fri Mar 10, 2006 5:23 am
Location: http://www.arasanchess.org

test positions for king safety

Post by jdart »

I have collected over the years some positions that are useful for king safety testing. I don't use these in any automated way, I just look at the king safety scoring details (which I can print out if a compile-time flag is set) and see if any look obviously wrong.

--Jon

Code: Select all

4r1k1/2q1pp2/3n2pQ/2rPR3/pp4P1/1P5R/1P2NP2/6K1 b - - 0 1
2R3Q1/5p1p/4p1p1/1P1b3k/3Pp2P/6P1/r4qBK/8 w - - 0 46
2rQkb1r/p2b1ppp/3p4/3Np1B1/4P2P/8/PP4P1/4KB1n b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