Last active
January 4, 2017 17:15
-
-
Save llSourcell/b87ae7feafa82c86b5c4645275e436fc to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #Step 1 - clone universe | |
| #git clone https://github.com/openai/universe.git | |
| #cd universe | |
| #pip install -e . (Editable mode) It makes installed packages editable. | |
| #And its reading which packages to download from setup.py | |
| #Step 2 - install command line tools | |
| #Step 3 - Install 3 more packages. Homebrew is like apt-get for OS X. | |
| #Step 4 - Install Docker (I recommend the binary) | |
| #sudo service docker start | |
| #It's kind of like a tv dinner. Comes in a box, and has every thing you | |
| #need to have a meal right there in the box. When you're done, you toss it. | |
| # Whether you microwave it, put it in the oven, or heat it over a fire, it'll | |
| #taste the same whenever, wherever. | |
| #Docker tries to make applications like TV Dinners. VMs are kind of like it, | |
| #but not quite the same. VMs typically include an entire operating system, | |
| #but a Docker ecosystem has a central engine that can run multiple Docker containers. | |
| # It cuts out the bloat of multiple operating systems, and simply packages the | |
| #runtime elements the program needs. | |
| #Docker = TV dinner | |
| #VM = Microwave with TV dinner inside | |
| #Step 5 - Run agent | |
| import gym | |
| import universe | |
| import random | |
| def determine_turn(turn, observation_n, j, total_sum, prev_total_sum, reward_n): | |
| # For every 15 iterations, sum total observations and take average. | |
| #If lower than 0, change direction | |
| # This makes it more accurate since the iterations are very fast. | |
| if(j >= 15): | |
| if((total_sum / j) == 0): | |
| turn = True | |
| else: | |
| turn = False | |
| #reset vars | |
| total_sum = 0 | |
| j = 0 | |
| prev_total_sum = total_sum | |
| total_sum = 0 | |
| else: | |
| turn = False | |
| #if we have an observation | |
| if(observation_n != None): | |
| #incremenet counter and sum | |
| j+=1 | |
| total_sum += reward_n | |
| #return for debugging | |
| return(turn, j, total_sum, prev_total_sum) | |
| def main(): | |
| # Init environment | |
| #Internally, a Universe environment consists of two pieces: a client and | |
| #a remote: | |
| #Client is a VNC environment. That’s where agent lives. Virtual network | |
| #computing. Desktop sharing system. | |
| #Sending keyboard and mouse events from one computer to another. | |
| #Remote is the game inside docker container | |
| #Both communicate via VNC main way | |
| #Have it run locally or on the web | |
| #Define your environment | |
| #Configure I downloads and starts a flashgames runtime | |
| env = gym.make('flashgames.CoasterRacer-v0') | |
| observation_n = env.reset() | |
| # Init starting variables | |
| #number of game iterations | |
| n = 0 | |
| j = 0 | |
| #sum of observations | |
| total_sum = 0 | |
| prev_total_sum = 0 | |
| turn = False | |
| # Init keys | |
| #up ,left, | |
| #up, right, | |
| #up | |
| #we have false because we want to align these up synchornously | |
| left = [('KeyEvent', 'ArrowUp', True),('KeyEvent', 'ArrowLeft', True), | |
| ('KeyEvent', 'ArrowRight', False)] | |
| right = [('KeyEvent', 'ArrowUp', True),('KeyEvent', 'ArrowLeft', False), | |
| ('KeyEvent', 'ArrowRight', True)] | |
| forward = [('KeyEvent', 'ArrowUp', True),('KeyEvent', 'ArrowRight', False), | |
| ('KeyEvent', 'ArrowLeft', False)] | |
| # Run while True | |
| while True: | |
| #increment a counter for number of iterations | |
| n += 1 | |
| # If at least one iteration has been made, check if turn is needed. | |
| if(n > 1): | |
| #if we've received an observation | |
| if(observation_n[0] != None): | |
| #store the reward in previous score | |
| prev_score = reward_n[0] | |
| #if its our turn | |
| if(turn): | |
| #Pick random event | |
| #(set of keyboard actions) | |
| event = random.choice([left, right]) | |
| #perform an action for each | |
| #environments obversation | |
| action_n = [event for ob in observation_n] | |
| #set turn to false | |
| turn = False | |
| elif(~turn): | |
| # If no turn is needed, go straight | |
| action_n = [forward for ob in observation_n] | |
| # If there is an observation, game has started and check if turn is needed or not | |
| if(observation_n[0] != None): | |
| turn, j, total_sum, prev_total_sum = | |
| determine_turn(turn, observation_n[0], j, total_sum, prev_total_sum, reward_n[0]) | |
| # Save new variables for each iterations | |
| observation_n, reward_n, done_n, info = env.step(action_n) | |
| env.render() | |
| if __name__ == '__main__': | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment