hfo_example_agent.py 1.41 KB
Newer Older
1 2 3
#!/usr/bin/env python
# encoding: utf-8

sanmit's avatar
sanmit committed
4 5
import sys

6
# First Start the server: $> bin/start.py
7
if __name__ == '__main__':
sanmit's avatar
sanmit committed
8 9 10
  port = 6000
  if len(sys.argv) > 1:
    port = int(sys.argv[1])
11
  try:
12
    from hfo import *
13
  except:
14 15 16
    print 'Failed to import hfo. To install hfo, in the HFO directory'\
      ' run: \"pip install .\"'
    exit()
17
  # Create the HFO Environment
18
  hfo = hfo.HFOEnvironment()
19 20
  # Connect to the agent server on port 6000 with the specified
  # feature set. See feature sets in hfo.py/hfo.hpp.
sanmit's avatar
sanmit committed
21
  hfo.connectToAgentServer(port, HFO_Features.HIGH_LEVEL_FEATURE_SET)
sanmit's avatar
sanmit committed
22
  # Play 100 episodes
23
  for episode in xrange(100):
24 25 26 27
    status = HFO_Status.IN_GAME
    while status == HFO_Status.IN_GAME:
      # Grab the state features from the environment
      features = hfo.getState()
28
      # Take an action and get the current game status
sanmit's avatar
sanmit committed
29
      hfo.act(HFO_Actions.DASH, 20.0, 0)
30
      status = hfo.step()
31 32 33
    print 'Episode', episode, 'ended with',
    # Check what the outcome of the episode was
    if status == HFO_Status.GOAL:
34
      print 'goal', hfo.playerOnBall().unum
35
    elif status == HFO_Status.CAPTURED_BY_DEFENSE:
36
      print 'captured by defense', hfo.playerOnBall().unum 
37 38 39 40 41 42 43
    elif status == HFO_Status.OUT_OF_BOUNDS:
      print 'out of bounds'
    elif status == HFO_Status.OUT_OF_TIME:
      print 'out of time'
    else:
      print 'Unknown status', status
      exit()
44 45
  # Cleanup when finished
  hfo.cleanup()