open_spiel icon indicating copy to clipboard operation
open_spiel copied to clipboard

PSRO Example v2

Open oslumbers opened this issue 6 years ago • 19 comments

Hi,

I'm trying to do some research on psro (v2) and have been trying to work through the example to understand the code. However, there seems to me to be an issue with https://github.com/deepmind/open_spiel/blob/f8110024dcf04fc3841dc30cc3da95c7bdf279a7/open_spiel/python/examples/psro_v2_example.py#L167 and similarly with the policy gradient responder. Using the example code yields the following error when running this part of the function https://github.com/deepmind/open_spiel/blob/f8110024dcf04fc3841dc30cc3da95c7bdf279a7/open_spiel/python/examples/psro_v2_example.py#L192-L201: __init__() takes 3 positional arguments but 6 were given which I currently havent found an obvious fix for.

Please let me know if I am making an obvious mistake here, thanks.

oslumbers avatar Mar 22 '20 17:03 oslumbers

Yeah, agreed. I have found some other errors including this one. A possible fix to this one could be: agents = [ agent_class( # pylint: disable=g-complex-comprehension env, player_id, session=sess, state_representation_size=info_state_size, #(for init_pg it should be info_state_size=info_state_size) num_actions=num_actions, **agent_kwargs) for player_id in range(FLAGS.n_players) ]

qmaai avatar Mar 22 '20 18:03 qmaai

After solving this bug, the following error occurs in rl_policy.py line 78 time_step=self.get_time_step(). It invokes the self._env.get_time_step(), whereas the self._env._state is still None. Proper fix to this is adding env.reset() to main() in psro_v2_example.py

qmaai avatar Mar 22 '20 18:03 qmaai

A third bug I have found so far is the following: abstract_meta_trainer.py calls "sample_episodes()", which start from an initial state (self._game.new_initial_state()). However, in prso_v2/rl_policy.py, in action_probabilities(self._sttate,player_id=None), the state that is passed in is not the same state that self._env is maintaining at all. Working on the possible fix right now.

qmaai avatar Mar 22 '20 18:03 qmaai

I actually wonder if there is an error in version control, that the master branch is not the tested version.

qmaai avatar Mar 22 '20 18:03 qmaai

I actually wonder if there is an error in version control, that the master branch is not the tested version.

No the examples are just not run during the tests (so it might be outdated?). @paulfmmuller, any ideas?

lanctot avatar Mar 22 '20 18:03 lanctot

For my part, the example does not run because tensorflow import is placed before pyspiel import. If their order is reversed, then it could run normally, with the aforementioned bugs though.

qmaai avatar Mar 22 '20 19:03 qmaai

Hi all,

Apologies about the bugs; they're now solved. Some mixup happened during the review process that has now been fixed. In addition, the update contains an improvement to the alpharank solver, greatly improving stability.

This should be submitted in tomorrow's update. Thank you very much for your interest in PSRO ! :)

Paul

PaulFMMuller avatar Mar 22 '20 20:03 PaulFMMuller

Thanks @oslumbers @qmaai for the reports and @PaulFMMuller for the fast fix, which will be included in tomorrow's update!

lanctot avatar Mar 22 '20 22:03 lanctot

Hi, just went to check these new updates, thanks @PaulFMMuller! however, have found a new error when testing on symmetric games (was using Blotto). you get a 'list index out of range' error at this part : https://github.com/deepmind/open_spiel/blob/58a3525c00ff29d93f5afde913fec31717aee674/open_spiel/python/algorithms/psro_v2/rl_policy.py#L103

doesnt seem to be a problem when using non symmetric games.

oslumbers avatar Mar 23 '20 18:03 oslumbers

Hi there,

Running psro_v2_example with flags game_name=blotto and symmetric_game=True doesn't result in an error on my end. Could you please elaborate on the settings used to run the experiment ?

Thanks !

Paul

PaulFMMuller avatar Mar 23 '20 18:03 PaulFMMuller

Hi @PaulFMMuller cheers for getting back to me

I'm not using Abseil as I am running this in Colab and haven't found a nice way to combine them yet, so that could potentially be an issue? But not entirely sure how - anyway have attached the code I'm using which is just psro_v2_example with the flags directly replaced:

https://github.com/oslumbers/open_spiel/blob/23e47e607c87dc6c24af4079ae2e36b7d0a22d61/open_spiel/python/examples/psro_v2_example_2.py#L167-L201

https://github.com/oslumbers/open_spiel/blob/23e47e607c87dc6c24af4079ae2e36b7d0a22d61/open_spiel/python/examples/psro_v2_example_2.py#L243-L267

https://github.com/oslumbers/open_spiel/blob/23e47e607c87dc6c24af4079ae2e36b7d0a22d61/open_spiel/python/examples/psro_v2_example_2.py#L292-L302

and upon running run() I get the error as listed above. - Apologies for the large code snippets

Let me know if you need to know anything else, Thanks!

oslumbers avatar Mar 23 '20 21:03 oslumbers

So i've been testing this today and have found it to be an intermittent problem where, from what I can tell, https://github.com/deepmind/open_spiel/blob/d55f38380bb53fc95f920c1a8a10a427662d1e0c/open_spiel/python/algorithms/psro_v2/rl_policy.py#L102 is an empty list and therefore an error is thrown up. Am using DQN btw. Thanks!

oslumbers avatar Mar 24 '20 15:03 oslumbers

Ho @oslumbers, we have now reproduced the problem on our end, and we are looking into it. I'm re-opening the issue. Thanks for reporting this.

lanctot avatar Mar 26 '20 17:03 lanctot

Hi @oslumbers , We have found & fixed the bug, thank you very much for this ! For more info, it was due to a simultaneous moves game being loaded as a turn-based game, preventing the sharing of policies from one player to the other.

This was fixed by loading simultaneous moves games as such.(And refactoring some issues in PSRO itself) Nevertheless, the policy aggregator code doesn't yet support simultaneous moves games, and adding their support doesn't seem trivial. The fix therefore makes the code run, but skips exploitability computation.

We expect to have time to address this in a month or so; if exploitability computation were to be important for you, please don't hesitate to implement it ! :)

PaulFMMuller avatar Mar 26 '20 18:03 PaulFMMuller

Cheers for this @PaulFMMuller & @lanctot, much appreciated! Will let you guys know if I get around to implementing exploitability :)

oslumbers avatar Mar 27 '20 10:03 oslumbers

Hi, maybe I am doing something stupid, but for me it doesn't seem to be fixed:

When I run psro_v2_example.py with blotto as the game, then regardless of whether I set symmetric_game to True or False, I get the following error:

File "/Users/julianstastny/Code/open_spiel/open_spiel/python/examples/psro_v2_example.py", line 310, in main
    oracle, agents = init_br_responder(env)
  File "/Users/julianstastny/Code/open_spiel/open_spiel/python/examples/psro_v2_example.py", line 163, in init_br_responder
    oracle = best_response_oracle.BestResponseOracle(
  File "/Users/julianstastny/Code/open_spiel/open_spiel/python/algorithms/psro_v2/best_response_oracle.py", line 58, in __init__
    utils.compute_states_and_info_states_if_none(
  File "/Users/julianstastny/Code/open_spiel/open_spiel/python/algorithms/psro_v2/utils.py", line 334, in compute_states_and_info_states_if_none
    state_to_information_state = {
  File "/Users/julianstastny/Code/open_spiel/open_spiel/python/algorithms/psro_v2/utils.py", line 335, in <dictcomp>
    state: all_states[state].information_state_string()
RuntimeError: /private/var/folders/ky/d6j8mhc916s0z6w0p91n27_r0000gn/T/pip-req-build-u6sxk4bj/open_spiel/../open_spiel/normal_form_game.h:47 player >= 0
player = -2, 0 = 0

So there seems to be an issue with the game being simultaneous. I have the same problem with a (repeated) matrix game I created. It also happens when setting the oracle-type to "PG" or "DQN", though then the errors appear like this:

 File "/Users/julianstastny/Code/open_spiel/open_spiel/python/examples/psro_v2_example.py", line 312, in main
    gpsro_looper(env, oracle, agents)
  File "/Users/julianstastny/Code/open_spiel/open_spiel/python/examples/psro_v2_example.py", line 250, in gpsro_looper
    g_psro_solver = psro_v2.PSROSolver(
  File "/Users/julianstastny/Code/open_spiel/open_spiel/python/algorithms/psro_v2/psro_v2.py", line 164, in __init__
    super(PSROSolver, self).__init__(
  File "/Users/julianstastny/Code/open_spiel/open_spiel/python/algorithms/psro_v2/abstract_meta_trainer.py", line 174, in __init__
    self._initialize_game_state()
  File "/Users/julianstastny/Code/open_spiel/open_spiel/python/algorithms/psro_v2/psro_v2.py", line 185, in _initialize_game_state
    self.update_empirical_gamestate(seed=None)
  File "/Users/julianstastny/Code/open_spiel/open_spiel/python/algorithms/psro_v2/psro_v2.py", line 432, in update_empirical_gamestate
    utility_estimates = self.sample_episodes(estimated_policies,
  File "/Users/julianstastny/Code/open_spiel/open_spiel/python/algorithms/psro_v2/abstract_meta_trainer.py", line 222, in sample_episodes
    totals += sample_episode(self._game.new_initial_state(),
  File "/Users/julianstastny/Code/open_spiel/open_spiel/python/algorithms/psro_v2/abstract_meta_trainer.py", line 75, in sample_episode
    state_policy = policies[player](state, player)
  File "/Users/julianstastny/Code/open_spiel/open_spiel/python/policy.py", line 103, in __call__
    return self.action_probabilities(state, player_id)
  File "/Users/julianstastny/Code/open_spiel/open_spiel/python/algorithms/psro_v2/rl_policy.py", line 78, in action_probabilities
    state.information_state_tensor(cur_player))
RuntimeError: /private/var/folders/ky/d6j8mhc916s0z6w0p91n27_r0000gn/T/pip-req-build-u6sxk4bj/open_spiel/spiel.cc:717 player >= 0
player = -2, 0 = 0

Thanks in advance!

julianstastny avatar Sep 06 '20 13:09 julianstastny

Update: I was able to "fix" it at least for DQN, by noticing that the problem is that DQN.step() returns an empty list when sampling the episode via the abstract_meta_trainer, i.e. that DQN.step() calls its else-block. This is because self.player_id != time_step.current_player() when calling in abstract_meta_trainer, so I could fix it for my use case by inserting a or time_step.current_player() >= 0 into the condition in DQN.step(). In addition, rl_policy.py must be cur_player = state.current_player() if player_id is None else player_id in line 70. I bet this would create problems for other games, probably if they also have non-simultaneous moves, but maybe this helps for fixing the overall problem :)

julianstastny avatar Sep 07 '20 09:09 julianstastny

Hi Julian,

We're rolling back an older, stronger fix we had let go of in favor of something more nuanced - but that was less tested. Games are now all loaded as turn-based, removing issues with non-sequential games. The fix should be included in the next update.

Tell us if that fixes things on your side (It does on ours). If the turn-based loading is an issue, I'll be happy to review a push request :)

PaulFMMuller avatar Sep 10 '20 20:09 PaulFMMuller

Hi, The 'fix' does not seem to work for me. When I run psro_v2_example.py (openspiel version 0.3.1) with oracle_type="PG", and symmetric_game=True on the default settings (game=kuhn_poker, load as turn based game), I still got

prob_dict = {action: p[action] for action in legal_actions}
IndexError: list index out of range

from rl_policy.py, line 96

It looks like the problem is caused by policy_gradient.py, line 307

if (not time_step.last()) and (time_step.is_simultaneous_move() or self.player_id == time_step.current_player()):,

where the last condition is False, so it return an empty.

Any suggestion on how to fix this problem? Thanks!

MachengShen avatar Jun 03 '21 09:06 MachengShen