# Chess evaluation function

I am trying to write a program to learn how to play chess. I have heard of programs that you can enter in a set of weights for certain parameters such as rook value, queen value and bishop mobility. And then, the program will output what move it thinks you should make given the state of the board, based on these weights.

I will then use a genetic algorithm to find some good weights.

Could anyone explain to me how this evaluation function actually works? How does it go from the weights to actually deciding what move it should make?

Or could someone at least link me to such a program?

• There is a lot more to evaluating the position than just material weights. AND the material weights change depending on the actual position. Sometimes bishops are worth more, sometimes a horsie is worth more. Sometimes a horsie is worth more than a rook. You need a lot more than evaluating material to play well with a program. Jan 22 '20 at 18:21

Maybe there's a simple program out there for didactic purposes, I don't know. But if not, you could have a look at Stockfish, which is open-source, and is a serious, competitive chess engine. You can find the sourcecode on Github.

Also, I bet you could learn more about evaluation functions and find some other pointers by browsing the Chess Programming Wiki.

I think this: https://www.chessprogramming.org/Sunfish is definitely the easiest Engine to learn from ...Written in Python in like 100 lines and plays strong enough to get the concept. I am in the same boat as you and this helped me a ton. Good luck.

The evaluation function of a chess programs returns a score for a given chess position in a static way. By "static" I mean it won't have into account, e. g., whether the queen is attacked, or if a checkmate is about to happen (that part is done within the search function of the program).

Most eval functions are based on the value of a single pawn. If you take a look at Seconchess' source code you'll find stuff like this:

``````#define VALUE_PAWN 100
#define VALUE_KNIGHT 310
#define VALUE_BISHOP 320
#define VALUE_ROOK 500
#define VALUE_QUEEN 900
``````

So for a given position, the eval function counts how many pawns, knights, etc. there are on one side (black or white) and it just sums them up. Then it makes the same for the other side and the final score is the difference between white and black scores.

Secondchess is a very simple chess engine, and so is its eval function. It only takes into account the material on the board and adds an extra bonus/malus for the position of each piece on the board. For example, a knight in d4 has a bonus of +15, and a knight in h1 has a "malus" of -40. (This info is stored in the array `pst_knight[64]`).

As you can imagine, serious chess engines have hundreds (thousands?) of these values; for doubled pawns, for rooks on open columns, pieces mobility, king safety... And it also take into account the phase of the game; opening, middle game, endgame. And yes, tuning these values is such an important and difficult task. Some techniques exist for optimising these values, being CLOP one of them.

About the question "How does it go from the weights to actually deciding what move it should make?". Ok, from the weights, you get the score of a single position, and using a search method (usually alpha-beta is the way to go) you find out the best move from a certain position.