示例#1
0
def create_flash_env(env_id, client_id, remotes, **_):
    env = gym.make(env_id)
    env = Vision(env)
    env = Logger(env)
    env = BlockingReset(env)

    reg = universe.runtime_spec('flashgames').server_registry
    height = reg[env_id]["height"]
    width = reg[env_id]["width"]
    env = CropScreen(env, height, width, 84, 18)
    env = FlashRescale(env)

    keys = ['left', 'right', 'up', 'down', 'x']
    env = DiscreteToFixedKeysVNCActions(env, keys)
    env = EpisodeID(env)
    env = DiagnosticsInfo(env)
    env = Unvectorize(env)
    env.configure(fps=5.0,
                  remotes=remotes,
                  start_timeout=15 * 60,
                  client_id=client_id,
                  vnc_driver='go',
                  vnc_kwargs={
                      'encoding': 'tight',
                      'compress_level': 0,
                      'fine_quality_level': 50,
                      'subsample_level': 3
                  })
    return env
示例#2
0
def create_flash_env(env_id, client_id, remotes, **_):
    env = gym.make(env_id)
    env = Vision(env)
    env = Logger(env)
    env = BlockingReset(env)

    reg = universe.runtime_spec('flashgames').server_registry
    height = reg[env_id]["height"]
    width = reg[env_id]["width"]
    env = CropScreen(env, height, width, 84, 18)
    env = FlashRescale(env)

    keys = ['left', 'right', 'up', 'down', 'x']
    if env_id == 'flashgames.NeonRace-v0':
        # Better key space for this game.
        keys = ['left', 'right', 'up', 'left up', 'right up', 'down', 'up x']
    logger.info('create_flash_env(%s): keys=%s', env_id, keys)

    env = DiscreteToFixedKeysVNCActions(env, keys)
    env = EpisodeID(env)
    env = DiagnosticsInfo(env)
    env = Unvectorize(env)
    env.configure(fps=5.0, remotes=remotes, start_timeout=15 * 60, client_id=client_id,
                  vnc_driver='go', vnc_kwargs={
                    'encoding': 'tight', 'compress_level': 0,
                    'fine_quality_level': 50, 'subsample_level': 3})
    return env
示例#3
0
def create_flash_env(env_id, client_id, remotes, **_):
    env = gym.make(env_id)
    env = Vision(env)
    env = BlockingReset(env)

    reg = universe.runtime_spec('flashgames').server_registry
    height = reg[env_id]["height"]
    width = reg[env_id]["width"]
    env = CropScreen(env, height, width, 84, 18)
    env = FlashRescale(env)

    keys = ['left', 'right', 'up', 'down', 'x']
    if env_id == 'flashgames.NeonRace-v0':
        # Better key space for this game.
        keys = ['left', 'right', 'up', 'left up', 'right up', 'down', 'up x']
    print('Create Flash Game [{}]: keys={}'.format(env_id, keys))

    env = DiscreteToFixedKeysVNCActions(env, keys)
    env = Unvectorize(env)
    env.configure(fps=5.0,
                  remotes=remotes,
                  start_timeout=15 * 60,
                  client_id=client_id,
                  vnc_driver='go',
                  vnc_kwargs={
                      'encoding': 'tight',
                      'compress_level': 0,
                      'fine_quality_level': 50,
                      'subsample_level': 3
                  })
    return env
示例#4
0
def CropObservations(env):
    """"
    Crops the visual observations of an environment so that they only contain the game screen.
    Removes anything outside the game that usually belongs to universe (browser borders and so on).
    """
    if env.spec.tags.get('flashgames', False):
        spec = runtime_spec('flashgames').server_registry[env.spec.id]
        return _CropObservations(env, x=18, y=84, height=spec["height"], width=spec["width"])
    elif (env.spec.tags.get('atari', False) and env.spec.tags.get('vnc', False)):
        return _CropObservations(env, height=194, width=160)
    else:
        # if unknown environment (or local atari), do nothing
        return env
