lantonov / asmFish

A continuation of the nice project asmFish by Mohammed Li. Latest version: 07.08.2019
https://lantonov.github.io/asmFish/
Other
118 stars 49 forks source link

Limit Book Depth #48

Closed jjoshua2 closed 7 years ago

jjoshua2 commented 7 years ago

The polyglot book support is amazing. It's nice to have the fastest engine and the best features. I write my own custom polyglot books, and there is one more feature (maybe two) I would absolutely love to have. Could you have an option to limit how much you use the book? The naive way is just to say don't use it after say 40 ply. But what I actually want is to do some probing to see the max depth left in the tree, because if there are variations 20 ply deep still left in the tree, it is probably a strong move, but if the depth left in the tree is 2, I would want my engine to take over in long games.

The other idea is related to it, can't I be pondering while I am in book? Especially when I get to the end of my book and play the last move in it, I shouldn't have to wait for his response to start thinking. This would be very helpful for faster games using big hash tables since it takes several seconds to initialize and get up to speed on my machine.

tthsqe12 commented 7 years ago

So the first request can be accommodated with a new option bookdepth that works like this: bookdepth = 5: don't play book moves after 5 have already been played. (easy to implement) bookdepth=-5: don't play a book move if there isn't a line of depth at least 5 from current position. (hard to implement and will require the book to be searched)

As for the second, this would require the engine to send a ponder move with each book move. Not sure how you would want to select this move.

lantonov commented 7 years ago

If you want to limit book at a set ply (say, ply 20) you can do it in 2 ways:

  1. translate Cerebellum with the command brain2polyglot depth 20 in "Cerebellum_light.bin" out "polybook.bin". This will be very fast
  2. Use GUI to limit book depth. All major GUIs have this function. For example, Winboard + Polyglot has options BookDepth and STFudge (for book variety). If you want depth before end of book variation, the engine has to search all variations from the given ply and this can be slow in addition to hard to implement. For pondering before end of book, I don't see the use of it because the book move is expected to be better than the ponder move.
jjoshua2 commented 7 years ago

I have made my own polyglot book kind of like cerebellum, so I know the compute depth of each move, and what TC I can exceed that towards the end of my book. I would like to be able to use logic like I can search to depth 33 at this TC and the book was calculated at depth 31, so when there are 2 moves left I should switch over, but I understand that is very tricky to program, and I could perhaps approximate by just only limiting book depth to 16, but this number is not really related to the depth of the knowledge.

So I cannot use brain2polyglot because it is my own polyglot book. Maybe there is some other polyglot book trimmer.

I am using the Infinity Chess GUI which has no polyglot support, so it is nice the engine has support.

There are two ways to select which move to ponder. The main goal is just to initialize the hash table so it is all in fast memory and not paged, and hopefully fill it with something that might be useful. The book could be used to select the best_move, or the move that we actually played from the book could be used, even though it would be thinking for the wrong color, it would fulfill our two goals, and perhaps after a few seconds we could ponder that top pick and be pondering a move we might actually use if our book is empty.

tthsqe12 commented 7 years ago

Im going to implement the positive and negative book depths as mentioned above. The only remaining issue is how to select a ponder move when none is available from the book. For now, Im going to simply choose a move that maximizes the value returned by qsearch.

tthsqe12 commented 7 years ago

Here is the behaviour of the patch in the works, with some annotations

setoption name ownbook value true
setoption name bookfile value performance.bin
info string 92954 entries in book (0 entries have zero weight)
position startpos moves e2e4 e7e5 g1f3 b8c6 f1b5 a7a6 b5a4 g8f6 e1g1 f8e7 f1e1 b7b5 a4b3 e8g8 c2c3 d7d6 d2d4 c8g4 c1e3 e5d4 c3d4 c6a5
// see all lines up to depth 9
bookprobe 9
b3c2(71) c7c5(45) h2h3(30) c5d4(10) e3d4(14) g4h5(10) g2g4(11)
                           g4h5(5)
                  d4c5(17) d6c5(17) b1c3(7)
         a5c4(25)
// since the default value of bookdepth is 100 and the game ply is only 22,
// it is going to pick a move from the book and a ponder move from the book
// the ponder move is c7c5 and not a5c4 since the former has the higher weight
go depth 1
info depth 200 score cp 71
bestmove b3c2 ponder c7c5
// setting bookdepth=-8 will not use this book move because the longest line is length 7
setoption name bookdepth value -8
go depth 1
info depth 1 seldepth 1 multipv 1 time 1 nps 81000 score cp 34 nodes 81 tbhits 0 pv h2h3 a
5b3 d1b3
bestmove h2h3 ponder a5b3
// setting bookdepth=-7 therefore will pick up this move
setoption name bookdepth value -7
go depth 1
info depth 200 score cp 71
bestmove b3c2 ponder c7c5
// go down the tree a bit and verify there is only one position in the book after
position startpos moves e2e4 e7e5 g1f3 b8c6 f1b5 a7a6 b5a4 g8f6 e1g1 f8e7 f1e1 b7b5 a4b3 e8g8 c2c3 d7d6 d2d4 c8g4 c1e3 e5d4 c3d4 c6a5 b3c2 c7c5 d4c5 d6c5
bookprobe 9
b1c3(7)
// setting bookdepth = -1 is essentially no restriction on book moves
// here since there is no move in the book after, ponder is not returned
setoption name bookdepth value -1
go depth 1
info depth 200 score cp 7
bestmove b1c3
jjoshua2 commented 7 years ago

That was fast! Thanks. So I don't understand why bookdepth = -1 is no restriction, that should lop of a move? I would think 0 would be no change, and each increase would lop off on ply starting with 1, which depending on if the line ended on your move or his move may or may not have an effect. Cerebellum was mostly calculated with 2 minutes/move or about depth 28 it seems according to the demo on the brainfish website. So I would imagine any TC above 16+1 would want to start dropping bookdepth slowly, especially since the calculation is likely with an older version of SF.

tthsqe12 commented 7 years ago

nope, its the other way: -2: lop off one move -1: no restriction 0: don't use book at all 1: don't use book past game ply 1 However, now that you describe it as lopping off moves from the tree, I see that the current implementation doesn't quite do this.

jjoshua2 commented 7 years ago

I understand your number system kind of now, but it seems convoluted, but as long as you include instructions it will be fine if that is easier. To me it would make more sence if use book was a checkbox, and then if BookDepth=0 had no limit otherwise positive limit is number of ply, and negative -1, would prune one move off the leaf nodes of book, and would be the setting I would most commonly use. Any chance I could use this in a tournament in 24 hours or in 72 hours?

tthsqe12 commented 7 years ago

I guess that system is not natural. It is now: -2: lop off the leaves twice -1: lop off the leaves once 0: no restriction on book moves 1: don't use book if game.ply >= 1 2: don't use book if game.ply >= 2 I will update my fork with the changes and make sure they work in a gui. After that they will get into lantonov's repo.

tthsqe12 commented 7 years ago

My fork seems to be working fine. Try it out a bit and if you find no problems, then we can merge it into lantonov's repo.

hero2017 commented 7 years ago

How can we try it out? This is lantonov's repo and I don't see how to download your fork.

jjoshua2 commented 7 years ago

I'm looking at the commit here: https://github.com/tthsqe12/asmFish/commit/fff37e92013b62d0df355354bc37bcf3a853c32e I think default should be 0, not 100, although the odds of having a book go deeper than 100 is slim.

jjoshua2 commented 7 years ago

Initial results seem that it is working. It is kind of confusing telling whether it is thinking or book, but that's the goal. It is getting the engine warmed up and it seems it's at full nps when its out of book. Can't easily tell if the bookdepth is working exactly as planned, but it seemed reasonable-ish.

lantonov commented 7 years ago

Sorry, I was sleeping for a while (it was a nighttime here :-). I will now make the executables and try it myself.

lantonov commented 7 years ago

The book ponder change committed and executables ready

hero2017 commented 7 years ago

Book depth seems to work well but I haven't yet tested if negative values work, like -10 (don't play a book move if there isn't a line of depth at least 5 from current position. Also, I would think that it may be useful for correspondence matches or engine vs engine that we can go -20. -10 is the current max.

jjoshua2 commented 7 years ago

-10 is somewhat arbitrary, but with correspondence people normally use infinite analysis alongside a book, so it is not really helpful. This feature is mostly for long engine v engine/centaur matches. And even with 90 minute match and lots of cores I haven't seen a depth greater than about 10 above what the Brainfish average depth is maybe 37 vs 27. I imagine numbers like -2 will be the most useful, since you actually want to use most of your book, or you would use a book that did you want to use, and if you only want it for some opening variety you can force max book depth of like 2 or 8 moves like fishtest does.

jjoshua2 commented 7 years ago

It seems the negative numbers are working as expected. I played a game where it didn't play the best move, and I looked afterwards and it looked like best move was checked. Is it possible that is broken? It may be more likely I messed up, because I haven't been able to replicate, it's playing best moves now.

hero2017 commented 7 years ago

Since a higher negative number like -10 means it will use my book less then that's fine because I don't want to use most or the entire book since that will just produce more draws if my opponent is doing the same. You want to leave opening with a decent setup on the board and leave the dirty play for the engines.

I think I'm also confused about the negative numbers. You implied that higher negative numbers such as -10 means it will play the book less, then what does 0 or 10 mean? Isn't that going to use no book or very little (10 ply)?

What should I use if I want to use about 12 of the best opening moves from the book but with some randomness so that it doesn't always play the same best moves but perhaps the 2nd best? For instance, I don't always want a 1.e4 e5 opening...maybe next game I want it 1.e4 c5 ? With ctg I can edit the weights in Fritz to do that but I can't edit the weights for polyglot.bin books :-(

tthsqe12 commented 7 years ago

yes, there was a bug that I just corrected in my fork. I think this issue is resolved now. If you don't want to use the bestmove from the book but would rather choose randomly from all available moves, set bestbookmove=false (this is the default value). I think the readme is perfectly clear on the meaning of the bookdepth option.

jjoshua2 commented 7 years ago

@Hero. You can edit the weights for a polyglot with a little programming or hex editing. You probably want bookdepth=12 and bestmove=false. @tthsqe12 that's good. It sounds like the main repo already picked up your patch, did it get the correct version yet?

lantonov commented 7 years ago

The latest change already in lantonov master

lantonov commented 7 years ago

The best method to make polyglot books is described in the polyglot Readme file. With Winboard you can do as much editing of bin books as you like with Edit book, including weigths, adding and deleting variations, etc. Also with buttons Book Depth you can control what depth you want, and with STFudge you can control variety - best move, second best, etc. I make polyglot books already 10 years and all your requests and questions make me think that you unnecessarily torture yourself in your way of dealing with bin books. At least, making polyglot books with a GUI which does not support polyglot at all seems like this. The editing setup in Winboard looks like this: image

lantonov commented 7 years ago

There are many variations on this. For example, if you want to compare your custom book with Cerebellum for a given variation, you can open two boards side-by-side, have 2 edit windows -- one for Cerebellum and one for custom -- and adjust weights, add / delete moves in custom, etc. You can also edit books on the fly by replaying book with engine analysis on each ply to as much depth as you like. You can analyse played games with edit book open and adjust according to preferred variations by opponent, etc.

jjoshua2 commented 7 years ago

I've used Scid, but it shows weights as percentages instead of the absolute numbers which I prefer since I give them meaning, and it does not let you edit them. Good to know about Winboard. That's easier than python-chess

lantonov commented 7 years ago

Scid is very slow and inconvenient. In Winboard, you can edit directly moves like '4 Nf3' and it automatically calculates the percentage, also showing the number you enter, in this case -- 4. You can enter any number you want until 65000+

lantonov commented 7 years ago

image This is an analysis of a game played on FICS with the custom book. The first board is Cerebellum and the second board is custom. The same game is loaded in the 2 boards. It is clearly seen that book is until ply 58. You can move the cursor forward and backward on book and outside book and watch and edit the plies in custom book in comparison with those in Cerebellum.

jjoshua2 commented 7 years ago

Cool. Just checked out the lantonov version, and I'll test that. I brought this up earlier but it wasn't addressed. Wouldn't bookdepth=0 make more sense as a default since that does nothing? I think the default=100 was a remnant from when 0 was disable book.

jjoshua2 commented 7 years ago

There's a bug now when I compiled the lantonov. It won't accept my book path in windows. It's E:\pgn\src\book123.bin. It works with book123.bin in same folder.

On Thu, Apr 27, 2017 at 5:51 PM, Lyudmil Antonov notifications@github.com wrote:

[image: image] https://cloud.githubusercontent.com/assets/6498222/25505906/9e94b2fe-2bac-11e7-9237-a482dcae3a98.png This is an analysis of a game played on FICS with the custom book. The first board is Cerebellum and the second board is custom. The same game is loaded in the 2 boards. It is clearly seen that book is until ply 58. You can move the cursor forward and backward on book and outside book and watch and edit the plies in custom book in comparison with those in Cerebellum.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/lantonov/asmFish/issues/48#issuecomment-297849299, or mute the thread https://github.com/notifications/unsubscribe-auth/AO6ING0ZMUgtzlZ3hhgL9ujkGF1S9xnjks5r0Q3jgaJpZM4M-agr .

jjoshua2 commented 7 years ago

