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

import imp

6
# First Start the server: $> bin/start.py
7

8 9
if __name__ == '__main__':
  # Load the HFO library
10 11 12 13
  try:
    hfo_module = imp.load_source('HFO', '../HFO.py')
  except:
    hfo_module = imp.load_source('HFO', 'HFO.py')
14
  # Get the possible actions
15 16 17
  HFO_Actions = hfo_module.HFO_Actions
  # Get the possible outcomes
  HFO_Status = hfo_module.HFO_Status
18 19
  # Create the HFO Environment
  hfo = hfo_module.HFOEnvironment()
20
  # Connect to the agent server
21
  hfo.connectToAgentServer()
22 23 24 25 26 27
  # Play 5 episodes
  for episode in xrange(5):
    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
29 30 31 32 33 34 35 36 37 38 39 40 41 42
      status = hfo.act((HFO_Actions.KICK, 100, 12.3))
    print 'Episode', episode, 'ended with',
    # Check what the outcome of the episode was
    if status == HFO_Status.GOAL:
      print 'goal'
    elif status == HFO_Status.CAPTURED_BY_DEFENSE:
      print 'captured by defense'
    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()
43 44
  # Cleanup when finished
  hfo.cleanup()