示例#5
0
def create_flash_env(env_id, client_id, remotes, **_):
    env = gym.make(env_id)
    env = Vision(env)
    env = Logger(env)
    env = BlockingReset(env)

    reg = universe.runtime_spec('flashgames').server_registry
    height = reg[env_id]["height"]
    width = reg[env_id]["width"]
    env = CropScreen(env, height, width, 84, 18)
    env = FlashRescale(env)

    #env = NormalizedEnv(env)

    keys = ['left', 'right', 'up', 'down', 'x']
    if env_id == 'flashgames.NeonRace-v0':
        # Better key space for this game.
        keys = ['left', 'right', 'up', 'left up', 'right up', 'down', 'up x']
    if env_id == 'flashgames.LuxUltimate-v0':
        keys = ['a', 's', 'd']
    if env_id == 'flashgames.RetroRunner-v0':
        keys = ['up', 'right']
    if env_id == 'flashgames.RunRamRun-v0-v0':
        keys = ['up', 'down']
    logger.info('create_flash_env(%s): keys=%s', env_id, keys)

    env = DiscreteToFixedKeysVNCActions(env, keys)
    env = EpisodeID(env)
    env = DiagnosticsInfo(env)
    if "multi_obs_settings" in _.keys():
        _multi = [env] + list(_["multi_obs_settings"])
        #env = SplitScreen(*_multi)
        env = SplitScreen(*_multi)

    #env = Unvectorize(env)
    if not "no_config" in _.keys():
        env.configure(fps=5.0,
                      remotes=remotes,
                      start_timeout=15 * 60,
                      client_id=client_id,
                      vnc_driver='go',
                      vnc_kwargs={
                          'encoding': 'tight',
                          'compress_level': 0,
                          'fine_quality_level': 50,
                          'subsample_level': 3
                      },
                      replace_on_crash=True)
    return env
示例#6
0
def create_flash_env(env_id, client_id, remotes, **_):
    """
    Create a Flash environment by passing environment id.

    Parameters
    ----------
    env_id : str
        environment id to be registered in Gym
    client_id : str
        Client ID
    remotes : str
        BLANK
    kwargs : dict
        BLANK
    """
    env = gym.make(env_id)
    env = Vision(env)
    env = Logger(env)
    env = BlockingReset(env)

    reg = universe.runtime_spec('flashgames').server_registry
    height = reg[env_id]["height"]
    width = reg[env_id]["width"]
    env = CropScreen(env, height, width, 84, 18)
    env = FlashRescale(env)

    keys = ['left', 'right', 'up', 'down', 'x']
    if env_id == 'flashgames.NeonRace-v0':
        # Better key space for this game.
        keys = ['left', 'right', 'up', 'left up', 'right up', 'down', 'up x']
    logger.info('create_flash_env(%s): keys=%s', env_id, keys)

    env = DiscreteToFixedKeysVNCActions(env, keys)
    env = EpisodeID(env)
    env = DiagnosticsInfo(env)
    env = Unvectorize(env)
    env.configure(fps=5.0,
                  remotes=remotes,
                  start_timeout=15 * 60,
                  client_id=client_id,
                  vnc_driver='go',
                  vnc_kwargs={
                      'encoding': 'tight',
                      'compress_level': 0,
                      'fine_quality_level': 50,
                      'subsample_level': 3
                  })
    return env
示例#7
0
def CropObservations(env):
    """"
    Crops the visual observations of an environment so that they only contain the game screen.
    Removes anything outside the game that usually belongs to universe (browser borders and so on).
    """
    if env.spec.tags.get('flashgames', False):
        spec = runtime_spec('flashgames').server_registry[env.spec.id]
        return _CropObservations(env,
                                 x=18,
                                 y=84,
                                 height=spec["height"],
                                 width=spec["width"])
    elif (env.spec.tags.get('atari', False)
          and env.spec.tags.get('vnc', False)):
        return _CropObservations(env, height=194, width=160)
    else:
        # if unknown environment (or local atari), do nothing
        return env
示例#8
0
def main():
    parser = argparse.ArgumentParser(description=None)
    parser.add_argument('-v',
                        '--verbose',
                        action='count',
                        dest='verbosity',
                        default=0,
                        help='Set verbosity.')
    args = parser.parse_args()

    reg = universe.runtime_spec('flashgames').server_registry
    height = reg[env_id]["height"]
    width = reg[env_id]["width"]

    if args.verbosity == 0:
        logger.setLevel(logging.INFO)
    elif args.verbosity >= 1:
        logger.setLevel(logging.DEBUG)

    env = gym.make(env_id)
    env.configure(remotes=1)  # automatically creates a local docker container

    # Restrict the valid random actions. (Try removing this and see
    # what happens when the agent is given full control of the
    # keyboard/mouse.)
    # env = wrappers.experimental.SafeActionSpace(env)
    observation_n = env.reset()

    while True:
        # your agent here
        #
        # Try sending this instead of a random action: ('KeyEvent', 'ArrowUp', True)
        action_n = [env.action_space.sample() for ob in observation_n]
        print(action_n)
        observation_n, reward_n, done_n, info = env.step(action_n)
        if (observation_n[0] != None):
            print(observation_n[0])
            state = observation_n[0]['vision']
            # cv2.imshow('image', state)
            # plt.imshow(state)
            cv2.imwrite('my.jpg', state[84:height, 18:width, :])
            break
        print(observation_n)
        env.render()
示例#9
0
    else:
        # Only gym-core are seedable
        env.seed([0])
    env = wrappers.Logger(env)

    if args.monitor:
        env = wrappers.Monitor(env, '/tmp/vnc_random_agent', force=True)

    if args.actions == 'random':
        action_space = env.action_space
    elif args.actions == 'noop':
        action_space = NoopSpace()
    elif args.actions == 'forward':
        action_space = ForwardSpace()
    elif args.actions == 'click':
        spec = universe.runtime_spec('flashgames').server_registry[args.env_id]
        height = spec["height"]
        width = spec["width"]
        noclick_regions = [
            r['coordinates'] for r in spec['regions'] if r['type'] == 'noclick'
        ] if spec.get('regions') else []
        active_region = (CHROME_X_OFFSET, CHROME_Y_OFFSET,
                         CHROME_X_OFFSET + width, CHROME_Y_OFFSET + height)
        env = wrappers.SoftmaxClickMouse(env,
                                         active_region=active_region,
                                         noclick_regions=noclick_regions)
        action_space = env.action_space
    else:
        logger.error("Invalid action choice: {}".format(args.actions))
        exit(1)
示例#10
0
    else:
        # Only gym-core are seedable
        env.seed([0])
    env = wrappers.Logger(env)

    if args.monitor:
        env = wrappers.Monitor(env, '/tmp/vnc_random_agent', force=True)

    if args.actions == 'random':
        action_space = env.action_space
    elif args.actions == 'noop':
        action_space = NoopSpace()
    elif args.actions == 'forward':
        action_space = ForwardSpace()
    elif args.actions == 'click':
        spec = universe.runtime_spec('flashgames').server_registry[args.env_id]
        height = spec["height"]
        width = spec["width"]
        noclick_regions = [r['coordinates'] for r in spec['regions'] if r['type'] == 'noclick'] if spec.get('regions') else []
        active_region = (CHROME_X_OFFSET, CHROME_Y_OFFSET, CHROME_X_OFFSET + width, CHROME_Y_OFFSET + height)
        env = wrappers.SoftmaxClickMouse(env, active_region=active_region, noclick_regions=noclick_regions)
        action_space = env.action_space
    else:
        logger.error("Invalid action choice: {}".format(args.actions))
        exit(1)

    env.configure(
        fps=args.fps,
        # print_frequency=None,
        # ignore_clock_skew=True,
        remotes=args.remote,