Also in a game today it didn't play the best moves, even though best moves was checked. Do you think that bug is still there? I made a few tweaks, to try and force bestmove always and didn't get it to work. Also, I wasn 't able to get the book to work at all on InfinityChess without using an adapter. I reported it to them, and they think they bug is with asmfish.

tthsqe12 commented 7 years ago

I could not reproduce any of this. I have tested bookdepth+bookfile+bestbookmove+ponder, and as far as I can see, there is no bug with asmfish. It always helps to get a log of the commands sent to the engine.

hero2017 commented 7 years ago

Thanks for the tips guys. I've recently been experimenting with Scid too but when opening the converted Cerebellum_light.bin book using asmF to polyglot and opening it in Scid, I only see one move from the book. The rest is only displayed when clicking Opponent's Move and you can't edit anything. The only thing I can do is add a move (which seems stupid since the move already exists as an opponent's move) but it's the only way to add a weight to the move. And while you can add moves, you can't even delete them.

With Winboard, can I edit the polybook.bin file directly and change weights etc? I've never used Winboard...I always thought it was just the older protocol which is now replaced with UCI so I didn't realize there's actually a program called Winboard. Will see if I can download it with help from Google. Then I'll report back if I've any issues. I'm new to editing books but after recently playing some games on IC I realized that is really what decides between you winning and losing...the hardware plays a small role in comparison.

jjoshua2 commented 7 years ago

