| 1 |
Opening Book Setup and Usage |
| 2 |
---------------------------- |
| 3 |
|
| 4 |
Crafty uses two pre-compiled opening books, called |
| 5 |
"book.bin" and "books.bin". |
| 6 |
|
| 7 |
The file book.bin is usually build from a large text file |
| 8 |
containing PGN games, often taken from collections of GM |
| 9 |
games. Building book.bin is a simple exercise and requires |
| 10 |
nothing other than the raw input file you are going to use. |
| 11 |
Generally this will be either medium.zip or the set of four |
| 12 |
files large[1-4].zip, all of which are stored on the ftp |
| 13 |
machine ftp.cis.uab.edu/pub/hyatt/. |
| 14 |
|
| 15 |
To create the file book.bin, you need a PGN game collection |
| 16 |
that is a *real* PGN-compliant file. Supposing this file is |
| 17 |
called "large.pgn" you would use the following command: |
| 18 |
|
| 19 |
book create large.pgn <m> [n] [wpct] |
| 20 |
|
| 21 |
The only thing you have to supply is <m>, a number indicat- |
| 22 |
ing how many moves from each game are to be stored in the |
| 23 |
book.bin database. I typically use 60, which stores the |
| 24 |
first 60 moves from each game. Increasing this number |
| 25 |
slightly increases the probability that Crafty will stay in |
| 26 |
book longer, but it also increases the probability that it |
| 27 |
will follow a game too far, so that it begins to reach posi- |
| 28 |
tions where the move actually played might not be the best |
| 29 |
move, letting it fall into a bad hole. Increasing this also |
| 30 |
increases the size of the database as well. |
| 31 |
|
| 32 |
You can decrease the size of the book, and also reduce the |
| 33 |
number of ugly moves by specifying <n>, which says that |
| 34 |
unless a move is played in at least N games, the move is |
| 35 |
discarded. This will substantially decrease the size of the |
| 36 |
book.bin file, and also eliminate single game moves that |
| 37 |
often have significant errors or blunders. |
| 38 |
|
| 39 |
You can increase the quality of book lines by specifying |
| 40 |
<wpct> which is the "winning percentage". This is specified |
| 41 |
as a percentage of lost games, and is used to discard moves |
| 42 |
that led to mostly losses. A safe value is 50, which says |
| 43 |
that if a particular opening move didn't win at least 50% as |
| 44 |
many games as it lost, the move is culled. A value of 100 |
| 45 |
would mean that moves are culled if they produced more |
| 46 |
losses than wins, and is really a strict criterion. |
| 47 |
|
| 48 |
After creating book.bin, you need to create books.bin. This |
| 49 |
is a small version of book.bin, which is intended to give |
| 50 |
you more control over the moves/openings Crafty will play. |
| 51 |
This is usually built from the file start.pgn on the ftp |
| 52 |
machine, but you can modify this file to suit your taste |
| 53 |
easily. To build books.bin, you use the following command: |
| 54 |
|
| 55 |
books create start.pgn 60 |
| 56 |
|
| 57 |
Again, 60 is what I use, but none of my start.pgn lines go |
| 58 |
anywhere near that many moves. The main point here is that |
| 59 |
in start.pgn, you can append a "!" to any move you want, and |
| 60 |
when it is Crafty's turn to move for that color, it will |
| 61 |
play from the set of moves with "!" if there are any, ignor- |
| 62 |
ing the rest of the book moves. If you only want it to play |
| 63 |
1. e4 as white, you would just enter the short game: |
| 64 |
|
| 65 |
[Crafty only plays 1. e4] 1. e4! |
| 66 |
|
| 67 |
and you are finished!. You can enter as many as you want. |
| 68 |
If on the other hand there is a move you don't want Crafty |
| 69 |
to play, then follow that move with a "?" and it will never |
| 70 |
play it. Moves in books.bin that are not flagged with ! or |
| 71 |
? don't have any influence on Crafty's choice at all. |
| 72 |
|
| 73 |
Here's how the files are used. When searching a position, |
| 74 |
Crafty first enumerates the set of moves it can find in the |
| 75 |
opening database. It then does the same for the books.bin |
| 76 |
database, and performs a "merge" operation to combine the ? |
| 77 |
and ! flags... The purpose of the books.bin file is to give |
| 78 |
you a small database that you can easily modify, rebuild, |
| 79 |
and repeat this process over and over. Since it takes a |
| 80 |
fraction of a second to completely rebuild this file, it is |
| 81 |
very easy to modify this to control what Crafty will play, |
| 82 |
without having to rebuild the large database. |
| 83 |
|
| 84 |
One important characteristic of the PGN input is the Result |
| 85 |
tag must be specified in most of the lines, because Crafty |
| 86 |
counts wins, losses and draws for each book move and uses |
| 87 |
these counts with some of the book selection options given |
| 88 |
below. |
| 89 |
|
| 90 |
How the flags are used. |
| 91 |
|
| 92 |
The ! and ? flags should only appear in the small books.bin |
| 93 |
file, although there is no reason why they can not appear in |
| 94 |
the large file as well. For this discussion, it doesn't |
| 95 |
matter since Crafty takes the moves from both files and |
| 96 |
"merges" the flags/etc into one entry for each move. |
| 97 |
|
| 98 |
Quite simply, if any book legal book move has a ! flag, then |
| 99 |
Crafty will only play moves from the set of moves which all |
| 100 |
have the ! flag. If a move has a ? flag, then that move is |
| 101 |
immediately removed from the set of possible book moves. If |
| 102 |
the only legal book move has a ? flag, it will not be played |
| 103 |
as a book move and Crafty will simply pretend that it found |
| 104 |
no book moves and will execute a normal tree search. Pretty |
| 105 |
simple. |
| 106 |
|
| 107 |
How to control the frequency of opening move selection. |
| 108 |
|
| 109 |
A new feature in version 15.15 and beyond allows you to |
| 110 |
append a PGN comment to any move in a text file used to cre- |
| 111 |
ate books.bin, of the form {play nn%}. This will force the |
| 112 |
move it follows to be played that percentage of the time, |
| 113 |
regardless of the normal book-ordering values based on fre- |
| 114 |
quency and so forth. |
| 115 |
|
| 116 |
Note that {play 0%} will not prevent a move from being |
| 117 |
played at all, as this will look just like a move with no |
| 118 |
percent specified. To avoid playing a move, use the ? flag. |
| 119 |
|
| 120 |
How does Crafty choose book moves? |
| 121 |
|
| 122 |
Crafty's book selection algorithm depends on two user- |
| 123 |
defined values that can be set at any time during a game: |
| 124 |
|
| 125 |
book random <n> |
| 126 |
|
| 127 |
book width <w> |
| 128 |
|
| 129 |
The selection algorithm first finds the set of legal book |
| 130 |
moves as above. This set will either be all ! moves, or |
| 131 |
will have no ! moves in it. This set is then sorted based |
| 132 |
on the setting of book random. Here's the options: |
| 133 |
|
| 134 |
book random 0. This is a special case for book selection. |
| 135 |
Crafty simply takes the set of book moves, and searches only |
| 136 |
these moves using a normal alpha/beta search, but with a |
| 137 |
shorter than usual time limit. It then plays the move that |
| 138 |
produces the best search value. This has one serious disad- |
| 139 |
vantage in that there is no randomness to this at all. It |
| 140 |
will always play the same move in the same position, unless |
| 141 |
the evaluation is modified, or the time per move is differ- |
| 142 |
ent enough to let the search find a different move from the |
| 143 |
book move set. |
| 144 |
|
| 145 |
book random 1. This enables a random form of book move |
| 146 |
selection, but you have a lot of control over how moves are |
| 147 |
randomly chosen. The moves are ordered, based on 4 |
| 148 |
parameters: frequency of play, win/lose ratio, static eval- |
| 149 |
uation and learned results. Normally these are factored |
| 150 |
into the value used to sort the moves, based on default set- |
| 151 |
tings that you can modify by using the command "bookw option |
| 152 |
N" "option" should be "freq", "ratio", "eval", "learn", or |
| 153 |
"CAP". N should be any real number between 0 and 1. |
| 154 |
|
| 155 |
Crafty finds the min and max values for each of the 4 param- |
| 156 |
eters, and then maps this into the range 0-1000 for each |
| 157 |
parameter. Each parameter is multiplied by the correspond- |
| 158 |
ing "weight" you have assigned, and this is used as a value |
| 159 |
to sort the moves from low to high. Note that the first |
| 160 |
sort value is always the "play percent" to move them to the |
| 161 |
top of the list. For moves with equal "play percent" val- |
| 162 |
ues, the normal sort-value is used as the second-level sort |
| 163 |
variable (if no moves have a play-percent, then this second- |
| 164 |
level variable is the only one used, of course.) |
| 165 |
|
| 166 |
Once Crafty has sorted the moves as given above, it next |
| 167 |
excludes any book moves which have 0 wins. This culls the |
| 168 |
odd lines where a player chose a bad line and lost quickly. |
| 169 |
With zero wins, it will never be chosen, although Crafty |
| 170 |
will happily follow it from the other side. :) This is not |
| 171 |
anywhere near perfect, however, because an opening could |
| 172 |
have 1 win and 19 losses and that still would stay in the |
| 173 |
list. |
| 174 |
|
| 175 |
If a move has a learned value of > 100, this move is ele- |
| 176 |
vated in priority to that of a ! move, since it appears to |
| 177 |
win material instantly. If a value is < -100, it is given a |
| 178 |
? since it appears to be a lemon. |
| 179 |
|
| 180 |
After this, the setting for "book width <w>" is used to keep |
| 181 |
the first <w> moves in the list, after the above culling has |
| 182 |
been completed. The smaller you make <w> the less random- |
| 183 |
ness you get, but the chance of Crafty popping out a really |
| 184 |
bizarre book move gets smaller as well. |
| 185 |
|
| 186 |
After sorting, the final step is to fold in any mandatory |
| 187 |
"play percent" values. What this step does is that it finds |
| 188 |
all the moves in the "playable move set" just computed, |
| 189 |
which have no percent-to-play value set. It sums the sort- |
| 190 |
values for these moves, then adjusts the sort-values for the |
| 191 |
other moves so that their percentages will be honored. |
| 192 |
|
| 193 |
Once this has been done, crafty simply uses the "sort value" |
| 194 |
for each move to compute a total for all moves. It then |
| 195 |
generates a random number between 1 and this limit, and |
| 196 |
chooses the move that this probability distribution matches. |
| 197 |
This will certainly evolve over time as new ideas are devel- |
| 198 |
oped. |
| 199 |
|
| 200 |
For my play on ICC, I use book random 1, and book width 5 |
| 201 |
normally, although for titled players this is reduced to |
| 202 |
book width 3. For computers, I reduce this further to 2, to |
| 203 |
try to play reasonable openings and cull the gambits and |
| 204 |
things that don't work out. |
| 205 |
|
| 206 |
How does book learning work and how can I share learned |
| 207 |
results? |
| 208 |
|
| 209 |
1. *all* of crafty's "learned knowledge" is in the book.bin |
| 210 |
file. It keeps the learned value and learned count right in |
| 211 |
the book file for speed. You can't modify it, although |
| 212 |
"show book" will make crafty display all the book stuff |
| 213 |
before it makes a move. |
| 214 |
|
| 215 |
2. the book.lrn file has two purposes: (a) to serve as a |
| 216 |
log for your prying eyes, so you can see what it's learned, |
| 217 |
against whom, and what the score was that got its attention |
| 218 |
in the first place. The values on the end of each book |
| 219 |
line, inside the {} characters are as follows: |
| 220 |
{value, depth, rating_difference} value is the value of |
| 221 |
the "key" search that comes from the first 10 moves out of |
| 222 |
book. it's in centipawns, and + is good - is bad. depth is |
| 223 |
the depth the search reached at this "key" position, the |
| 224 |
deeper the search, the more the value is "trusted." rat- |
| 225 |
ing_difference is crafty's rating - opponent's rating a neg- |
| 226 |
ative value means pay more attention to the score since the |
| 227 |
opponent is better than crafty, a positive value means to |
| 228 |
take the score with a grain of salt, because the opponent |
| 229 |
was weaker than Crafty. |
| 230 |
|
| 231 |
You can delete this file at any time, and it has no effect |
| 232 |
on learning. As I mentioned, the learning takes place in |
| 233 |
book.bin... this is mainly for you to peek at if you are |
| 234 |
interested. However, this is the "portable learning data |
| 235 |
file" also, and can be given to others to import into their |
| 236 |
crafty, where it will affect the opening book just like |
| 237 |
their crafty had played the openings and got the same |
| 238 |
scores. There are two ways to use such "lrn" files: |
| 239 |
|
| 240 |
1. "import <filename>" will read <filename> and import the |
| 241 |
knowledge therein into your book.bin. Since I use the same |
| 242 |
learning code as is used when playing games, this informa- |
| 243 |
tion also gets appended to *your* book.lrn file as well, so |
| 244 |
that your book.lrn always reflects *everything* your program |
| 245 |
has learned, so long as you don't ever remove this file. It |
| 246 |
would be a mistake to use this command on your own book.lrn |
| 247 |
file, because the things would get counted twice, which |
| 248 |
might or might not be a good thing. |
| 249 |
|
| 250 |
2. "import <filename> clear" will read <filename) and |
| 251 |
import the knowledge as above, but first clears *all* |
| 252 |
learned results from book.bin. you will want to do this if |
| 253 |
you import my book.lrn, *and*, you have contributed to my |
| 254 |
book.lrn data by sending me yours. I'll take care of elimi- |
| 255 |
nating duplicates if you screw up in what you send me, but |
| 256 |
once you send me something, you run the risk of getting it |
| 257 |
"back again" later. This is going to be a problem until |
| 258 |
everyone gets used to sharing something that is rather |
| 259 |
"vapid" like this "learned info" is... |
| 260 |
|
| 261 |
Other than that, we are now "open for business"... when the |
| 262 |
urge strikes you, email me your .lrn file, I'll keep a large |
| 263 |
master here and update it on occasion. Probably the best |
| 264 |
thing to do is to send me your .lrn and at the same |
| 265 |
*instant* delete yours. This will capture anything learned |
| 266 |
*after* you send me the file, but you'll get it all right |
| 267 |
back with the next version of book.lrn that I distribute. |
| 268 |
after getting this new book.lrn back, here's what you should |
| 269 |
do: |
| 270 |
|
| 271 |
1. rename your old book.lrn to something else. I'll call |
| 272 |
it "book.lrn.old" here. |
| 273 |
|
| 274 |
2. copy my blearn.dat to your machine, but *do not* put it |
| 275 |
in the directory with your book.bin and books.bin file |
| 276 |
because it will get confusing very quickly if you do. put |
| 277 |
it somewhere else, because you are going to remove it |
| 278 |
quickly anyway. I'll call it new.lrn for this example. |
| 279 |
|
| 280 |
3. import new.lrn clear |
| 281 |
import book.lrn.old |
| 282 |
|
| 283 |
and you are ready to rumble again. The first command clears |
| 284 |
the learned values, sucks in my new learn file and updates |
| 285 |
everything. the second command re-learns everything you've |
| 286 |
learned since you sent me the last book.lrn file. After |
| 287 |
doing this your book.lrn will have my .lrn stuff, plus your |
| 288 |
old.lrn stuff, just waiting to be sent to me again... |
| 289 |
|
| 290 |
If this is confusing, I can probably add an automatic com- |
| 291 |
mand to do all of this by renaming book.lrn, etc. Hopefully |
| 292 |
this is not too error-prone for the time being anyway... |
| 293 |
|
| 294 |
What is this new Position Learning I've heard about? |
| 295 |
|
| 296 |
Crafty now has a "permanent" hash table that is kept from |
| 297 |
game to game. A position gets into this "hash file" when |
| 298 |
Crafty executes a search and the search value is signifi- |
| 299 |
cantly lower than the last search value. |
| 300 |
|
| 301 |
When this happens, Crafty stores the current information for |
| 302 |
this position in the permanent hash file, which can hold up |
| 303 |
to 65536 positions. Once it fills up, the positions are |
| 304 |
replaced on a FIFO basic always keeping the most recent 64K |
| 305 |
entries. |
| 306 |
|
| 307 |
Each time crafty starts a search, the positions/scores from |
| 308 |
this file are stuffed into the normal transposition table, |
| 309 |
and used during the search just like any other table entry. |
| 310 |
Here's how it helps: In a game that was played, the follow- |
| 311 |
ing moves and scores were found by crafty (playing white): |
| 312 |
|
| 313 |
1. Ng5 (+.277) h6 2. Nh7 (+.321) Kg8 3. Qh5 (+.133) |
| 314 |
Qg7 4. Ng5 (-2.122) hxg5 |
| 315 |
|
| 316 |
So, the knight got trapped at h7, and at move 4 crafty dis- |
| 317 |
covered that this is gross and "learns" this result/posi- |
| 318 |
tion. |
| 319 |
|
| 320 |
We play the exact same game again: except that two things |
| 321 |
can happen here. It might be that Ng7 is the *only* square |
| 322 |
the knight can move to here, which means this whole thing is |
| 323 |
forced. the first search would find: |
| 324 |
|
| 325 |
1. Ng5 (-2.122) if the search can reach 8 plies deep, which |
| 326 |
happens even in 5 second games. It's learned that Ng5 is |
| 327 |
bad. It stores *this* position in the permanent hash file |
| 328 |
also, and the next time you try this same trap, it will dis- |
| 329 |
cover 4-5 moves earlier that if the knight gets to g5 it is |
| 330 |
in trouble. Each game will diverge from the first game 3-4 |
| 331 |
moves earlier. Simple and effective. |
| 332 |
|
| 333 |
2. Ng5 might not be forced, and if not, it knows Ng5 loses |
| 334 |
a piece for a pawn, so it will promptly play something else, |
| 335 |
which is exactly what is desired. |
| 336 |
|
| 337 |
This is implemented with two (count 'em, two) files. One |
| 338 |
file "position.bin" is a binary file that contains the hash |
| 339 |
table entries, and it right at one megabyte in size, *max*. |
| 340 |
(16 bytes per hash entry X 65536 entries = exactly one meg, |
| 341 |
but I have 8 extra bytes for the FIFO queue implementation |
| 342 |
and to see how many entries are currently in the file if it |
| 343 |
is not full. |
| 344 |
|
| 345 |
The second file is "position.lrn" and is, you guessed it, a |
| 346 |
file that can be shared with others, just like book.lrn. It |
| 347 |
contains all information needed to reconstruct the position, |
| 348 |
the score, the depth, etc. and also included the pgn tags |
| 349 |
for who was what color and when the game was played... |
| 350 |
|
| 351 |
This data can be imported with the new "import" command (the |
| 352 |
old book learn <filename> is no longer around) which will |
| 353 |
import either book.lrn type data or position.lrn type data |
| 354 |
and can tell them apart without your having to do anything. |
| 355 |
The <clear> option is still there, should you want to use |
| 356 |
it, and simply removes the position.lrn and position.bin |
| 357 |
files before starting the import process for position learn- |
| 358 |
ing. |
| 359 |
|
| 360 |
This can be turned off, if you like, by checking out the |
| 361 |
"learn" command, which gives you the ability to turn off |
| 362 |
book learning (as it presently works), position learning, |
| 363 |
and the next book learning stage which will add to the book |
| 364 |
in addition to learning which book lines are good and bad. |
| 365 |
|
| 366 |
What is this new "result" learning? |
| 367 |
|
| 368 |
Result learning works just like normal book learning, except |
| 369 |
that if Crafty is checkmated or resigns, it will step back |
| 370 |
through the book line to find the last point where it had |
| 371 |
more than one move to choose from. It will flag the move it |
| 372 |
chose as "never play again". |
| 373 |
|
| 374 |
This handles the case where the first ten non-book moves |
| 375 |
produce reasonable scores, but the position is one that |
| 376 |
Crafty simply can't handle very well. If it loses such a |
| 377 |
game, it will still vary the next time this opening is |
| 378 |
played, as otherwise it would possibly repeat the same open- |
| 379 |
ing, and would certainly repeat the remainder of the game. |
| 380 |
|
| 381 |
All three learning modes are turned on by default, although |
| 382 |
any of them can be disabled with the appropriate command |
| 383 |
option to "learn". |
| 384 |
|