示例#1
0
def create_game(
        environment_config: EnvironmentConfig
) -> t.Tuple[DoomGame, ActionList]:
    """Creates an instance of VizDoom.

    Args:
        scenario: The name of the scenario to play.
        environment_config: An environment configuration instance.

    Returns:
        A Doom game instance that respects OpenAI's gym interface.
    """
    game = DoomGame()

    # Game configuration
    game.load_config(f'{paths.SCENARIOS}/{environment_config.scenario}.cfg')
    game.set_doom_scenario_path(
        f'{paths.SCENARIOS}/{environment_config.scenario}.wad')
    game.set_mode(environment_config.game_mode)
    game.set_screen_format(environment_config.screen_mode)
    game.init()

    possible_actions = controls.get_available_actions(
        game.get_available_buttons())

    return game, possible_actions
示例#2
0
class DoomTrainer:
    def __init__(self, params):
        self.game = DoomGame()
        self.game.load_config("../scenarios/" + params.scenario + ".cfg")

        if params.model == 'human':
            self.game.set_mode(Mode.SPECTATOR)
        else:
            self.actions = create_actions(params.scenario)

    def play_human(self):
        episodes = 10
        for i in range(episodes):

            self.game.new_episode()
            while not self.game.is_episode_finished():
                self.game.get_state()
                self.game.advance_action()
                self.game.get_last_action()
                self.game.get_last_reward()

    def start_game(self):
        self.game.init()

    def set_seed(self, seed):
        self.game.set_seed(seed)

    def new_episode(self):
        self.game.new_episode()

    def get_screen(self):
        return torch.from_numpy(
            scale(self.game.get_state().screen_buffer, None, None, True))

    def get_screen_buffer(self):
        return self.game.get_state().screen_buffer

    def make_action(self, action):
        reward = self.game.make_action(self.actions[action])
        done = self.game.is_episode_finished()

        return reward, done

    def num_actions(self):
        return len(self.actions)