open_spiel icon indicating copy to clipboard operation
open_spiel copied to clipboard

Training an Agent to play Chess using Different Algorithms

Open TalfrynK opened this issue 6 years ago • 12 comments

I want to be able to train a model on the Chess game environment using different Algorithms to compare and test performance as part of my research and experimentation for my extended essay. I need some assistance and guidance as to how (and if) I can implement the PSRO_rn model (playing against itself in a competitive multi-agent scenario) and the A2C playing against a random opponent, to train these models, and save the models as well for later use.

TalfrynK avatar Apr 09 '20 08:04 TalfrynK

I need some assistance and guidance as to how (and if) I can implement the PSRO_rn model (playing against itself in a competitive multi-agent scenario)

I'm quite sure that we do not have PSRO_{rn} implemented... @PaulFMMuller, can you confirm?

and the A2C playing against a random opponent, to train these models, and save the models as well for later use.

There are some examples of how to run policy gradients in here: https://github.com/deepmind/open_spiel/blob/master/open_spiel/python/algorithms/policy_gradient_test.py. E.g. you can replace one of the agents with a random agent in the list constructed on line 43. You will want to use a loss="a2c" (take a look at policy_gradient.py for documentation on the PG parameters).

Saving and reloading models is not something we many examples of in the code base, but should be doable in the usual way with Tensorflow. (Best bet is to look up some examples)

Hope this helps, let us know if you have any other questions!

lanctot avatar Apr 09 '20 17:04 lanctot

Hi there !

We do have support for both PSRO_N and PSRO_rN - a fact we probably should underline more !

To run these, run PSRO with rectifier='rectified' (This will train each training strategy only against strategies it beats) and training_strategy_selector='rectified' (This will select all strategies with selection probability > 0 as starting point for training)

Hope this helps !

Paul

PaulFMMuller avatar Apr 09 '20 19:04 PaulFMMuller

Thank you for your assistance; however, I am new to Open Spiel as a whole and I am completely unclear on how to actually use the framework and environments to train an agent (basic stuff). Also @PaulFMMuller by setting rectifier='rectified' will this essentially place them in a competitive environment with each other? or does the model learn this itself while running.

TalfrynK avatar Apr 09 '20 22:04 TalfrynK

Ah! ok, probably the best place to start before looking at any code is the paper: https://arxiv.org/abs/1908.09453. This will help provide some context on formalisms and lingo.

I gave a tutorial recently, it mostly covered just the basic API, but also useful: http://mlanctot.info/open_spiel-tutorial-kuleuven-mar11-2020.pdf

But probably the best advice if you want to know how to train agents is to delve into the code; i.e. look through and start playing with the examples, here: https://github.com/deepmind/open_spiel/tree/master/open_spiel/python/examples . E.g. there is a psro_v2_example.py, rl_example.py, single_agent_catch.py, breakthrough_dqn.py, tic_tac_toe_dqn_vs_qlearner.py, etc. If you don't find what you need in that examples directory, the tests are good places to look because they often have simple example uses (such as the policy_gradient_test.py I pointed you to above).

lanctot avatar Apr 10 '20 03:04 lanctot

I thought that the python examples provided used pre-trained models? (do correct me if I am wrong) Also when I tried to run the breakthrough_dqn.py file it didn't work with the player parameters I provided to it.

TalfrynK avatar Apr 10 '20 05:04 TalfrynK

No I don't think there are any that use pre-trained models as we are not storing any in the repos. The examples are intended to show example usage, like how to run the algorithms etc.

breakthrough_dqn.py may be out of date, we have not used it in a while. What's the error?

lanctot avatar Apr 10 '20 05:04 lanctot

breakthrough_dqn.py still works on my end. Can you tell me what error you are getting?

Did you try an unsupported parameter? The supported ones are documented here: https://github.com/deepmind/open_spiel/blob/master/open_spiel/games/breakthrough.h (rows and columns)

lanctot avatar Apr 10 '20 13:04 lanctot

Ah yes, I was using an unsupported parameter. I was trying to run it the same way mcts.py was run in the example as I was unclear as to how to use the file.

How do I go about training a model then? Could you provide any guidance?

TalfrynK avatar Apr 11 '20 11:04 TalfrynK

How can I run the PSRO_rn model on breakthrough?

TalfrynK avatar Apr 13 '20 05:04 TalfrynK

Have you looked at psro_v2_example?

You can run PSRO_{rn} on chess using this command:

python3 python/examples/psro_v2_example.py --game_name=chess --meta_strategy_method=uniform --oracle_type=DQN

You will need to comment out lines 277-288 because chess is too large to run exploitability.

There are lots of setting to play with. I strongly recommend going through the code to understand what is going on under the hood and how to run it.

It may have trouble with small feed forward nets. If so, try something larger or use conv nets.

lanctot avatar Apr 13 '20 13:04 lanctot

Also just noticed that the default learning rate for dqn is 0.01, that is likely too high for chess. Try 0.0001. You will almost certainly need to play with those settings, as the defaults were tuned for small poker games.

Edit: just realized you were asking about breakthrough. All the answers still hold, just use --game_name=breakthrough instead.

lanctot avatar Apr 13 '20 13:04 lanctot

Closing this for now since it's not an outstanding issue, but feel free to keep using the thread to ask questions!

lanctot avatar Apr 28 '20 14:04 lanctot