Creating an opening book in a chess variant

Discussion of chess software programming and technical issues.

Moderator: Ras

User avatar
mohzus
Posts: 106
Joined: Tue Sep 24, 2013 2:54 am

Creating an opening book in a chess variant

Post by mohzus »

Hello,
I am trying to create an opening book (.bin) from a pgn database. So far the .pgn database has only one game inside it.
Here's the command I tried:

Code: Select all

polyglot make-book -pgn GuestVMSJ-stockfishbot.pgn -bin sfwild.bin -min-game 1
And here's the error returned:

Code: Select all

PolyGlot 2.0.1 by Fabien Letouzey.
inserting games ...
tellusererror POLYGLOT: book_insert(): illegal move "Nc6" at line 14, column 3,game 1
The game is not a standard chess game. Instead, it's a game with the same rules of chess but with a different starting position.
Here's the complete .pgn file:

Code: Select all

[Event "ICS Unrated wild/5 match"]
[Site "localhost"]
[Date "2014.02.07"]
[Round "-"]
[White "GuestVMSJ"]
[Black "stockfishbot"]
[Result "1-0"]
[WhiteElo "0"]
[BlackElo "2848"]
[TimeControl "900+5"]
[SetUp "1"]
[FEN "RNBKQBNR/PPPPPPPP/8/8/8/8/pppppppp/rnbkqbnr w - - 0 1"]

1. Nc6 Nc3 2. Nh6 Nh3 3. g8=Q g1=Q 4. b8=Q Bg2 5. Bg7 b1=Q 6. f8=Q f1=Q 7.
Ba6 Bxc6 8. Bxc3 Ba3 9. c8=Q Qbb6+ 10. Qxb6 Qxb6+ 11. Qc7 Rb1 12. Rc8 Rg1
13. Ng4 Qg3 14. Qxb6 Rxb6 15. a8=Q h1=Q 16. Qh5 Qxc3 17. Qxa2 Rxg4 18. Qxg4
Rxa6 1-0
So that you can see that with this starting position Nc6 should be a legal move.
I tried to replace Nc6 by e4 and then I'd get the error that Nc3 is an illegal move. While in fact Nc3 is a legal move and e4 should have been tagged as illegal but was not.
So it seems that the problem is that polyglot doesn't seem to realize that the start position differs from normal chess. I have seen some examples of pgn's of Fischer Random chess and they look like this one, so I'm really puzzled as to why I fail at creating an opening book.

I should mention that xboard and any other interface have no problem in opening and going through the pgn database. So it is not corrupted.

Any help is appreciated, thanks.
User avatar
hgm
Posts: 28454
Joined: Fri Mar 10, 2006 10:06 am
Location: Amsterdam
Full name: H G Muller

Re: Creating an opening book in a chess variant

Post by hgm »

Indeed, it seems that Polyglot ignores the FEN tag. I noticed that before, when someone tried to convert a PGN collection to book that contained a Rook-odds game, and it complained that the move Rd8 was not disambiguated, while in fact there was only one Rook.

You could try to make a book with XBoard. This feature is still a bit experimental, but when done carefully it should work:

* Start XBoard in -ncp mode
* Load the PGN file
* Put the name of the book you want to build in Common Engine Options
* Also set the desired Book Depth parameter there
* Select File -> Save Games as Book from the menu

This might take some time (depending on how large your PGN file is) during which XBoard is unresponsive, and perhaps not even updating its display. Just wait that out. (Perhaps try with a small collection of games first, to get an estimate of what would be a reasonable time to get the job done.)

Eventually XBoard should become responsive again, and at that point it should have created the book file.
User avatar
mohzus
Posts: 106
Joined: Tue Sep 24, 2013 2:54 am

Re: Creating an opening book in a chess variant

Post by mohzus »

Hello and thank you for your reply!
hgm wrote: * Start XBoard in -ncp mode
Done I think. I typed "xboard -ncp" in the terminal, I guess that's it?
hgm wrote:* Load the PGN file
* Put the name of the book you want to build in Common Engine Options
* Also set the desired Book Depth parameter there
Done.
hgm wrote:* Select File -> Save Games as Book from the menu
Here is my problem. I get:

