Commit a625a971 authored by Matthew Hausknecht's avatar Matthew Hausknecht

Refactored example python agents.

parent abc00c38
...@@ -4,29 +4,39 @@ ...@@ -4,29 +4,39 @@
# Before running this program, first Start HFO server: # Before running this program, first Start HFO server:
# $> ./bin/HFO --offense-agents 1 # $> ./bin/HFO --offense-agents 1
import sys import sys, itertools
from hfo import * from hfo import *
if __name__ == '__main__': def main():
# Create the HFO Environment # Create the HFO Environment
hfo = hfo.HFOEnvironment() hfo = HFOEnvironment()
# Connect to the server with the specified # Connect to the server with the specified
# feature set. See feature sets in hfo.py/hfo.hpp. # feature set. See feature sets in hfo.py/hfo.hpp.
hfo.connectToServer(HIGH_LEVEL_FEATURE_SET, hfo.connectToServer(HIGH_LEVEL_FEATURE_SET,
'bin/teams/base/config/formations-dt', 6000, 'bin/teams/base/config/formations-dt', 6000,
'localhost', 'base_left', False) 'localhost', 'base_left', False)
for episode in xrange(10): for episode in itertools.count():
status = IN_GAME status = IN_GAME
while status == IN_GAME: while status == IN_GAME:
# Grab the state features from the environment # Grab the state features from the environment
features = hfo.getState() features = hfo.getState()
# Get any incoming communication # Get any incoming communication
msg = hfo.hear() msg = hfo.hear()
# Do something with incoming communication # Print the incoming communication
print 'Heard: ', msg if msg:
# Take an action and get the current game status print('Heard: %s'% msg)
hfo.act(DASH, 20.0, 0) # Take an action
# Do something with outgoing communication hfo.act(DASH, 20.0, 0.)
hfo.say('Message') # Create outgoing communication
hfo.say('Hello!')
# Advance the environment and get the game status
status = hfo.step() status = hfo.step()
print 'Episode', episode, 'ended with', hfo.statusToString(status) # Check the outcome of the episode
print('Episode %d ended with %s'%(episode, hfo.statusToString(status)))
# Quit if the server goes down
if status == SERVER_DOWN:
hfo.act(QUIT)
break
if __name__ == '__main__':
main()
...@@ -4,9 +4,10 @@ ...@@ -4,9 +4,10 @@
# Before running this program, first Start HFO server: # Before running this program, first Start HFO server:
# $> ./bin/HFO --offense-agents 1 # $> ./bin/HFO --offense-agents 1
import itertools
from hfo import * from hfo import *
if __name__ == '__main__': def main():
# Create the HFO Environment # Create the HFO Environment
hfo = HFOEnvironment() hfo = HFOEnvironment()
# Connect to the server with the specified # Connect to the server with the specified
...@@ -14,12 +15,21 @@ if __name__ == '__main__': ...@@ -14,12 +15,21 @@ if __name__ == '__main__':
hfo.connectToServer(LOW_LEVEL_FEATURE_SET, hfo.connectToServer(LOW_LEVEL_FEATURE_SET,
'bin/teams/base/config/formations-dt', 6000, 'bin/teams/base/config/formations-dt', 6000,
'localhost', 'base_left', False) 'localhost', 'base_left', False)
for episode in xrange(10): for episode in itertools.count():
status = IN_GAME status = IN_GAME
while status == IN_GAME: while status == IN_GAME:
# Grab the state features from the environment # Grab the state features from the environment
features = hfo.getState() features = hfo.getState()
# Take an action and get the current game status # Take an action and get the current game status
hfo.act(DASH, 20.0, 0) hfo.act(DASH, 20.0, 0.)
# Advance the environment and get the game status
status = hfo.step() status = hfo.step()
print 'Episode', episode, 'ended with', hfo.statusToString(status) # Check the outcome of the episode
print('Episode %d ended with %s'%(episode, hfo.statusToString(status)))
# Quit if the server goes down
if status == SERVER_DOWN:
hfo.act(QUIT)
break
if __name__ == '__main__':
main()
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
# Before running this program, first Start HFO server: # Before running this program, first Start HFO server:
# $> ./bin/HFO --offense-agents 1 # $> ./bin/HFO --offense-agents 1
import random import random, itertools
from hfo import * from hfo import *
def main(): def main():
...@@ -15,17 +15,24 @@ def main(): ...@@ -15,17 +15,24 @@ def main():
hfo.connectToServer(HIGH_LEVEL_FEATURE_SET, hfo.connectToServer(HIGH_LEVEL_FEATURE_SET,
'bin/teams/base/config/formations-dt', 6000, 'bin/teams/base/config/formations-dt', 6000,
'localhost', 'base_left', False) 'localhost', 'base_left', False)
for episode in xrange(10): for episode in itertools.count():
status = IN_GAME status = IN_GAME
while status == IN_GAME: while status == IN_GAME:
# Get the vector of state features for the current state
state = hfo.getState() state = hfo.getState()
# Perform the action
if state[5] == 1: # State[5] is 1 when the player can kick the ball if state[5] == 1: # State[5] is 1 when the player can kick the ball
hfo.act(random.choice([SHOOT, DRIBBLE])) hfo.act(random.choice([SHOOT, DRIBBLE]))
else: else:
hfo.act(MOVE) hfo.act(MOVE)
# Advance the environment and get the game status
status = hfo.step() status = hfo.step()
# print 'Episode', episode, 'ended with', hfo.statusToString(status) # Check the outcome of the episode
print('Episode %d ended with %s'%(episode, hfo.statusToString(status))) print('Episode %d ended with %s'%(episode, hfo.statusToString(status)))
# Quit if the server goes down
if status == SERVER_DOWN:
hfo.act(QUIT)
break
if __name__ == '__main__': if __name__ == '__main__':
main() main()
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