how to improve endgame play?

Discussion of chess software programming and technical issues.

Moderator: Ras

Whiskers
Posts: 243
Joined: Tue Jan 31, 2023 4:34 pm
Full name: Adam Kulju

how to improve endgame play?

Post by Whiskers »

Hi! I decided to try writing a chess engine a few months ago (named Willow), and so far I'm very pleased with its progress. For a programming noob like me, ~2100 is quite a feat :lol:

However, recently while testing king safety in self-play, I noticed something; the majority of decisive games are not being won or lost in the middlegame, but rather in the endgame - specifically rook endgames or endgames with a couple pieces each. The manner in which it happens is usually that Willow is shuffling around in a drawish piece endgame, then suddenly the eval spikes and after a couple moves it drops a pawn or trades into a won/lost pawn endgame (it has no problems with those, solving Fine 70 in one second for example).

It also gets itself stuck thinking its winning in drawn endgames much more than it should. I added a patch to stop it going into pawnless endgames like RvB RvRN etc., but it still has a lot of games that wind up in positions like this, where Willow thinks Black is completely winning and continues to think so until the 50 move rule finally steps in.


[fen]8/8/8/8/4rBbk/4Pp2/5K2/4N3 w - - 0 1[/fen]


What can I do to improve its endgame play? I have thought about using 2 move repetition checks at every node except the root, to stop it from delaying moves that reduce its advantage by repeating once. I've also thought about adding a scaled penalty (like "if pawns < 4 then eval = (eval/4-pawns)") to prevent trading off too many pawns, but I worry that doing that may make Willow unable to see continuations that end in a winning Lucena, for instance.

It might be too that the search just needs to be improved somehow, though I already do most of the basic stuff like LMR, PVS, NMP if both sides have a piece, etc. In an average rook endgame Willow gets to about depth 15 in one second, there's no extensions but it doesn't reduce checks. Maybe I should add passed pawn extensions? Or perhaps the depth it reaches just isn't enough to play endgames properly in fast testing?

As for evaluation I've got PSTs, piece values, and mobility values for the endgame. I also have passed pawn evals but that's just a general 10*rank centipawn bonus. (Even that simple passed pawn eval was enough to give a >50 elo gain in self play, perhaps improving that will help a lot?)

I'd appreciate any advice. It's getting quite annoying to try and test king safety and space evals for the early middlegame, only for it to feel like any game that isn't very clearly won going into the endgame is a coinflip!
User avatar
hgm
Posts: 28353
Joined: Fri Mar 10, 2006 10:06 am
Location: Amsterdam
Full name: H G Muller

Re: how to improve endgame play?

Post by hgm »

Scaling KRNKR towards a draw often does not help much, because it then draws in KRNPKRN. The defending Knight can stop the advance of the Pawn forever under the threat to sacrifice it for converting to the draw that is recognized. So you have to scale down positions with a single Pawn already, if the opponent has a piece it can safely sacrifice for it. Otherwise the engine would happily trade Pawns in KRNPPKRNP.

You should score the first repetition as a draw. Otherwise the engine will abuse the privilage to repeat for pushing tactics it doesn't like over the horizon, as you say. At the very least you should scale a repetion towards a draw, but this is a bit tricky; if you plan to half the score that results from an already once repeated position, you should double alpha and beta to precompensate for that, or out-of-window scores could be mistaken for exact scores (with unlimited blundering as a consequence).

The modern way of doing things is to keep your engine stupid, but use End-Game Tables to get exact mate scores for positions with up to 6 men, and then rely on those tables.

There is a theoretical possbility to generate the EGT on-the-fly if there are only Kings, two pieces (sometimes even 3) and many Pawns, which would allow you to perfectly score such positions. But no one does that.
Whiskers
Posts: 243
Joined: Tue Jan 31, 2023 4:34 pm
Full name: Adam Kulju

Re: how to improve endgame play?

Post by Whiskers »

hgm wrote: Tue Jan 31, 2023 7:23 pm Scaling KRNKR towards a draw often does not help much, because it then draws in KRNPKRN. The defending Knight can stop the advance of the Pawn forever under the threat to sacrifice it for converting to the draw that is recognized. So you have to scale down positions with a single Pawn already, if the opponent has a piece it can safely sacrifice for it. Otherwise the engine would happily trade Pawns in KRNPPKRNP.

You should score the first repetition as a draw. Otherwise the engine will abuse the privilage to repeat for pushing tactics it doesn't like over the horizon, as you say. At the very least you should scale a repetion towards a draw, but this is a bit tricky; if you plan to half the score that results from an already once repeated position, you should double alpha and beta to precompensate for that, or out-of-window scores could be mistaken for exact scores (with unlimited blundering as a consequence).

The modern way of doing things is to keep your engine stupid, but use End-Game Tables to get exact mate scores for positions with up to 6 men, and then rely on those tables.

There is a theoretical possbility to generate the EGT on-the-fly if there are only Kings, two pieces (sometimes even 3) and many Pawns, which would allow you to perfectly score such positions. But no one does that.
Well, I discovered a mistake that makes me feel dumb. It turns out that repeating the same position three times is only a draw if it's the same side to move for each of those positions, and if for example the position repeats via triangulation it doesn't count towards the repetition. My TT Table obviously doesn't consider one position with White to move the same as that position with Black to move, yet my draw checker does. Oops.

