Opening Book Setup and Usage ---------------------------- Crafty uses two pre-compiled opening books, called "book.bin" and "books.bin". The file book.bin is usually build from a large text file containing PGN games, often taken from collections of GM games. Building book.bin is a simple exercise and requires nothing other than the raw input file you are going to use. Generally this will be either medium.zip or the set of four files large[1-4].zip, all of which are stored on the ftp machine ftp.cis.uab.edu/pub/hyatt/. To create the file book.bin, you need a PGN game collection that is a *real* PGN-compliant file. Supposing this file is called "large.pgn" you would use the following command: book create large.pgn [n] [wpct] The only thing you have to supply is , a number indicat- ing how many moves from each game are to be stored in the book.bin database. I typically use 60, which stores the first 60 moves from each game. Increasing this number slightly increases the probability that Crafty will stay in book longer, but it also increases the probability that it will follow a game too far, so that it begins to reach posi- tions where the move actually played might not be the best move, letting it fall into a bad hole. Increasing this also increases the size of the database as well. You can decrease the size of the book, and also reduce the number of ugly moves by specifying , which says that unless a move is played in at least N games, the move is discarded. This will substantially decrease the size of the book.bin file, and also eliminate single game moves that often have significant errors or blunders. You can increase the quality of book lines by specifying which is the "winning percentage". This is specified as a percentage of lost games, and is used to discard moves that led to mostly losses. A safe value is 50, which says that if a particular opening move didn't win at least 50% as many games as it lost, the move is culled. A value of 100 would mean that moves are culled if they produced more losses than wins, and is really a strict criterion. After creating book.bin, you need to create books.bin. This is a small version of book.bin, which is intended to give you more control over the moves/openings Crafty will play. This is usually built from the file start.pgn on the ftp machine, but you can modify this file to suit your taste easily. To build books.bin, you use the following command: books create start.pgn 60 Again, 60 is what I use, but none of my start.pgn lines go anywhere near that many moves. The main point here is that in start.pgn, you can append a "!" to any move you want, and when it is Crafty's turn to move for that color, it will play from the set of moves with "!" if there are any, ignor- ing the rest of the book moves. If you only want it to play 1. e4 as white, you would just enter the short game: [Crafty only plays 1. e4] 1. e4! and you are finished!. You can enter as many as you want. If on the other hand there is a move you don't want Crafty to play, then follow that move with a "?" and it will never play it. Moves in books.bin that are not flagged with ! or ? don't have any influence on Crafty's choice at all. Here's how the files are used. When searching a position, Crafty first enumerates the set of moves it can find in the opening database. It then does the same for the books.bin database, and performs a "merge" operation to combine the ? and ! flags... The purpose of the books.bin file is to give you a small database that you can easily modify, rebuild, and repeat this process over and over. Since it takes a fraction of a second to completely rebuild this file, it is very easy to modify this to control what Crafty will play, without having to rebuild the large database. One important characteristic of the PGN input is the Result tag must be specified in most of the lines, because Crafty counts wins, losses and draws for each book move and uses these counts with some of the book selection options given below. How the flags are used. The ! and ? flags should only appear in the small books.bin file, although there is no reason why they can not appear in the large file as well. For this discussion, it doesn't matter since Crafty takes the moves from both files and "merges" the flags/etc into one entry for each move. Quite simply, if any book legal book move has a ! flag, then Crafty will only play moves from the set of moves which all have the ! flag. If a move has a ? flag, then that move is immediately removed from the set of possible book moves. If the only legal book move has a ? flag, it will not be played as a book move and Crafty will simply pretend that it found no book moves and will execute a normal tree search. Pretty simple. How to control the frequency of opening move selection. A new feature in version 15.15 and beyond allows you to append a PGN comment to any move in a text file used to cre- ate books.bin, of the form {play nn%}. This will force the move it follows to be played that percentage of the time, regardless of the normal book-ordering values based on fre- quency and so forth. Note that {play 0%} will not prevent a move from being played at all, as this will look just like a move with no percent specified. To avoid playing a move, use the ? flag. How does Crafty choose book moves? Crafty's book selection algorithm depends on two user- defined values that can be set at any time during a game: book random book width The selection algorithm first finds the set of legal book moves as above. This set will either be all ! moves, or will have no ! moves in it. This set is then sorted based on the setting of book random. Here's the options: book random 0. This is a special case for book selection. Crafty simply takes the set of book moves, and searches only these moves using a normal alpha/beta search, but with a shorter than usual time limit. It then plays the move that produces the best search value. This has one serious disad- vantage in that there is no randomness to this at all. It will always play the same move in the same position, unless the evaluation is modified, or the time per move is differ- ent enough to let the search find a different move from the book move set. book random 1. This enables a random form of book move selection, but you have a lot of control over how moves are randomly chosen. The moves are ordered, based on 4 parameters: frequency of play, win/lose ratio, static eval- uation and learned results. Normally these are factored into the value used to sort the moves, based on default set- tings that you can modify by using the command "bookw option N" "option" should be "freq", "ratio", "eval", "learn", or "CAP". N should be any real number between 0 and 1. Crafty finds the min and max values for each of the 4 param- eters, and then maps this into the range 0-1000 for each parameter. Each parameter is multiplied by the correspond- ing "weight" you have assigned, and this is used as a value to sort the moves from low to high. Note that the first sort value is always the "play percent" to move them to the top of the list. For moves with equal "play percent" val- ues, the normal sort-value is used as the second-level sort variable (if no moves have a play-percent, then this second- level variable is the only one used, of course.) Once Crafty has sorted the moves as given above, it next excludes any book moves which have 0 wins. This culls the odd lines where a player chose a bad line and lost quickly. With zero wins, it will never be chosen, although Crafty will happily follow it from the other side. :) This is not anywhere near perfect, however, because an opening could have 1 win and 19 losses and that still would stay in the list. If a move has a learned value of > 100, this move is ele- vated in priority to that of a ! move, since it appears to win material instantly. If a value is < -100, it is given a ? since it appears to be a lemon. After this, the setting for "book width " is used to keep the first moves in the list, after the above culling has been completed. The smaller you make the less random- ness you get, but the chance of Crafty popping out a really bizarre book move gets smaller as well. After sorting, the final step is to fold in any mandatory "play percent" values. What this step does is that it finds all the moves in the "playable move set" just computed, which have no percent-to-play value set. It sums the sort- values for these moves, then adjusts the sort-values for the other moves so that their percentages will be honored. Once this has been done, crafty simply uses the "sort value" for each move to compute a total for all moves. It then generates a random number between 1 and this limit, and chooses the move that this probability distribution matches. This will certainly evolve over time as new ideas are devel- oped. For my play on ICC, I use book random 1, and book width 5 normally, although for titled players this is reduced to book width 3. For computers, I reduce this further to 2, to try to play reasonable openings and cull the gambits and things that don't work out. How does book learning work and how can I share learned results? 1. *all* of crafty's "learned knowledge" is in the book.bin file. It keeps the learned value and learned count right in the book file for speed. You can't modify it, although "show book" will make crafty display all the book stuff before it makes a move. 2. the book.lrn file has two purposes: (a) to serve as a log for your prying eyes, so you can see what it's learned, against whom, and what the score was that got its attention in the first place. The values on the end of each book line, inside the {} characters are as follows: {value, depth, rating_difference} value is the value of the "key" search that comes from the first 10 moves out of book. it's in centipawns, and + is good - is bad. depth is the depth the search reached at this "key" position, the deeper the search, the more the value is "trusted." rat- ing_difference is crafty's rating - opponent's rating a neg- ative value means pay more attention to the score since the opponent is better than crafty, a positive value means to take the score with a grain of salt, because the opponent was weaker than Crafty. You can delete this file at any time, and it has no effect on learning. As I mentioned, the learning takes place in book.bin... this is mainly for you to peek at if you are interested. However, this is the "portable learning data file" also, and can be given to others to import into their crafty, where it will affect the opening book just like their crafty had played the openings and got the same scores. There are two ways to use such "lrn" files: 1. "import " will read and import the knowledge therein into your book.bin. Since I use the same learning code as is used when playing games, this informa- tion also gets appended to *your* book.lrn file as well, so that your book.lrn always reflects *everything* your program has learned, so long as you don't ever remove this file. It would be a mistake to use this command on your own book.lrn file, because the things would get counted twice, which might or might not be a good thing. 2. "import clear" will read is no longer around) which will import either book.lrn type data or position.lrn type data and can tell them apart without your having to do anything. The option is still there, should you want to use it, and simply removes the position.lrn and position.bin files before starting the import process for position learn- ing. This can be turned off, if you like, by checking out the "learn" command, which gives you the ability to turn off book learning (as it presently works), position learning, and the next book learning stage which will add to the book in addition to learning which book lines are good and bad. What is this new "result" learning? Result learning works just like normal book learning, except that if Crafty is checkmated or resigns, it will step back through the book line to find the last point where it had more than one move to choose from. It will flag the move it chose as "never play again". This handles the case where the first ten non-book moves produce reasonable scores, but the position is one that Crafty simply can't handle very well. If it loses such a game, it will still vary the next time this opening is played, as otherwise it would possibly repeat the same open- ing, and would certainly repeat the remainder of the game. All three learning modes are turned on by default, although any of them can be disabled with the appropriate command option to "learn".