Advertisement
Guest User

Untitled

a guest
May 25th, 2016
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.36 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # python_example.py
  3. # Author: Ben Goodrich
  4. #
  5. # This is a direct port to python of the shared library example from
  6. # ALE provided in doc/examples/sharedLibraryInterfaceExample.cpp
  7. import sys
  8. import numpy as np
  9. from random import randrange
  10. from ale_python_interface import ALEInterface
  11.  
  12.  
  13. def squareLoop(I,J):
  14.   for i in (I, I+4):
  15.     for j in (J, J+4):
  16.       if(trimmed_data2[i,j] != 87):
  17.         pooling_data[I/4,J/4] = 1
  18.         return
  19.  
  20. if len(sys.argv) < 2:
  21.   print 'Usage:', sys.argv[0], 'rom_file'
  22.   sys.exit()
  23.  
  24. ale = ALEInterface()
  25.  
  26. # Get & Set the desired settings
  27. ale.setInt('random_seed', 123)
  28.  
  29. # Set USE_SDL to true to display the screen. ALE must be compilied
  30. # with SDL enabled for this to work. On OSX, pygame init is used to
  31. # proxy-call SDL_main.
  32. USE_SDL = True
  33. if USE_SDL:
  34.   if sys.platform == 'darwin':
  35.     import pygame
  36.     pygame.init()
  37.     ale.setBool('sound', False) # Sound doesn't work on OSX
  38.   elif sys.platform.startswith('linux'):
  39.     ale.setBool('sound', True)
  40.   ale.setBool('display_screen', True)
  41.  
  42. # Load the ROM file
  43. ale.loadROM(sys.argv[1])
  44. # Get the list of legal actions
  45. legal_actions = ale.getLegalActionSet()
  46. #--------------------------------------------------------
  47. (screen_width, screen_height) = ale.getScreenDims()
  48. #screen_data = np.zeros(screen_width*screen_height, dtype=np.uint32)
  49. screen_data = np.zeros((screen_height,screen_width), dtype=np.uint8)
  50. pooling_data = np.zeros((40,40), dtype=np.uint8)
  51. #--------------------------------------------------------
  52. # Play 10 episodes
  53. for episode in xrange(1):
  54.   total_reward = 0
  55.   #while not ale.game_over():
  56.   for i in xrange(200):
  57.    
  58.  
  59.  
  60.  
  61.     a = legal_actions[randrange(len(legal_actions))]
  62.     # Apply an action and get the resulting reward
  63.     reward = ale.act(a);
  64.     total_reward += reward
  65.  
  66.   ale.getScreenGrayscale(screen_data)
  67.   trimmed_data = np.delete(screen_data, np.s_[195::], 0)
  68.   trimmed_data2 = np.delete(trimmed_data, np.s_[0:35], 0)
  69.   for I in range(0,156,4):
  70.     for J in range(0,156,4):
  71.       squareLoop(I,J)
  72.  
  73.  
  74.  
  75.   np.savetxt('dat1.txt', trimmed_data2, fmt='%2.2f')
  76.   np.savetxt('dat1pooled.txt', pooling_data, fmt='%2.2f')
  77.   print 'Episode', episode, 'ended with score:', total_reward
  78.   print screen_data.shape
  79.   print trimmed_data.shape
  80.   print trimmed_data2.shape
  81.   print pooling_data.shape
  82.   ale.reset_game()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement