openai / retro

Retro Games in Gym
MIT License
3.39k stars 526 forks source link

Retro Gets Stuck with Pong-Atari Multiplayers #215

Closed Ann-eat-apple closed 3 years ago

Ann-eat-apple commented 4 years ago

Issue summary

When I launch Pong-Atari with 2 players, it looks like the game gets stuck (never terminate), please render to see it.

[Put a detailed description of the issue here.]

import retro
import matplotlib.pyplot as plt
import gym
#from IPython import display

import gym
import gym.spaces
import numpy as np
import retro

def PongDiscretizer(env):
    """
    Discretize Retro Pong-Atari2600 environment
    """
    return Discretizer(env, buttons=env.unwrapped.buttons, combos=[['DOWN'], ['UP'], ['BUTTON'],])

class Discretizer(gym.ActionWrapper):
    """
    Wrap a gym environment and make it use discrete actions.
    based on https://github.com/openai/retro-baselines/blob/master/agents/sonic_util.py
    Args:
        buttons: ordered list of buttons, corresponding to each dimension of the MultiBinary action space
        combos: ordered list of lists of valid button combinations
    """

    def __init__(self, env, buttons, combos):
        super().__init__(env)
        assert isinstance(env.action_space, gym.spaces.MultiBinary)
        self._decode_discrete_action = []
        self._decode_discrete_action2 = []
        for combo in combos:
            arr = np.array([False] * env.action_space.n)
            for button in combo:
                arr[buttons.index(button)] = True
            self._decode_discrete_action.append(arr)
        # # pkayer 2 : 7: DOWN, 6: 'UP', 15:'BUTTOM'

        arr = np.array([False] * env.action_space.n)
        arr[7] = True
        self._decode_discrete_action2.append(arr)

        arr = np.array([False] * env.action_space.n)
        arr[6] = True
        self._decode_discrete_action2.append(arr)

        arr = np.array([False] * env.action_space.n)
        arr[15] = True
        self._decode_discrete_action2.append(arr)

        self.action_space = gym.spaces.Discrete(len(self._decode_discrete_action))

        print(arr)
        print(self._decode_discrete_action)
    def action(self, act1, act2):
        act1_v = self._decode_discrete_action[act1].copy()
        act2_v = self._decode_discrete_action2[act2].copy()
        return np.logical_or(act1_v, act2_v).copy()

    def step(self, act1, act2):
        return self.env.step(self.action(act1, act2))
        #return self._decode_discrete_action[act].copy()

env = PongDiscretizer(retro.make(game='Pong-Atari2600', players=2))
env.reset()
done = False
steps = 0
total_rew = 0.0
while not done:
    #obs, rew, done, info = env.step(np.random.randint(env.action_space.n))
    #obs, rew, done, info = env.step(steps %3, np.random.randint(env.action_space.n))
    obs, rew, done, info = env.step(1,1 )
#     plt.imshow(env.render(mode='rgb_array'))
#     display.display(plt.gcf())  
    #env.render()
    steps += 1
    print(steps)
    if done:
        print("done == True")
        print("info: {}".format(info))
        print('total step ={}'.format(steps))
        print(rew)
        break

System information

christopher-hesse commented 3 years ago

This looks similar to https://github.com/openai/retro/issues/198

For whatever reason, Atari Pong in retro has two problems: the button layout is weird, and the game requires that each player hit "BUTTON" after scoring to serve the ball.

2 player atari has 16 buttons available, here are some decent names for those buttons (in order): ["P1_BUTTON", None, "P1_SELECT", "P1_RESET", "P1_UP", "P1_DOWN", "P1_LEFT", "P1_RIGHT", "P2_BUTTON", None, "P2_SELECT", "P2_RESET", "P2_UP", "P2_DOWN", "P2_LEFT", "P2_RIGHT"]

These seem to work fine for FishingDerby-Atari2600, but for Pong-Atari2600, you actually need to use some different buttons:

Here are the indices for button, up, and down for each player (unless I counted incorrectly) P1: 0, 4, 5 P2: 15, 6, 7

Your script seems to work fine if I do obs, rew, done, info = env.step(np.random.randint(env.action_space.n), np.random.randint(env.action_space.n))

christopherhesse commented 3 years ago

Closing due to inactivity

djsu-andy commented 2 years ago

any help on this? I got the same issue, and the ball gets stuck

djsu-andy commented 2 years ago

@christopherhesse

christopherhesse commented 2 years ago

Did you try the workaround that I posted?