alan-turing-institute / p2lab-pokemon

A Python library for running genetic algorithms to optimize Pokemon teams!
BSD 3-Clause "New" or "Revised" License
8 stars 1 forks source link

Poke-env errors when battling using teams of size greater than 1 #36

Closed phinate closed 1 year ago

phinate commented 1 year ago

When running many battles, we often hit this message:

Player 2 - ERROR - Unhandled exception raised while handling message:
>battle-gen7anythinggoes-268198
|
|t:|1687186125
|move|p2a: Ivysaur|Giga Drain|p1a: Wigglytuff
|-damage|p1a: Wigglytuff|24/100
|move|p1a: Wigglytuff|Hyper Voice|p2a: Ivysaur
|-damage|p2a: Ivysaur|255/324
|
|-heal|p1a: Wigglytuff|30/100|[from] item: Leftovers
|upkeep
|turn|9
Traceback (most recent call last):
  File "/Users/nsimpson/P2-Lab/poke-env/src/poke_env/player/player_network_interface.py", line 178, in _handle_message
    await self._handle_battle_message(split_messages)
  File "/Users/nsimpson/P2-Lab/poke-env/src/poke_env/player/player.py", line 332, in _handle_battle_message
    await self._handle_battle_request(battle)
  File "/Users/nsimpson/P2-Lab/poke-env/src/poke_env/player/player.py", line 354, in _handle_battle_request
    message = self.choose_move(battle)
              ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/nsimpson/P2-Lab/poke-env/src/poke_env/player/baselines.py", line 110, in choose_move
    physical_ratio = self._stat_estimation(active, "atk") / self._stat_estimation(
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/nsimpson/P2-Lab/poke-env/src/poke_env/player/baselines.py", line 98, in _stat_estimation
    if mon.boosts[stat] > 1:
       ^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'boosts'

Somehow, the pokemon object here is being treated as non-None, but is None.@AoifeHughes mentioned this might have something to do with switching?

This happens both with poke_env.players.RandomPlayer and poke_env.players.SimpleHeuristicsPlayer.

This is holding back most large-scale tests for now, but doesn't affect 1v1 scenarios.

AoifeHughes commented 1 year ago

Okay, looking now. Would be super useful to have a solid reproducible example. Im going to set some seeds and see if I can't proc it.

AoifeHughes commented 1 year ago

Hmmm, does this happen at the start, mid or end of a battle. I'm wondering if that Wigglytuff vs. Ivysaur battle is over and then it triggers?

AoifeHughes commented 1 year ago

oho I managed to trigger it again :)

 cd /Users/ahughes/git/P2-Lab ; /usr/bin/env /usr/bin/python3 /Users/ahughes/.vscode/extensions/ms-python.python-2023.10.1/pythonFiles/lib/python/debugpy/adapter/../../debugpy/launcher 61102 -- -m p2lab -s 2 -g 2 
