Commit e9af5a81 authored by drallensmith's avatar drallensmith

Got rand-pass working (need to backport to randomization); other bugfixes

parent f461c24a
#!/usr/bin/env python #!/usr/bin/env python
from __future__ import print_function
# encoding: utf-8 # encoding: utf-8
# Before running this program, first Start HFO server: # Before running this program, first Start HFO server:
...@@ -30,7 +31,12 @@ def main(): ...@@ -30,7 +31,12 @@ def main():
hfo_env.connectToServer(hfo.LOW_LEVEL_FEATURE_SET, hfo_env.connectToServer(hfo.LOW_LEVEL_FEATURE_SET,
'bin/teams/base/config/formations-dt', args.port, 'bin/teams/base/config/formations-dt', args.port,
'localhost', 'base_left', False) 'localhost', 'base_left', False)
if args.seed:
print("Python randomization seed: {0:d}".format(args.seed))
for episode in itertools.count(): for episode in itertools.count():
num_reorient = 0
num_move = 0
num_had_ball = 0
status = hfo.IN_GAME status = hfo.IN_GAME
while status == hfo.IN_GAME: while status == hfo.IN_GAME:
# Get the vector of state features for the current state # Get the vector of state features for the current state
...@@ -41,16 +47,23 @@ def main(): ...@@ -41,16 +47,23 @@ def main():
hfo_env.act(hfo.SHOOT) hfo_env.act(hfo.SHOOT)
else: else:
hfo_env.act(hfo.DRIBBLE) hfo_env.act(hfo.DRIBBLE)
num_had_ball += 1
# 8 is frozen; rest are self or ball position/velocity valid # 8 is frozen; rest are self or ball position/velocity valid
elif (state[8] > 0) or (min(state[0],state[1],state[50],state[54]) < 0): elif (state[8] > 0) or (min(state[0],state[1],state[50],state[54]) < 0):
hfo_env.act(hfo.REORIENT) hfo_env.act(hfo.REORIENT)
num_reorient += 1
else: else:
hfo_env.act(hfo.MOVE) hfo_env.act(hfo.MOVE)
num_move += 1
# Advance the environment and get the game status # Advance the environment and get the game status
status = hfo_env.step() status = hfo_env.step()
# Check the outcome of the episode # Check the outcome of the episode
print(('Episode %d ended with %s'%(episode, hfo.statusToString(status)))) print("Episode {0:d} ended with status {1}".format(episode,
hfo_env.statusToString(status)))
print("\tHad ball: {0:d}; Reorient: {1:d}; Move: {2:d}".format(num_had_ball,
num_reorient,
num_move))
# Quit if the server goes down # Quit if the server goes down
if status == hfo.SERVER_DOWN: if status == hfo.SERVER_DOWN:
hfo_env.act(hfo.QUIT) hfo_env.act(hfo.QUIT)
......
#!/usr/bin/env python #!/usr/bin/env python
from __future__ import print_function
# encoding: utf-8 # encoding: utf-8
#MODIFIED# #MODIFIED#
# First Start the server: $> bin/start.py # First Start the server: $> bin/start.py
import argparse import argparse
import itertools
import random import random
try: try:
import hfo import hfo
...@@ -27,9 +29,6 @@ def has_better_pos(dist_to_op, goal_angle, pass_angle, curr_goal_angle): ...@@ -27,9 +29,6 @@ def has_better_pos(dist_to_op, goal_angle, pass_angle, curr_goal_angle):
return False return False
return True return True
def can_dribble(dist_to_op):
return bool(dist_to_op > params['DRIB_DST'])
def get_action(state,hfo_env,num_teammates,rand_pass): def get_action(state,hfo_env,num_teammates,rand_pass):
"""Decides and performs the action to be taken by the agent.""" """Decides and performs the action to be taken by the agent."""
...@@ -38,10 +37,9 @@ def get_action(state,hfo_env,num_teammates,rand_pass): ...@@ -38,10 +37,9 @@ def get_action(state,hfo_env,num_teammates,rand_pass):
if can_shoot(goal_dist, goal_op_angle): if can_shoot(goal_dist, goal_op_angle):
hfo_env.act(hfo.SHOOT) hfo_env.act(hfo.SHOOT)
return return
team_list = list(range(num_teammates))
if rand_pass and (num_teammates > 1): if rand_pass and (num_teammates > 1):
team_list = random.shuffle(range(num_teammates)) random.shuffle(team_list)
else:
team_list = range(num_teammates)
for i in team_list: for i in team_list:
teammate_uniform_number=state[10 + 3*num_teammates + 3*i +2] teammate_uniform_number=state[10 + 3*num_teammates + 3*i +2]
if has_better_pos(dist_to_op = float(state[10 + num_teammates + i]), if has_better_pos(dist_to_op = float(state[10 + num_teammates + i]),
...@@ -50,14 +48,10 @@ def get_action(state,hfo_env,num_teammates,rand_pass): ...@@ -50,14 +48,10 @@ def get_action(state,hfo_env,num_teammates,rand_pass):
curr_goal_angle = goal_op_angle): curr_goal_angle = goal_op_angle):
hfo_env.act(hfo.PASS, teammate_uniform_number) hfo_env.act(hfo.PASS, teammate_uniform_number)
return return
# not sure if below check is needed - doDribble in agent.cpp includes # no check for can_dribble is needed; doDribble in agent.cpp includes
# (via doPreprocess) doForceKick, which may cover this situation depending # (via doPreprocess) doForceKick, which will cover this situation since
# on what existKickableOpponent returns. # existKickableOpponent is based on distance.
if can_dribble(dist_to_op = state[9]):
hfo_env.act(hfo.DRIBBLE) hfo_env.act(hfo.DRIBBLE)
else:
# If nothing can be done, do not do anything
hfo_env.act(hfo.NOOP)
return return
...@@ -79,8 +73,21 @@ def main(): ...@@ -79,8 +73,21 @@ def main():
hfo_env.connectToServer(hfo.HIGH_LEVEL_FEATURE_SET, hfo_env.connectToServer(hfo.HIGH_LEVEL_FEATURE_SET,
'bin/teams/base/config/formations-dt', args.port, 'bin/teams/base/config/formations-dt', args.port,
'localhost', 'base_left', False) 'localhost', 'base_left', False)
status=hfo.IN_GAME if args.seed:
while status != hfo.SERVER_DOWN: if args.rand_pass or (args.eps > 0):
print("Python randomization seed: {0:d}".format(args.seed))
else:
print("Python randomization seed setting is useless without --rand-pass or --eps >0")
if args.rand_pass and (args.numTeammates > 0):
print("Randomizing order of checking for a pass")
if args.eps > 0:
print("Using eps(ilon) {0:n}".format(args.eps))
for episode in itertools.count():
num_eps = 0
num_had_ball = 0
num_move = 0
status = hfo.IN_GAME
while status == hfo.IN_GAME:
state = hfo_env.getState() state = hfo_env.getState()
#print(state) #print(state)
if int(state[5]) == 1: # state[5] is 1 when player has the ball if int(state[5]) == 1: # state[5] is 1 when player has the ball
...@@ -89,16 +96,29 @@ def main(): ...@@ -89,16 +96,29 @@ def main():
hfo_env.act(hfo.SHOOT) hfo_env.act(hfo.SHOOT)
else: else:
hfo_env.act(hfo.DRIBBLE) hfo_env.act(hfo.DRIBBLE)
num_eps += 1
else: else:
get_action(state,hfo_env,args.numTeammates,args.rand_pass) get_action(state,hfo_env,args.numTeammates,args.rand_pass)
num_had_ball += 1
else: else:
hfo_env.act(hfo.MOVE) hfo_env.act(hfo.MOVE)
num_move += 1
status=hfo_env.step() status=hfo_env.step()
#print(status) #print(status)
# Quit if the server goes down
if status == hfo.SERVER_DOWN:
hfo_env.act(hfo.QUIT) hfo_env.act(hfo.QUIT)
exit() exit()
# Check the outcome of the episode
print("Episode {0:d} ended with {1:s}".format(episode,
hfo_env.statusToString(status)))
if args.eps > 0:
print("\tNum move: {0:d}; Random action: {1:d}; Nonrandom: {2:d}".format(num_move,
num_eps,
(num_had_ball-
num_eps)))
if __name__ == '__main__': if __name__ == '__main__':
main() main()
#!/usr/bin/env python #!/usr/bin/env python
from __future__ import print_function
# encoding: utf-8 # encoding: utf-8
# Before running this program, first Start HFO server: # Before running this program, first Start HFO server:
...@@ -30,6 +31,8 @@ def main(): ...@@ -30,6 +31,8 @@ def main():
hfo_env.connectToServer(hfo.HIGH_LEVEL_FEATURE_SET, hfo_env.connectToServer(hfo.HIGH_LEVEL_FEATURE_SET,
'bin/teams/base/config/formations-dt', args.port, 'bin/teams/base/config/formations-dt', args.port,
'localhost', 'base_left', False) 'localhost', 'base_left', False)
if args.seed:
print("Python randomization seed: {0:d}".format(args.seed))
for episode in itertools.count(): for episode in itertools.count():
status = hfo.IN_GAME status = hfo.IN_GAME
while status == hfo.IN_GAME: while status == hfo.IN_GAME:
...@@ -47,8 +50,8 @@ def main(): ...@@ -47,8 +50,8 @@ def main():
status = hfo_env.step() status = hfo_env.step()
# Check the outcome of the episode # Check the outcome of the episode
print(('Episode %d ended with %s'%(episode, print("Episode {0:d} ended with {1:s}".format(episode,
hfo_env.statusToString(status)))) hfo_env.statusToString(status)))
# Quit if the server goes down # Quit if the server goes down
if status == hfo.SERVER_DOWN: if status == hfo.SERVER_DOWN:
hfo_env.act(hfo.QUIT) hfo_env.act(hfo.QUIT)
......
#!/bin/bash
./bin/HFO --offense-agents=2 --defense-npcs=3 --offense-npcs=1 --trials 20 --headless &
sleep 5
# -x is needed to skip first line - otherwise whatever default python version is will run
python2.7 -x ./example/high_level_custom_agent.py --numTeammates=2 --numOpponents=3 --rand-pass --port 6000 &> agent1.txt &
sleep 5
python3 -x ./example/high_level_custom_agent.py --numTeammates=2 --numOpponents=3 --rand-pass --port 6000 &> agent2.txt &
# The magic line
# $$ holds the PID for this script
# Negation means kill by process group id instead of PID
trap "kill -TERM -$$" SIGINT
wait
#!/bin/bash
./bin/HFO --offense-agents=2 --defense-npcs=1 --trials 20 --headless &
sleep 5
python2.7 -x example/high_action_random_agent.py --port 6000 &> agent1.txt &
sleep 5
python3 -x example/high_action_random_agent.py --port 6000 &> agent2.txt &
# The magic line
# $$ holds the PID for this script
# Negation means kill by process group id instead of PID
trap "kill -TERM -$$" SIGINT
wait
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