Answers.Body: <p>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 <a href="https://github.com/official-stockfish/Stockfish" rel="nofollow noreferrer">sourcecode on Github</a>.</p>
  
  <p>Also, I bet you could learn more about evaluation functions and find some other pointers by browsing the <a href="https://www.chessprogramming.org/Main_Page" rel="nofollow noreferrer">Chess Programming Wiki</a>.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-11-10T15:25:57.357Z
Answers.LastActivityDate: 2019-11-10T15:25:57.357Z
Answers.UserDisplayName: itub
Answers.Body: <p>I think this: <a href="https://www.chessprogramming.org/Sunfish" rel="nofollow noreferrer">https://www.chessprogramming.org/Sunfish</a> 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.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-11-11T05:45:16.417Z
Answers.LastActivityDate: 2019-11-11T05:45:16.417Z
Answers.UserDisplayName: GMovesChess
Answers.Body: <p>The evaluation function of a chess programs returns a score for a given chess position in a static way. By &quot;static&quot; 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).</p>
  <p>Most eval functions are based on the value of a single pawn. If you take a look at <a href="https://github.com/emdio/secondchess/blob/master/secondchess.c" rel="nofollow noreferrer">Seconchess' source code</a> you'll find stuff like this:</p>
  <pre><code>#define VALUE_PAWN 100
  #define VALUE_KNIGHT 310
  #define VALUE_BISHOP 320
  #define VALUE_ROOK 500
  #define VALUE_QUEEN 900
  </code></pre>
  <p>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.</p>
  <p>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 &quot;malus&quot; of -40. (This info is stored in the array <code>pst_knight[64]</code>).</p>
  <p>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 <a href="https://www.chessprogramming.org/CLOP" rel="nofollow noreferrer">CLOP</a> one of them.</p>
  <p>About the question &quot;How does it go from the weights to actually deciding what move it should make?&quot;. Ok, from the weights, you get the score of a single position, and using a search method (usually <a href="https://www.chessprogramming.org/Alpha-Beta" rel="nofollow noreferrer">alpha-beta</a> is the way to go) you find out the best move from a certain position.</p>
  <h1 id="see-also-omjr">See also</h1>
  <p>This is another simple chess program for beginners; <a href="http://www.tckerrigan.com/Chess/TSCP/" rel="nofollow noreferrer">Tom's Simple Chess Program</a>. But take into account this one is not free software. In <a href="https://github.com/" rel="nofollow noreferrer">GitHub</a> you can find many chess engines in several languages.</p>
  <p>Finally, as far as I know, the place to go for chess programming questions is <a href="http://talkchess.com" rel="nofollow noreferrer">http://talkchess.com</a>.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-11-10T17:46:32.713Z
Answers.Text: 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?
Answers.UserDisplayName: F J
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-11-10T18:08:10.167Z
Answers.Text: 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).
Answers.UserDisplayName: emdio
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-11-11T02:29:53.037Z
Answers.Text: From the descriptions of Lc0, it sounded like pieces being threatened or in a poor location was taken into account.
Answers.UserDisplayName: Riking
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-11-10T16:53:04.640Z
Answers.LastActivityDate: 2021-07-26T14:11:09.623Z
Answers.LastEditDate: 2021-07-26T14:11:09.623Z
Answers.UserDisplayName: emdio
Body: <p>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.</p>
  
  <p>I will then use a genetic algorithm to find some good weights.</p>
  
  <p>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?</p>
  
  <p>Or could someone at least link me to such a program?</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-01-22T18:21:00.597Z
Comments.Text: 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.
Comments.UserDisplayName: edwina oliver
Tags: <evaluation>
Title: Chess evaluation function
ThreadText: # Chess evaluation function
  
  (Asked by: username_0 on 2019-11-10)
  
  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?
  
  **Tags:** evaluation
  
  > **Comments:**
  > **username_1**: 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.  
  
  ---
  
  ## 3 answers
  
  ### Answer by username_2 on 2019-11-10. Score: 3
  
  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](https://github.com/official-stockfish/Stockfish).
  
  Also, I bet you could learn more about evaluation functions and find some other pointers by browsing the [Chess Programming Wiki](https://www.chessprogramming.org/Main_Page).
  
  
  ---
  
  ### Answer by username_3 on 2019-11-11. Score: 3
  
  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.
  
  
  ---
  
  ### Answer by username_4 on 2019-11-10. Score: 2
  
  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.
  See also
  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.
  
  > **Comments:**
  > **username_0**: 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?  
  > **username_4**: 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).  
  > **username_5**: From the descriptions of Lc0, it sounded like pieces being threatened or in a poor location was taken into account.  
  
  ---
  
  