Advertisement
Guest User

Untitled

a guest
May 3rd, 2016
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.55 KB | None | 0 0
  1. import logging
  2. import os
  3.  
  4. import gym
  5.  
  6. # The world's simplest agent!
  7. class RandomAgent(object):
  8. def __init__(self, action_space):
  9. self.action_space = action_space
  10.  
  11. def act(self, observation, reward, done):
  12. return self.action_space.sample()
  13.  
  14. if __name__ == '__main__':
  15. # You can optionally set up the logger. Also fine to set the level
  16. # to logging.DEBUG or logging.WARN if you want to change the
  17. # amount of outut.
  18. logger = logging.getLogger()
  19. logger.setLevel(logging.INFO)
  20.  
  21. env = gym.make('CartPole-v0')
  22. agent = RandomAgent(env.action_space)
  23.  
  24. # You provide the directory to write to (can be an existing
  25. # directory, but can't contain previous monitor results. You can
  26. # also dump to a tempdir if you'd like: tempfile.mkdtemp().
  27. outdir = '/tmp/random-agent-results'
  28. env.monitor.start(outdir, force=True)
  29.  
  30. episode_count = 100
  31. max_steps = 200
  32. reward = 0
  33. done = False
  34.  
  35. for i in xrange(episode_count):
  36. ob = env.reset()
  37.  
  38. for j in xrange(max_steps):
  39. action = agent.act(ob, reward, done)
  40. ob, reward, done, _ = env.step(action)
  41. if done:
  42. break
  43.  
  44. # Dump result info to disk
  45. env.monitor.close()
  46.  
  47. # Upload to the scoreboard. We could also do this from another
  48. # process if we wanted.
  49. logger.info("Successfully ran RandomAgent. Now trying to upload results to the scoreboard. If it breaks, you can always just try re-uploading the same results.")
  50. gym.upload('/tmp/random-agent-results', api_key='sk_w8fHsr7MTMybFRwBPZWlw')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement