Knight Evaluation

Discussion of anything and everything relating to chess playing software and machines.

Moderator: Ras

jorose
Posts: 373
Joined: Thu Jan 22, 2015 3:21 pm
Location: Zurich, Switzerland
Full name: Jonathan Rosenthal

Knight Evaluation

Post by jorose »

First off I don't know what other engines implement in their evaluation functions, so if what I am writing here or in another thread yet to be written on piece mobility is common knowledge then I apologize.

For reference the chess-programming wiki already mentions quite a few standard evaluation terms in respect to a knights value, specifically outposts and trapped pieces amongst others none of which I've actually implemented yet with exception of PST values which in the case of the knight fairly accurately describe its activity. In this post I will be describing the evaluation terms I use instead of the first evaluation term ("decreasing value as pawns disappear") mentioned by the CPW in my engine, The Machine.

First off I think the rule is a fine rule of thumb to implement in a fairly primitive engine as it is correct in a lot of positions. That being said however, there are a few positions it does not really capture the essence of a few positions. To start off, consider the following positions:

Position 1
[d]6k1/ppp2ppp/2n5/4p3/4P3/8/PPP1BPPP/6K1 b - - 0 1
Position 2
[d]6k1/ppp2ppp/2n1p3/8/8/4P3/PPP1BPPP/6K1 b - - 0 1
Position 3
[d]6k1/pp3ppp/2n1p3/8/8/4P3/PP2BPPP/6K1 b - - 0 1
Position 4
[d]6k1/p4ppp/2n1p3/8/8/4P3/P3BPPP/6K1 b - - 0 1

In my opinion these positions are increasingly promising for white. The first position is out of the bishops perspective an improved version of the structure found in the famous game Saidy - Fischer 1964 which however was extremely beneficial for the knight.

[pgn]
[Event "US Championship 1963/64"]
[Site "New York City, NY USA"]
[Date "1964.01.01"]
[EventDate "1963.??.??"]
[Round "11"]
[Result "0-1"]
[White "Anthony Saidy"]
[Black "Robert James Fischer"]
[ECO "A33"]
[WhiteElo "?"]
[BlackElo "?"]
[PlyCount "112"]

1. c4 c5 2. Nf3 Nc6 3. d4 cxd4 4. Nxd4 Nf6 5. Nc3 e6 6. Ndb5
Bb4 7. a3 Bxc3+ 8. Nxc3 d5 9. e3 O-O 10. cxd5 exd5 11. Be2 Bf5
12. Nb5 Qb6 13. O-O a6 14. Nd4 Nxd4 15. Qxd4 Qxd4 16. exd4
Rac8 17. Bd1 Bc2 18. Be3 Bxd1 19. Rfxd1 Rc2 20. Rd2 Rfc8
21. Rxc2 Rxc2 22. Rc1 Rxc1+ 23. Bxc1 Nd7 24. Kf1 Nf8 25. Ke2
Ne6 26. Kd3 h5 27. Be3 Kh7 28. f3 Kg6 29. a4 Kf5 30. Ke2 g5
31. Kf2 Nd8 32. Bd2 Kg6 33. Ke3 Ne6 34. Kd3 Kf5 35. Be3 f6
36. Ke2 Kg6 37. Kd3 f5 38. Ke2 f4 39. Bf2 Ng7 40. h3 Nf5
41. Kd3 g4 42. hxg4 hxg4 43. fxg4 Nh6 44. Be1 Nxg4 45. Bd2 Kf5
46. Be1 Nf6 47. Bh4 Ne4 48. Be1 Kg4 49. Ke2 Ng3+ 50. Kd3 Nf5
51. Bf2 Nh4 52. a5 Nxg2 53. Kc3 Kf3 54. Bg1 Ke2 55. Bh2 f3
56. Bg3 Ne3 0-1
[/pgn]

The second position is a further clear improvement for the bishop as the pawns are more elastic and the knight has more trouble finding useful squares. In order to describe that the structural benefit the knight has in the first diagram compared to the second I added an evaluation heuristic in The Machine which gives a bonus to the knight for each pawn blocked by a pawn of the opposite color.

