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

4 5
# Before running this program, first Start HFO server:
# $> ./bin/HFO --offense-agents 1
6

7 8 9 10 11 12 13 14 15
import argparse
import itertools
import random
try:
  import hfo
except ImportError:
  print('Failed to import hfo. To install hfo, in the HFO directory'\
    ' run: \"pip install .\"')
  exit()
16 17

def main():
18 19 20 21 22 23
  parser = argparse.ArgumentParser()
  parser.add_argument('--port', type=int, default=6000)
  parser.add_argument('--seed', type=int, default=None)
  args=parser.parse_args()
  if args.seed:
    random.seed(args.seed)
24
  # Create the HFO Environment
25
  hfo_env = hfo.HFOEnvironment()
26 27
  # Connect to the server with the specified
  # feature set. See feature sets in hfo.py/hfo.hpp.
28 29 30
  hfo_env.connectToServer(hfo.HIGH_LEVEL_FEATURE_SET,
                          'bin/teams/base/config/formations-dt', args.port,
                          'localhost', 'base_left', False)
31
  for episode in itertools.count():
32 33
    status = hfo.IN_GAME
    while status == hfo.IN_GAME:
34
      # Get the vector of state features for the current state
35
      state = hfo_env.getState()
36
      # Perform the action
37
      if state[5] == 1: # State[5] is 1 when the player can kick the ball
38 39 40 41
        if random.random() < 0.5: # more efficient than random.choice for 2
          hfo_env.act(hfo.SHOOT)
        else:
          hfo_env.act(hfo.DRIBBLE)
42
      else:
43
        hfo_env.act(hfo.MOVE)
44
      # Advance the environment and get the game status
45 46
      status = hfo_env.step()
      
47
    # Check the outcome of the episode
DurgeshSamant's avatar
DurgeshSamant committed
48
    print(('Episode %d ended with %s'%(episode, hfo.statusToString(status))))
49
    # Quit if the server goes down
50 51
    if status == hfo.SERVER_DOWN:
      hfo_env.act(hfo.QUIT)
52
      exit()
53 54 55

if __name__ == '__main__':
  main()