I think that the bestbookmove is working properly now. However, I think there is still a bug with string parsing in windows as I said before (I'm having to have the book in the same folder instead of E:...), and I think the InfinityChess GUI does not work properly with the book, because I think the ponder in book confuses it, but I think the engine is doing as desired when I look at the logs from inbetween.exe (which i have to use to make it work).

lantonov commented 7 years ago

@hero2017 Confirming that polybook.bin (the book translated from Cerebellum) displays only one (the best) move from a given position. Winboard works together with polyglot.exe. It uses Polyglot as an adapter to run UCI engines. Winboard + Polyglot is the original setup which was then implemented in the other GUIs. Therefore, it has all the features necessary to work with natural polyglot books. You can add, change weights and delete moves. Careful with delete because you can lose good moves in certain positions because of transpositions. Editing in the book can be direct by typing the weight and the move or indirect by pressing the button Play Move and then actually playing it on the board. However, you adjust the weight manually in the second method if you want so. For Scid, I am not sure if it can't adjust weights and delete moves. I used Scid vs PC and it can do these operations. However, I don't recommend it because it is slow. In a large book, it can takes ages for saving moves. Additionally, polyglot.exe has command-line tool for making, merging, and repairing books (such as those that have lost moves because of deletions). Polyglot allows to make books from pgn files with games by choosing separately only the good variations for white and for black. There are also other possibilities which are described in the readme.txt file of polyglot distro. You can also see this video https://www.youtube.com/watch?v=rskcUNoirPU. It uses scid for preparing the pgn files but the essential things are the polyglot commands for move separation and merging the books. And there are also other programs than scid for the initial work with pgn files.

hero2017 commented 7 years ago

Thanks for responding lantonov. I've seen that youtube video several times already and have used those tricks to make my pgn's. Scid is very powerful but I agree it's very slow for editing books because each move you change (by adding a move or changing its weight) you have to press save which takes a couple of seconds. While you can't delete a move from the book you can change its weight at least but this is a time consuming process when you have tens of lines to add manually, changing the weight to 0 for the main move, adding the move you want and giving it a weight of 100 for the main move that you want, then pressing Save and moving on to the next move. There's more things I don't like about it but that's another issue.

With Winboard, I see the option to Edit Book (under Edit) and another one just called Book (under Engine menu) which does nothing when I click it. I've no option to select the book I want to edit which I assume would be in the menu option Book.

And you have to manually type in the move into the book it seems and in a format I'm not used to so I can see a lot of space for user error. But the point is, I don't know what book I'm editing and I'm unable to edit the book I actually want (polyglot.bin) because there's no way to load or open any book.

lantonov commented 7 years ago

Winboard opens for editing the book that is specified as a common gui book (Engine / Common settings / Use book). Entering a move is almost error-free in my experience if you ignore percentages altogether (they are calculated automatically). For example, if the book contains no move for a given position (window Edit Book is empty), you can enter a move by simply typing '1 <move>' and pressing Save (saving is much faster than in Scid). If it contains only 1 move, for example, '99.9% 1 a3', you can enter a move with a bigger weight by writing on the second line '2 <move>', etc. If you still think that you may err, you can use the button Play move, and actually play the required move on the board. It will be entered automatically in the window with weight 1 which you can change if required and then save. This second way is slower, however.

hero2017 commented 7 years ago

I don't have the option Engine -> Common Settings. I do have Options -> Common Engine. Using Winboard 4.8.0b + Fairy-Max 4.8V. I have specified the polyglot book there (Under Use Book) so I assume it's using that.

Assuming this is ok I will need to play with it more and see if I can edit the polyglot book more easily. Thanks for all your help!

lantonov commented 7 years ago

In WB4.8.2 and greater Common Engine was moved under Engines. The latest Winboard is 4.9+ . I recommend upgrading it because it has additional features concerning also book editing. Unfortunately, it is not available as an executable and has to be compiled from sources in http://hgm.nubati.net/cgi-bin/gitweb.cgi?p=xboard.git;a=shortlog;h=refs/heads/v4.9.x. In Winboard, you can install any UCI and Winboard-compatible engine like asmFish, SF, Komodo, Houdini, etc., and they can use the book loaded in common engine that you edit. You can also start e.g. asmFish, put it on analysis and enter in the book the best move it shows. If you have a pgn files with games, you can compile with them a new bin book with File / Save games to book.

jjoshua2 commented 7 years ago

I had the engine not play the best move again with an unchanged version of asmfish 5/3/17 with bestmove checked. I couldn't find any engine logs. Is there an easy way to strip polyglot books of all non optimal moves so this can't happen?

In this position r1bqk2r/2ppbppp/p1n2n2/1p2p3/4P3/1B3N2/PPPP1PPP/RNBQ1RK1 w kq - 2 6 The engine played Re1 instead of d4. Some of my weights are double (because of transpositions in my lines I think, but I still want it to pick the highest value, which in this case is obvious), which may cause trouble? Winboard shows: 20.9% 52 f1e1 27.3% 68 d2d4 30.1% 75 d2d4 21.7% 54 f1e1

On Thu, May 4, 2017 at 5:28 PM, Lyudmil Antonov notifications@github.com wrote:

In WB4.8.2 and greater Common Engine was moved under Engines. The latest Winboard is 4.9+ . I recommend upgrading it because it has additional features concerning also book editing. Unfortunately, it is not available as an executable and has to be compiled from sources in http://hgm.nubati.net/cgi-bin/gitweb.cgi?p=xboard.git;a= shortlog;h=refs/heads/v4.9.x. In Winboard, you can install any UCI and Winboard-compatible engine like asmFish, SF, Komodo, Houdini, etc., and they can use the book loaded in common engine that you edit. You can also start e.g. asmFish, put it on analysis and enter in the book the best move it shows. If you have a pgn files with games, you can compile with them a new bin book with File / Save games to book.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/lantonov/asmFish/issues/48#issuecomment-299314201, or mute the thread https://github.com/notifications/unsubscribe-auth/AO6INKl-zhJPXFvOu9ANZyRBrpRMaNouks5r2kLpgaJpZM4M-agr .

lantonov commented 7 years ago

The problem is an isolated node as a result of deleted move in some transposition. That's why I said to be careful and sparing with deletions. You can safely delete moves only at the end of variations. I had my share of these problems at the beginning. In this situation the obvious try is to delete the fourth line (the doubled move f1e1). If this has no effect you have to use the merge facility of Polyglot which also repairs books and connects isolated nodes. You merge your book with a semi-empty book (for example such, containing only 1. e4).

jjoshua2 commented 7 years ago

Ok. Well they both are doubled. But my main concern now isn't to fix that but to make the best move be played. I think it's a bug in asmfish. But I could also fix both problems just by keeping the one highest scoring move

On May 5, 2017 1:10 PM, "Lyudmil Antonov" notifications@github.com wrote:

The problem is an isolated node as a result of deleted move in some transposition. That's why I said to be careful and sparing with deletions. You can safely delete moves only at the end of variations. I had my share of these problems at the beginning. In this situation the obvious try is to delete the fourth line (the doubled move f1e1). If this has no effect you have to use the merge facility of Polyglot which also repairs books and connects isolated nodes. You merge your book with a semi-empty book (for example such, containing only 1. e4).

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/lantonov/asmFish/issues/48#issuecomment-299521845, or mute the thread https://github.com/notifications/unsubscribe-auth/AO6INAeg5pMOXDbKltxYXuNGpcLHOydRks5r21gPgaJpZM4M-agr .

lantonov commented 7 years ago

I am almost sure that it is not a bug in asmfish. The book depend exclusively on Winboard-Polyglot and not on the engine. The move d2d4 will not be played if you don't fix the book.