Class: ALEInterface

Inherits:
Object
  • Object
show all
Defined in:
lib/ale_ruby_interface.rb

Overview

This is the main class

Instance Method Summary collapse

Constructor Details

#initializeALEInterface

initialize method



65
66
67
68
69
70
71
72
73
74
# 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

Instance Method Details

#act(action) ⇒ Object

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).



133
134
135
# File 'lib/ale_ruby_interface.rb', line 133

def act(action)
  ALELib.act(@obj, action.to_i)
end

#clone_stateObject

Makes a copy of the environment state. This copy does not include pseudo-randomness, making it suitable for planning purposes. By contrast, see cloneSystemState.



331
332
333
# File 'lib/ale_ruby_interface.rb', line 331

def clone_state
  return ALELib.cloneState(@obj)
end

#clone_system_stateObject

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.



343
344
345
# File 'lib/ale_ruby_interface.rb', line 343

def clone_system_state
  return ALELib.cloneSystemState(@obj)
end

#decode_stateObject

encode_staten method



371
# File 'lib/ale_ruby_interface.rb', line 371

def decode_state; end

#delete_state(state) ⇒ Object

delete_state method



355
356
357
# File 'lib/ale_ruby_interface.rb', line 355

def delete_state(state)
  ALELib.deleteState(state)
end

#encode_stateObject

encode_staten method



367
# File 'lib/ale_ruby_interface.rb', line 367

def encode_state; end

#encode_state_len(state) ⇒ Object

encode_state_len method



361
362
363
# File 'lib/ale_ruby_interface.rb', line 361

def encode_state_len(state)
  return ALELib.encodeStateLen(state)
end

#game_overObject

Indicates if the game has ended.



139
140
141
# File 'lib/ale_ruby_interface.rb', line 139

def game_over
  ALELib.game_over(@obj)
end

#get_available_difficultiesObject

Returns the vector of difficulties available for the current game. This should be called only after the ROM is loaded.



198
199
200
201
202
203
204
205
206
# 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_modesObject

Returns the vector of modes available for the current game. This should be called only after the ROM is loaded.



178
179
180
181
182
183
184
185
186
# 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) ⇒ Object

Gets the value of any flag passed as parameter that has a boolean value



90
91
92
# File 'lib/ale_ruby_interface.rb', line 90

def get_bool(key)
  ALELib.getBool(@obj, key)
end

#get_episode_frame_numberObject

Returns the current frame number since the start of the cur- rent episode.



229
230
231
# File 'lib/ale_ruby_interface.rb', line 229

def get_episode_frame_number
  ALELib.getEpisodeFrameNumber(@obj)
end

#get_float(key) ⇒ Object

Gets the value of any flag passed as parameter that has a float value



96
97
98
# File 'lib/ale_ruby_interface.rb', line 96

def get_float(key)
  ALELib.getFloat(@obj, key)
end

#get_frame_numberObject

Returns the current frame number since the loading of the ROM.



217
218
219
# File 'lib/ale_ruby_interface.rb', line 217

def get_frame_number
  ALELib.getFrameNumber(@obj)
end

#get_int(key) ⇒ Object

Gets the value of any flag passed as parameter that has an integer value



84
85
86
# 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.



152
153
154
155
156
157
158
159
160
# 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

#get_minimal_action_setObject

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.



165
166
167
168
169
170
171
172
173
# 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_RAMObject

Returns a vector containing current RAM content (byte-level).



299
300
301
302
303
304
305
306
307
308
309
# 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_sizeObject

get_RAM_size method



293
294
295
# File 'lib/ale_ruby_interface.rb', line 293

def get_RAM_size
  ALELib.getRAMSize(@obj)
end

#get_screen(screen_data = nil) ⇒ Object

Returns a matrix containing the current game screen.



243
244
245
246
247
248
249
250
251
252
253
254
255
256
# 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_dimsObject

get_screen_dims method



235
236
237
238
239
# 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) ⇒ Object

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.



277
278
279
280
281
282
283
284
285
286
287
288
289
# 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_screen_RGBObject

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.



260
261
262
263
264
265
266
267
268
269
270
271
272
273
# 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_string(key) ⇒ Object

Gets the value of any flag passed as parameter that has a string value



78
79
80
# File 'lib/ale_ruby_interface.rb', line 78

def get_string(key)
  ALELib.getString(@obj, key)
end

#livesObject

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.



223
224
225
# File 'lib/ale_ruby_interface.rb', line 223

def lives
  ALELib.lives(@obj)
end

#load_ROM(rom_file) ⇒ Object

TODO: load_ROM method



126
127
128
# File 'lib/ale_ruby_interface.rb', line 126

def load_ROM(rom_file)
  ALELib.loadROM(@obj, rom_file)
end

#load_stateObject

Loads a previous saved state of the system once we have a state saved.



325
326
327
# File 'lib/ale_ruby_interface.rb', line 325

def load_state
  return ALELib.loadState(@obj)
end

#reset_gameObject

Resets the game, but not the full system (it is not “equivalent” to un- plugging the console from electricity).



145
146
147
# File 'lib/ale_ruby_interface.rb', line 145

def reset_game
  ALELib.reset_game(@obj)
end

#restore_state(state) ⇒ Object

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.



337
338
339
# File 'lib/ale_ruby_interface.rb', line 337

def restore_state(state)
  ALELib.restoreState(@obj, state)
end

#restore_system_stateObject

Reverse operation of cloneSystemState.



349
350
351
# File 'lib/ale_ruby_interface.rb', line 349

def restore_system_state
  ALELib.restoreSystemState(@obj)
end

#save_screen_PNG(filename) ⇒ Object

Saves the current screen as a png file.



313
314
315
# File 'lib/ale_ruby_interface.rb', line 313

def save_screen_PNG(filename)
  return ALELib.saveScreenPNG(@obj, filename)
end

#save_stateObject

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.



319
320
321
# File 'lib/ale_ruby_interface.rb', line 319

def save_state
  return ALELib.saveState(@obj)
end

#set_bool(key, value) ⇒ Object

Sets the value of any flag that has a boolean type



114
115
116
# File 'lib/ale_ruby_interface.rb', line 114

def set_bool(key, value)
  ALELib.setBool(@obj, key, value)
end

#set_difficulty(difficulty) ⇒ Object

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.



211
212
213
# File 'lib/ale_ruby_interface.rb', line 211

def set_difficulty(difficulty)
  ALELib.set_mode(@obj, difficulty)
end

#set_float(key, value) ⇒ Object

Sets the value of any flag that has a float type



120
121
122
# File 'lib/ale_ruby_interface.rb', line 120

def set_float(key, value)
  ALELib.setFloat(@obj, key, value)
end

#set_int(key, value) ⇒ Object

Sets the value of any flag that has an integer type



108
109
110
# File 'lib/ale_ruby_interface.rb', line 108

def set_int(key, value)
  ALELib.setInt(@obj, key, value)
end

#set_mode(mode) ⇒ Object

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.



191
192
193
# File 'lib/ale_ruby_interface.rb', line 191

def set_mode(mode)
  ALELib.set_mode(@obj, mode)
end

#set_string(key, value) ⇒ Object

Sets the value of any flag that has a string type



102
103
104
# File 'lib/ale_ruby_interface.rb', line 102

def set_string(key, value)
  ALELib.setString(@obj, key, value)
end