Commit 09fe6e7e authored by Matthew Hausknecht's avatar Matthew Hausknecht Committed by GitHub

Merge pull request #25 from DurgeshSamant/master

Python3-port
parents eb26f8fc 350bdb69
...@@ -11,7 +11,7 @@ addons: ...@@ -11,7 +11,7 @@ addons:
- libboost-filesystem-dev - libboost-filesystem-dev
install: install:
- if [ "${TRAVIS_OS_NAME}" = "osx" ]; then - if [ "${TRAVIS_OS_NAME}" = "osx" ]; then
brew install qt brew install cartr/qt4/qt
; ;
fi fi
os: os:
......
...@@ -9,7 +9,7 @@ Modified: 2010-11-07 ...@@ -9,7 +9,7 @@ Modified: 2010-11-07
''' '''
import socket, sys, time import socket, sys, time
import cPickle as pickle import pickle as pickle
defaultPort = 5557 defaultPort = 5557
...@@ -43,7 +43,7 @@ class Communicator(object): ...@@ -43,7 +43,7 @@ class Communicator(object):
def sendMsg(self,msg): def sendMsg(self,msg):
#print 'sending',msg #print 'sending',msg
self._sock.sendto(msg + '\0',self._addr) self._sock.sendto((msg+'\0').encode('utf-8'),self._addr)
def recvMsg(self,event=None,retryCount=None): def recvMsg(self,event=None,retryCount=None):
msg = self._storedMsg msg = self._storedMsg
...@@ -53,7 +53,7 @@ class Communicator(object): ...@@ -53,7 +53,7 @@ class Communicator(object):
newMsg = '' newMsg = ''
try: try:
newMsg,self._addr = self._sock.recvfrom(8192) newMsg,self._addr = self._sock.recvfrom(8192)
msg += newMsg msg += newMsg.decode('utf-8')
except socket.error: except socket.error:
#time.sleep(0.1) #time.sleep(0.1)
pass pass
...@@ -62,7 +62,7 @@ class Communicator(object): ...@@ -62,7 +62,7 @@ class Communicator(object):
raise TimeoutError raise TimeoutError
else: else:
retryCount -= 1 retryCount -= 1
print '[Trainer] waiting for message, retry =', retryCount print('[Trainer] waiting for message, retry =', retryCount)
time.sleep(0.3) time.sleep(0.3)
#raise ValueError('Error while receiving message') #raise ValueError('Error while receiving message')
(msg,sep,rest) = msg.partition('\0') (msg,sep,rest) = msg.partition('\0')
...@@ -88,5 +88,5 @@ class ClientCommunicator(Communicator): ...@@ -88,5 +88,5 @@ class ClientCommunicator(Communicator):
self._sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) self._sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self._sock.settimeout(5) self._sock.settimeout(5)
except: except:
print >>sys.stderr,'Error creating socket' sys.stderr.write('Error creating socket')
raise raise
...@@ -84,9 +84,9 @@ def main(args): ...@@ -84,9 +84,9 @@ def main(args):
# Run # Run
trainer.run(necProcesses, args.offenseTeam, args.defenseTeam) trainer.run(necProcesses, args.offenseTeam, args.defenseTeam)
except KeyboardInterrupt: except KeyboardInterrupt:
print '[start.py] Exiting for CTRL-C' print('[start.py] Exiting for CTRL-C')
finally: finally:
print '[start.py] Cleaning up server and other processes' print('[start.py] Cleaning up server and other processes')
for p in reversed(processes): for p in reversed(processes):
try: try:
p.terminate() p.terminate()
...@@ -160,23 +160,23 @@ def parseArgs(): ...@@ -160,23 +160,23 @@ def parseArgs():
p.add_argument('--verbose', dest='verbose', action='store_true', p.add_argument('--verbose', dest='verbose', action='store_true',
default=False, help='Print verbose output.') default=False, help='Print verbose output.')
args = p.parse_args() args = p.parse_args()
if args.offenseAgents not in xrange(0, 11): if args.offenseAgents not in list(range(0, 11)):
p.error('argument --offense-agents: invalid choice: '\ p.error('argument --offense-agents: invalid choice: '\
+ str(args.offenseAgents) + ' (choose from [0-10])') + str(args.offenseAgents) + ' (choose from [0-10])')
if args.offenseNPCs not in xrange(0, 11): if args.offenseNPCs not in list(range(0, 11)):
p.error('argument --offense-npcs: invalid choice: '\ p.error('argument --offense-npcs: invalid choice: '\
+ str(args.offenseNPCs) + ' (choose from [0-10])') + str(args.offenseNPCs) + ' (choose from [0-10])')
if args.defenseAgents not in xrange(0, 12): if args.defenseAgents not in list(range(0, 12)):
p.error('argument --defense-agents: invalid choice: '\ p.error('argument --defense-agents: invalid choice: '\
+ str(args.defenseAgents) + ' (choose from [0-11])') + str(args.defenseAgents) + ' (choose from [0-11])')
if args.defenseNPCs not in xrange(0, 12): if args.defenseNPCs not in list(range(0, 12)):
p.error('argument --offense-npcs: invalid choice: '\ p.error('argument --offense-npcs: invalid choice: '\
+ str(args.defenseNPCs) + ' (choose from [0-11])') + str(args.defenseNPCs) + ' (choose from [0-11])')
if args.offenseAgents + args.offenseNPCs not in xrange(1, 11): if args.offenseAgents + args.offenseNPCs not in list(range(1, 11)):
p.error('Offense players (offense-agents + offense-npcs): '\ p.error('Offense players (offense-agents + offense-npcs): '\
'invalid choice: ' + str(args.offenseAgents + args.offenseNPCs) +\ 'invalid choice: ' + str(args.offenseAgents + args.offenseNPCs) +\
' (choose from [1,10])') ' (choose from [1,10])')
if args.defenseAgents + args.defenseNPCs not in xrange(0, 12): if args.defenseAgents + args.defenseNPCs not in list(range(0, 12)):
p.error('Defense players (defense-agents + defense-npcs): '\ p.error('Defense players (defense-agents + defense-npcs): '\
'invalid choice: ' + str(args.defenseAgents + args.defenseNPCs) +\ 'invalid choice: ' + str(args.defenseAgents + args.defenseNPCs) +\
' (choose from [0,11])') ' (choose from [0,11])')
......
...@@ -71,7 +71,7 @@ class Agent2d(Team): ...@@ -71,7 +71,7 @@ class Agent2d(Team):
launchOpts = None launchOpts = None
if player_num == 1: if player_num == 1:
launchOpts = '-g' launchOpts = '-g'
print 'Launch npc %s-%d' % (self._name, player_num) print('Launch npc %s-%d' % (self._name, player_num))
return self.start_npc_proc(launchOpts) return self.start_npc_proc(launchOpts)
...@@ -99,5 +99,5 @@ class Helios(Team): ...@@ -99,5 +99,5 @@ class Helios(Team):
launchOpts = None launchOpts = None
if player_num == 1: if player_num == 1:
launchOpts = '-g' launchOpts = '-g'
print 'Launch npc %s-%d' % (self._name, player_num) print('Launch npc %s-%d' % (self._name, player_num))
return self.start_npc_proc(launchOpts) return self.start_npc_proc(launchOpts)
...@@ -72,11 +72,11 @@ class Trainer(object): ...@@ -72,11 +72,11 @@ class Trainer(object):
binary_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), binary_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)),
'teams', 'base') 'teams', 'base')
config_dir = os.path.join(binary_dir, 'config/formations-dt') config_dir = os.path.join(binary_dir, 'config/formations-dt')
print("Waiting for player-controlled agent %s-%d: config_dir=%s, "\ print(("Waiting for player-controlled agent %s-%d: config_dir=%s, "\
"server_port=%d, server_addr=%s, team_name=%s, play_goalie=%r" "server_port=%d, server_addr=%s, team_name=%s, play_goalie=%r"
% (self._offenseTeamName if play_offense else self._defenseTeamName, % (self._offenseTeamName if play_offense else self._defenseTeamName,
agent_num, config_dir, self._serverPort, "localhost", team_name, agent_num, config_dir, self._serverPort, "localhost", team_name,
agent_ext_num==1)) agent_ext_num==1)))
if wait_until_join: if wait_until_join:
self.waitOnPlayer(agent_ext_num, play_offense) self.waitOnPlayer(agent_ext_num, play_offense)
return None return None
...@@ -85,7 +85,7 @@ class Trainer(object): ...@@ -85,7 +85,7 @@ class Trainer(object):
""" Given a team name, returns the team object. """ """ Given a team name, returns the team object. """
teams_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'teams') teams_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'teams')
if requested_team_name == 'helios': if requested_team_name == 'helios':
print 'Creating team Helios' print('Creating team Helios')
team_name = 'HELIOS_' + ('left' if play_offense else 'right') team_name = 'HELIOS_' + ('left' if play_offense else 'right')
team_dir = os.path.join(teams_dir, 'helios', 'helios-13Eindhoven') team_dir = os.path.join(teams_dir, 'helios', 'helios-13Eindhoven')
lib_dir = os.path.join(teams_dir, 'helios', 'local', 'lib') lib_dir = os.path.join(teams_dir, 'helios', 'local', 'lib')
...@@ -93,7 +93,7 @@ class Trainer(object): ...@@ -93,7 +93,7 @@ class Trainer(object):
binaryName='helios_player', host='localhost', binaryName='helios_player', host='localhost',
port=self._serverPort) port=self._serverPort)
elif requested_team_name == 'base': elif requested_team_name == 'base':
print 'Creating team Agent2d (base)' print('Creating team Agent2d (base)')
team_name = 'base_' + ('left' if play_offense else 'right') team_name = 'base_' + ('left' if play_offense else 'right')
team_dir = os.path.join(teams_dir, 'base') team_dir = os.path.join(teams_dir, 'base')
lib_dir = None lib_dir = None
...@@ -102,7 +102,7 @@ class Trainer(object): ...@@ -102,7 +102,7 @@ class Trainer(object):
record=self._record, host='localhost', record=self._record, host='localhost',
port=self._serverPort) port=self._serverPort)
else: else:
print 'Unknown team requested: ' + requested_team_name print('Unknown team requested: ' + requested_team_name)
sys.exit(1) sys.exit(1)
def getTeams(self, offense_team_name, defense_team_name): def getTeams(self, offense_team_name, defense_team_name):
...@@ -179,8 +179,8 @@ class Trainer(object): ...@@ -179,8 +179,8 @@ class Trainer(object):
self._done = True self._done = True
if endOfTrial: if endOfTrial:
self._numTrials += 1 self._numTrials += 1
print 'EndOfTrial: %d / %d %d %s'%\ print('EndOfTrial: %d / %d %d %s'%\
(self._numGoals, self._numTrials, self._frame, event) (self._numGoals, self._numTrials, self._frame, event))
self._numFrames += self._frame - self._lastTrialStart self._numFrames += self._frame - self._lastTrialStart
self._lastTrialStart = self._frame self._lastTrialStart = self._frame
self.getConnectedPlayers() self.getConnectedPlayers()
...@@ -199,16 +199,16 @@ class Trainer(object): ...@@ -199,16 +199,16 @@ class Trainer(object):
msg = msg[1:length+1] msg = msg[1:length+1]
if msg == 'START': if msg == 'START':
if self._isPlaying: if self._isPlaying:
print 'Already playing, ignoring message' print('Already playing, ignoring message')
else: else:
self.startGame() self.startGame()
elif msg == 'DONE': elif msg == 'DONE':
raise DoneError raise DoneError
elif msg == 'ready': elif msg == 'ready':
print 'Agent Connected:', team, player print('Agent Connected:', team, player)
self._agentReady.add((team, player)) self._agentReady.add((team, player))
else: else:
print 'Unhandled message from agent: %s' % msg print('Unhandled message from agent: %s' % msg)
def initMsgHandlers(self): def initMsgHandlers(self):
""" Create handlers for different messages. """ """ Create handlers for different messages. """
...@@ -235,9 +235,9 @@ class Trainer(object): ...@@ -235,9 +235,9 @@ class Trainer(object):
""" Check that the next message is same as expected message. """ """ Check that the next message is same as expected message. """
msg = self.recv(retryCount) msg = self.recv(retryCount)
if msg != expectedMsg: if msg != expectedMsg:
print >>sys.stderr,'Error with message' sys.stderr.write('Error with message')
print >>sys.stderr,' expected: %s' % expectedMsg sys.stderr.write(' expected: ' + expectedMsg)
print >>sys.stderr,' received: %s' % msg sys.stderr.write(' received: ' + msg)
# print >>sys.stderr,len(expectedMsg),len(msg) # print >>sys.stderr,len(expectedMsg),len(msg)
raise ValueError raise ValueError
...@@ -263,7 +263,7 @@ class Trainer(object): ...@@ -263,7 +263,7 @@ class Trainer(object):
self._msgHandlers.append([args,handler]) self._msgHandlers.append([args,handler])
else: else:
if ('quiet' not in kwargs) or (not kwargs['quiet']): if ('quiet' not in kwargs) or (not kwargs['quiet']):
print 'Updating handler for %s' % (' '.join(args)) print('Updating handler for %s' % (' '.join(args)))
self._msgHandlers[i] = [args,handler] self._msgHandlers[i] = [args,handler]
def unregisterMsgHandler(self, *args): def unregisterMsgHandler(self, *args):
...@@ -285,7 +285,7 @@ class Trainer(object): ...@@ -285,7 +285,7 @@ class Trainer(object):
""" Handle a message using the registered handlers. """ """ Handle a message using the registered handlers. """
i,prefixLength,handler = self._findHandlerInd(msg) i,prefixLength,handler = self._findHandlerInd(msg)
if i < 0: if i < 0:
print 'Unhandled message:',msg[0:2] print('Unhandled message:',msg[0:2])
else: else:
handler(msg[prefixLength:]) handler(msg[prefixLength:])
...@@ -319,7 +319,7 @@ class Trainer(object): ...@@ -319,7 +319,7 @@ class Trainer(object):
def f(body): def f(body):
self._gotLook = True self._gotLook = True
del self._connectedPlayers[:] del self._connectedPlayers[:]
for i in xrange(4, len(body)): for i in range(4, len(body)):
_,team,num = body[i][0][:3] _,team,num = body[i][0][:3]
if (team, num) not in self._connectedPlayers: if (team, num) not in self._connectedPlayers:
self._connectedPlayers.append((team,num)) self._connectedPlayers.append((team,num))
...@@ -342,9 +342,9 @@ class Trainer(object): ...@@ -342,9 +342,9 @@ class Trainer(object):
def sendHFOConfig(self): def sendHFOConfig(self):
""" Broadcast the HFO configuration """ """ Broadcast the HFO configuration """
offense_nums = ' '.join([str(self.convertToExtPlayer(self._offenseTeamName, i)) offense_nums = ' '.join([str(self.convertToExtPlayer(self._offenseTeamName, i))
for i in xrange(1, self._numOffense + 1)]) for i in range(1, self._numOffense + 1)])
defense_nums = ' '.join([str(self.convertToExtPlayer(self._defenseTeamName, i)) defense_nums = ' '.join([str(self.convertToExtPlayer(self._defenseTeamName, i))
for i in xrange(self._numDefense)]) for i in range(self._numDefense)])
self.send('(say HFO_SETUP offense_name %s defense_name %s num_offense %d'\ self.send('(say HFO_SETUP offense_name %s defense_name %s num_offense %d'\
' num_defense %d offense_nums %s defense_nums %s)' ' num_defense %d offense_nums %s defense_nums %s)'
%(self._offenseTeamName, self._defenseTeamName, %(self._offenseTeamName, self._defenseTeamName,
...@@ -357,15 +357,15 @@ class Trainer(object): ...@@ -357,15 +357,15 @@ class Trainer(object):
self._isPlaying = True self._isPlaying = True
def printStats(self): def printStats(self):
print 'TotalFrames = %i, AvgFramesPerTrial = %.1f, AvgFramesPerGoal = %.1f'\ print('TotalFrames = %i, AvgFramesPerTrial = %.1f, AvgFramesPerGoal = %.1f'\
%(self._numFrames, %(self._numFrames,
self._numFrames / float(self._numTrials) if self._numTrials > 0 else float('nan'), self._numFrames / float(self._numTrials) if self._numTrials > 0 else float('nan'),
self._numGoalFrames / float(self._numGoals) if self._numGoals > 0 else float('nan')) self._numGoalFrames / float(self._numGoals) if self._numGoals > 0 else float('nan')))
print 'Trials : %i' % self._numTrials print('Trials : %i' % self._numTrials)
print 'Goals : %i' % self._numGoals print('Goals : %i' % self._numGoals)
print 'Defense Captured : %i' % self._numBallsCaptured print('Defense Captured : %i' % self._numBallsCaptured)
print 'Balls Out of Bounds: %i' % self._numBallsOOB print('Balls Out of Bounds: %i' % self._numBallsOOB)
print 'Out of Time : %i' % self._numOutOfTime print('Out of Time : %i' % self._numOutOfTime)
def checkLive(self, necProcesses): def checkLive(self, necProcesses):
"""Returns true if each of the necessary processes is still alive and """Returns true if each of the necessary processes is still alive and
...@@ -374,7 +374,7 @@ class Trainer(object): ...@@ -374,7 +374,7 @@ class Trainer(object):
""" """
for p,name in necProcesses: for p,name in necProcesses:
if p is not None and p.poll() is not None: if p is not None and p.poll() is not None:
print 'Something necessary closed (%s), exiting' % name print('Something necessary closed (%s), exiting' % name)
return False return False
return True return True
...@@ -391,7 +391,7 @@ class Trainer(object): ...@@ -391,7 +391,7 @@ class Trainer(object):
# Launch offense # Launch offense
agent_num = 0 agent_num = 0
for player_num in xrange(1, 12): for player_num in range(1, 12):
if agent_num < self._offenseAgents and player_num == sorted_offense_agent_unums[agent_num]: if agent_num < self._offenseAgents and player_num == sorted_offense_agent_unums[agent_num]:
port = self._agentServerPort + agent_num port = self._agentServerPort + agent_num
agent = self.launch_agent(agent_num, sorted_offense_agent_unums[agent_num], agent = self.launch_agent(agent_num, sorted_offense_agent_unums[agent_num],
...@@ -410,7 +410,7 @@ class Trainer(object): ...@@ -410,7 +410,7 @@ class Trainer(object):
# Launch defense # Launch defense
agent_num = 0 agent_num = 0
for player_num in xrange(1, 12): for player_num in range(1, 12):
if agent_num < self._defenseAgents and player_num == sorted_defense_agent_unums[agent_num]: if agent_num < self._defenseAgents and player_num == sorted_defense_agent_unums[agent_num]:
port = self._agentServerPort + agent_num + self._offenseAgents port = self._agentServerPort + agent_num + self._offenseAgents
agent = self.launch_agent(agent_num, sorted_defense_agent_unums[agent_num], agent = self.launch_agent(agent_num, sorted_defense_agent_unums[agent_num],
...@@ -427,23 +427,23 @@ class Trainer(object): ...@@ -427,23 +427,23 @@ class Trainer(object):
else: else:
self.disconnectPlayer(player, player_num, on_offense=False) self.disconnectPlayer(player, player_num, on_offense=False)
print 'Checking all players connected' print('Checking all players connected')
while not self.allPlayersConnected(): while not self.allPlayersConnected():
self.getConnectedPlayers() self.getConnectedPlayers()
time.sleep(0.1) time.sleep(0.1)
self.sendHFOConfig() self.sendHFOConfig()
print 'Starting game' print('Starting game')
time.sleep(0.1) time.sleep(0.1)
self.startGame() self.startGame()
while self.allPlayersConnected() and self.checkLive(necProcesses) and not self._done: while self.allPlayersConnected() and self.checkLive(necProcesses) and not self._done:
prevFrame = self._frame prevFrame = self._frame
self.listenAndProcess() self.listenAndProcess()
except TimeoutError: except TimeoutError:
print 'Haven\'t heard from the server for too long, Exiting' print('Haven\'t heard from the server for too long, Exiting')
except (KeyboardInterrupt, DoneError): except (KeyboardInterrupt, DoneError):
print 'Finished' print('Finished')
finally: finally:
try: try:
self._comm.sendMsg('(bye)') self._comm.sendMsg('(bye)')
......
...@@ -24,7 +24,7 @@ def main(): ...@@ -24,7 +24,7 @@ def main():
msg = hfo.hear() msg = hfo.hear()
# Print the incoming communication # Print the incoming communication
if msg: if msg:
print('Heard: %s'% msg) print(('Heard: %s'% msg))
# Take an action # Take an action
hfo.act(DASH, 20.0, 0.) hfo.act(DASH, 20.0, 0.)
# Create outgoing communication # Create outgoing communication
...@@ -32,7 +32,7 @@ def main(): ...@@ -32,7 +32,7 @@ def main():
# Advance the environment and get the game status # Advance the environment and get the game status
status = hfo.step() status = hfo.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 %d ended with %s'%(episode, hfo.statusToString(status))))
# Quit if the server goes down # Quit if the server goes down
if status == SERVER_DOWN: if status == SERVER_DOWN:
hfo.act(QUIT) hfo.act(QUIT)
......
...@@ -25,7 +25,7 @@ def main(): ...@@ -25,7 +25,7 @@ def main():
# Advance the environment and get the game status # Advance the environment and get the game status
status = hfo.step() status = hfo.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 %d ended with %s'%(episode, hfo.statusToString(status))))
# Quit if the server goes down # Quit if the server goes down
if status == SERVER_DOWN: if status == SERVER_DOWN:
hfo.act(QUIT) hfo.act(QUIT)
......
#!/usr/bin/env python
# encoding: utf-8
#MODIFIED#
# First Start the server: $> bin/start.py
import random, threading, argparse
import itertools
try:
from hfo import *
except:
print('Failed to import hfo. To install hfo, in the HFO directory'\
' run: \"pip install .\"')
exit()
params = {'SHT_DST':0.136664020547, 'SHT_ANG':-0.747394386098,
'PASS_ANG':0.464086704478, 'DRIB_DST':-0.999052871962}
def can_shoot(goal_dist, goal_angle):
"""Returns True if if player can have a good shot at goal"""
if goal_dist < params['SHT_DST'] and goal_angle > params['SHT_ANG']:
return True
else:
return False
def has_better_pos(dist_to_op, goal_angle, pass_angle, curr_goal_angle):
"""Returns True if teammate is in a better attacking position"""
if curr_goal_angle > goal_angle or dist_to_op<params['DRIB_DST']:
return False
if pass_angle < params['PASS_ANG']:
return False
return True
def can_dribble(dist_to_op):
if dist_to_op > params['DRIB_DST']:
return True
else:
return False
def get_action(state,hfo_env,num_teammates):
"""Returns the action to be taken by the agent"""
goal_dist = float(state[6])
goal_op_angle = float(state[8])
if can_shoot(goal_dist, goal_op_angle):
hfo_env.act(SHOOT)
return
for i in range(num_teammates):
teammate_uniform_number=state[10 + 3*num_teammates + 3*i +2]
if has_better_pos(dist_to_op = float(state[10 + num_teammates + i]),
goal_angle = float(state[10 + i]),
pass_angle = float(state[10 + 2*num_teammates + i]),
curr_goal_angle = goal_op_angle):
hfo_env.act(PASS, teammate_uniform_number)
return
if can_dribble(dist_to_op = state[9]):
hfo_env.act(DRIBBLE)
return
# If nothing can be done pass
hfo_env.act(PASS)
def main():
parser = argparse.ArgumentParser()
parser.add_argument('--port', type=int, default=6000)
parser.add_argument('--numTeammates', type=int, default=0)
parser.add_argument('--numOpponents', type=int, default=1)
args=parser.parse_args()
hfo_env = HFOEnvironment()
hfo_env.connectToServer(HIGH_LEVEL_FEATURE_SET,
'bin/teams/base/config/formations-dt', args.port,
'localhost', 'base_left', False)
#itertools.count() counts forever
for episode in itertools.count():
status=IN_GAME
count=0
while status==IN_GAME:
state = hfo_env.getState()
#print(state)
if int(state[5]) == 1: # state[5] is 1 when player has the ball
tmp = get_action(state,hfo_env,args.numTeammates)
#print(tmp)
#hfo_env.act(tmp)
else:
hfo_env.act(MOVE)
status=hfo_env.step()
#print(status)
if status == SERVER_DOWN:
hfo_env.act(QUIT)
break
if __name__ == '__main__':
main()
...@@ -28,7 +28,7 @@ def main(): ...@@ -28,7 +28,7 @@ def main():
# Advance the environment and get the game status # Advance the environment and get the game status
status = hfo.step() status = hfo.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 %d ended with %s'%(episode, hfo.statusToString(status))))
# Quit if the server goes down # Quit if the server goes down
if status == SERVER_DOWN: if status == SERVER_DOWN:
hfo.act(QUIT) hfo.act(QUIT)
......
#!/bin/bash
./bin/HFO --offense-agents=2 --defense-npcs=3 --offense-npcs=1 --trials 20 --headless &
sleep 5
/usr/bin/python2.7 ./example/high_level_custom_agent.py --numTeammates=2 --numOpponents=3 --port 6000 &> agent1.txt &
sleep 5
/usr/bin/python3 ./example/high_level_custom_agent.py --numTeammates=2 --numOpponents=3 --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
...@@ -7,7 +7,7 @@ hfo_lib = cdll.LoadLibrary(os.path.join(os.path.dirname(__file__), ...@@ -7,7 +7,7 @@ hfo_lib = cdll.LoadLibrary(os.path.join(os.path.dirname(__file__),
'libhfo_c.so')) 'libhfo_c.so'))
''' Possible feature sets ''' ''' Possible feature sets '''
LOW_LEVEL_FEATURE_SET, HIGH_LEVEL_FEATURE_SET = range(2) LOW_LEVEL_FEATURE_SET, HIGH_LEVEL_FEATURE_SET = list(range(2))
''' An enum of the possible HFO actions ''' An enum of the possible HFO actions
[Low-Level] Dash(power, relative_direction) [Low-Level] Dash(power, relative_direction)
...@@ -26,7 +26,7 @@ LOW_LEVEL_FEATURE_SET, HIGH_LEVEL_FEATURE_SET = range(2) ...@@ -26,7 +26,7 @@ LOW_LEVEL_FEATURE_SET, HIGH_LEVEL_FEATURE_SET = range(2)
NOOP(): Do Nothing NOOP(): Do Nothing
QUIT(): Quit the game ''' QUIT(): Quit the game '''
DASH, TURN, TACKLE, KICK, KICK_TO, MOVE_TO, DRIBBLE_TO, INTERCEPT, \ DASH, TURN, TACKLE, KICK, KICK_TO, MOVE_TO, DRIBBLE_TO, INTERCEPT, \
MOVE, SHOOT, PASS, DRIBBLE, CATCH, NOOP, QUIT, REDUCE_ANGLE_TO_GOAL,MARK_PLAYER,DEFEND_GOAL,GO_TO_BALL = range(19) MOVE, SHOOT, PASS, DRIBBLE, CATCH, NOOP, QUIT, REDUCE_ANGLE_TO_GOAL,MARK_PLAYER,DEFEND_GOAL,GO_TO_BALL = list(range(19))
''' Possible game status ''' Possible game status
[IN_GAME] Game is currently active [IN_GAME] Game is currently active
...@@ -36,10 +36,10 @@ DASH, TURN, TACKLE, KICK, KICK_TO, MOVE_TO, DRIBBLE_TO, INTERCEPT, \ ...@@ -36,10 +36,10 @@ DASH, TURN, TACKLE, KICK, KICK_TO, MOVE_TO, DRIBBLE_TO, INTERCEPT, \
[OUT_OF_TIME] Trial has ended due to time limit [OUT_OF_TIME] Trial has ended due to time limit
[SERVER_DOWN] Server is not alive [SERVER_DOWN] Server is not alive
''' '''
IN_GAME, GOAL, CAPTURED_BY_DEFENSE, OUT_OF_BOUNDS, OUT_OF_TIME, SERVER_DOWN = range(6) IN_GAME, GOAL, CAPTURED_BY_DEFENSE, OUT_OF_BOUNDS, OUT_OF_TIME, SERVER_DOWN = list(range(6))
''' Possible sides ''' ''' Possible sides '''
RIGHT, NEUTRAL, LEFT = range(-1,2) RIGHT, NEUTRAL, LEFT = list(range(-1,2))
class Player(Structure): pass class Player(Structure): pass
Player._fields_ = [ Player._fields_ = [
...@@ -104,9 +104,7 @@ class HFOEnvironment(object): ...@@ -104,9 +104,7 @@ class HFOEnvironment(object):
play_goalie: is this player the goalie play_goalie: is this player the goalie
record_dir: record agent's states/actions/rewards to this directory record_dir: record agent's states/actions/rewards to this directory
""" """
hfo_lib.connectToServer(self.obj, feature_set, config_dir, server_port, hfo_lib.connectToServer(self.obj, feature_set, config_dir.encode('utf-8'), server_port,server_addr.encode('utf-8'), team_name.encode('utf-8'), play_goalie, record_dir.encode('utf-8'))
server_addr, team_name, play_goalie, record_dir)
def getStateSize(self): def getStateSize(self):
""" Returns the number of state features """ """ Returns the number of state features """
return hfo_lib.getStateSize(self.obj) return hfo_lib.getStateSize(self.obj)
...@@ -130,11 +128,11 @@ class HFOEnvironment(object): ...@@ -130,11 +128,11 @@ class HFOEnvironment(object):
def say(self, message): def say(self, message):
""" Transmit a message """ """ Transmit a message """
hfo_lib.say(self.obj, message) hfo_lib.say(self.obj, message.encode('utf-8'))
def hear(self): def hear(self):
""" Returns the message heard from another player """ """ Returns the message heard from another player """
return hfo_lib.hear(self.obj) return hfo_lib.hear(self.obj).decode('utf-8')
def playerOnBall(self): def playerOnBall(self):
""" Returns a player object who last touched the ball """ """ Returns a player object who last touched the ball """
...@@ -146,7 +144,7 @@ class HFOEnvironment(object): ...@@ -146,7 +144,7 @@ class HFOEnvironment(object):
def actionToString(self, action): def actionToString(self, action):
""" Returns a string representation of an action """ """ Returns a string representation of an action """
return hfo_lib.actionToString(action) return hfo_lib.actionToString(action.decode('utf-8'))
def statusToString(self, status): def statusToString(self, status):
""" Returns a string representation of a game status """ """ Returns a string representation of a game status """
......
...@@ -3,7 +3,7 @@ import os.path, sys ...@@ -3,7 +3,7 @@ import os.path, sys
hfo_c_lib = 'hfo/libhfo_c.so' hfo_c_lib = 'hfo/libhfo_c.so'
if not os.path.isfile(hfo_c_lib): if not os.path.isfile(hfo_c_lib):
print('ERROR: Unable to find required library: %s.'%(hfo_c_lib)) print(('ERROR: Unable to find required library: %s.'%(hfo_c_lib)))
sys.exit() sys.exit()
module1 = Extension('hfo.hfo_c_wrapper', module1 = Extension('hfo.hfo_c_wrapper',
......
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