king safety and castling ?

Discussion of chess software programming and technical issues.

Moderator: Ras

Lyudmil Tsvetkov
Posts: 6052
Joined: Tue Jun 12, 2012 12:41 pm

Re: king safety and castling ?

Post by Lyudmil Tsvetkov »

MahmoudUthman wrote:In king safety evaluation (pawn shield) should I evaluate the safety of the position after castling only if it was strictly legal castling (flags set & the king and it's path aren't under attack) or should I consider it even if the king or the path are under attack ?
I suppose in the case one square in the path is attacked by an enemy piece, a small penalty is due, 15cps or so, for the inability to castle/go to safer square.

sometimes, such an attack migth be only temporary, at other times more permanent, but a penalty is due in both cases.
User avatar
Ras
Posts: 2751
Joined: Tue Aug 30, 2016 8:19 pm
Full name: Rasmus Althoff

Re: king safety and castling ?

Post by Ras »

Daniel Anulliero wrote:Then interpolating the two scores?
That would half miss the case when castling is desirable, but not possible. I have seen test games where a nasty opponent bishop prevented the castling, and that was the very reason why it was put there.

Besides, the search tree will show both variants anyway, provided that castling is possible. Since castling gets a reward, the engine will try to castle. So I recommend to do the static eval on the position as-is and leave the rest to the search tree.

Castling is made even easier with special mobility treatment - early on, the rooks don't matter, neither the queen, but the minor pieces do. Once they are out, the way is also free for castling.

OK, I've also had some games where both wings ended up open, but that were games where the other side also was shaken, so it was chaotic for both sides. And sometimes, it is actually safer in the centre, though that is the exception.
Personally in Isa I do two diférents évaluations , if the king is on files a, b,c,f,g,h or files d,e
Same for NG-Play and the CT800.
Necromancer
Posts: 37
Joined: Wed Nov 23, 2016 1:30 am
Location: Brazil

Re: king safety and castling ?

Post by Necromancer »

I'm my engine I didn't handle castle eval yet, since it castles 'naturally' most of the time. But sometimes it loses the right to castle (against much stronger opponents):

I'm planning to add something like:

Code: Select all

//The same for QS
if (hasRightsCastleKS() && canCastleKS() && ksWingIsSafe()) {
    if (enemyHasQueen())
        assign big penalty
    else
        assign smaller penalty
}
The truth comes from inside.
https://github.com/fernandotenorio/Tunguska
User avatar
Evert
Posts: 2929
Joined: Sat Jan 22, 2011 12:42 am
Location: NL

Re: king safety and castling ?

Post by Evert »

Necromancer wrote:I'm my engine I didn't handle castle eval yet, since it castles 'naturally' most of the time. But sometimes it loses the right to castle (against much stronger opponents):

I'm planning to add something like:

Code: Select all

//The same for QS
if (hasRightsCastleKS() && canCastleKS() && ksWingIsSafe()) {
    if (enemyHasQueen())
        assign big penalty
    else
        assign smaller penalty
}
That will probably just make it give up castling rights just to get rid of the penalty. Unless you've castled, having the option to do it later has to be worth more than any penalty for not having done so (which probably means there shouldn't be one).
User avatar
Ras
Posts: 2751
Joined: Tue Aug 30, 2016 8:19 pm
Full name: Rasmus Althoff

Re: king safety and castling ?

Post by Ras »

A possible reason why it loses the right to castle could be that the dynamic search keeps pushing out the castling until it is too late. In every turn, there are "more important" moves, but at the end of the search tree, there is castling anyway. Until it is too late.

Having a debug PV printout will help here. If you have such a game where castling just doesn't happen, but the debug prints always show castling near the end of the PV, then this probably is what's happening.

Not sure how to fix it; I'm mostly bypassing this issue using the opening book.

But there could also be a small "king still lingering in the centre" penalty that grows with every move, limited to a maximum penalty. Any tested ideas?
Necromancer
Posts: 37
Joined: Wed Nov 23, 2016 1:30 am
Location: Brazil

Re: king safety and castling ?

Post by Necromancer »

My engine castle most of the time, but not as fast as I wished. It
sometimes loses the castle against much stronger opponents, when they are attacking.

Do you guys score castle moves while searching? I didn't.
The truth comes from inside.
https://github.com/fernandotenorio/Tunguska