Commit 37af3268 authored by Matthew Hausknecht's avatar Matthew Hausknecht

Threading in high level random agent.

parent 86cf7950
...@@ -2,48 +2,39 @@ ...@@ -2,48 +2,39 @@
# encoding: utf-8 # encoding: utf-8
# First Start the server: $> bin/start.py # First Start the server: $> bin/start.py
import random import random, threading, argparse
if __name__ == '__main__': try:
try:
from hfo import * from hfo import *
except: except:
print 'Failed to import hfo. To install hfo, in the HFO directory'\ print 'Failed to import hfo. To install hfo, in the HFO directory'\
' run: \"pip install .\"' ' run: \"pip install .\"'
exit() exit()
# Connect 4 agents
agents = [] def get_random_action():
for i in xrange(4): """ Returns a random high-level action """
agents.append(hfo.HFOEnvironment()) high_lv_actions = [HFO_Actions.MOVE, HFO_Actions.SHOOT,
agents[i].connectToAgentServer(6000-i, HFO_Features.HIGH_LEVEL_FEATURE_SET) HFO_Actions.PASS, HFO_Actions.DRIBBLE]
return (random.choice(high_lv_actions), 0, 0)
def play_hfo(num):
""" Method called by a thread to play 5 games of HFO """
hfo_env = hfo.HFOEnvironment()
hfo_env.connectToAgentServer(6000 + num, HFO_Features.HIGH_LEVEL_FEATURE_SET)
for episode in xrange(5): for episode in xrange(5):
status = HFO_Status.IN_GAME status = HFO_Status.IN_GAME
while status == HFO_Status.IN_GAME: while status == HFO_Status.IN_GAME:
# Grab the state features from the environment from the first agent state = hfo_env.getState()
features = agents[0].getState() status = hfo_env.act(get_random_action())
# Take an action and get the current game status hfo_env.cleanup()
for agent in agents:
rand_int = random.randint(0,3) def main():
if rand_int == 0: parser = argparse.ArgumentParser()
status = agent.act((HFO_Actions.MOVE, 0, 0)) parser.add_argument('num_agents', type=int, help='Number of agents to start. '\
elif rand_int == 1: 'NOTE: server must be started with this number of agents.')
status = agent.act((HFO_Actions.SHOOT, 0, 0)) args = parser.parse_args()
elif rand_int == 2: for i in xrange(args.num_agents):
status = agent.act((HFO_Actions.PASS, 0, 0)) t = threading.Thread(target=play_hfo, args=(i,))
elif rand_int == 3: t.start()
status = agent.act((HFO_Actions.DRIBBLE, 0, 0))
print 'Episode', episode, 'ended with', if __name__ == '__main__':
# Check what the outcome of the episode was main()
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()
# Cleanup when finished
for agent in agents:
agent.cleanup()
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