resources on how to write an eval function?

Discussion of chess software programming and technical issues.

Moderators: hgm, Rebel, chrisw

cyberfish

resources on how to write an eval function?

Post by cyberfish »

I am getting stuck on the evaluation function part of my chess engine, because I have extremely limited chess knowledge myself. I have read some articles online that suggest what to include in evaluation functions, but it is the actual numerical values that I can't seem to figure out, and I don't know how much to weigh different factors, too. With its current material + piece square eval func, it's playing at ~2000 std on FICS.

Does anyone have links to good (specific) resources on the eval function?

Thanks
wgarvin
Posts: 838
Joined: Thu Jul 05, 2007 5:03 pm
Location: British Columbia, Canada

Re: resources on how to write an eval function?

Post by wgarvin »

The best resource for getting evaluation ideas might be the source code of other engines. This page about how Rebel plays chess has some good ideas, and there's probably others out there.

As far as tuning the weights for each term of the evaluation function, I think that is something of a black art. Again you could get ideas of the approximate weight things should have by reading the source code of other engines, or ask people around here about specific ones. Then you just have to tweak them and test, and tweak them and test, over and over, to find the ones that work best for your program. (Its kind of amazing to me how much "personality" chess engines have in that way).
FrancoisK
Posts: 80
Joined: Tue Jul 18, 2006 10:46 pm

Re: resources on how to write an eval function?

Post by FrancoisK »

A very interesting post by Tord (the rest of the discussion is worth reading too) : http://64.68.157.89/forum/viewtopic.php ... 33&t=15504

François
cyberfish

Re: resources on how to write an eval function?

Post by cyberfish »

Thanks for the suggestions, found some great help in other engines' sources, as well as the discussion pointed to by the link.

For future references, I found TSCP's evaluation to be of great help. It has the simplicity which is exactly what I wanted (heavily commented, too). By the length of it (~400 lines including comments), I don't think it would be as accurate as something like crafty's, but I am not aiming to win the computer chess champion or anything like that, so it works for me.
PK
Posts: 893
Joined: Mon Jan 15, 2007 11:23 am
Location: Warsza

Re: resources on how to write an eval function?

Post by PK »

I played thousands of test games against TSCP, and found out that its values are not optimal even for the simple scheme it uses. This program tends to sac a minor piece for two pawns where it's unnecessary. Out of curiosity, I tried to modify its piece values, and found slightly better set:

pawn=100 knight=320 bishop=330 rook = 500 queen = 925

This in turn sometimes too readily gets an exchange down, so rook and queen values also can be slightly increased, but it would take much more games to verify if it's better than quick-and-dirty first try.

BTW if You wish, I might try to write an evaluation function for Your program. Mine, called Hopeless, is of Chesswar "E" strength, despite being outsearched by just about any opponent. So it means that if it has anything good inside, it's eval.

regards,

Pawel Koziol
mjlef
Posts: 1494
Joined: Thu Mar 30, 2006 2:08 pm

Re: resources on how to write an eval function?

Post by mjlef »

cyberfish wrote:Thanks for the suggestions, found some great help in other engines' sources, as well as the discussion pointed to by the link.

For future references, I found TSCP's evaluation to be of great help. It has the simplicity which is exactly what I wanted (heavily commented, too). By the length of it (~400 lines including comments), I don't think it would be as accurate as something like crafty's, but I am not aiming to win the computer chess champion or anything like that, so it works for me.
One thing I do is use fixed depth searches to tune eval functions. Try not to change many eval terms at a time. This is what I do:

Find a good opponent, say Fruit 2.1.
Do a match (use something like one of the Nunn set for starting positions, auto color switch) with your program playing fixed X depth versus fixed Y depth for the opponent. Repeat until you find a good X for your program where its score is close to 50%. For example, on a new program, you might need to play say depth 6 searches against Fruit's depth 3.
Record your win percentage and ELO difference as a base.
Change a very few eval parameters
repeat the match. If the changes help, then keep them, otherwise try something else.

If you keep the depth small, you can do a few hundred games in an hour, and try several things each day. Always note what you try in case you ever want to revert back to some values in the future. You can get eval term ideas from chess strategy books and from technical papers of how other programs work.

Having your oppponent open source gives you something else you can try. Once you find something that you think improves your program, if the other program has a similar term, you can change your opponent, then try playing that new opponent program against its original version.

It is always better to test against multiple opponents. And keep in mind you need hundred or even thousands of games to test an eval term that only helps a little.

This scheme does not help if you are making search related changes (extensions, reductions or eval terms that might change the search). But for basic eval terms it seems to work most of the time.

Mark
User avatar
Bill Rogers
Posts: 3562
Joined: Thu Mar 09, 2006 3:54 am
Location: San Jose, California

Re: resources on how to write an eval function?

Post by Bill Rogers »

Marks ideas are very good. What I do though is to set the value of a pawn first and then adjust the other pieces accordingly. I set my pawns to worth 100 points as this makes it easier to give miscellaneous points to other features with out going to decimals. Decimals don't add as fast as whole numbers. After doing that then work on one other eval feature at a time as making to many changes can confuse the issue and make it harder to see which one gives you a positive point flow. As a second item I would suggest piece/square tables for the different pieces. Start with one at first then add others as you go alone. Remember that each new feature can have a drastic effect on all the other features in your eval.
Bill
cyberfish

Re: resources on how to write an eval function?

Post by cyberfish »

BTW if You wish, I might try to write an evaluation function for Your program. Mine, called Hopeless, is of Chesswar "E" strength, despite being outsearched by just about any opponent. So it means that if it has anything good inside, it's eval.
That would really be great. My engine (Brainless), as of now, has a half-decent search (alpha-beta, quie, ID, non-recursive nullmove, TT, history heuristic, simple SEE, selective extension on checks) but the eval is really brain dead. I cannot vouch for the bug-free-ness of it, but I have watched it playing quite a few games on FICS (under the handle BrainlessChess, 2112 std, 1832 blitz) and I don't see anything wrong with it (I don't usually play against it, perhaps it's because of my 1200 rating...). It has not played any tournaments yet, though I have registered it for WBEC. I use rotated bitboards (many thanks to Dr. Hyatt) for move generation but also keep a 8x8 mailbox for other purposes. One problem is though, is that the code is not pretty by any standard =) (never knew that someone but me would see it). Please let me know if you are interested.

I am more of a computer science guy than a chess guy, so I pay more attention to the search than to the eval.
cyberfish

Re: resources on how to write an eval function?

Post by cyberfish »

Thanks Mark for your suggestion, I will certainly try it sometime. Sounds like a good idea, but I am not sure if my nearly non-existent chess knowledge will serve me well...
cyberfish

Re: resources on how to write an eval function?

Post by cyberfish »

Marks ideas are very good. What I do though is to set the value of a pawn first and then adjust the other pieces accordingly. I set my pawns to worth 100 points as this makes it easier to give miscellaneous points to other features with out going to decimals. Decimals don't add as fast as whole numbers. After doing that then work on one other eval feature at a time as making to many changes can confuse the issue and make it harder to see which one gives you a positive point flow. As a second item I would suggest piece/square tables for the different pieces. Start with one at first then add others as you go alone. Remember that each new feature can have a drastic effect on all the other features in your eval.
Thanks for the reply. That is essentially where I am at right now, although I am sure the pcsq tables are nowhere near optimal.