Search code examples
pythonmachine-learningopenai-gym

How to make a python agent observe?


I'm trying to make a simple python agent that can detect a turn in a game and turn left or right accordingly. However, I'm confused as to how to make the agent observe the screen and how to implement an agent into my code.

I'm still very new to machine learning and gym. I have the basic layout for using gym below,

import gym
import universe

env = gym.make(‘flashgames.NeonRace-v0’)
env.configure(remotes=1)
observation_n = env.reset()

while True:
    action_n = [[('KeyEvent', 'ArrowUp', True)] for ob in observation_n]
    #Your agent here
    observation_n, reward_n, done_n, info = env.step(action_n)
    env.render()

Below is the layout for an agent,

def getAgent():
   """ The daemon searches for this callable function to create a new agent with """
   return MyAgent()

class MyAgent(object):

    def __init__(self):
       """ standard object init """
       self.done = False

    def run(self, messaging, args):
       """ Call by daemon when the agent is to start running """
       while not self.done:
           pass

    def stop(self):
       """ Called by daemon when the thread is requested to stop """
       self.done = True

I would start implementing the code, but whenever it got to observing the screen I would get stuck.


Solution

  • You've already got the observations in the returns of env.reset() and env.step(action_n). An agent should take the observation and use some supervised learning method (e.g. deep neural network) to predict the action from an observation. Is this what is missing for you?