-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpython_example.py
More file actions
executable file
·51 lines (43 loc) · 1.39 KB
/
python_example.py
File metadata and controls
executable file
·51 lines (43 loc) · 1.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/usr/bin/env python
# python_example.py
# Author: Ben Goodrich
#
# This is a direct port to python of the shared library example from
# ALE provided in doc/examples/sharedLibraryInterfaceExample.cpp
import sys
from random import randrange
from ale_python_interface import ALEInterface
if len(sys.argv) < 2:
print 'Usage:', sys.argv[0], 'rom_file'
sys.exit()
ale = ALEInterface()
# Get & Set the desired settings
ale.setInt('random_seed', 123)
# 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 = False
if USE_SDL:
if sys.platform == 'darwin':
import pygame
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)
# Load the ROM file
ale.loadROM(sys.argv[1])
# Get the list of legal actions
legal_actions = ale.getLegalActionSet()
#TODO: initialize the replay memory
#TODO: initialize the action value function with random weights
# 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);
total_reward += reward
print 'Episode', episode, 'ended with score:', total_reward
ale.reset_game()