本文整理汇总了Python中ale_python_interface.ALEInterface.getScreenDims方法的典型用法代码示例。如果您正苦于以下问题:Python ALEInterface.getScreenDims方法的具体用法?Python ALEInterface.getScreenDims怎么用?Python ALEInterface.getScreenDims使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ale_python_interface.ALEInterface
的用法示例。
在下文中一共展示了ALEInterface.getScreenDims方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: from ale_python_interface import ALEInterface [as 别名]
# 或者: from ale_python_interface.ALEInterface import getScreenDims [as 别名]
class Environment:
def __init__(self, show_screen, history_length):
self.ale = ALEInterface()
self.ale.setInt('frame_skip', 4)
self.history = None
self.history_length = history_length
if show_screen:
self.display_screen()
self.load_game()
(screen_width, screen_height) = self.ale.getScreenDims()
self.screen_data = np.empty((screen_height, screen_width, 1), dtype=np.uint8) # 210x160 screen data
self.dims = (84, 84) # input size for neural network
self.actions = [3, 0, 1, 4] # noop, left, right, fire,
def display_screen(self):
self.ale.setBool("display_screen", True)
def turn_on_sound(self):
self.ale.setBool("sound", True)
def restart(self):
"""reset game"""
self.ale.reset_game()
def act(self, action):
""":returns reward of an action"""
return self.ale.act(self.actions[action])
def __get_screen(self):
""":returns Grayscale thresholded resized screen image """
self.ale.getScreenGrayscale(self.screen_data)
resized = cv2.resize(self.screen_data, self.dims)
return resized
def get_state(self):
binary_screen = self.__get_screen()
if self.history is None:
self.history = deque(maxlen=self.history_length)
for _ in range(self.history_length - 1):
self.history.append(binary_screen)
self.history.append(binary_screen)
result = np.stack(self.history, axis=0)
return result
def isTerminal(self):
"""checks if game is over"""
return self.ale.game_over()
def load_game(self):
"""load game from file"""
self.ale.loadROM("Breakout.bin")
示例2: __init__
# 需要导入模块: from ale_python_interface import ALEInterface [as 别名]
# 或者: from ale_python_interface.ALEInterface import getScreenDims [as 别名]
class emulator:
def __init__(self, rom_name, vis,windowname='preview'):
self.ale = ALEInterface()
self.max_frames_per_episode = self.ale.getInt("max_num_frames_per_episode");
self.ale.setInt("random_seed",123)
self.ale.setInt("frame_skip",4)
self.ale.loadROM('roms/' + rom_name )
self.legal_actions = self.ale.getMinimalActionSet()
self.action_map = dict()
self.windowname = windowname
for i in range(len(self.legal_actions)):
self.action_map[self.legal_actions[i]] = i
self.init_frame_number = 0
# print(self.legal_actions)
self.screen_width,self.screen_height = self.ale.getScreenDims()
print("width/height: " +str(self.screen_width) + "/" + str(self.screen_height))
self.vis = vis
if vis:
cv2.startWindowThread()
cv2.namedWindow(self.windowname)
def get_image(self):
numpy_surface = np.zeros(self.screen_height*self.screen_width*3, dtype=np.uint8)
self.ale.getScreenRGB(numpy_surface)
image = np.reshape(numpy_surface, (self.screen_height, self.screen_width, 3))
return image
def newGame(self):
# Instead of resetting the game, we load a checkpoint and start from there.
# self.ale.reset_game()
self.ale.restoreState(self.ale.decodeState(checkpoints[random.randint(0,99)].astype('uint8')))
self.init_frame_number = self.ale.getFrameNumber()
#self.ale.restoreState(self.ale.decodeState(np.reshape(checkpoint,(1009,1))))
return self.get_image()
def next(self, action_indx):
reward = self.ale.act(action_indx)
nextstate = self.get_image()
# scipy.misc.imsave('test.png',nextstate)
if self.vis:
cv2.imshow(self.windowname,nextstate)
return nextstate, reward, self.ale.game_over()
def get_frame_number(self):
return self.ale.getFrameNumber() - self.init_frame_number
示例3: __init__
# 需要导入模块: from ale_python_interface import ALEInterface [as 别名]
# 或者: from ale_python_interface.ALEInterface import getScreenDims [as 别名]
class emulator:
def __init__(self, rom_name, vis):
if vis:
import cv2
self.ale = ALEInterface()
self.max_frames_per_episode = self.ale.getInt("max_num_frames_per_episode");
self.ale.setInt("random_seed",123)
self.ale.setInt("frame_skip",4)
self.ale.loadROM('roms/' + rom_name )
self.legal_actions = self.ale.getMinimalActionSet()
self.action_map = dict()
for i in range(len(self.legal_actions)):
self.action_map[self.legal_actions[i]] = i
# print(self.legal_actions)
self.screen_width,self.screen_height = self.ale.getScreenDims()
print("width/height: " +str(self.screen_width) + "/" + str(self.screen_height))
self.vis = vis
if vis:
cv2.startWindowThread()
cv2.namedWindow("preview")
def get_image(self):
numpy_surface = np.zeros(self.screen_height*self.screen_width*3, dtype=np.uint8)
self.ale.getScreenRGB(numpy_surface)
image = np.reshape(numpy_surface, (self.screen_height, self.screen_width, 3))
return image
def newGame(self):
self.ale.reset_game()
return self.get_image()
def next(self, action_indx):
reward = self.ale.act(action_indx)
nextstate = self.get_image()
# scipy.misc.imsave('test.png',nextstate)
if self.vis:
cv2.imshow('preview',nextstate)
return nextstate, reward, self.ale.game_over()
示例4: __init__
# 需要导入模块: from ale_python_interface import ALEInterface [as 别名]
# 或者: from ale_python_interface.ALEInterface import getScreenDims [as 别名]
class Atari:
def __init__(self,rom_name):
self.ale = ALEInterface()
self.max_frames_per_episode = self.ale.getInt("max_num_frames_per_episode")
self.ale.setInt("random_seed",123)
self.ale.setInt("frame_skip",4)
self.ale.loadROM('./' +rom_name)
self.screen_width,self.screen_height = self.ale.getScreenDims()
self.legal_actions = self.ale.getMinimalActionSet()
self.action_map = dict()
for i in range(len(self.legal_actions)):
self.action_map[self.legal_actions[i]] = i
#print len(self.legal_actions)
self.windowname = rom_name
#cv2.startWindowThread()
#cv2.namedWindow(rom_name)
def get_image(self):
numpy_surface = np.zeros(self.screen_height*self.screen_width*3, dtype=np.uint8)
self.ale.getScreenRGB(numpy_surface)
image = np.reshape(numpy_surface, (self.screen_height, self.screen_width, 3))
return image
def newGame(self):
self.ale.reset_game()
return self.get_image()
def next(self, action):
reward = self.ale.act(self.legal_actions[np.argmax(action)])
nextstate = self.get_image()
#cv2.imshow(self.windowname,nextstate)
if self.ale.game_over():
self.newGame()
#print "reward %d" % reward
return nextstate, reward, self.ale.game_over()
示例5: __init__
# 需要导入模块: from ale_python_interface import ALEInterface [as 别名]
# 或者: from ale_python_interface.ALEInterface import getScreenDims [as 别名]
class AleAgent:
##
# @param processing_cls Class for processing game visual unput
def __init__(self, processing_cls, game_rom=None, encoder_model=None, encoder_weights=None, NFQ_model=None, NFQ_weights=None):
assert game_rom is not None
self.game = ALEInterface()
if encoder_weights is not None and encoder_model is not None:
self.encoder = Encoder(path_to_model=encoder_model, path_to_weights=encoder_weights)
else:
self.encoder = Encoder()
self.processor = processing_cls()
# Get & Set the desired settings
self.game.setInt('random_seed', 0)
self.game.setInt('frame_skip', 4)
# Set USE_SDL to true to display the screen. ALE must be compilied
# with SDL enabled for this to work. On OSX, pygame init is used to
# proxy-call SDL_main.
USE_SDL = True
if USE_SDL:
if sys.platform == 'darwin':
pygame.init()
self.game.setBool('sound', False) # Sound doesn't work on OSX
elif sys.platform.startswith('linux'):
self.game.setBool('sound', False) # no sound
self.game.setBool('display_screen', True)
# Load the ROM file
self.game.loadROM(game_rom)
# Get the list of legal actions
self.legal_actions = self.game.getLegalActionSet()
# Get actions applicable in current game
self.minimal_actions = self.game.getMinimalActionSet()
if NFQ_model is not None and NFQ_weights is not None:
self.NFQ = NFQ(
self.encoder.out_dim,
len(self.minimal_actions),
model_path=NFQ_model,
weights_path=NFQ_weights
)
else:
self.NFQ = NFQ(self.encoder.out_dim, len(self.minimal_actions))
(self.screen_width, self.screen_height) = self.game.getScreenDims()
self.screen_data = np.zeros(
(self.screen_height, self.screen_width),
dtype=np.uint8
)
##
# Initialize the reinforcement learning
def train(self, num_of_episodes=1500, eps=0.995, key_binding=None):
pygame.init()
for episode in xrange(num_of_episodes):
total_reward = 0
moves = 0
hits = 0
print 'Starting episode: ', episode+1
if key_binding:
eps = 0.05
else:
eps -= 2/num_of_episodes
self.game.getScreenGrayscale(self.screen_data)
pooled_data = self.processor.process(self.screen_data)
next_state = self.encoder.encode(pooled_data)
while not self.game.game_over():
current_state = next_state
x = None
if key_binding:
key_pressed = pygame.key.get_pressed()
x = key_binding(key_pressed)
if x is None:
r = np.random.rand()
if r < eps:
x = np.random.randint(self.minimal_actions.size)
else:
x = self.NFQ.predict_action(current_state)
a = self.minimal_actions[x]
# Apply an action and get the resulting reward
reward = self.game.act(a)
# record only every 3 frames
# if not moves % 3:
self.game.getScreenGrayscale(self.screen_data)
pooled_data = self.processor.process(self.screen_data)
next_state = self.encoder.encode(pooled_data)
transition = np.append(current_state, x)
transition = np.append(transition, next_state)
#.........这里部分代码省略.........
示例6: AleEnv
# 需要导入模块: from ale_python_interface import ALEInterface [as 别名]
# 或者: from ale_python_interface.ALEInterface import getScreenDims [as 别名]
class AleEnv():
def __init__(self, rom, display_screen, use_env_frame_skip, frame_repeat):
self.actions = None
self.rom = rom
self.display_screen = display_screen
self.use_env_frame_skip = use_env_frame_skip
self.frame_repeat = frame_repeat
def initialize(self):
self.ale = ALEInterface()
self.ale.setInt("random_seed", random.randint(1, 1000))
if self.display_screen:
self.ale.setBool('display_screen', True)
if self.use_env_frame_skip == True:
self.ale.setInt('frame_skip', self.frame_repeat)
self.ale.setBool('color_averaging', True)
self.ale.setFloat('repeat_action_probability', 0)
self.ale.loadROM(self.rom)
self.actions = self.ale.getMinimalActionSet()
print 'actions: %s' % self.actions
(self.screen_width,self.screen_height) = self.ale.getScreenDims()
print("width/height: " +str(self.screen_width) + "/" + str(self.screen_height))
self.initialized = True
def get_actions(self, rom=None):
if self.actions is None and rom != None:
ale = ALEInterface()
ale.loadROM(rom)
self.actions = ale.getMinimalActionSet()
return self.actions
@property
def state_dtype(self):
return np.uint8
@property
def continuous_action(self):
return False
def reset_game(self):
self.ale.reset_game()
def lives(self):
return self.ale.lives()
def getScreenRGB(self):
return self.ale.getScreenRGB()
def getState(self, debug_display=False, debug_input=None):
screen = self.ale.getScreenGrayscale()
if screen is not None and debug_display:
debug_input.show(screen.reshape(screen.shape[0], screen.shape[1]))
return screen.reshape(self.screen_height, self.screen_width)
def act(self, action):
return self.ale.act(action)
def game_over(self):
return self.ale.game_over()
def finish(self):
return
示例7: ALEEnvironment
# 需要导入模块: from ale_python_interface import ALEInterface [as 别名]
# 或者: from ale_python_interface.ALEInterface import getScreenDims [as 别名]
class ALEEnvironment(BaseEnvironment):
"""
The :class:`MinimalGameHandler` class takes care of the interface to the ALE and tries to do nothing else. It's
meant for advanced users who need fine control over every aspect of the process. It has many functions that are simply
wrappers of the underlying ALE but with pythonic names/usage.
Parameters
----------
rom : byte string
Specifies the directory to load the rom from. Must be a byte string: b'dir_for_rom/rom.bin'
display_screen : boolean
Default False. Whether or not to show the game. True takes longer to run but can be fun to watch
step_cap: int
Default None. Maximum number of steps to run in an episode. Breakout can sometimes not return terminal
even when game is ended. This fixes that and will return terminal after stepping above this count
"""
def __init__(self, rom, resize_shape=(84, 84), skip_frame=1, repeat_action_probability=0.0,
step_cap=None, loss_of_life_termination=False, loss_of_life_negative_reward=False,
grayscale=True, display_screen=False, seed=np.random.RandomState()):
# set up emulator
self.ale = ALEInterface()
if display_screen:
self.ale.setBool(b'display_screen', True)
self.ale.setInt(b'frame_skip', skip_frame)
self.ale.setInt(b'random_seed', seed.randint(0, 9999))
self.ale.setFloat(b'repeat_action_probability', repeat_action_probability)
self.ale.setBool(b'color_averaging', False)
self.ale.loadROM(rom.encode())
# setup gamescreen object. I think this is faster than recreating an empty each time
width, height = self.ale.getScreenDims()
channels = 1 if grayscale else 3
self.grayscale = grayscale
self.gamescreen = np.empty((height, width, 1), dtype=np.uint8)
self.resize_shape = resize_shape
self.skip_frame = skip_frame
self.step_cap = step_cap
self.curr_step_count = 0
# setup action converter
# ALE returns legal action indexes, convert these to just numbers
self.action_inds = self.ale.getMinimalActionSet()
# setup lives
self.loss_of_life_negative_reward = loss_of_life_negative_reward
self.cur_lives = self.ale.lives()
self.loss_of_life_termination = loss_of_life_termination
self.life_lost = False
def reset(self):
self.ale.reset_game()
self.cur_lives = self.ale.lives()
self.life_lost = False
self.curr_step_count = 0
def step(self, action):
self.curr_step_count += 1
ale_action = self.action_inds[action]
return self._step(ale_action)
def _step(self, ale_action):
if not self.loss_of_life_termination and not self.loss_of_life_negative_reward:
return self.ale.act(ale_action)
else:
rew = self.ale.act(ale_action)
new_lives = self.ale.lives()
if new_lives < self.cur_lives:
# if loss of life is negative reward subtract 1 from reward
if self.loss_of_life_negative_reward:
rew -= 1
self.cur_lives = new_lives
self.life_lost = True
return rew
def get_state(self):
if self.grayscale:
self.gamescreen = self.ale.getScreenGrayscale(self.gamescreen)
else:
self.gamescreen = self.ale.getScreenRGB(self.gamescreen)
# if resize_shape is none then don't resize
if self.resize_shape is not None:
# if grayscale we remove the last dimmension (channel)
if self.grayscale:
processedImg = imresize(self.gamescreen[:, :, 0], self.resize_shape)
else:
processedImg = imresize(self.gamescreen, self.resize_shape)
return processedImg
def get_state_shape(self):
return self.resize_shape
def get_terminal(self):
if self.loss_of_life_termination and self.life_lost:
return True
elif self.step_cap is not None and self.curr_step_count > self.step_cap:
return True
#.........这里部分代码省略.........
示例8: __init__
# 需要导入模块: from ale_python_interface import ALEInterface [as 别名]
# 或者: from ale_python_interface.ALEInterface import getScreenDims [as 别名]
class Emulator:
def __init__(self, rom_path, rom_name, visualize, actor_id, rseed, single_life_episodes = False):
self.ale = ALEInterface()
self.ale.setInt("random_seed", rseed * (actor_id +1))
# For fuller control on explicit action repeat (>= ALE 0.5.0)
self.ale.setFloat("repeat_action_probability", 0.0)
# Disable frame_skip and color_averaging
# See: http://is.gd/tYzVpj
self.ale.setInt("frame_skip", 1)
self.ale.setBool("color_averaging", False)
self.ale.loadROM(rom_path + "/" + rom_name + ".bin")
self.legal_actions = self.ale.getMinimalActionSet()
self.screen_width,self.screen_height = self.ale.getScreenDims()
#self.ale.setBool('display_screen', True)
# Processed historcal frames that will be fed in to the network
# (i.e., four 84x84 images)
self.screen_images_processed = np.zeros((IMG_SIZE_X, IMG_SIZE_Y,
NR_IMAGES))
self.rgb_screen = np.zeros((self.screen_height,self.screen_width, 3), dtype=np.uint8)
self.gray_screen = np.zeros((self.screen_height,self.screen_width,1), dtype=np.uint8)
self.frame_pool = np.empty((2, self.screen_height, self.screen_width))
self.current = 0
self.lives = self.ale.lives()
self.visualize = visualize
self.visualize_processed = False
self.windowname = rom_name + ' ' + str(actor_id)
if self.visualize:
logger.debug("Opening emulator window...")
#from skimage import io
#io.use_plugin('qt')
cv2.startWindowThread()
cv2.namedWindow(self.windowname)
logger.debug("Emulator window opened")
if self.visualize_processed:
logger.debug("Opening processed frame window...")
cv2.startWindowThread()
logger.debug("Processed frame window opened")
cv2.namedWindow(self.windowname + "_processed")
self.single_life_episodes = single_life_episodes
def get_screen_image(self):
""" Add screen (luminance) to frame pool """
# [screen_image, screen_image_rgb] = [self.ale.getScreenGrayscale(),
# self.ale.getScreenRGB()]
self.ale.getScreenGrayscale(self.gray_screen)
self.ale.getScreenRGB(self.rgb_screen)
self.frame_pool[self.current] = np.squeeze(self.gray_screen)
self.current = (self.current + 1) % FRAMES_IN_POOL
return self.rgb_screen
def new_game(self):
""" Restart game """
self.ale.reset_game()
self.lives = self.ale.lives()
if MAX_START_WAIT < 0:
logger.debug("Cannot time travel yet.")
sys.exit()
elif MAX_START_WAIT > 0:
wait = random.randint(0, MAX_START_WAIT)
else:
wait = 0
for _ in xrange(wait):
self.ale.act(self.legal_actions[0])
def process_frame_pool(self):
""" Preprocess frame pool """
img = None
if BLEND_METHOD == "max_pool":
img = np.amax(self.frame_pool, axis=0)
#img resize(img[:210, :], (84, 84))
img = cv2.resize(img[:210, :], (84, 84),
interpolation=cv2.INTER_LINEAR)
img = img.astype(np.float32)
img *= (1.0/255.0)
return img
# Reduce height to 210, if not so
#cropped_img = img[:210, :]
# Downsample to 110x84
#down_sampled_img = resize(cropped_img, (84, 84))
# Crop to 84x84 playing area
#stackable_image = down_sampled_img[:, 26:110]
#return stackable_image
def action_repeat(self, a):
""" Repeat action and grab screen into frame pool """
#.........这里部分代码省略.........
示例9: type
# 需要导入模块: from ale_python_interface import ALEInterface [as 别名]
# 或者: from ale_python_interface.ALEInterface import getScreenDims [as 别名]
# ale.setBool('sound', True)
# ale.setBool('display_screen', True)
# Load the ROM file
ale.loadROM('Breakout.bin')
# Get the list of legal actions
# legal_actions = ale.getLegalActionSet()
legal_actions = ale.getMinimalActionSet()
print legal_actions
# (screen_width,screen_height) = ale.getScreenDims()
# screen_data = np.zeros(screen_width*screen_height,dtype=np.uint32)
# ale.getScreenRGB(screen_data)
(screen_width, screen_height) = ale.getScreenDims()
screen_data = np.zeros(screen_width * screen_height, dtype=np.uint8)
print type(ale.getScreen(screen_data))
# Play 10 episodes
for episode in xrange(10):
total_reward = 0
while not ale.game_over():
a = legal_actions[randrange(len(legal_actions))]
# Apply an action and get the resulting reward
reward = ale.act(a)
print reward
total_reward += reward
print 'Episode', episode, 'ended with score:', total_reward
ale.reset_game()
示例10: __init__
# 需要导入模块: from ale_python_interface import ALEInterface [as 别名]
# 或者: from ale_python_interface.ALEInterface import getScreenDims [as 别名]
class Environment:
"""docstring for Environment"""
BUFFER_LEN = 2
EPISODE_FRAMES = 18000
EPOCH_COUNT = 200
EPOCH_STEPS = 250000
EVAL_EPS = 0.001
FRAMES_SKIP = 4
FRAME_HEIGHT = 84
FRAME_WIDTH = 84
MAX_NO_OP = 30
MAX_REWARD = 1
def __init__(self, rom_name, rng, display_screen = False):
self.api = ALEInterface()
self.api.setInt('random_seed', rng.randint(333))
self.api.setBool('display_screen', display_screen)
self.api.setFloat('repeat_action_probability', 0.0)
self.rom_name = rom_name
self.display_screen = display_screen
self.rng = rng
self.repeat = Environment.FRAMES_SKIP
self.buffer_len = Environment.BUFFER_LEN
self.height = Environment.FRAME_HEIGHT
self.width = Environment.FRAME_WIDTH
self.episode_steps = Environment.EPISODE_FRAMES / Environment.FRAMES_SKIP
self.merge_id = 0
self.max_reward = Environment.MAX_REWARD
self.eval_eps = Environment.EVAL_EPS
self.log_dir = ''
self.network_dir = ''
self.api.loadROM('../rom/' + self.rom_name)
self.minimal_actions = self.api.getMinimalActionSet()
original_width, original_height = self.api.getScreenDims()
self.merge_frame = np.zeros((self.buffer_len
, original_height
, original_width)
, dtype = np.uint8)
def get_action_count(self):
return len(self.minimal_actions)
def train(self, agent, store_freq, folder = None, start_epoch = 0):
self._open_log_files(agent, folder)
obs = np.zeros((self.height, self.width), dtype = np.uint8)
epoch_count = Environment.EPOCH_COUNT
for epoch in xrange(start_epoch, epoch_count):
self.need_reset = True
steps_left = Environment.EPOCH_STEPS
print "\n" + "=" * 50
print "Epoch #%d" % (epoch + 1)
episode = 0
train_start = time.time()
while steps_left > 0:
num_step, _ = self._run_episode(agent, steps_left, obs)
steps_left -= num_step
episode += 1
if steps_left == 0 or episode % 10 == 0:
print "Finished episode #%d, steps_left = %d" \
% (episode, steps_left)
train_end = time.time()
valid_values = agent.get_validate_values()
eval_values = self.evaluate(agent)
test_end = time.time()
train_time = train_end - train_start
test_time = test_end - train_end
step_per_sec = Environment.EPOCH_STEPS * 1. / max(1, train_time)
print "\tFinished epoch #%d, episode trained = %d\n" \
"\tValidate values = %.3f, evaluate reward = %.3f\n"\
"\tTrain time = %.0fs, test time = %.0fs, steps/sec = %.4f" \
% (epoch + 1, episode, valid_values, eval_values\
, train_time, test_time, step_per_sec)
self._update_log_files(agent, epoch + 1, episode
, valid_values, eval_values
, train_time, test_time
, step_per_sec, store_freq)
gc.collect()
def evaluate(self, agent, episodes = 30, obs = None):
print "\n***Start evaluating"
if obs is None:
obs = np.zeros((self.height, self.width), dtype = np.uint8)
sum_reward = 0.0
sum_step = 0.0
for episode in xrange(episodes):
self.need_reset = True
step, reward = self._run_episode(agent, self.episode_steps, obs
, self.eval_eps, evaluating = True)
sum_reward += reward
sum_step += step
print "Finished episode %d, reward = %d, step = %d" \
% (episode + 1, reward, step)
self.need_reset = True
#.........这里部分代码省略.........
示例11: AtariSimulator
# 需要导入模块: from ale_python_interface import ALEInterface [as 别名]
# 或者: from ale_python_interface.ALEInterface import getScreenDims [as 别名]
class AtariSimulator(object):
def __init__(self, settings):
'''Initiate Arcade Learning Environment (ALE) using Python interface
https://github.com/bbitmaster/ale_python_interface/wiki
- Set number of frames to be skipped, random seed, ROM and title for display.
- Retrieve a set of legal actions and their number.
- Retrieve dimensions of the original screen (width/height), and set the dimensions
of the cropped screen, together with the padding used to crop the screen rectangle.
- Set dimensions of the pygame display that will show visualization of the simulation.
(May be cropped --- showing what the learner sees, or not --- showing full Atari screen)
- Allocate memory for generated grayscale screenshots. Accepts dims in (height/width) format
'''
self.ale = ALEInterface()
self.ale.setInt("frame_skip",settings["frame_skip"])
self.ale.setInt("random_seed",settings["seed_simulator"])
self.ale.loadROM(settings["rom_dir"] + '/' + settings["rom"])
self.title = "ALE Simulator: " + str(settings["rom"])
self.actions = self.ale.getLegalActionSet()
self.n_actions = self.actions.size
self.screen_dims = self.ale.getScreenDims()
self.model_dims = settings['model_dims']
self.pad = settings['pad']
print("Original screen width/height: " + str(self.screen_dims[0]) + "/" + str(self.screen_dims[1]))
print("Cropped screen width/height: " + str(self.model_dims[0]) + "/" + str(self.model_dims[1]))
self.viz_cropped = settings['viz_cropped']
if self.viz_cropped:
self.display_dims = (int(self.model_dims[0]*2), int(self.model_dims[1]*2))
else:
self.display_dims = (int(self.screen_dims[0]*2), int(self.screen_dims[1]*2))
# preallocate an array to accept ALE screen data (height/width) !
self.screen_data = np.empty((self.screen_dims[1],self.screen_dims[0]),dtype=np.uint8)
def get_screenshot(self):
'''returns a cropped snapshot of the simulator
- store grayscale values in a preallocated array
- cut out a square from the rectangle, using provided padding value
- downsample to the desired size and transpose from (height/width) to (width/height)
'''
self.ale.getScreenGrayscale(self.screen_data)
self.tmp = self.screen_data[(self.screen_dims[1]-self.screen_dims[0]-self.pad):(self.screen_dims[1]-self.pad),:]
self.frame = spm.imresize(self.tmp,self.model_dims[::-1],interp='nearest').T #, interp='nearest'
return self.frame
def act(self,action_index):
'''function to transition the simulator from s to s' using provided action
the action that is provided is in form of an index
simulator deals with translating the index into an actual action'''
self.last_reward = self.ale.act(self.actions[action_index])
def reward(self):
'''return reward - has to be called after the "act" function'''
return self.last_reward
def episode_over(self):
'''return a boolean indicator on whether the game is still running'''
return self.ale.game_over()
def reset_episode(self):
'''reset the game that ended'''
self.ale.reset_game()
def init_viz_display(self):
'''initialize display that will show visualization'''
pygame.init()
self.screen = pygame.display.set_mode(self.display_dims)
if self.title:
pygame.display.set_caption(self.title)
def refresh_viz_display(self):
'''if display is shut down, shut the game down
else move the current simulator's frame (cropped or not cropped) into the pygame display,
after expanding it 2x along x and y dimensions'''
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit
#.........这里部分代码省略.........
示例12: xrange
# 需要导入模块: from ale_python_interface import ALEInterface [as 别名]
# 或者: from ale_python_interface.ALEInterface import getScreenDims [as 别名]
if USE_SDL:
if sys.platform == 'darwin':
import pygame
print "!!!"
pygame.init()
ale.setBool('sound', False) # Sound doesn't work on OSX
elif sys.platform.startswith('linux'):
ale.setBool('sound', True)
ale.setBool('display_screen', True)
ale.setInt('frame_skip',1)
# Load the ROM file
ale.loadROM('roms/breakout.bin')
ale.setInt('max_num_frames',1)
# Get the list of legal actions
legal_actions = ale.getMinimalActionSet()
a1,a2 = ale.getScreenDims()
cnt = 0
# Play 10 episodes
import numpy as np
d = np.empty((a1,a2),dtype=np.uint8)
for episode in xrange(10):
total_reward = 0
while not ale.game_over():
a = legal_actions[randrange(len(legal_actions))]
#print legal_actions
# Apply an action and get the resulting reward
reward = ale.act(a);
ale.getScreenGrayscale(d)
io.imshow(d)
io.show()
示例13: AtariPlayer
# 需要导入模块: from ale_python_interface import ALEInterface [as 别名]
# 或者: from ale_python_interface.ALEInterface import getScreenDims [as 别名]
class AtariPlayer(gym.Env):
"""
A wrapper for ALE emulator, with configurations to mimic DeepMind DQN settings.
Info:
score: the accumulated reward in the current game
gameOver: True when the current game is Over
"""
def __init__(self, rom_file, viz=0,
frame_skip=4, nullop_start=30,
live_lost_as_eoe=True, max_num_frames=0):
"""
Args:
rom_file: path to the rom
frame_skip: skip every k frames and repeat the action
viz: visualization to be done.
Set to 0 to disable.
Set to a positive number to be the delay between frames to show.
Set to a string to be a directory to store frames.
nullop_start: start with random number of null ops.
live_losts_as_eoe: consider lost of lives as end of episode. Useful for training.
max_num_frames: maximum number of frames per episode.
"""
super(AtariPlayer, self).__init__()
if not os.path.isfile(rom_file) and '/' not in rom_file:
rom_file = get_dataset_path('atari_rom', rom_file)
assert os.path.isfile(rom_file), \
"rom {} not found. Please download at {}".format(rom_file, ROM_URL)
try:
ALEInterface.setLoggerMode(ALEInterface.Logger.Error)
except AttributeError:
if execute_only_once():
logger.warn("You're not using latest ALE")
# avoid simulator bugs: https://github.com/mgbellemare/Arcade-Learning-Environment/issues/86
with _ALE_LOCK:
self.ale = ALEInterface()
self.rng = get_rng(self)
self.ale.setInt(b"random_seed", self.rng.randint(0, 30000))
self.ale.setInt(b"max_num_frames_per_episode", max_num_frames)
self.ale.setBool(b"showinfo", False)
self.ale.setInt(b"frame_skip", 1)
self.ale.setBool(b'color_averaging', False)
# manual.pdf suggests otherwise.
self.ale.setFloat(b'repeat_action_probability', 0.0)
# viz setup
if isinstance(viz, six.string_types):
assert os.path.isdir(viz), viz
self.ale.setString(b'record_screen_dir', viz)
viz = 0
if isinstance(viz, int):
viz = float(viz)
self.viz = viz
if self.viz and isinstance(self.viz, float):
self.windowname = os.path.basename(rom_file)
cv2.startWindowThread()
cv2.namedWindow(self.windowname)
self.ale.loadROM(rom_file.encode('utf-8'))
self.width, self.height = self.ale.getScreenDims()
self.actions = self.ale.getMinimalActionSet()
self.live_lost_as_eoe = live_lost_as_eoe
self.frame_skip = frame_skip
self.nullop_start = nullop_start
self.action_space = spaces.Discrete(len(self.actions))
self.observation_space = spaces.Box(
low=0, high=255, shape=(self.height, self.width))
self._restart_episode()
def get_action_meanings(self):
return [ACTION_MEANING[i] for i in self.actions]
def _grab_raw_image(self):
"""
:returns: the current 3-channel image
"""
m = self.ale.getScreenRGB()
return m.reshape((self.height, self.width, 3))
def _current_state(self):
"""
:returns: a gray-scale (h, w) uint8 image
"""
ret = self._grab_raw_image()
# max-pooled over the last screen
ret = np.maximum(ret, self.last_raw_screen)
if self.viz:
if isinstance(self.viz, float):
cv2.imshow(self.windowname, ret)
cv2.waitKey(int(self.viz * 1000))
ret = ret.astype('float32')
# 0.299,0.587.0.114. same as rgb2y in torch/image
ret = cv2.cvtColor(ret, cv2.COLOR_RGB2GRAY)
return ret.astype('uint8') # to save some memory
#.........这里部分代码省略.........
示例14: __init__
# 需要导入模块: from ale_python_interface import ALEInterface [as 别名]
# 或者: from ale_python_interface.ALEInterface import getScreenDims [as 别名]
class GameEnvironment:
def __init__(self, settings):
self.ale = ALEInterface()
self.ale.setBool('display_screen', settings['DISPLAY_SCREEN'])
self.ale.setBool('sound', settings['SOUND'])
self.ale.setBool('color_averaging', settings['COLOR_AVERAGING'])
self.ale.setInt('random_seed', settings['RANDOM_SEED'])
self.ale.setInt('frame_skip', settings['FRAME_SKIP'])
self.ale.setFloat('repeat_action_probability', settings['REPEAT_ACTION_PROB'])
roms_dir = settings['ROMS_DIR']
rom_name = settings['ROM_NAME']
ROM = None
if(rom_name.endswith('.bin')):
self.name = rom_name[:-4]
ROM = rom_name
else:
self.name = rom_name
ROM = rom_name + '.bin'
self.ale.loadROM(os.path.join(roms_dir, ROM))
self.random_starts = settings['RANDOM_STARTS']
self.rng = settings['RNG']
if(settings['MINIMAL_ACTION_SET']):
self.actions = self.ale.getMinimalActionSet()
else:
self.actions = self.ale.getLegalActionSet()
self.n_actions = len(self.actions)
self.width, self.height = self.ale.getScreenDims()
self.observation = np.zeros((self.height, self.width), dtype='uint8')
self.reward = None
self.game_over = None
self.terminal = None
self.total_lives = None
self.init()
def init(self):
self.restartGame()
self.reward = 0
self.game_over = self.gameOver()
self.terminal = self.game_over
self.total_lives = self.lives()
self.step(0)
def getState(self):
return self.observation, self.reward, self.terminal, self.game_over
def step(self, action, training=False):
self.reward = self.act(action)
self.paint()
lives = self.lives()
self.game_over = self.gameOver()
self.terminal = self.game_over
if(training and (lives < self.total_lives)):
self.terminal = True
self.total_lives = lives
return self.getState()
def newGame(self):
self.init()
for i in xrange(self.rng.randint(1, self.random_starts)):
self.act(0)
terminal = self.gameOver()
if(terminal):
print "Warning terminal in random init"
return self.step(0)
def newTestGame(self):
self.init()
return self.getState()
def paint(self):
self.ale.getScreenGrayscale(self.observation)
def getScreenRGB(self):
return self.ale.getScreenRGB()
def act(self, action):
assert ((action >= 0) and (action < self.n_actions))
return self.ale.act(self.actions[action])
def lives(self):
return self.ale.lives()
#.........这里部分代码省略.........
示例15: AtariPlayer
# 需要导入模块: from ale_python_interface import ALEInterface [as 别名]
# 或者: from ale_python_interface.ALEInterface import getScreenDims [as 别名]
class AtariPlayer(RLEnvironment):
"""
A wrapper for atari emulator.
NOTE: will automatically restart when a real episode ends
"""
def __init__(self, rom_file, viz=0, height_range=(None,None),
frame_skip=4, image_shape=(84, 84), nullop_start=30,
live_lost_as_eoe=True):
"""
:param rom_file: path to the rom
:param frame_skip: skip every k frames and repeat the action
:param image_shape: (w, h)
:param height_range: (h1, h2) to cut
:param viz: visualization to be done.
Set to 0 to disable.
Set to a positive number to be the delay between frames to show.
Set to a string to be a directory to store frames.
:param nullop_start: start with random number of null ops
:param live_losts_as_eoe: consider lost of lives as end of episode. useful for training.
"""
super(AtariPlayer, self).__init__()
if not os.path.isfile(rom_file) and '/' not in rom_file:
rom_file = get_dataset_dir('atari_rom', rom_file)
assert os.path.isfile(rom_file), \
"rom {} not found. Please download at {}".format(rom_file, ROM_URL)
try:
ALEInterface.setLoggerMode(ALEInterface.Logger.Warning)
except AttributeError:
log_once()
# avoid simulator bugs: https://github.com/mgbellemare/Arcade-Learning-Environment/issues/86
with _ALE_LOCK:
self.ale = ALEInterface()
self.rng = get_rng(self)
self.ale.setInt(b"random_seed", self.rng.randint(0, 10000))
self.ale.setBool(b"showinfo", False)
self.ale.setInt(b"frame_skip", 1)
self.ale.setBool(b'color_averaging', False)
# manual.pdf suggests otherwise.
self.ale.setFloat(b'repeat_action_probability', 0.0)
# viz setup
if isinstance(viz, six.string_types):
assert os.path.isdir(viz), viz
self.ale.setString(b'record_screen_dir', viz)
viz = 0
if isinstance(viz, int):
viz = float(viz)
self.viz = viz
if self.viz and isinstance(self.viz, float):
self.windowname = os.path.basename(rom_file)
cv2.startWindowThread()
cv2.namedWindow(self.windowname)
self.ale.loadROM(rom_file.encode('utf-8'))
self.width, self.height = self.ale.getScreenDims()
self.actions = self.ale.getMinimalActionSet()
self.live_lost_as_eoe = live_lost_as_eoe
self.frame_skip = frame_skip
self.nullop_start = nullop_start
self.height_range = height_range
self.image_shape = image_shape
self.current_episode_score = StatCounter()
self.restart_episode()
def _grab_raw_image(self):
"""
:returns: the current 3-channel image
"""
m = self.ale.getScreenRGB()
return m.reshape((self.height, self.width, 3))
def current_state(self):
"""
:returns: a gray-scale (h, w, 1) float32 image
"""
ret = self._grab_raw_image()
# max-pooled over the last screen
ret = np.maximum(ret, self.last_raw_screen)
if self.viz:
if isinstance(self.viz, float):
#m = cv2.resize(ret, (1920,1200))
cv2.imshow(self.windowname, ret)
time.sleep(self.viz)
ret = ret[self.height_range[0]:self.height_range[1],:].astype('float32')
# 0.299,0.587.0.114. same as rgb2y in torch/image
ret = cv2.cvtColor(ret, cv2.COLOR_RGB2GRAY)
ret = cv2.resize(ret, self.image_shape)
ret = np.expand_dims(ret, axis=2)
return ret
def get_action_space(self):
return DiscreteActionSpace(len(self.actions))
#.........这里部分代码省略.........