class ALEInterface
This is the main class
Public Class Methods
initialize method
# File lib/ale_ruby_interface.rb, line 65 def initialize # if ale.cfg doesn't exist, will create one. src = File.join(File.dirname(__FILE__), "ale.cfg") dest = './ale.cfg' if !File.exist?('./ale.cfg') FileUtils.cp(src, dest) puts "Created ale.cfg successfully" end @obj = ALELib.ALE_new end
Public Instance Methods
Applies an action to the game and returns the reward. It is the user’s responsibility to check if the game has ended and to reset it when necessary (this method will keep pressing buttons on the game over screen).
# File lib/ale_ruby_interface.rb, line 133 def act(action) ALELib.act(@obj, action.to_i) end
Makes a copy of the environment state. This copy does not include pseudo-randomness, making it suitable for planning purposes. By contrast, see cloneSystemState.
# File lib/ale_ruby_interface.rb, line 331 def clone_state return ALELib.cloneState(@obj) end
This makes a copy of the system and environment state, suitable for serialization. This includes pseudo-randomness and so is not suitable for planning purposes.
# File lib/ale_ruby_interface.rb, line 343 def clone_system_state return ALELib.cloneSystemState(@obj) end
encode_staten method
# File lib/ale_ruby_interface.rb, line 371 def decode_state; end
delete_state
method
# File lib/ale_ruby_interface.rb, line 355 def delete_state(state) ALELib.deleteState(state) end
encode_staten method
# File lib/ale_ruby_interface.rb, line 367 def encode_state; end
encode_state_len
method
# File lib/ale_ruby_interface.rb, line 361 def encode_state_len(state) return ALELib.encodeStateLen(state) end
Indicates if the game has ended.
# File lib/ale_ruby_interface.rb, line 139 def game_over ALELib.game_over(@obj) end
Returns a vector containing current RAM content (byte-level).
# File lib/ale_ruby_interface.rb, line 299 def get_RAM() ram_size = ALELib.getRAMSize(@obj) FFI::MemoryPointer.new(:uint64, ram_size) do |p| ALELib.getRAM(@obj, p) return NMatrix.new( [ram_size], p.read_array_of_uint8(ram_size), dtype: :int16 ) end end
get_RAM_size
method
# File lib/ale_ruby_interface.rb, line 293 def get_RAM_size ALELib.getRAMSize(@obj) end
Returns the vector of difficulties available for the current game. This should be called only after the ROM is loaded.
# File lib/ale_ruby_interface.rb, line 198 def get_available_difficulties difficulties_size = ALELib.getAvailableDifficultiesSize(@obj) difficulties = NMatrix.zeros [difficulties_size] FFI::MemoryPointer.new(:int, difficulties.size) do |p| p.put_array_of_int(0, difficulties) ALELib.getAvailableDifficulties(@obj, p) return p.read_array_of_int(difficulties_size) end end
Returns the vector of modes available for the current game. This should be called only after the ROM is loaded.
# File lib/ale_ruby_interface.rb, line 178 def get_available_modes modes_size = ALELib.getAvailableModesSize(@obj) modes = NMatrix.zeros [modes_size] FFI::MemoryPointer.new(:int, modes.size) do |p| p.put_array_of_int(0, modes) ALELib.getAvailableModes(@obj, p) return p.read_array_of_int(modes_size) end end
Gets the value of any flag passed as parameter that has a boolean value
# File lib/ale_ruby_interface.rb, line 90 def get_bool(key) ALELib.getBool(@obj, key) end
Returns the current frame number since the start of the cur- rent episode.
# File lib/ale_ruby_interface.rb, line 229 def get_episode_frame_number ALELib.getEpisodeFrameNumber(@obj) end
Gets the value of any flag passed as parameter that has a float value
# File lib/ale_ruby_interface.rb, line 96 def get_float(key) ALELib.getFloat(@obj, key) end
Returns the current frame number since the loading of the ROM.
# File lib/ale_ruby_interface.rb, line 217 def get_frame_number ALELib.getFrameNumber(@obj) end
Gets the value of any flag passed as parameter that has an integer value
# File lib/ale_ruby_interface.rb, line 84 def get_int(key) ALELib.getInt(@obj, key) end
Returns the vector of legal actions (all the 18 actions). This should be called only after the ROM is loaded.
# File lib/ale_ruby_interface.rb, line 152 def get_legal_action_set act_size = ALELib.getLegalActionSize(@obj) act = NMatrix.zeros [act_size] FFI::MemoryPointer.new(:int, act.size) do |p| p.put_array_of_int(0, act) ALELib.getLegalActionSet(@obj, p) return p.read_array_of_int(act_size) end end
Returns the vector of the minimal set of actions needed to play the game (all actions that have some effect on the game). This should be called only after the ROM is loaded.
# File lib/ale_ruby_interface.rb, line 165 def get_minimal_action_set act_size = ALELib.getMinimalActionSize(@obj) act = NMatrix.zeros [act_size] FFI::MemoryPointer.new(:int, act.size) do |p| p.put_array_of_int(0, act) ALELib.getMinimalActionSet(@obj, p) return p.read_array_of_int(act_size) end end
Returns a matrix containing the current game screen.
# File lib/ale_ruby_interface.rb, line 243 def get_screen(screen_data = nil) # This function fills screen_data with the RAW Pixel data width = ALELib.getScreenWidth(@obj) height = ALELib.getScreenHeight(@obj) size = width * height FFI::MemoryPointer.new(:uint8, size) do |p| ALELib.getScreen(@obj, p) return NMatrix.new( [width * height], p.read_array_of_uint8(size), dtype: :int16 ) end end
This method fills the given vector with a RGB version of the current screen, provided in row, column, then colour channel order (typically yielding 210 × 160 × 3 = 100, 800 entries). The colour channels themselves are, in order: R, G, B. For example, output_rgb_buffer[(160 * 3) * 1 + 52 * 3 + 1] corresponds to the 2nd row, 53rd column pixel’s green value. The vector is resized as needed. Still, for efficiency it is recommended to initialize the vector beforehand, to make sure an allocation is not performed at each time step.
# File lib/ale_ruby_interface.rb, line 260 def get_screen_RGB() # This function fills screen_data with the data in RGB format width = ALELib.getScreenWidth(@obj) height = ALELib.getScreenHeight(@obj) size = width * height * 3 FFI::MemoryPointer.new(:uint8, size) do |p| ALELib.getScreenRGB(@obj, p) return NMatrix.new( [width, height, 3], p.read_array_of_uint8(size), dtype: :int16 ) end end
get_screen_dims
method
# File lib/ale_ruby_interface.rb, line 235 def get_screen_dims width = ALELib.getScreenWidth(@obj) height = ALELib.getScreenHeight(@obj) { width: width, height: height } end
This method fills the given vector with a grayscale version of the current screen, provided in row- major order (typically yielding 210 × 160 = 33, 600 entries). The vector is resized as needed. For efficiency it is recommended to initialize the vector beforehand, to make sure an allocation is not performed at each time step. Note that the grayscale value corresponds to the pixel’s luminance; for more details, consult the web.
# File lib/ale_ruby_interface.rb, line 277 def get_screen_grayscale(screen_data = nil) width = ALELib.getScreenWidth(@obj) height = ALELib.getScreenHeight(@obj) size = width * height * 1 FFI::MemoryPointer.new(:uint8, size) do |p| ALELib.getScreenGrayscale(@obj, p) return NMatrix.new( [width, height, 1], p.read_array_of_uint8(size), dtype: :int16 ) end end
Gets the value of any flag passed as parameter that has a string value
# File lib/ale_ruby_interface.rb, line 78 def get_string(key) ALELib.getString(@obj, key) end
Returns the agent’s remaining number of lives. If the game does not have the concept of lives (e.g. Freeway), this function returns 0.
# File lib/ale_ruby_interface.rb, line 223 def lives ALELib.lives(@obj) end
TODO: load_ROM
method
# File lib/ale_ruby_interface.rb, line 126 def load_ROM(rom_file) ALELib.loadROM(@obj, rom_file) end
Loads a previous saved state of the system once we have a state saved.
# File lib/ale_ruby_interface.rb, line 325 def load_state return ALELib.loadState(@obj) end
Resets the game, but not the full system (it is not “equivalent” to un- plugging the console from electricity).
# File lib/ale_ruby_interface.rb, line 145 def reset_game ALELib.reset_game(@obj) end
Reverse operation of cloneState(). This does not restore pseudo-randomness, so that repeated calls to restoreState() in the stochastic controls setting will not lead to the same outcomes. By contrast, see restoreSystemState.
# File lib/ale_ruby_interface.rb, line 337 def restore_state(state) ALELib.restoreState(@obj, state) end
Reverse operation of cloneSystemState.
# File lib/ale_ruby_interface.rb, line 349 def restore_system_state ALELib.restoreSystemState(@obj) end
Saves the current screen as a png file.
# File lib/ale_ruby_interface.rb, line 313 def save_screen_PNG(filename) return ALELib.saveScreenPNG(@obj, filename) end
Saves the current state of the system if one wants to be able to recover a state in the future; e.g. in search algorithms.
# File lib/ale_ruby_interface.rb, line 319 def save_state return ALELib.saveState(@obj) end
Sets the value of any flag that has a boolean type
# File lib/ale_ruby_interface.rb, line 114 def set_bool(key, value) ALELib.setBool(@obj, key, value) end
Sets the difficulty of the game. The difficulty must be an available mode (otherwise it throws an exception). This should be called only after the ROM is loaded.
# File lib/ale_ruby_interface.rb, line 211 def set_difficulty(difficulty) ALELib.set_mode(@obj, difficulty) end
Sets the value of any flag that has a float type
# File lib/ale_ruby_interface.rb, line 120 def set_float(key, value) ALELib.setFloat(@obj, key, value) end
Sets the value of any flag that has an integer type
# File lib/ale_ruby_interface.rb, line 108 def set_int(key, value) ALELib.setInt(@obj, key, value) end
Sets the mode of the game. The mode must be an available mode (otherwise it throws an exception). This should be called only after the ROM is loaded.
# File lib/ale_ruby_interface.rb, line 191 def set_mode(mode) ALELib.set_mode(@obj, mode) end
Sets the value of any flag that has a string type
# File lib/ale_ruby_interface.rb, line 102 def set_string(key, value) ALELib.setString(@obj, key, value) end
Private Instance Methods
# File lib/ale_ruby_interface.rb, line 375 def as_types; end