After fixing that, it seems that Willow is playing the endgame much better. (no, I have no intention of testing how much ELO making the engine comply with the rules of chess gives :D ). I'm testing "first repetition as draw" now against "second repetition as draw", and it's too soon to make any statements yet, but both sides seem to be playing their endgames much cleaner than before. They still make mistakes but the ones I've seen so far are all related to the other issues (search depth and endgames with low numbers of pawns).
Whiskers
Posts: 243
Joined: Tue Jan 31, 2023 4:34 pm
Full name: Adam Kulju

Re: how to improve endgame play?

Post by Whiskers »

300 games later, nothing. It scores exactly the same. Oh well, I'll keep it in. The endgame play does look significantly better.
User avatar
hgm
Posts: 28353
Joined: Fri Mar 10, 2006 10:06 am
Location: Amsterdam
Full name: H G Muller

Re: how to improve endgame play?

Post by hgm »

Pawn structure becomes very important in the end-game. So improving that might really help.

Even if you do only passed pawns: a linear bonus is not ideal. Two passers on 3rd rank would get the same bonus as one passer on 6th. But in a Pawn ending the one who queens first easily wins. So it is better to have a quadratic bonus.
User avatar
j.t.
Posts: 263
Joined: Wed Jun 16, 2021 2:08 am
Location: Berlin
Full name: Jost Triller

Re: how to improve endgame play?

Post by j.t. »

If you already have PSTs and in case you are autotuning them, just add another entry specifically for passed pawns.
Whiskers
Posts: 243
Joined: Tue Jan 31, 2023 4:34 pm
Full name: Adam Kulju

Re: how to improve endgame play?

Post by Whiskers »

hgm wrote: Wed Feb 01, 2023 7:41 am Pawn structure becomes very important in the end-game. So improving that might really help.

Even if you do only passed pawns: a linear bonus is not ideal. Two passers on 3rd rank would get the same bonus as one passer on 6th. But in a Pawn ending the one who queens first easily wins. So it is better to have a quadratic bonus.
I tried that and it didn't work too well because it would keep allowing the other side to get passed pawns on say a2 in the middlegame thinking they weren't much of a deal, only for the eval to spike once that pawn got rolling in the endgame.

Instead I wrote up a "do not reduce" clause for moves of a passed pawn past the 4th rank in my search function to help improve play in the types of positions that you were describing. That helped somewhat, I could squeeze more value out of it with tapered passed pawn evals, evaluating blockers, etc. but I'll leave those for later as just by the eye test Willow is playing these endgames much better than before. Thanks for the help!
KhepriChess
Posts: 93
Joined: Sun Aug 08, 2021 9:14 pm
Full name: Kurt Peters

Re: how to improve endgame play?

Post by KhepriChess »

Whiskers wrote: Thu Feb 02, 2023 10:50 pm I tried that and it didn't work too well because it would keep allowing the other side to get passed pawns on say a2 in the middlegame thinking they weren't much of a deal, only for the eval to spike once that pawn got rolling in the endgame.

Instead I wrote up a "do not reduce" clause for moves of a passed pawn past the 4th rank in my search function to help improve play in the types of positions that you were describing. That helped somewhat, I could squeeze more value out of it with tapered passed pawn evals, evaluating blockers, etc. but I'll leave those for later as just by the eye test Willow is playing these endgames much better than before. Thanks for the help!
How are you determining middlegame vs endgame in your eval? Do you have just a hard cut-off (like pieceValue < X) or do you have a tapered eval? Either way, you could try two values for passed pawns, one for middlegame and one for endgame (with the endgame values likely being higher). Because a passed pawn at any point in the game can still be a threat.
Puffin: Github
KhepriChess: Github
Whiskers
Posts: 243
Joined: Tue Jan 31, 2023 4:34 pm
Full name: Adam Kulju

Re: how to improve endgame play?

Post by Whiskers »

KhepriChess wrote: Thu Feb 02, 2023 11:30 pm
Whiskers wrote: Thu Feb 02, 2023 10:50 pm I tried that and it didn't work too well because it would keep allowing the other side to get passed pawns on say a2 in the middlegame thinking they weren't much of a deal, only for the eval to spike once that pawn got rolling in the endgame.

Instead I wrote up a "do not reduce" clause for moves of a passed pawn past the 4th rank in my search function to help improve play in the types of positions that you were describing. That helped somewhat, I could squeeze more value out of it with tapered passed pawn evals, evaluating blockers, etc. but I'll leave those for later as just by the eye test Willow is playing these endgames much better than before. Thanks for the help!
How are you determining middlegame vs endgame in your eval? Do you have just a hard cut-off (like pieceValue < X) or do you have a tapered eval? Either way, you could try two values for passed pawns, one for middlegame and one for endgame (with the endgame values likely being higher). Because a passed pawn at any point in the game can still be a threat.
Yes, I have a tapered eval, it's a simple NB=1 R=2 Q=4 implementation like the one in PesTO. I will probably get around to doing what you suggest eventually, and adding bonuses for connected/protected passed pawns, but I don't want to do too much in one area at a time and neglect everything else. There's more things to work on than I can shake a stick at!
User avatar
hgm
Posts: 28353
Joined: Fri Mar 10, 2006 10:06 am
Location: Amsterdam
Full name: H G Muller

Re: how to improve endgame play?

Post by hgm »

You should always give a bonus for a passed pawn. The point is that when having two (isolated) passers on 4th rank, advancing both to 5th should be worth less than advancing one to 6th (and leaving the other at 4th. So the 5th-6th step should be worth more than the 4th-5th step. The 2nd-3rd step isn't worth anything, because of the double push.

Also, advancing a passer on the opponent half too early in the game should get a penalty, rather than a bonus, unless it is protected by another Pawn.