Chess engine code – the evaluation function

This article is part of a series – the contents page is here.

Throughout this series we’ve referred to the evaluation function and in the last article we saw how the minimax algorithm calls it.  Now, at last, we can reveal it.

You might be surprised by how simple it is.  Or maybe not.  It probably depends on how good you are at chess, and whether you’re a glass-half-empty or glass-half-full person.  Personally, I’m quite intrigued by how such a naïve function can sometimes pull off moves that aren’t half bad.  But yes, I have to concede that sometimes it does some pretty dumb things (although it can be very hard to tell how much of that is due to the over-simplification of the evaluation versus the horizon effect).

The dirty secret

So here it is: the dirty secret at the heart of this engine.  The evaluation function’s score comes from just two factors:

  • A simple count of material, counting pawns as 100, queens as 900, etc.
  • A weighted sum of attacked squares.  Each player gets a few points for each square that they attack.

Attacked square weightings

In evaluating the attack scores, both squares and pieces are weighted.  For example, the squares e4 and e5 have a much higher weighting than a1 and h1.  The square weightings for Black are reversed (so Black’s score for f6 is the same as White’s score for f3).

The attacking pieces are also weighted, but in the reverse of the usual order.  So a pawn on e4 attacking d5 gets a higher score than a queen attacking d5.

That’s it?!

Yep.  To the extent that Shallow Thought plays half-way-decent moves, most of the credit goes to the computer’s thoroughness.  Unlike many humans, the machine diligently evaluates even the most obscure lines and doesn’t suffer from what magicians call isolation of interest.  When playing a beginner, this methodical rigour tends to compensate for its mediocre understanding of strategy.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s