Approaches to king safety?

Discussion of chess software programming and technical issues.

Moderators: hgm, Rebel, chrisw

User avatar
hgm
Posts: 27808
Joined: Fri Mar 10, 2006 10:06 am
Location: Amsterdam
Full name: H G Muller

Re: Approaches to king safety?

Post by hgm »

mike_bike_kite wrote: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.
This is the way most top programs do it, so it can't be bad.

Be sure to use some non-linearity in the number of attackers, though. A single opponent piece attacking squares next to your King is never very dangerous, even if it is a Queen (but of course a Queen is still more dangerous than a Bishop). It is when two or three pieces are ganging up on your King that things starts to get scary. The penalty for this should easily be the value of a minor.

Scale the whole thing with total material. If there is no Queen on the board, an R+B attack on your fortress is not nearly as dangerous as when a Queen floats around that could be added to it.

Don't award piling up defenses per se. (Except for the Pawn shield, as you can't form one 'on demand'). This prevents awarding defense if there is nothing to defend against. Defense must get only points because it reduces the attacks.
micron
Posts: 155
Joined: Mon Feb 15, 2010 9:33 am
Location: New Zealand

Re: Approaches to king safety?

Post by micron »

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

Re: Approaches to king safety?

Post by lucasart »

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.

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)?
If you're program hasn't reached 2500 elo or so, I don't think you should spend time on a king safety. Better fix search bugs, and do a check and single reply extension. That should fix your king safety problem. To give you an idea, Fruit has no king safety and plays at 2700 elo or so...
User avatar
hgm
Posts: 27808
Joined: Fri Mar 10, 2006 10:06 am
Location: Amsterdam
Full name: H G Muller

Re: Approaches to king safety?

Post by hgm »

lucasart wrote:That should fix your king safety problem. To give you an idea, Fruit has no king safety and plays at 2700 elo or so...
:shock: :shock: :shock: :shock: :shock: :shock:

King safety is actually one of the largest terms in the Fruit 2.1 evaluation. And it has a UCI option to tune its weight.
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:
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.

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)?
If you're program hasn't reached 2500 elo or so, I don't think you should spend time on a king safety. Better fix search bugs, and do a check and single reply extension. That should fix your king safety problem. To give you an idea, Fruit has no king safety and plays at 2700 elo or so...
The point is that if your program doesn't understand these sacrificial king attcks by search means, I don't think the eval will be able to compensate for the flaws of the search. Here's what I do in DoubleCheck, I suggest you give a try:

1/ check extension:
if the move is a check with a SEE >= 0, or if it's a revealed check (in which case the SEE is not to be trusted), then I extend by one ply.

2/ single reply extension:
if there's only one legal move in the position, I extend by one ply

=> that should already improve greatly the ability of your program to cope with "wild" positions.

In the eval, I don't have any king safety :) All I have is in the pawn evaluation a king shelter bonus. Basically I look at the 3 squares in front of the king (Zone 1) and the 3 in front of those (Zone 2). I count a small bonus for a friendly pawn in Zone 1, and the same penalty for an enemy pawn in Zone 1. Same for Zone 2 dividing by 2 the bonus. The bonus I use is 4 centipawns. I'm not saying this is optimal in anyway, but it's simple and disincentivises stupid self weakening pawn pushes at least (conversly it incentivises pawn attacks on the enemy king's pawn shelter). A benefit of this is that it's done in the pawn eval and uses the pawn hash table (faster).
User avatar
lucasart
Posts: 3232
Joined: Mon May 31, 2010 1:29 pm
Full name: lucasart

Re: Approaches to king safety?

Post by lucasart »

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

Re: Approaches to king safety?

Post by lucasart »

hgm wrote:
lucasart wrote:That should fix your king safety problem. To give you an idea, Fruit has no king safety and plays at 2700 elo or so...
:shock: :shock: :shock: :shock: :shock: :shock:

King safety is actually one of the largest terms in the Fruit 2.1 evaluation. And it has a UCI option to tune its weight.
Ah yes you're right, but Fabien only added it in Fruit 2.1, wasn't in 2.0. And his king safety doesn't return large scores, no ?
Anyway, my point is that the best way to fix a lame engine is not by introducing eval terms, but by fixing the search
User avatar
hgm
Posts: 27808
Joined: Fri Mar 10, 2006 10:06 am
Location: Amsterdam
Full name: H G Muller

Re: Approaches to king safety?

Post by hgm »

I am not sure that is good advice. With faulty eval a deep search will just be better at digging a hole for itself. Search is no substitute for strategic knowledge, and if you value a minor below three pawns, no amount of search will ever get you above 1500 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 »

hgm wrote:I am not sure that is good advice. With faulty eval a deep search will just be better at digging a hole for itself. Search is no substitute for strategic knowledge, and if you value a minor below three pawns, no amount of search will ever get you above 1500 Elo.
well doublecheck has no king safety and perorms at 2400 elo+ now... i could remove the pawn shelter, and i'm sure i wouldn't see any big difference.
the only eval i have is: mobility (linear and very simple), pawns (inc passed pawns and king shelter)... and that's all :)
User avatar
hgm
Posts: 27808
Joined: Fri Mar 10, 2006 10:06 am
Location: Amsterdam
Full name: H G Muller

Re: Approaches to king safety?

Post by hgm »

Well, Pawn shelter is King Safety, even when you prefer to call it differently.