Commit fdef3ceb authored by Matthew Hausknecht's avatar Matthew Hausknecht

Added example python agent.

parent 2598397e
import socket, struct, thread, time import socket, struct, thread, time
class HFOEnvironment(object): class HFOEnvironment(object):
'''The HFOEnvironment is designed to be the single point of contact '''The HFOEnvironment is designed to be the main point of contact
between a learning agent and the Half-Field-Offense domain. between a learning agent and the Half-Field-Offense domain.
''' '''
...@@ -9,24 +9,8 @@ class HFOEnvironment(object): ...@@ -9,24 +9,8 @@ class HFOEnvironment(object):
def __init__(self): def __init__(self):
self.socket = None # Socket connection to server self.socket = None # Socket connection to server
self.numFeatures = None # Given by the server in handshake self.numFeatures = None # Given by the server in handshake
self.trainerThreadID = None # Thread of the trainer process
self.actions = ['DASH', 'TURN', 'TACKLE', 'KICK'] self.actions = ['DASH', 'TURN', 'TACKLE', 'KICK']
def startDomain(self, args=[]):
'''Covenience method to start the HFO domain by calling the
/bin/start.py script and providing it kwargs. Call this method
before connectToAgentServer.
args: a list of argument strings passed to the start script.
(e.g. ['--offense','3']). See ./bin/start.py -h for all args.
'''
# This method calls the trainer in bin directory
def runTrainer():
from bin import start
start.main(start.parseArgs(args))
self.trainerThreadID = thread.start_new_thread(runTrainer,())
time.sleep(2)
def connectToAgentServer(self, server_port=6008): def connectToAgentServer(self, server_port=6008):
'''Connect to the server that controls the agent on the specified port. ''' '''Connect to the server that controls the agent on the specified port. '''
self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
...@@ -72,20 +56,9 @@ class HFOEnvironment(object): ...@@ -72,20 +56,9 @@ class HFOEnvironment(object):
def act(self, action_number): def act(self, action_number):
''' Send an action and recieve the resulting reward from the environment.''' ''' Send an action and recieve the resulting reward from the environment.'''
self.socket.send(struct.pack("i", action_number)) self.socket.send(struct.pack("i", action_number))
# TODO: Get the rewards from the domain
return 0 return 0
def cleanup(self): def cleanup(self):
''' Close the connection to the agent's server. ''' ''' Close the connection to the agent's server. '''
self.socket.close() self.socket.close()
if self.trainerThreadID is not None:
thread.interrupt_main()
if __name__ == '__main__':
hfo = HFOEnvironment()
trainer_args = '--offense 1 --defense 0 --headless'.split(' ')
hfo.startDomain(trainer_args)
hfo.connectToAgentServer()
while True:
features = hfo.getState()
reward = hfo.act(0)
hfo.cleanup()
...@@ -10,6 +10,7 @@ HFO ...@@ -10,6 +10,7 @@ HFO
- Only run single-threaded make. Multi-threaded make (eg `make -j4`) fails. - Only run single-threaded make. Multi-threaded make (eg `make -j4`) fails.
- [librcsc-4.1.0](http://en.sourceforge.jp/projects/rctools/downloads/51941/librcsc-4.1.0.tar.gz/) - [librcsc-4.1.0](http://en.sourceforge.jp/projects/rctools/downloads/51941/librcsc-4.1.0.tar.gz/)
- [soccerwindow2-5.1.0](http://en.sourceforge.jp/projects/rctools/downloads/51942/soccerwindow2-5.1.0.tar.gz/) (Optional) - [soccerwindow2-5.1.0](http://en.sourceforge.jp/projects/rctools/downloads/51942/soccerwindow2-5.1.0.tar.gz/) (Optional)
- If -laudio is not found during make, it can be safely removed from the link command.
## Install ## Install
1. Edit the `LIBRCSC_INCLUDE`/`LIBRCSC_LINK` variables in `CMakeLists.txt` to point to your librcsc include/lib directories. 1. Edit the `LIBRCSC_INCLUDE`/`LIBRCSC_LINK` variables in `CMakeLists.txt` to point to your librcsc include/lib directories.
......
#!/usr/bin/env python
# encoding: utf-8
import imp
if __name__ == '__main__':
# First Start the server by calling start.py in bin
# Load the HFO library
hfo_module = imp.load_source('HFO', '../HFO.py')
# Create the HFO Environment
hfo = hfo_module.HFOEnvironment()
hfo.connectToAgentServer()
# Continue until finished
while True:
# Grab the state features from the environment
features = hfo.getState()
# Take an action and get the reward
reward = hfo.act(0)
# Cleanup when finished
hfo.cleanup()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment