From fdef3cebfc2c8f51408056660c4e53c8e103deef Mon Sep 17 00:00:00 2001
From: Matthew Hausknecht <matthew.hausknecht@gmail.com>
Date: Fri, 6 Mar 2015 15:53:02 -0600
Subject: [PATCH] Added example python agent.

---
 HFO.py           | 31 ++-----------------------------
 README.md        |  1 +
 example/agent.py | 20 ++++++++++++++++++++
 3 files changed, 23 insertions(+), 29 deletions(-)
 create mode 100755 example/agent.py

diff --git a/HFO.py b/HFO.py
index 331ecc6..1d8a5e7 100644
--- a/HFO.py
+++ b/HFO.py
@@ -1,7 +1,7 @@
 import socket, struct, thread, time
 
 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.
 
   '''
@@ -9,24 +9,8 @@ class HFOEnvironment(object):
   def __init__(self):
     self.socket = None # Socket connection to server
     self.numFeatures = None # Given by the server in handshake
-    self.trainerThreadID = None # Thread of the trainer process
     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):
     '''Connect to the server that controls the agent on the specified port. '''
     self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
@@ -72,20 +56,9 @@ class HFOEnvironment(object):
   def act(self, action_number):
     ''' Send an action and recieve the resulting reward from the environment.'''
     self.socket.send(struct.pack("i", action_number))
+    # TODO: Get the rewards from the domain
     return 0
 
   def cleanup(self):
     ''' Close the connection to the agent's server. '''
     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()
diff --git a/README.md b/README.md
index 75b8a62..848c466 100644
--- a/README.md
+++ b/README.md
@@ -10,6 +10,7 @@ HFO
   - 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/)
  - [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
 1. Edit the `LIBRCSC_INCLUDE`/`LIBRCSC_LINK` variables in `CMakeLists.txt` to point to your librcsc include/lib directories. 
diff --git a/example/agent.py b/example/agent.py
new file mode 100755
index 0000000..5022247
--- /dev/null
+++ b/example/agent.py
@@ -0,0 +1,20 @@
+#!/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()
-- 
2.24.1