Code: Select all

Game list not loaded or empty
hgm wrote:This might take some time (depending on how large your PGN file is) during which XBoard is unresponsive, and perhaps not even updating its display. Just wait that out. (Perhaps try with a small collection of games first, to get an estimate of what would be a reasonable time to get the job done.)

Eventually XBoard should become responsive again, and at that point it should have created the book file.
Well I have only one game for the book so far. Overall the book won't need that many lines, I'm guessing that the outcome of that variant is a win for white and with only a few lines in the book, the bot would become invincible as white. I could be wrong of course, but that's the feeling I get.
User avatar
hgm
Posts: 28454
Joined: Fri Mar 10, 2006 10:06 am
Location: Amsterdam
Full name: H G Muller

Re: Creating an opening book in a chess variant

Post by hgm »

Hmm, weird. I thought I had fixed that. Perhaps only for WinBoard. I will check it out.
User avatar
hgm
Posts: 28454
Joined: Fri Mar 10, 2006 10:06 am
Location: Amsterdam
Full name: H G Muller

Re: Creating an opening book in a chess variant

Post by hgm »

Oh, wait!

Only now I notice you have only a single game. If you load a PGN file with only a single game in it, XBoard will not create and pop up a game list. So it is no mystery that you get this message.

The feature was not designed to make a book from a single game. You might as well just create the book by hand in that case, using the Edit Book dialog and starting from an empty book. It was of course designed to convert files with many thousands of games in them. Hence it works from the game list. The idea was that in the future it should only use the selected part of the game list, and convert that to book. So that you can select a minimum Elo, or take only recent games, or only use games that are won for a particular color, etc. Currently it will take the entire game list, though.

You could simply duplicate the game in the file. Two games is enough to get a game list, and Save Games to Book should work then.
User avatar
mohzus
Posts: 106
Joined: Tue Sep 24, 2013 2:54 am

Re: Creating an opening book in a chess variant

Post by mohzus »

Thanks again, as you said, this time this worked fine. I added one game to the "book", so now I have two games.

However the .bin file doesn't "work": When I load it with xboard I see no pieces and it says "white to play". I tried to make stockfish use the book, but to no avail. The file is 16 bytes and I'm guessing invalid or corrupted.

Here is the complete .pgn book:

Code: Select all

[Event "ICS Unrated wild/5 match"]
[Site "localhost"]
[Date "2014.02.08"]
[Round "-"]
[White "stockfishbot"]
[Black "GuestPHMG"]
[Result "*"]
[WhiteElo "2848"]
[BlackElo "0"]
[TimeControl "900+5"]
[FEN "RNBKQBNR/PPPPPPPP/8/8/8/8/pppppppp/rnbkqbnr w - - 0 1"]
[SetUp "1"]

{--------------
R N B K Q B N R
P P P P P P P P
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
p p p p p p p p
r n b k q b n r
white to play
--------------}
1. Nh6 Nc3 2. g8=Q Nh3 3. Nf5 Nd5 4. Bh6 g1=Q 5. f8=Q Bg2 6. Na6 f1=Q 7.
b8=Q Qef2 8. Bb7 Qxf5 9. Qxf5 Qxf5 10. Bxd5 Bxd5 11. Qgf8 Qe5 12. c8=Q e1=Q
13. Qc3 Q5e2 14. Nb4 b1=Q 15. Nxd5 Bb2 16. Qbxb2 Qxb2 17. Qxb2 Qc5 18. Nc3+
Qxc3 19. Qxc3 Rg1 20. Rc8 Rc1 21. a8=Q h1=Q 22. Qxa2 Qhe4 23. Rc4 Ng5 24.
Bxg5 Qg3 25. Qxg3 Rxg3 26. Rxe4 Rb1 27. Rxe2 Rb8+ 28. Kc7 c1=Q+ 29. Kxb8
Kxe2 30. Qh5+ Kd3 31. d8=Q+ Kc3 32. Bxd2+ Qxd2 33. Qdxd2#
*

