# 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. – edwina oliver Jan 22 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.

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, v. g., whether the queen is attacked, or if a check mate 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 of 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 optimizing 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.

This is another simple chess program for beginners; Tom's Simple Chess Program. But take into account this one is not free software.

In github you can find many chess engines in several languages.

Finally, as far as I know, the place to go for chess programming questions is http://talkchess.com.

• Okay, so would a very simple approach be to assign a random value to each of the pieces. Then, calculate all of the possible moves given the current state of the board (only 1 move deep) and calculate the score of the board given each of these changes. Then just make the move with the most favourable score? Then the genetic algorithm can try and optimise these values? – F J Nov 10 '19 at 17:46
• I know nothing about genetic algorithms, but if I understand correctly, on this approach you won't get a "best move" if in the position there are no possible captures (like for example happens in the initial position). – emdio Nov 10 '19 at 18:08
• From the descriptions of Lc0, it sounded like pieces being threatened or in a poor location was taken into account. – Riking Nov 11 '19 at 2:29

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.