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
class VizDoomEnv(Env): ''' Wrapper for vizdoom to use as an OpenAI gym environment. ''' metadata = {'render.modes': ['human', 'rgb_array']} def __init__(self, cfg_name, repeat=1): super(VizDoomEnv, self).__init__() self.game = DoomGame() self.game.load_config('./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_height(), self.game.get_screen_width(), self.game.get_screen_channels()) self.observation_space = spaces.Box(low=0, high=255, shape=output_shape, dtype='uint8') self.game.init() def close(self): self.game.close() if self._viewer is not None: self._viewer.close() self._viewer = None def seed(self, seed=None): self.game.set_seed(seed) def step(self, action): reward = self.game.make_action(list(action), self.repeat) state = self.game.get_state() done = self.game.is_episode_finished() # info = self._get_game_variables(state.game_variables) info = {} if state is not None: observation = state.screen_buffer.transpose(1, 2, 0) else: observation = np.zeros(shape=self.observation_space.shape, dtype=np.uint8) return observation, reward, done, info def reset(self): # self.seed(seed) self.game.new_episode() return self.game.get_state().screen_buffer.transpose(1, 2, 0) def render(self, mode='human', close=False): if close: if self._viewer is not None: self._viewer.close() self._viewer = None return img = None state = self.game.get_state() if state is not None: img = state.screen_buffer if img is None: # at the end of the episode img = np.zeros(shape=self.observation_space.shape, dtype=np.uint8) if mode == 'rgb_array': return img elif mode is 'human': if self._viewer is None: self._viewer = rendering.SimpleImageViewer() self._viewer.imshow(img.transpose(1, 2, 0)) def _get_game_variables(self, state_variables): info = {} if state_variables is not None: info['KILLCOUNT'] = state_variables[0] info['ITEMCOUNT'] = state_variables[1] info['SECRETCOUNT'] = state_variables[2] info['FRAGCOUNT'] = state_variables[3] info['HEALTH'] = state_variables[4] info['ARMOR'] = state_variables[5] info['DEAD'] = state_variables[6] info['ON_GROUND'] = state_variables[7] info['ATTACK_READY'] = state_variables[8] info['ALTATTACK_READY'] = state_variables[9] info['SELECTED_WEAPON'] = state_variables[10] info['SELECTED_WEAPON_AMMO'] = state_variables[11] info['AMMO1'] = state_variables[12] info['AMMO2'] = state_variables[13] info['AMMO3'] = state_variables[14] info['AMMO4'] = state_variables[15] info['AMMO5'] = state_variables[16] info['AMMO6'] = state_variables[17] info['AMMO7'] = state_variables[18] info['AMMO8'] = state_variables[19] info['AMMO9'] = state_variables[20] info['AMMO0'] = state_variables[21] return info
class VizDoomEnv(gym.Env): ''' Wrapper for vizdoom to use as an OpenAI gym environment. ''' metadata = {'render.modes': ['human', 'rgb_array']} 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 close(self): self.game.close() if self._viewer is not None: self._viewer.close() self._viewer = None def seed(self, seed=None): self.game.set_seed(seed) def step(self, action): reward = self.game.make_action(list(action), self.frameskip) state = self.game.get_state() done = self.game.is_episode_finished() if state is not None: observation = state.screen_buffer info = state.game_variables # Return the chosen game variables in info else: observation = np.zeros(shape=self.observation_space.shape, dtype=np.uint8) info = None processedObservation = self._preProcessImage(observation) del self.frameBuffer[0] self.frameBuffer.append(processedObservation) return self.frameBuffer, reward, done, info # Preprocess image for use in network def _preProcessImage(self, image): if image.shape != self.inputShape: image = cv2.resize(image.transpose(1, 2, 0), (self.inputShape[2], self.inputShape[1]), interpolation=cv2.INTER_AREA).transpose( 2, 0, 1) return image def reset(self): self.game.new_episode() state = self._preProcessImage(self.game.get_state().screen_buffer) self.frameBuffer = [state] * self.sequenceLength return self.frameBuffer def render(self, mode='human', close=False): if close: if self._viewer is not None: self._viewer.close() self._viewer = None return img = None state = self.game.get_state() if state is not None: img = state.screen_buffer if img is None: # at the end of the episode img = np.zeros(shape=self.observation_space.shape, dtype=np.uint8) if mode == 'rgb_array': return img elif mode is 'human': if self._viewer is None: self._viewer = rendering.SimpleImageViewer() self._viewer.imshow(img.transpose(1, 2, 0))
class VizdoomEnv(gym.Env): 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 step(self, action): # convert action to vizdoom action space (one hot) act = np.zeros(self.action_space.n) act[action] = 1 act = np.uint8(act) act = act.tolist() reward = self.game.make_action(act) state = self.game.get_state() done = self.game.is_episode_finished() info = {} if not done: observation = np.transpose(state.screen_buffer, (1, 2, 0)) else: observation = np.uint8(np.zeros(self.observation_space.shape)) info = {"episode": {"r": self.game.get_total_reward()}} return observation, reward, done, info def seed(self, seed): self.game.set_seed(seed) def close(self): self.game.close() def reset(self): self.game.new_episode() self.state = self.game.get_state() img = self.state.screen_buffer return np.transpose(img, (1, 2, 0)) def render(self, mode='human'): try: img = self.game.get_state().screen_buffer img = np.transpose(img, [1, 2, 0]) if self.viewer is None: self.viewer = rendering.SimpleImageViewer() self.viewer.imshow(img) except AttributeError: pass @staticmethod def get_keys_to_action(): # you can press only one key at a time! keys = { (): 2, (ord('a'), ): 0, (ord('d'), ): 1, (ord('w'), ): 3, (ord('s'), ): 4, (ord('q'), ): 5, (ord('e'), ): 6 } return keys