king safety and castling ?

Discussion of chess software programming and technical issues.

Moderator: Ras

MahmoudUthman
Posts: 237
Joined: Sat Jan 17, 2015 11:54 pm

king safety and castling ?

Post by MahmoudUthman »

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 ?
User avatar
cdani
Posts: 2204
Joined: Sat Jan 18, 2014 10:24 am
Location: Andorra

Re: king safety and castling ?

Post by cdani »

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 ?
The fact that there was a castling done is not really important, as the safety of the king is what is regardless of this flag.
User avatar
Evert
Posts: 2929
Joined: Sat Jan 22, 2011 12:42 am
Location: NL

Re: king safety and castling ?

Post by Evert »

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 ?
You should not call the evaluation if the position under consideration is illegal. So the king of the side not on move should not be under attack (and arguably you shouldn't use the evaluation if the side to move is in check either).
User avatar
cdani
Posts: 2204
Joined: Sat Jan 18, 2014 10:24 am
Location: Andorra

Re: king safety and castling ?

Post by cdani »

Evert wrote:
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 ?
You should not call the evaluation if the position under consideration is illegal. So the king of the side not on move should not be under attack (and arguably you shouldn't use the evaluation if the side to move is in check either).
I think I hadn't understood the question :oops:
Sven
Posts: 4052
Joined: Thu May 15, 2008 9:57 pm
Location: Berlin, Germany
Full name: Sven Schüle

Re: king safety and castling ?

Post by Sven »

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 ?
Please explain what you mean. "Evaluate the safety of the position after castling" (bold-face added by me) and "flags set & the king and its path aren't under attack" are contradicting to each other, so what do you want to evaluate (safety of uncastled or of castled king), and at which time?
User avatar
Evert
Posts: 2929
Joined: Sat Jan 22, 2011 12:42 am
Location: NL

Re: king safety and castling ?

Post by Evert »

cdani wrote:
Evert wrote:
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 ?
You should not call the evaluation if the position under consideration is illegal. So the king of the side not on move should not be under attack (and arguably you shouldn't use the evaluation if the side to move is in check either).
I think I hadn't understood the question :oops:
That remains to be seen.
He may want to calculate the castling-rights bonus for the current position using the king-safety for the hypothetical case where the king has castled, ie, not using the actual position of the king.
In that case the answer is to ignore whether castling is legal or not, because if you don't the term will not prevent the engine from throwing away its right to castle.
User avatar
Ras
Posts: 2735
Joined: Tue Aug 30, 2016 8:19 pm
Full name: Rasmus Althoff

Re: king safety and castling ?

Post by Ras »

Since you should weed out illegal moves from the pseudo legal move list before deepening them anyway, having made the castling implies that it must have been legal.
Daniel Anulliero
Posts: 773
Joined: Fri Jan 04, 2013 4:55 pm
Location: Nice

Re: king safety and castling ?

Post by Daniel Anulliero »

May be he would say :
Doing an evaluation
-in case of castling
-in case of no castling
Then interpolating the two scores?
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
MahmoudUthman
Posts: 237
Joined: Sat Jan 17, 2015 11:54 pm

Re: king safety and castling ?

Post by MahmoudUthman »

I meant something like this sf code :

Code: Select all

template<Color Us>
Score Entry::do_king_safety(const Position& pos, Square ksq) {

  kingSquares[Us] = ksq;
  castlingRights[Us] = pos.can_castle(Us);
  minKingPawnDistance[Us] = 0;

  Bitboard pawns = pos.pieces(Us, PAWN);
  if (pawns)
      while (!(DistanceRingBB[ksq][minKingPawnDistance[Us]++] & pawns)) {}

  if (relative_rank(Us, ksq) > RANK_4)
      return make_score(0, -16 * minKingPawnDistance[Us]);

  Value bonus = shelter_storm<Us>(pos, ksq);

  // If we can castle use the bonus after the castling if it is bigger
  if (pos.can_castle(MakeCastling<Us, KING_SIDE>::right))
      bonus = std::max(bonus, shelter_storm<Us>(pos, relative_square(Us, SQ_G1)));

  if (pos.can_castle(MakeCastling<Us, QUEEN_SIDE>::right))
      bonus = std::max(bonus, shelter_storm<Us>(pos, relative_square(Us, SQ_C1)));

  return make_score(bonus, -16 * minKingPawnDistance[Us]);
}
Lyudmil Tsvetkov
Posts: 6052
Joined: Tue Jun 12, 2012 12:41 pm

Re: king safety and castling ?

Post by Lyudmil Tsvetkov »

Daniel Anulliero wrote:May be he would say :
Doing an evaluation
-in case of castling
-in case of no castling
Then interpolating the two scores?
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
I do not see what the difference is where the king is : on central e|d, or other non-central files. In all cases the shelter remains the same: squares on the 2nd & 3rd ranks file of king&file of king -1 & file of king +1. the fact that the king is more exposed in the center is of course seen by the psqt mg values.

main problems with modern engines' shelter eval is not the way they score the shelter, but the way they do not score it. and there are a whole lot of very valuable things one can score about the shelter:

- isolated pawns within the shelter
- doubled pawns within the shelter
- backward pawns within the shelter
- twice backward pawns within the shelter
- semibackward pawns within the shelter
- no pawn shelter (0 own pawns within), etc., etc.

I guess many programs would score isolated/doubled pawns within the shelter, but, believe me or not, the most important thing about pawn shelter is the presence of a backward pawn, part of it: this is at least 3 times more important than an isloated pawn within, 3 times more frequent, and, of course, believe it or not, deserving a bigger penalty.

all backward pawn penalties within the shelter are important and have big or very big penalties. for example, a twice backward pawn in the shelter only on f2/c2 is worth some 100cps penalty in the mg. engines however seem not to score such factors, SF does not score them, and they do not understand them.

one of the reasons for this is that while isolated shelter pawns are associated with shallower search, backward pawns suppose a very deep search on average, you migth have to do some 30+ moves until you see the value of the penalty, and, in case your move ordering prefers attacking moves to quiet preparatory ones, you migth never see its value. :)