[Event "ICS Unrated wild/5 match"]
[Site "localhost"]
[Date "2014.02.07"]
[Round "-"]
[White "GuestVMSJ"]
[Black "stockfishbot"]
[Result "*"]
[WhiteElo "0"]
[BlackElo "2848"]
[TimeControl "900+5"]
[FEN "RNBKQBNR/PPPPPPPP/8/8/8/8/pppppppp/rnbkqbnr w - - 0 1"]
[SetUp "1"]

{--------------
R N B K Q B N R
P P P P P P P P
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
p p p p p p p p
r n b k q b n r
white to play
--------------}
1. Nc6 Nc3 2. Nh6 Nh3 3. g8=Q g1=Q 4. b8=Q Bg2 5. Bg7 b1=Q 6. f8=Q f1=Q 7.
Ba6 Bxc6 8. Bxc3 Ba3 9. c8=Q Qbb6+ 10. Qxb6 Qxb6+ 11. Qc7 Rb1 12. Rc8 Rg1
13. Ng4 Qg3 14. Qxb6 Rxb6 15. a8=Q h1=Q 16. Qh5 Qxc3 17. Qxa2 Rxg4 18. Qxg4
Rxa6
*
Any idea what could be wrong with the .bin?
User avatar
hgm
Posts: 28454
Joined: Fri Mar 10, 2006 10:06 am
Location: Amsterdam
Full name: H G Muller

Re: Creating an opening book in a chess variant

Post by hgm »

Indeed, 16 bytes is an empty book. Each (position,move) pair takes 16 bytest in a Polyglot book.

But I see the two games you post do not have a result. I am not sure if that works. It could be that it considers them a draw, but it might ignore them altogether. The result is important, because in building the book XBoard (or Polyglot, for that matter) only count the points scored with the various move. So moves of the losing side do not receive any points. And moves without points would not be saved in the book. (Moves are played in proportion to the points they have, so moves with zero points would never be played, and it would be pointless to have them in the book.)

I edited the files to indicate a result, but that still didn't work. It seems I made XBoard only include moves in the book that have at least two point. (Well, that makes sense, I guess.) When I duplicated the games, so that all moves where played twice, I got a non-empty book.

If you want the moves for both sides to be included, you can set the result to 1/2-1/2. I think it counts a draw as 1 win + 1 loss (like BayesElo does), so that two draws also get the move in the book.

Btw, to do ebvery game twice you can also simply click Save Games to Book a second time. XBoard builds the book in a memory buffer, which it never clears (unless you quit it, of course). It just adds the games in the current PGN to that buffer each time you invoke Save Games to Book, (and then saves it), to allow you to combine several PGN files into one book. SO after reading the same pGN file twice, every move that did not always lose will have two points.
User avatar
mohzus
Posts: 106
Joined: Tue Sep 24, 2013 2:54 am

Re: Creating an opening book in a chess variant

Post by mohzus »

I'm confused now. I have changed the results to 1/2-1/2. I could create a .bin book over 1 kb, so I guess this time it isn't empty. When I select stockfish or critter to use the book, they would use it normally without problems, for black and white, so it works.

However, I do not really know how to add the games to the book. What I did was opening the .pgn of the 2 games, manually go through each game up to the very last move and then select "File >> Save Game as book", twice for each game. If I don't go through the games I get that there are no games to add to the book. I repeated the process 2 or 3 times so I might have saved 6 times the 2 games in the .bin file.

The problem is that I cannot open the .bin with xboard (when I do this, it sets up a strange position with white pawns only on some squares of the board and there are no moves, no games) to examine how many lines I added. If I added too many, I'd like to delete some because later I plan to add new lines and they won't be selected with the same priority than the most numerous lines I suppose.
The fact that I cannot examine the .bin book is a problem for me. I tried to open it with scidvspc too but no position would appear, as if the book was invalid (while it is not since engines can use it without problems).

