class ALEInterface

This is the main class

Public Class Methods

new() click to toggle source

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

act(action) click to toggle source

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
clone_state() click to toggle source

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
clone_system_state() click to toggle source

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
decode_state() click to toggle source

encode_staten method

# File lib/ale_ruby_interface.rb, line 371
def decode_state; end
delete_state(state) click to toggle source

delete_state method

# File lib/ale_ruby_interface.rb, line 355
def delete_state(state)
  ALELib.deleteState(state)
end
encode_state() click to toggle source

encode_staten method

# File lib/ale_ruby_interface.rb, line 367
def encode_state; end
encode_state_len(state) click to toggle source

encode_state_len method

# File lib/ale_ruby_interface.rb, line 361
def encode_state_len(state)
  return ALELib.encodeStateLen(state)
end
game_over() click to toggle source

Indicates if the game has ended.

# File lib/ale_ruby_interface.rb, line 139
def game_over
  ALELib.game_over(@obj)
end
get_RAM() click to toggle source

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() click to toggle source

get_RAM_size method

# File lib/ale_ruby_interface.rb, line 293
def get_RAM_size
  ALELib.getRAMSize(@obj)
end
get_available_difficulties() click to toggle source

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
get_available_modes() click to toggle source

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
get_bool(key) click to toggle source

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
get_episode_frame_number() click to toggle source

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
get_float(key) click to toggle source

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
get_frame_number() click to toggle source

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
get_int(key) click to toggle source

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
get_minimal_action_set() click to toggle source

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
get_screen(screen_data = nil) click to toggle source

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
get_screen_RGB() click to toggle source

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() click to toggle source

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
get_screen_grayscale(screen_data = nil) click to toggle source

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
get_string(key) click to toggle source

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
lives() click to toggle source

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
load_ROM(rom_file) click to toggle source

TODO: load_ROM method

# File lib/ale_ruby_interface.rb, line 126
def load_ROM(rom_file)
  ALELib.loadROM(@obj, rom_file)
end
load_state() click to toggle source

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
reset_game() click to toggle source

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
restore_state(state) click to toggle source

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
restore_system_state() click to toggle source

Reverse operation of cloneSystemState.

# File lib/ale_ruby_interface.rb, line 349
def restore_system_state
  ALELib.restoreSystemState(@obj)
end
save_screen_PNG(filename) click to toggle source

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
save_state() click to toggle source

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
set_bool(key, value) click to toggle source

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
set_difficulty(difficulty) click to toggle source

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
set_float(key, value) click to toggle source

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
set_int(key, value) click to toggle source

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
set_mode(mode) click to toggle source

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
set_string(key, value) click to toggle source

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

as_types() click to toggle source
# File lib/ale_ruby_interface.rb, line 375
def as_types; end