KataGo icon indicating copy to clipboard operation
KataGo copied to clipboard

Interface for a custom algorithm to play as a bot

Open stephen-lazarionok opened this issue 1 year ago • 8 comments

Is there any described interface that I can leverage to plug in my own algorithm?

Also what's the purpose of cpp and python source codes?

stephen-lazarionok avatar Feb 20 '24 13:02 stephen-lazarionok

It depends entirely on what you are doing. If you just want to entirely write your own Go bot, then you don't need this repo, you want to just implement Go Text Protocol yourself such this demo engine in https://github.com/jtauber/gtp. Detailed protocol is here https://www.lysator.liu.se/~gunnar/gtp/

If you want instead to experiment with manual hacks to KataGo's internal behavior but still want to keep KataGo's logic intact otherwise, then it's up to you to read the source code and change what you want.

If you want to use KataGo as a black-box service or subroutine to do analysis and then do anything else you like with the results, then you don't modify KataGo at all, instead you would run KataGo as a subprocess and query it. For example look at this example code https://github.com/lightvector/KataGo/blob/master/python/query_analysis_engine_example.py and with detailed docs on the interface here https://github.com/lightvector/KataGo/blob/master/docs/Analysis_Engine.md

cpp is KataGo's main implementation. python is where the training is done (in Pytorch) and also various useful side utilities such as that example analysis engine querying code above.

lightvector avatar Feb 20 '24 20:02 lightvector

I would like to create my own bot that will compete with the bots published.

I see two options available:

  1. Create a bot from scratch and implement GTP protocol - https://www.lysator.liu.se/~gunnar/gtp/ as you mentioned
  2. Train my own NN that has the same inputs / outputs (e.g. "NN interface") as NN defined in your codebase. In this case I can leverage KataGo "ecosystem". For instance emulates matches between different bots.

For option (2) can you please document the inputs/outputs? Do you think it's worth doing this?

stephen-lazarionok avatar Feb 20 '24 20:02 stephen-lazarionok

No, it's not worth doing this. You should implement your own bot. Take a look at the jtauber repo there - it already provides all the starting point you need for GTP. And if you're going to train your own neural net then enough will likely be different that you actively don't want to use KataGo - because it would only create extra work for you to support the wide variety of training targets, different rulesets, etc.

A far better point to start would be to just download some pro games databases and use https://github.com/mattheww/sgfmill to read the files and follow a pytorch tutorial on how to do supervised learning to predict the next move with a convolutional neural net, then plug that into the GTP framework from the jtauber repo and go from there.

lightvector avatar Feb 20 '24 20:02 lightvector

Understood. Thanks for the clarifications. Then the question is how can I make the games happening between my model and KataGo models using GTP protocol?

stephen-lazarionok avatar Feb 20 '24 20:02 stephen-lazarionok

As far as I know, the options aren't great for that because the existing options are just passable enough that nobody has bothered making better ones, and the KataGo repo does not contain a solution for this anyways. For KataGo people have generally relied on gogui-twogtp (search online for it, it's a old java-based program), and the other option is "gomill" which is a python 2 (NOT python 3) predecessor to sgfmill, which the author of that library only had the capacity to update the SGF handling parts to python 3 but not the GTP related parts. I have an old sandbox script attached that I modify as needed that uses gomill when I need more flexibility than gogui-twogtp allows.

custom_gtp_runner.zip

lightvector avatar Feb 20 '24 21:02 lightvector

Basically it means that if I want to create a bot to compete with KataGo bots then it's easier to use "the interface" of NN you described :)

stephen-lazarionok avatar Feb 21 '24 05:02 stephen-lazarionok

Also, do you think it's worth implementing such an adapter or interface within KataGo project? Thus if I have "some algorithm" that implements GTP protocol I can easily simulate multiple games with other bots?

stephen-lazarionok avatar Feb 21 '24 11:02 stephen-lazarionok

I haven't implemented something like that myself because as I said earlier, the other options do work well enough that it's not worth me spending all the time to implement the same thing again.

In case it wasn't clear, both gogui-twogtp AND gomill independently and separately allow you to play any GTP bots against one another and they work fine even if they're ancient. No adapter is needed. Search online and try one or both, gogui-twogtp is not bad to use if you don't mind java.

lightvector avatar Feb 21 '24 14:02 lightvector