Well, UCI engines all cheat, don't they? They are not real Chess engines, just search engines, and use a cloned, non-original front end (namely Polyglot or the GUI) for some essential tasks, such as handling the opening book. They should all be disqualified from official events for this.
How would such an implementation work? Would an engine that is supposed to use the book be kept in force mode, the book supplying the moves that engine is supposed to do (and forcing them into the engine as well)? Until the book is exhausted, and only then send the go command?
How would such an implementation work? Would an engine that is supposed to use the book be kept in force mode, the book supplying the moves that engine is supposed to do (and forcing them into the engine as well)? Until the book is exhausted, and only then send the go command?
Yes this is what I do in my private fics client icsdrone-ng.
One small point: you cannot say that a PG book is exhausted (it uses positions and not lines). So after a few non book moves the GUI may find itself in the book again and hence it will put the engine back into force mode.
I suppose it would be OK to have both engines use the same book always, if they use it at all. But I guess that you might want to enable the use of the book for both engines independently. Is this where the 'HasOwnBook' options are for, to tell Polyglot that it should not consult the Polyglot book for that engine, even if it has one and even isusePolyglotBook is enabled? Or is it someting different.
Problem is that I don't know much about UCI or polyglot.ini files, and I never conciously put an opening book on my computer.
-firstBook and -secondBook would be better, to make the commands availableas engine-following options in a tournament manager (e.g. "engine.exe WBopt -%sBook <filename>"). But -sb and -fb could be supplied as short-cuts.
But for now we could try it without any extra arguments, simply using the book given in the -polyglotBook option as book. Can you supply a C routine that would probe if a position is in a given book, and returns the move to be played? In what form does the position have to be presented? WinBoard has a representation as an 8x8 array, which is updated as soon as an engine move comes in. So at the time the move is sent to the opponent, the position info is available.
So if I undestand it correctly, the point is to lookup the position just before sending the move, if the book is switched on. If there is a match, (and as a consequence a book move), then engine to receive has to be put in force mode (if it isn't already, but it never hurts to do it again), receive the opponent move, receive the just retrieves book move. The tricky part is that it then also has to communicate the book move to the opponent. The quick and dirty way to do this would be to jump back to the HandleMachineMove routine (in stead of returning from it, and waiting until an input event causes it to be called again), after chaning the engine and move message (which are parameters to this routine).
If a book miss occurs and the engine was in force mode, a go command should be sent after the move.
It sounds easy enough, once we have a probing routine. The only real job is to convert the xboard internal position to an access key for passing to the query routine. Is this some kind of zobrist mhashing mechanism? How woud you alow for un-orthodox pieces on the board?
But for now we could try it without any extra arguments, simply using the book given in the -polyglotBook option as book. Can you supply a C routine that would probe if a position is in a given book, and returns the move to be played? In what form does the position have to be presented?
There is some sample code there which I think does exactly what you ask for (pg_key.c/hash() computes the key of a position and pg_show/find_key() looks it up in the book, pg_key.c converts a fen first to an 8x8 board so it could be shortened).
EDIT: In fact find_key() only looks up the position. Searching for the moves is done in main(). The results are probabilities. Since xboard is GPL you may also look at book.cpp/book_move() in the polyglot source code. This generates the move in one go (starting from the key) and is actually more elegant code than mine.
How would you alow for un-orthodox pieces on the board?
Good question. As far as I know there are no opening books for fairychess. But in principle the PG format is easy enough to extend in a backward compatible way to allow for non-standard pieces. The input array of random numbers would have to extended, and there should be agreement on the coding of the special pieces (for standard chess they are numbered from 0 to 11), but otherwise there would be no problem.
OK, it seems the book file is simply a list of (position,move) pairs sorted by Zobrist key of the position. The Zobrist key is indeed easily generalized to more piece types or other board sizes. I am sure a lot of opening theory must exist for Xiangqi and Shogi, compared to those FIDE Chess is only a minor variant. So I would it is good news that Polyglot books for those variants pose no problem at all.
The remaining question is this: Using the routines of the link I will be able to retrieve a list of moves + weights for that position. So what am I supposed to do exactly with that list? Pick a move with a probablity proportional to the book weight? Or to a power of the book weight that the user can set?
Or to a power of the book weight that the user can set?
Polyglot provides two options. Take the move with the maximum probability, or else choose it with probability proportional to the weight (this gives more varied games and is the default).
Your proposal to interpolate between these two possibilities is interesting!
OK, I will try to include the book stuff in the backend in tomorrow. I will use the -polyglotBook parameter for the filename, and for starters just use directly proportional probabilities. Only picking the best move seems to defeat the most important purpose of the book anyway.
The option -usePolyglotBook will determine if the GUI consults the book on behalf of WinBoard engines. (So I will suppress it if the engine is known to WinBoard as UCI through the -fUCI or -sUCI option.)
How am I supposed to react to the "HasOwnBook" options? Should I also suppress accessing of the GUI book for engines that have this option set? What does Polyglot (or the engine) use this information for?