Generation 0:
Battling!: 100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 435/435 [01:28<00:00,  4.91it/s]
Generation 1:
Battling!:   0%|                                                                                                                                                                                                                          | 0/406 [00:00<?, ?it/s]None
2023-06-20 07:55:14,220 - Player 2 - ERROR - Unhandled exception raised while handling message:
>battle-gen7anythinggoes-312177
|
|t:|1687244114
|move|p1a: Hypno|Psychic|p2a: Hypno
|-resisted|p2a: Hypno
|-damage|p2a: Hypno|335/374
|move|p2a: Hypno|Psychic|p1a: Hypno
|-resisted|p1a: Hypno
|-damage|p1a: Hypno|90/100
|
|-heal|p2a: Hypno|358/374|[from] item: Leftovers
|-heal|p1a: Hypno|96/100|[from] item: Leftovers
|upkeep
|turn|2
Traceback (most recent call last):
  File "/Users/ahughes/miniconda3/envs/playground/lib/python3.10/site-packages/poke_env/player/player_network_interface.py", line 178, in _handle_message
    await self._handle_battle_message(split_messages)
  File "/Users/ahughes/miniconda3/envs/playground/lib/python3.10/site-packages/poke_env/player/player.py", line 335, in _handle_battle_message
    await self._handle_battle_request(battle)
  File "/Users/ahughes/miniconda3/envs/playground/lib/python3.10/site-packages/poke_env/player/player.py", line 357, in _handle_battle_request
    message = self.choose_move(battle)
  File "/Users/ahughes/miniconda3/envs/playground/lib/python3.10/site-packages/poke_env/player/baselines.py", line 113, in choose_move
    physical_ratio = self._stat_estimation(active, "atk") / self._stat_estimation(
  File "/Users/ahughes/miniconda3/envs/playground/lib/python3.10/site-packages/poke_env/player/baselines.py", line 101, in _stat_estimation
    if mon.boosts[stat] > 1:
AttributeError: 'NoneType' object has no attribute 'boosts'
None
2023-06-20 07:55:14,227 - Player 1 - ERROR - Unhandled exception raised while handling message:
>battle-gen7anythinggoes-312177
|
|t:|1687244114
|move|p1a: Hypno|Psychic|p2a: Hypno
|-resisted|p2a: Hypno
|-damage|p2a: Hypno|90/100
|move|p2a: Hypno|Psychic|p1a: Hypno
|-resisted|p1a: Hypno
|-damage|p1a: Hypno|335/374
|
|-heal|p2a: Hypno|96/100|[from] item: Leftovers
|-heal|p1a: Hypno|358/374|[from] item: Leftovers
|upkeep
|turn|2
Traceback (most recent call last):
  File "/Users/ahughes/miniconda3/envs/playground/lib/python3.10/site-packages/poke_env/player/player_network_interface.py", line 178, in _handle_message
    await self._handle_battle_message(split_messages)
  File "/Users/ahughes/miniconda3/envs/playground/lib/python3.10/site-packages/poke_env/player/player.py", line 335, in _handle_battle_message
    await self._handle_battle_request(battle)
  File "/Users/ahughes/miniconda3/envs/playground/lib/python3.10/site-packages/poke_env/player/player.py", line 357, in _handle_battle_request
    message = self.choose_move(battle)
  File "/Users/ahughes/miniconda3/envs/playground/lib/python3.10/site-packages/poke_env/player/baselines.py", line 113, in choose_move
    physical_ratio = self._stat_estimation(active, "atk") / self._stat_estimation(
  File "/Users/ahughes/miniconda3/envs/playground/lib/python3.10/site-packages/poke_env/player/baselines.py", line 101, in _stat_estimation
    if mon.boosts[stat] > 1:
AttributeError: 'NoneType' object has no attribute 'boosts'

So it's made a match between 2 players with no Pokemon? Hmmmm 🤔

AoifeHughes commented 1 year ago

Also, just saying leftovers were in both these teams

AoifeHughes commented 1 year ago
(playground) ➜  P2-Lab git:(patch/poke-env) ✗  cd /Users/ahughes/git/P2-Lab ; /usr/bin/env /usr/bin/python3 /Users/ahughes/.vscode/extensions/ms-python.python-2023.10.1/pythonFiles/lib/python/debugpy/adapter/../../deb
ugpy/launcher 61340 -- -m p2lab -s 1337 -g 100 
Generation 0:
Battling!: 100%|███████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 435/435 [01:23<00:00,  5.24it/s]
Generation 1:
Battling!:   0%|▍                                                                                                                                                                        | 1/406 [00:00<00:53,  7.56it/s]None
2023-06-20 08:43:19,074 - Player 2 - ERROR - Unhandled exception raised while handling message:
>battle-gen7anythinggoes-338127
|
|t:|1687246999
|switch|p1a: Nidoran♂|Nidoran-M, M|100/100|[from] U-turn
|move|p2a: Wartortle|Scald|p1a: Nidoran♂
|-damage|p1a: Nidoran♂|72/100
|
|upkeep
|turn|2
Traceback (most recent call last):
  File "/Users/ahughes/miniconda3/envs/playground/lib/python3.10/site-packages/poke_env/player/player_network_interface.py", line 178, in _handle_message
    await self._handle_battle_message(split_messages)
  File "/Users/ahughes/miniconda3/envs/playground/lib/python3.10/site-packages/poke_env/player/player.py", line 335, in _handle_battle_message
    await self._handle_battle_request(battle)
  File "/Users/ahughes/miniconda3/envs/playground/lib/python3.10/site-packages/poke_env/player/player.py", line 357, in _handle_battle_request
    message = self.choose_move(battle)
  File "/Users/ahughes/miniconda3/envs/playground/lib/python3.10/site-packages/poke_env/player/baselines.py", line 113, in choose_move
    physical_ratio = self._stat_estimation(active, "atk") / self._stat_estimation(
  File "/Users/ahughes/miniconda3/envs/playground/lib/python3.10/site-packages/poke_env/player/baselines.py", line 101, in _stat_estimation
    if mon.boosts[stat] > 1:
AttributeError: 'NoneType' object has no attribute 'boosts'
phinate commented 1 year ago

Same error, more verbose message (with Player.log_level = 10)

|error|[Invalid choice] Can't switch: You can't switch to an active Pokémon
2023-06-20 08:54:29,254 - Player 2 - PS_ERROR - Error message received: |error|[Invalid choice] Can't switch: You can't switch to an active Pokémon
2023-06-20 08:54:29,254 - Player 2 - INFO - >>> battle-gen7anythinggoes-290712|/choose switch sandshrew
2023-06-20 08:54:29,254 - Player 2 - INFO - <<< >battle-gen7anythinggoes-290712
|error|[Invalid choice] Can't switch: You can't switch to an active Pokémon
2023-06-20 08:54:29,254 - Player 2 - PS_ERROR - Error message received: |error|[Invalid choice] Can't switch: You can't switch to an active Pokémon
2023-06-20 08:54:29,254 - Player 2 - INFO - >>> battle-gen7anythinggoes-290712|/choose switch sandshrew
2023-06-20 08:54:29,254 - Player 2 - INFO - <<< >battle-gen7anythinggoes-290712
|error|[Invalid choice] Can't switch: You can't switch to an active Pokémon
2023-06-20 08:54:29,254 - Player 2 - PS_ERROR - Error message received: |error|[Invalid choice] Can't switch: You can't switch to an active Pokémon
2023-06-20 08:54:29,254 - Player 2 - INFO - >>> battle-gen7anythinggoes-290712|/choose default
2023-06-20 08:54:29,256 - Player 1 - INFO - <<< >battle-gen7anythinggoes-290712
|request|{"active":[{"moves":[{"move":"Brave Bird","id":"bravebird","pp":23,"maxpp":24,"target":"any","disabled":false},{"move":"U-turn","id":"uturn","pp":32,"maxpp":32,"target":"normal","disabled":false},{"move":"Poison Fang","id":"poisonfang","pp":24,"maxpp":24,"target":"normal","disabled":false},{"move":"Defog","id":"defog","pp":24,"maxpp":24,"target":"normal","disabled":false}]}],"side":{"name":"Player 1","id":"p1","pokemon":[{"ident":"p1: Zubat","details":"Zubat, F","condition":"199/221","active":true,"stats":{"atk":189,"def":106,"spa":86,"spd":117,"spe":229},"moves":["bravebird","uturn","poisonfang","defog"],"baseAbility":"infiltrator","item":"focussash","pokeball":"pokeball","ability":"infiltrator"},{"ident":"p1: Starmie","details":"Starmie","condition":"261/261","active":false,"stats":{"atk":167,"def":206,"spa":299,"spd":207,"spe":361},"moves":["hydropump","thunderbolt","icebeam","recover"],"baseAbility":"naturalcure","item":"leftovers","pokeball":"pokeball","ability":"naturalcure"}]},"rqid":6}
2023-06-20 08:54:29,256 - Player 2 - INFO - <<< >battle-gen7anythinggoes-290712
|request|{"active":[{"moves":[{"move":"Earthquake","id":"earthquake","pp":15,"maxpp":16,"target":"allAdjacent","disabled":false},{"move":"Stealth Rock","id":"stealthrock","pp":32,"maxpp":32,"target":"foeSide","disabled":false},{"move":"Toxic","id":"toxic","pp":16,"maxpp":16,"target":"normal","disabled":false},{"move":"Rapid Spin","id":"rapidspin","pp":64,"maxpp":64,"target":"normal","disabled":false}]}],"side":{"name":"Player 2","id":"p2","pokemon":[{"ident":"p2: Sandshrew","details":"Sandshrew, M","condition":"237/304","active":true,"stats":{"atk":186,"def":295,"spa":68,"spd":97,"spe":116},"moves":["earthquake","stealthrock","toxic","rapidspin"],"baseAbility":"sandrush","item":"eviolite","pokeball":"pokeball","ability":"sandrush"},{"ident":"p2: Sandshrew","details":"Sandshrew, M","condition":"304/304","active":false,"stats":{"atk":186,"def":295,"spa":68,"spd":97,"spe":116},"moves":["earthquake","stealthrock","toxic","rapidspin"],"baseAbility":"sandrush","item":"eviolite","pokeball":"pokeball","ability":"sandrush"}]},"rqid":7}
2023-06-20 08:54:29,256 - Player 1 - INFO - <<< >battle-gen7anythinggoes-290712
|
|t:|1687247669
|move|p1a: Zubat|Brave Bird|p2a: Sandshrew
|-damage|p2a: Sandshrew|78/100
|-damage|p1a: Zubat|199/221|[from] Recoil
|move|p2a: Sandshrew|Earthquake|p1a: Zubat
|-immune|p1a: Zubat
|
|upkeep
|turn|2
2023-06-20 08:54:29,256 - Player 1 - INFO - >>> battle-gen7anythinggoes-290712|/choose move bravebird
2023-06-20 08:54:29,256 - Player 2 - INFO - <<< >battle-gen7anythinggoes-290712
|
|t:|1687247669
|move|p1a: Zubat|Brave Bird|p2a: Sandshrew
|-damage|p2a: Sandshrew|237/304
|-damage|p1a: Zubat|91/100|[from] Recoil
|move|p2a: Sandshrew|Earthquake|p1a: Zubat
|-immune|p1a: Zubat
|
|upkeep
|turn|2
2023-06-20 08:54:29,256 - Player 2 - ERROR - Unhandled exception raised while handling message:
>battle-gen7anythinggoes-290712
|
|t:|1687247669
|move|p1a: Zubat|Brave Bird|p2a: Sandshrew
|-damage|p2a: Sandshrew|237/304
|-damage|p1a: Zubat|91/100|[from] Recoil
|move|p2a: Sandshrew|Earthquake|p1a: Zubat
|-immune|p1a: Zubat
|
|upkeep
|turn|2
Traceback (most recent call last):
  File "/Users/nsimpson/P2-Lab/poke-env/src/poke_env/player/player_network_interface.py", line 178, in _handle_message
    await self._handle_battle_message(split_messages)
  File "/Users/nsimpson/P2-Lab/poke-env/src/poke_env/player/player.py", line 332, in _handle_battle_message
    await self._handle_battle_request(battle)
  File "/Users/nsimpson/P2-Lab/poke-env/src/poke_env/player/player.py", line 354, in _handle_battle_request
    message = self.choose_move(battle)
              ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/nsimpson/P2-Lab/poke-env/src/poke_env/player/baselines.py", line 110, in choose_move
    physical_ratio = self._stat_estimation(active, "atk") / self._stat_estimation(
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/nsimpson/P2-Lab/poke-env/src/poke_env/player/baselines.py", line 98, in _stat_estimation
    if mon.boosts[stat] > 1:
       ^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'boosts'
2023-06-20 09:01:17,927 - Player 1 - INFO - <<< |updatesearch|{"searching":[],"games":{"battle-gen7anythinggoes-290712":"[Gen 7] Anything Goes Battle","battle-gen7anythinggoes-290713":"[Gen 7] Anything Goes Battle"}}
2023-06-20 09:01:17,927 - Player 2 - INFO - <<< |updatesearch|{"searching":[],"games":{"battle-gen7anythinggoes-290712":"[Gen 7] Anything Goes Battle","battle-gen7anythinggoes-290713":"[Gen 7] Anything Goes Battle"}}
Task exception was never retrieved
future: <Task finished name='Task-726481' coro=<PlayerNetwork._handle_message() done, defined at /Users/nsimpson/P2-Lab/poke-env/src/poke_env/player/player_network_interface.py:164> exception=AttributeError("'NoneType' object has no attribute 'boosts'")>
Traceback (most recent call last):
  File "/Users/nsimpson/P2-Lab/poke-env/src/poke_env/player/player_network_interface.py", line 229, in _handle_message
    raise exception
  File "/Users/nsimpson/P2-Lab/poke-env/src/poke_env/player/player_network_interface.py", line 178, in _handle_message
    await self._handle_battle_message(split_messages)
  File "/Users/nsimpson/P2-Lab/poke-env/src/poke_env/player/player.py", line 332, in _handle_battle_message
    await self._handle_battle_request(battle)
  File "/Users/nsimpson/P2-Lab/poke-env/src/poke_env/player/player.py", line 354, in _handle_battle_request
    message = self.choose_move(battle)
              ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/nsimpson/P2-Lab/poke-env/src/poke_env/player/baselines.py", line 110, in choose_move
    physical_ratio = self._stat_estimation(active, "atk") / self._stat_estimation(
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/nsimpson/P2-Lab/poke-env/src/poke_env/player/baselines.py", line 98, in _stat_estimation
    if mon.boosts[stat] > 1:
       ^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'boosts'
phinate commented 1 year ago

just to remind myself later: this is all happening because the opponent's pokemon somehow don't have the active property set, which makes the helper fn to get opponents active pokemon return None.

AoifeHughes commented 1 year ago

hmm, I'm going to replicate one of these battles explicitly and see what happens (once I work out how to trigger debugging from async)

AoifeHughes commented 1 year ago

@phinate do you have the full teams of the error you posted? Current hypothesis is that this happens when a team has two identical Pokemon. Not sure why that is exactly... still digging

AoifeHughes commented 1 year ago

I think https://github.com/alan-turing-institute/p2lab-pokemon/issues/39 could have something to do with

AoifeHughes commented 1 year ago

Closing for now as I think a recent PR addresses - please re-open if this crops up again