def _setup_game(scenario: str, visible: bool): game = DoomGame() game.load_config('data/vizdoom/%s.cfg' % scenario) game.set_doom_scenario_path('data/vizdoom/%s.wad' % scenario) game.set_window_visible(visible) game.set_sound_enabled(False) game.init() return game
def create_environment(): game = DoomGame() game.load_config("basic.cfg") game.set_doom_scenario_path("basic.wad") game.init() left = [1, 0, 0] right = [0, 1, 0] shoot = [0, 0, 1] possible_actions = [left, right, shoot] return game, possible_actions
def test_environment(): game = DoomGame() # https://github.com/simoninithomas/Deep_reinforcement_learning_Course/blob/master/Deep%20Q%20Learning/Doom/basic.cfg game.load_config('basic.cfg') game.set_doom_scenario_path('basic.wad') game.init() shoot = [0, 0, 1] left = [1, 0, 0] right = [0, 1, 0] actions = [shoot, left, right] episodes = 10 for i in range(episodes): game.new_episode() while not game.is_episode_finished(): state = game.get_state() img = state.screen_buffer misc = state.game_variables action = random.choice(actions) print('Action', action) reward = game.make_action(action) print('Reward', reward) time.sleep(0.02) print('Result', game.get_total_reward()) time.sleep(2) game.close()
def create_environment(): game = DoomGame() game.load_config('defend_the_center.cfg') game.set_doom_scenario_path('defend_the_center.wad') game.init() possible_actions = np.identity(3, dtype=int).tolist() return game, possible_actions
def __init__(self, vizdoom_dir=os.path.expanduser('~/ViZDoom'), window_visible=True, scenario='basic', skipcount=10, resolution_width=640, sleep=0.0, seed=None): self.skipcount = skipcount self.sleep = sleep sys.path.append(os.path.join(vizdoom_dir, "examples/python")) from vizdoom import DoomGame from vizdoom import ScreenFormat from vizdoom import ScreenResolution game = DoomGame() if seed is not None: assert seed >= 0 and seed < 2 ** 16, \ "ViZDoom's random seed must be represented by unsigned int" else: # Use numpy's random state seed = np.random.randint(0, 2**16) game.set_seed(seed) # Load a config file game.load_config( os.path.join(vizdoom_dir, "examples", 'config', scenario + '.cfg')) # Replace default relative paths with actual paths game.set_vizdoom_path(os.path.join(vizdoom_dir, "bin/vizdoom")) game.set_doom_game_path( os.path.join(vizdoom_dir, 'scenarios/freedoom2.wad')) game.set_doom_scenario_path( os.path.join(vizdoom_dir, 'scenarios', scenario + '.wad')) # Set screen settings resolutions = { 640: ScreenResolution.RES_640X480, 320: ScreenResolution.RES_320X240, 160: ScreenResolution.RES_160X120 } game.set_screen_resolution(resolutions[resolution_width]) game.set_screen_format(ScreenFormat.RGB24) game.set_window_visible(window_visible) game.set_sound_enabled(window_visible) game.init() self.game = game # Use one-hot actions self.n_actions = game.get_available_buttons_size() self.actions = [] for i in range(self.n_actions): self.actions.append([i == j for j in range(self.n_actions)])
def init(buttons): print("init") game = DoomGame() game.set_vizdoom_path("../../ViZDoom/bin/vizdoom") game.set_doom_game_path("../../ViZDoom/scenarios/freedoom2.wad") game.set_doom_scenario_path("../../ViZDoom/scenarios/basic.wad") game.set_doom_map("map01") game.set_screen_resolution(ScreenResolution.RES_320X240) game.set_screen_format(ScreenFormat.RGB24) game.set_depth_buffer_enabled(True) game.set_labels_buffer_enabled(True) game.set_automap_buffer_enabled(True) # Sets other rendering options game.set_render_hud(False) game.set_render_minimal_hud(False) game.set_render_crosshair(False) game.set_render_weapon(True) game.set_render_decals(False) game.set_render_particles(False) game.set_render_effects_sprites(False) # Adds buttons that will be allowed. for button in buttons: game.add_available_button(button) # Adds game variables that will be included in state. game.add_available_game_variable(GameVariable.AMMO2) game.add_available_game_variable(GameVariable.SELECTED_WEAPON) # Causes episodes to finish after 200 tics (actions) game.set_episode_timeout(300) # Makes episodes start after 10 tics (~after raising the weapon) game.set_episode_start_time(10) # Makes the window appear (turned on by default) game.set_window_visible(True) # Turns on the sound. (turned off by default) game.set_sound_enabled(True) # Sets the livin reward (for each move) to -1 game.set_living_reward(-1) # Sets ViZDoom mode (PLAYER, ASYNC_PLAYER, SPECTATOR, ASYNC_SPECTATOR, PLAYER mode is default) game.set_mode(Mode.PLAYER) # Initialize the game. Further configuration won't take any effect from now on. # game.set_console_enabled(True) game.init() return game
def create_environment(episode_render=True): game = DoomGame() game.load_config('basic.cfg') game.set_doom_scenario_path('basic.wad') game.set_window_visible(episode_render) game.init() left = [1, 0, 0] right = [0, 1, 0] shoot = [0, 0, 1] possible_actions = [left, right, shoot] return game, possible_actions
def __init__(self, state_size=(64, 64, 4), scenario='defend_the_center.cfg', record_episode=False): game = DoomGame() path_to_scenario = os.path.join(current_dir, scenario) game.load_config(path_to_scenario) game.set_sound_enabled(True) game.set_screen_resolution(ScreenResolution.RES_640X480) game.set_window_visible(False) game.set_available_game_variables( [GameVariable.KILLCOUNT, GameVariable.AMMO2, GameVariable.HEALTH]) game.init() self.game = game self.skiprate = 4 self.state = None self.state_size = state_size self.action_size = self.game.get_available_buttons_size() self.steps = 0 self.life = deque(maxlen=30) self.kills = deque(maxlen=30) self.record_episode = record_episode self.game_rec = []
def __init__(self, level='deathmatch', obs_type='ram'): # super(DoomEnv, self).__init__() EzPickle.__init__(self, level.split('.')[0], obs_type) assert obs_type in ('ram', 'image') level = level.split('.')[0] Config.init(level) self.curr_seed = 0 self.game = DoomGame() self.lock = (DoomLock()).get_lock() self.level = level self.obs_type = obs_type self.tick = 4 self._mode = 'algo' self.is_render_in_human_mode = True self.is_game_initialized = False self.is_level_loaded = False self.viewer = None self.set_game(self.level, resolution=None, render=True) print()
def __load_level(self, level=None): if level is not None: self.level = level.split('.')[0] self.is_level_loaded = False if self.is_level_loaded: return if self.is_game_initialized: self.is_game_initialized = False self.game.close() self.game = DoomGame() if not self.is_game_initialized: self.game.set_vizdoom_path(Config.VIZDOOM_PATH) self.game.set_doom_game_path(Config.FREEDOOM_PATH) # Common settings self.record_file_path = Config.RECORD_FILE_PATH self.game.load_config(Config.VIZDOOM_SCENARIO_PATH + Config.DOOM_SETTINGS[self.level][Config.CONFIG]) self.game.set_doom_scenario_path( Config.VIZDOOM_SCENARIO_PATH + Config.DOOM_SETTINGS[self.level][Config.SCENARIO]) if Config.DOOM_SETTINGS[self.level][Config.MAP] != '': self.game.set_doom_map( Config.DOOM_SETTINGS[self.level][Config.MAP]) self.game.set_doom_skill( Config.DOOM_SETTINGS[self.level][Config.DIFFICULTY]) self.allowed_actions = Config.DOOM_SETTINGS[self.level][Config.ACTIONS] self.available_game_variables = Config.DOOM_SETTINGS[self.level][ Config.GAME_VARIABLES] self.is_level_loaded = True
def doom_game(): game = DoomGame() #game.load_config("../scenarios/basic.cfg") game.load_config("../scenarios/defend_the_center.cfg") #game.set_doom_map("map01") game.set_screen_resolution(ScreenResolution.RES_320X240) #game.set_screen_resolution(ScreenResolution.RES_640X480) game.set_render_hud(False) game.set_render_crosshair(False) game.set_render_weapon(True) game.set_render_decals(False) game.set_render_particles(False) #game.add_available_button(Button.MOVE_LEFT) #game.add_available_button(Button.MOVE_RIGHT) game.add_available_button(Button.TURN_LEFT) game.add_available_button(Button.TURN_RIGHT) game.add_available_button(Button.ATTACK) game.set_episode_timeout(2100) game.set_episode_start_time(10) game.set_window_visible(True) #False) game.set_sound_enabled(False) game.set_living_reward(0.2) # -1 for basic game.set_mode(Mode.PLAYER) game.init() return game
def __init__(self, level): self.previous_level = -1 self.level = level self.game = DoomGame() self.loader = Loader() self.doom_dir = os.path.dirname(os.path.abspath(__file__)) self._mode = 'algo' # 'algo' or 'human' self.no_render = False # To disable double rendering in human mode self.viewer = None self.is_initialized = False # Indicates that reset() has been called self.curr_seed = 0 self.lock = (DoomLock()).get_lock() # self.action_space = spaces.Discrete(43) # used to be in the old code self.action_space = spaces.MultiBinary(NUM_ACTIONS) self.allowed_actions = list(range(NUM_ACTIONS)) self.screen_height = 120 self.screen_width = 160 self.screen_resolution = ScreenResolution.RES_160X120 self.observation_space = spaces.Box(low=0, high=255, shape=(self.screen_height, self.screen_width, 3), dtype=np.uint8) self.seed() self._configure()
def __init__(self, params): super(VizDoomEnv, self).__init__() self.params = params self.game = DoomGame() self.game.load_config(params.scenarioPath) self._viewer = None self.frameskip = params.frameskip self.inputShape = params.inputShape self.sequenceLength = params.sequenceLength self.seqInputShape = (self.inputShape[0] * self.sequenceLength, self.inputShape[1], self.inputShape[2]) self.gameVariables = params.gameVariables self.numGameVariables = len(self.gameVariables) self.action_space = spaces.MultiDiscrete( [2] * self.game.get_available_buttons_size()) self.action_space.dtype = 'uint8' output_shape = (self.game.get_screen_channels(), self.game.get_screen_height(), self.game.get_screen_width()) self.observation_space = spaces.Box(low=0, high=255, shape=output_shape, dtype='uint8') self.game.init() # Maintain a buffer of last seq len frames. self.frameBuffer = [np.zeros(self.inputShape)] * self.sequenceLength
def __init__(self, config='vizdoom_env/asset/default.cfg', verbose=False, perception_type='more_simple'): self.verbose = verbose self.game = DoomGame() self.game.load_config(config) if self.verbose: self.game.set_window_visible(True) self.game.set_screen_resolution(ScreenResolution.RES_1280X960) self.game_variables = self.game.get_available_game_variables() self.buttons = self.game.get_available_buttons() self.action_strings = [b.__str__().replace('Button.', '') for b in self.buttons] self.game_variable_strings = [v.__str__().replace('GameVariable.', '') for v in self.game_variables] self.perception_type = perception_type if perception_type == 'clear': self.distance_dict = CLEAR_DISTANCE_DICT self.horizontal_dict = CLEAR_HORIZONTAL_DICT elif perception_type == 'simple': pass elif perception_type == 'more_simple': pass else: self.distance_dict = DISTANCE_DICT self.horizontal_dict = HORIZONTAL_DICT
def __init__(self, scenario, path_to_config="doom/config"): self.game = DoomGame() self.game.load_config(path_to_config + "/" + scenario + ".cfg") self.game.set_doom_scenario_path(path_to_config + "/" + scenario + ".wad") self.game.set_window_visible(False) self.game.init() self.num_actions = len(self.game.get_available_buttons())
def configure_doom(config_name, episode_timeout): game = DoomGame() game.load_config(config_name) game.set_window_visible(False) game.set_labels_buffer_enabled(True) game.set_episode_timeout(episode_timeout) game.init() return game
def initialize_vizdoom(config): game = DoomGame() game.load_config(config) game.set_window_visible(False) game.set_mode(Mode.PLAYER) game.set_screen_format(ScreenFormat.GRAY8) game.set_screen_resolution(ScreenResolution.RES_640X480) game.init() return game
def initGameWithParams(configFilePath): game = DoomGame() game.load_config(configFilePath) game.set_window_visible(False) game.set_mode(Mode.PLAYER) game.set_screen_format(ScreenFormat.CRCGCB) game.set_screen_resolution(ScreenResolution.RES_400X225) game.init() return game
def __init__(self, config, visible, skiprate): self._game = DoomGame() self._game.load_config(config) self._game.set_window_visible(visible) self._game.set_mode(Mode.PLAYER) self._game.init() n_actions = self._game.get_available_buttons_size() self._actions = [list(a) for a in it.product([0, 1], repeat=n_actions)] self._skiprate = skiprate
def __init__(self, config='my_way_home.cfg', repeat_action=1, render=False): self._game = DoomGame() self._game.load_config(config) self._game.set_mode(Mode.PLAYER) self._game.set_screen_format(ScreenFormat.GRAY8) self._game.set_screen_resolution(ScreenResolution.RES_640X480) self._game.set_window_visible(render) self._game.init() self._actions = self._get_actions() self._repeat_action = repeat_action self._is_rendered = False
def __init__(self, cfg_name, repeat=1): super().__init__() self.game = DoomGame() self.game.load_config(f'./slm_lab/env/vizdoom/cfgs/{cfg_name}.cfg') self._viewer = None self.repeat = 1 # TODO In future, need to update action to handle (continuous) DELTA buttons using gym's Box space self.action_space = spaces.MultiDiscrete([2] * self.game.get_available_buttons_size()) self.action_space.dtype = 'uint8' output_shape = (self.game.get_screen_channels(), self.game.get_screen_height(), self.game.get_screen_width()) self.observation_space = spaces.Box(low=0, high=255, shape=output_shape, dtype='uint8') self.game.init()
def initialize_vizdoom(CONFIG_FILE_PATH, seed): print("Initializing doom...") game = DoomGame() game.load_config(CONFIG_FILE_PATH) game.set_window_visible(True) game.set_mode(Mode.PLAYER) game.set_screen_format(ScreenFormat.GRAY8) game.set_screen_resolution(ScreenResolution.RES_640X480) game.set_labels_buffer_enabled(True) game.set_seed(seed) game.init() print("Doom initialized.") return game
def __init__(self, cfg, name='Player1', color='0', map='map01'): game = DoomGame() game_args = "" game_args += " -name %s" % name game_args += " -colorset %s" % color game.add_game_args(game_args) game.load_config(cfg) game.set_death_penalty(1) game.set_doom_map(map) self.env = game self.observation_space = spaces.Box(0, 255, game.get_screen_size()) self.action_space = spaces.Discrete(game.get_available_buttons_size()) self.reward_range = None
def __init__(self, configuration): configuration = CONFIGURATIONS[configuration] game = DoomGame() game.load_config( os.path.join(CONFIGURATIONS_DIR, configuration + ".cfg")) game.set_screen_resolution(ScreenResolution.RES_160X120) game.set_window_visible(False) game.init() action_dim = game.get_available_buttons_size() action_space = AttrDict() action_space.low = [0 for i in range(action_dim)] action_space.high = [1 for i in range(action_dim)] self.action_space = action_space self.game = game
def __init__(self, cfg, name='Player1', color='0', host=True, map='map01', dm=True, port=None, num_players=7): game = DoomGame() game.load_config(cfg) game_args = "" if host: # This machine will function as a host for a multiplayer game with this many # players (including this machine). It will wait for other machines to connect using # the -join parameter and then start the game when everyone is connected. game_args += "-host %s " % num_players # The game (episode) will end after this many minutes have elapsed. game_args += "+timelimit 10.0 " # Players will respawn automatically after they die. game_args += "+sv_forcerespawn 1 " # Autoaim is disabled for all players. game_args += "+sv_noautoaim 1 " # Players will be invulnerable for two second after spawning. game_args += "+sv_respawnprotect 1 " # Players will be spawned as far as possible from any other players. game_args += "+sv_spawnfarthest 1 " # Disables crouching. game_args += "+sv_nocrouch 1 " # Sets delay between respanws (in seconds). game_args += "+viz_respawn_delay 10 " game_args += "+viz_nocheat 1" if dm: # Deathmatch rules are used for the game. game_args += " -deathmatch" if port is not None: game_args += " -port %s" % port else: game_args += " -join 127.0.0.1" if port is not None: game_args += ":%s" % port game_args += " -name %s" % name game_args += " -colorset %s" % color game.add_game_args(game_args) game.set_death_penalty(1) game.set_doom_map(map) self.env = game self.observation_space = spaces.Box(0, 255, game.get_screen_size()) self.action_space = spaces.Discrete(game.get_available_buttons_size()) self.reward_range = None
def __init__(self, emulator_id, game, resource_folder, gray=False, reward_coef=1 / 100, action_repeat=6, history_window=1, screen_size=(60, 90), verbose=0, visualize=False, **unknown): if verbose >= 2: logging.debug('Initializing Vizdoom.{}. emulator_id={}'.format( game, emulator_id)) logging.debug('Emulator#{} received unknown args: {}'.format( emulator_id, unknown)) doom_game = DoomGame() config_file_path = join_path(resource_folder, game + '.cfg') doom_game.load_config(config_file_path) doom_game.set_window_visible(visualize) doom_game.set_screen_resolution(self.SCREEN_RESOLUTION) doom_game.set_screen_format( ScreenFormat.GRAY8 if gray else ScreenFormat.BGR24) doom_game.set_mode(self.MODE) if self.MODE == Mode.SPECTATOR: doom_game.add_game_args("+freelook 1") # with a fixed seed all episodes in this environment will be identical #doom_game.set_seed(args.random_seed) # doom_game.add_available_game_variable(vizdoom.GameVariable.AMMO2) doom_game.init() self.game = doom_game self.legal_actions, self.noop = self._define_actions(self.game) self._preprocess = cv2_resize self.screen_size = screen_size self.reward_coef = reward_coef self.action_repeat = action_repeat self.history_window = history_window num_channels = doom_game.get_screen_channels() self.observation_shape = (self.history_window * num_channels, ) + self.screen_size self.history = create_history_observation(self.history_window) # If episode is done WorkerProcess drops last returned state and # returns the initial state of a new episode. # Therefore it doesn't really matter that terminal_screen is None self.terminal_obs = None
def __init__(self, config_file): """ Initialize ViZDoom environment. Args: config_file: .cfg file path, which defines how a world works and look like (maps) """ self.game = DoomGame() # load configurations from file self.game.load_config(config_file) self.game.init() self.state_shape = self.featurize(self.game.get_state()).shape self.num_actions = len(self.game.get_available_buttons())
def __init__(self, level): # init game self.game = DoomGame() self.game.set_screen_resolution(ScreenResolution.RES_640X480) scenarios_dir = os.path.join(os.path.dirname(__file__), 'scenarios') self.game.load_config(os.path.join(scenarios_dir, CONFIGS[level][0])) self.game.set_window_visible(False) self.game.init() self.state = None self.action_space = spaces.Discrete(CONFIGS[level][1]) self.observation_space = spaces.Box( 0, 255, (self.game.get_screen_height(), self.game.get_screen_width(), self.game.get_screen_channels()), dtype=np.uint8) self.viewer = None
def __init__(self, level, visualize=False, include_variables=False, factored_action=False, frame_skip=12, seed=None): super().__init__() from vizdoom import DoomGame, Mode, ScreenFormat, ScreenResolution self.config_file = level self.include_variables = include_variables self.factored_action = factored_action self.visualize = visualize self.frame_skip = frame_skip self.environment = DoomGame() self.environment.load_config(self.config_file) if self.visualize: self.environment.set_window_visible(True) self.environment.set_mode(Mode.ASYNC_PLAYER) else: self.environment.set_window_visible(False) self.environment.set_mode(Mode.PLAYER) # e.g. CRCGCB, RGB24, GRAY8 self.environment.set_screen_format(ScreenFormat.RGB24) # e.g. RES_320X240, RES_640X480, RES_1920X1080 self.environment.set_screen_resolution(ScreenResolution.RES_640X480) self.environment.set_depth_buffer_enabled(False) self.environment.set_labels_buffer_enabled(False) self.environment.set_automap_buffer_enabled(False) if seed is not None: self.environment.setSeed(seed) self.environment.init() self.state_shape = (480, 640, 3) self.num_variables = self.environment.get_available_game_variables_size( ) self.num_buttons = self.environment.get_available_buttons_size() self.available_actions = [ tuple(a) for a in itertools.product([0, 1], repeat=self.num_buttons) ]
def __init__(self, level): self.previous_level = -1 self.level = level self.game = DoomGame() self.loader = Loader() self.doom_dir = os.path.dirname(os.path.abspath(__file__)) self._mode = 'algo' # 'algo' or 'human' self.no_render = False # To disable double rendering in human mode self.viewer = None self.is_initialized = False # Indicates that reset() has been called self.curr_seed = 0 self.lock = (DoomLock()).get_lock() self.action_space = spaces.MultiDiscrete([[0, 1]] * 38 + [[-10, 10]] * 2 + [[-100, 100]] * 3) self.allowed_actions = list(range(NUM_ACTIONS)) self.screen_height = 480 self.screen_width = 640 self.screen_resolution = ScreenResolution.RES_640X480 self.observation_space = spaces.Box(low=0, high=255, shape=(self.screen_height, self.screen_width, 3)) self._seed() self._configure()