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 »

lucasart wrote: 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.
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.

What I do in Sjaak (based on what I do in Jazz, which so far has a more direct translation of Ed's algorithm) is assign each piece a weight based on its attack strength on "small boards", which is a proxy for how dangerous such a piece is if it is part of a king attack. This is essentially the total of squares attacked on a 3x3 and a 5x5 board, with some penalty for colour-bound pieces. Every piece joining the attack increments a counter with a score proportional to its "attack strength", but with a cap. The idea is that at some point adding pieces to the attack is no longer enough to increase the threat and you need to do things like remove attackers.
The index is also icremented for defects in the pawn shield and otherwise missing defenders. Finally, there's a penalty for the king straying too far from the corner.
The counter is used to index a table, which is calculated at startup using a Fermi-function. There are three adjustable parameters: the asymptotic value (the normalisation, which is ~ 2/3 of a minor piece), the index where the value starts to rise steeply (this plays the role of the Fermi energy) and a parameter describing the slope (which plays the role of temperature).
The final result is then scaled with the total attack strength of the enemy army, normalised to the attack strength in the initial chess array.
This works reasonably ok, but I'm sure there's room for improvement.
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: 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
User avatar
lucasart
Posts: 3232
Joined: Mon May 31, 2010 1:29 pm
Full name: lucasart

Re: Approaches to king safety?

Post by lucasart »

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:
User avatar
lucasart
Posts: 3232
Joined: Mon May 31, 2010 1:29 pm
Full name: lucasart

Re: Approaches to king safety?

Post by lucasart »

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
Here';s something even more shocking: +70 elo in self play, but against a varied population of engines, no improvement...
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: 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.
User avatar
Don
Posts: 5106
Joined: Tue Apr 29, 2008 4:27 pm

Re: Approaches to king safety?

Post by Don »

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.
bob
Posts: 20943
Joined: Mon Feb 27, 2006 7:30 pm
Location: Birmingham, AL

Re: Approaches to king safety?

Post by bob »

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.
User avatar
Don
Posts: 5106
Joined: Tue Apr 29, 2008 4:27 pm

Re: Approaches to king safety?

Post by Don »

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.
User avatar
Rebel
Posts: 6995
Joined: Thu Aug 18, 2011 12:04 pm

Re: Approaches to king safety?

Post by Rebel »

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.
Absolutely true statement.

I have divided the square measuring around the king divided into 3 groups and every group has its own "defender" weight factor.
Uri Blass
Posts: 10299
Joined: Thu Mar 09, 2006 12:37 am
Location: Tel-Aviv Israel

Re: Approaches to king safety?

Post by Uri Blass »

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.