From The Machine's source code:

Code: Select all

#ifdef BLOCKED_PAWN_KNIGHT_BONUS
	value = popCount(north(wPawns) & bPawns) * (Board::pieceCount[WHITE][KNIGHT] - Board::pieceCount[BLACK][KNIGHT]);
	scoreOp += knightBlockedPawnBonus[OPENING] * value;
	scoreEnd += knightBlockedPawnBonus[ENDING] * value;
#endif
The structural improvement for the bishop over the in the following positions is indeed accurately described by CPW's "decreasing value as pawns disappear" however disaster strikes when we analyse the following diagram

[d]6k1/5ppp/2n1p3/8/8/4P3/4BPPP/6K1 b - - 0 1
According to CPW's aforementioned diagram this is a further improvement for the bishop, however this is not the case! With the pawns all on the same side of the board the bishop has absolutely no benefit over the knight. So what is going on? Well simply counting pawns is usually right since as more pawns are found on the board, more pawns blocking one another are likely to exist. Furthermore the more pawns exist the more likely a knight is to be doing it's part in the struggle for victory. The problem the knight has is if a games fight can quickly change sides of the board from one move to another the knight has trouble keeping up. The further apart pawns get the more likely it is for the knight to be overwhelmed compared to the bishop. With less pawns on the board, it becomes less likely that the pawns are together and less likely the knight will be doing something useful.

This is where my next evaluation term comes into play: Pawn Gap Penalty for Knights. What I do is that I calculate the fileset of all the pawns on the board. Then I calculate the greatest consecutive number of non set bits between two set bits. Though you could give a different penalty for each value this returns I simply multiply this number by a flat penalty for simplicity and then add this penalty for each knight on the board. Originally I considered calculating the pawn gaps for white and or black pawns separately and adding them on top of the penalty I just described but I haven't tried it out and was worried about orthogonality issues. My implementation is further sped up by calculating pawn gap values in advance at program initialization and entering them into an array which may be referenced by the fileset byte.

From my source:

Code: Select all

#ifdef PAWN_GAP_KNIGHT_PENALTY
	value = (Magic::getPawnGap(bPawnsSouthFill | wPawnsSouthFill)) * (Board::pieceCount[WHITE][KNIGHT] - Board::pieceCount[BLACK][KNIGHT]);
	scoreOp += knightGapPenalty[OPENING] * value;
	scoreEnd += knightGapPenalty[ENDING] * value;
#endif

Code: Select all

unsigned char getPawnGap(const unsigned char fileSet) {
	return pawnGap[fileSet];
}

Code: Select all

void generatePawnGap() {
	for (unsigned int i = 0; i < 256; i++) {
		unsigned char c = 1;
		char max = -1, current = 0;
		while (c) {
			if ((c & ~i)) {if (max != -1) current++;}
			else {
				if (current > max) max = current;
				current = 0;
			}
			c <<= 1;
		}
		if (max == -1) max = 0;
		pawnGap[i] = max;
	}
}
I realize I might not have described my thoughts and ideas very well. If you have any questions and/or feedback feel free to reply!

Finally one last useful position to think about or possibly discuss:
[d]8/pp2k3/4n3/8/8/4B3/3K2PP/8 b - - 0 1
Obviously according to pawn gaps this should be a troublesome position for the knight. Though with how reduced material is, I imagine black holds.
Lyudmil Tsvetkov
Posts: 6052
Joined: Tue Jun 12, 2012 12:41 pm

Re: Knight Evaluation

Post by Lyudmil Tsvetkov »

Interesting thing you post, Jonathan.

Unfortunately, I am not a programmer.

If you ask me, you do not need any knight eval: when you do the bishop eval, and the knights are also done, as the factors that benefit the bishops hamper the knights, and vice-versa.

Just my quick remarks.

the knight is favoured by:

