lightvector / KataGo

GTP engine and self-play learning in Go
https://katagotraining.org/
Other
3.47k stars 563 forks source link

Interact with KataGo via Python #830

Open lgnashold opened 1 year ago

lgnashold commented 1 year ago

If I want to evaluate KataGo via a python method, what would be the best way to do that.

For example, lets say I have an in-memory board state, and want to compute the next best move. How would I do this directly in Python?

lightvector commented 1 year ago

Probably partly answered here? https://github.com/lightvector/KataGo/issues/788#issuecomment-1683099083

If your in-memory board state is an sgfmill board, then take the example code there verbatim, with any minor changes you want (e.g. changing query["rules"] = "Chinese" if you want to use different rules), and once you've created the katago object (which starts up KataGo's process), call

result = katago.query(board, moves, komi)

If you literally only have a board state, then you can have moves be the empty list, but it's recommended to use move lists instead of just a board state because otherwise you can't tell if a ko capture is legal or not.

Then you can find the best move like:

sorted_move_infos = sorted(result["moveInfos"], key=lambda x: x["order"])  # sort moves considered by the search in order of most preferred to least preferred
best_move = sorted_move_infos[0]["move"]

And of course you can see a bunch of other stats about the moves in the move infos dicts.

lightvector commented 1 year ago

Full docs for what fields you can expect in the result dict, as well as what other fields you can adjust on the query, are in https://github.com/lightvector/KataGo/blob/master/docs/Analysis_Engine.md

lightvector commented 1 year ago

Note that the example code at https://github.com/lightvector/KataGo/blob/master/python/query_analysis_engine_example.py is probably not the most robust in terms of handling warnings or errors returned by KataGo. Warnings and errors should be nonexistent for queries that are fully proper in the parameters they pass and don't contain illegal board states or illegal moves in the moves list, but it could still be improved a bit. I guess I should improve the example on that.

Also if you come up with any simple improvements for your own use case that you want to share, or there are any additions you think would make a big difference and want to share, feel free to share and I'd be happy to consider augmenting the example code to include them.