I also plan to merge the book.bin with another .bin book used for normal chess. So that when I let the engine on FICS, it will use a single book able to handle both normal chess and the wild 5 variant.
User avatar
hgm
Posts: 28454
Joined: Fri Mar 10, 2006 10:06 am
Location: Amsterdam
Full name: H G Muller

Re: Creating an opening book in a chess variant

Post by hgm »

mohzus wrote:However, I do not really know how to add the games to the book. What I did was opening the .pgn of the 2 games, manually go through each game up to the very last move and then select "File >> Save Game as book", twice for each game. If I don't go through the games I get that there are no games to add to the book. I repeated the process 2 or 3 times so I might have saved 6 times the 2 games in the .bin file.
There should be no need to go through the games. Save Games to Book is a 'batch command' that processes all the files in the game list. I didn't go through any games, and I did get a non-empty book. I did duplicate the games with an editor first, though, in the PGN file, before loading it in WinBoard.

But I think using the original file with each game appearing once should also work, if you just load the PGN, and select Save Games to Book 3 times. (The second time it will refuse the command, because the book already exists from the first time, and requests you to do it again (i.e. a third time) if you really want to overwrite, I added this as a safety measure, to prevent someone hitting the menu item by accident from overwriting a valuable book.)
The problem is that I cannot open the .bin with xboard (when I do this, it sets up a strange position with white pawns only on some squares of the board and there are no moves, no games) to examine how many lines I added.
What do you mean, "open the book"? Are you trying to use Load Game on it? That would never work; Load Game is a command for PGN game files, not for books, Bin books are not in PGN format, and in fact contain no games at all, as I already mentioned.
If I added too many, I'd like to delete some because later I plan to add new lines and they won't be selected with the same priority than the most numerous lines I suppose.
You can check with Edit Book. Just go to a position that should be in the book (e.g. by loading one of the games on which you based it, and playing through it with the Edit Book window open). You will see the list of moves for the position, together with their weights. The weights will tell you the number of games the move occurred in succesfully.
The fact that I cannot examine the .bin book is a problem for me. I tried to open it with scidvspc too but no position would appear, as if the book was invalid (while it is not since engines can use it without problems).
Bin books cannot be examined. When I said they contain (position, move) pairs, i should have mentioned that the position is actually encoded as a 64-bit hash key. This cannot be decoded into a position. Not even in theory, as many positions would map to the same key. (There are much more than 2^64 Chess positions). And certainly not in practice, as it could only be done by random search, which would take millions of years. If SCID can display books for normal Chess it must use a trick: it probably guesses which positions might be in the book, and then probes for their hash key in the book. And the only way to guess is by starting from the opening position, to see if it is in the book, and for every position that is in the book, generate the daughter positions, and check if they are in the book. But of course it would not find any positions from wild5 that way. It only would if it started guessing from the wild5 opening position, which it probably doesn't (and probably cannot be made to do.)
I also plan to merge the book.bin with another .bin book used for normal chess. So that when I let the engine on FICS, it will use a single book able to handle both normal chess and the wild 5 variant.
That should be possible, even with Polyglot. Because the book is non-decodable, Polyglot could never know that it is a variant book, and should merge it like any other. In XBoard you could only do it if you had the PGN you based the normal Chess book on. Then you could use the Save Games to Book for that, and then open the wild5 file, and do it again to add it. I guess I could make an "Import Book" command to load an existing book into the memory buffer, or even add it to it, so that you could merge books with XBoard as well. But Polyglot should do it fine. (At least for variants that would not have unusual move encoding, e.g. because the board is not 8x8. Otherwise it might become pedantic even during merge.)
User avatar
mohzus
Posts: 106
Joined: Tue Sep 24, 2013 2:54 am

Re: Creating an opening book in a chess variant

Post by mohzus »

Thank you very much HGM, all works fine now!
I understand better now that the book.bin doesn't have "games" loaded in it. Setting the FEN + "Edit book" works fine, this is perfect.
I also merged my normal chess book.bin with the wildbook.bin via polyglot and tested it on FICS, all works fine.
Great! Thanks a bunch :D