- larger number of pawns
- larger number of blocked pawns, even more important, especially central blocked pawns
- symmetrical pawn structure, i.e. all or most pawns for one colour are opposed
- play/pawns just on one side of the board, king or queen side

the bishop is favoured by:

- smaller number of pawns
- smaller number of blocked pawns, especially when there are no blocked central pawns on squares the colour of the bishop
- asymmetrical pawn structure in terms of file placements, i.e. when there is a large number of unooposed pawns
- play/pawns present on both sides of the board, queen and king side

There are many other things, but those are the most essential.

So, as you see, the above rules are mutually exclusive.
When you do a good bishop evaluation, you have already done a good knight eval.

Sorry about not being able to provide any programming feedback.
jorose
Posts: 373
Joined: Thu Jan 22, 2015 3:21 pm
Location: Zurich, Switzerland
Full name: Jonathan Rosenthal

Re: Knight Evaluation

Post by jorose »

Hi Lyudmil!

Your evaluation terms seem to mostly be similar to mine (obviously there are many more terms one could and should consider) so I won't comment on those.

I think it's an interesting thought that you could avoid a complex evaluation function for knights by evaluating bishops more precisely. While I could see this working in the most common imbalancing scenarios (knight vs bishop) it becomes more difficult once you consider imbalances such as knight vs 3 pawns or knight vs rook.

Thanks again for your feedback!
Lyudmil Tsvetkov
Posts: 6052
Joined: Tue Jun 12, 2012 12:41 pm

Re: Knight Evaluation

Post by Lyudmil Tsvetkov »

jorose wrote:Hi Lyudmil!

Your evaluation terms seem to mostly be similar to mine (obviously there are many more terms one could and should consider) so I won't comment on those.

I think it's an interesting thought that you could avoid a complex evaluation function for knights by evaluating bishops more precisely. While I could see this working in the most common imbalancing scenarios (knight vs bishop) it becomes more difficult once you consider imbalances such as knight vs 3 pawns or knight vs rook.

Thanks again for your feedback!
In knight vs 3 pawns, the knight is stronger all the way until non-pawn material is bigger than 2/3 of total non-pawn material, then the 3 pawns get stronger.

In R vs N, the knight plus 2 pawns are stronger than the R until tnpm reaches the limit between mg and eg, then the R gets stronger than N+2 pawns.
bob
Posts: 20943
Joined: Mon Feb 27, 2006 7:30 pm
Location: Birmingham, AL

Re: Knight Evaluation

Post by bob »

Some of this seems a bit mangled. First, the biggest advantage the bishop has occurs when pawns are on both wings of the board. This is due to the bishops ability to cover both sides of the board simultaneously while a knight can not. This has been mentioned in most every chess book I own, somewhere along the way.

I also don't think blocked pawns should figure in to a piece's evaluation, except in the case of a bishop where own-pawns blocked and held on squares with the same color of the bishop can be problematic since they reduce the bishop's ability to move freely.

In your last example, I'd expect most programs to not think the bishop is any better than the knight overall since the king and knight can defend the same side of the board pretty handily. And I would expect most programs to know that trading pawns to eliminate pawns on one of the wings is a drawish plan, a pretty good plan if you have a knight, not so good if you have a bishop against a knight. I have always created this "file set" bit board, and used the difference between the leftmost and rightmost bit as the bonus. The bigger the better if you are trying to win (positionally ahead, have a bishop, etc) or the smaller the better if you are trying to draw/not lose.
Daniel Anulliero
Posts: 773
Joined: Fri Jan 04, 2013 4:55 pm
Location: Nice

Re: Knight Evaluation

Post by Daniel Anulliero »

Interesting topic !
I'm working on my eval too, hard but very interesting !
Personally I have only mobility and N near the ennemy king (by manhattan distance , yes old stuff :wink: ) and I try to tune that with my bishop caractéristics

For the bishop I give a bonus dependant on diagonal without pawns , small bonus if 1 pawn and a penalty when pawns > 1
A bonus is added for the proximity to the ennemy king

Yes , not much knowledges , It's the begenning. .
:wink: