Commit b7af2fe4 authored by DurgeshSamant's avatar DurgeshSamant

python3 port

parent eb26f8fc
...@@ -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])')
......
#!/usr/bin/env python
# encoding: utf-8
import subprocess, os, time, numpy, sys
# Global list of all/essential running processes
processes, necProcesses = [], []
# Command to run the rcssserver. Edit as needed.
SERVER_BIN = 'rcssserver'
# Command to run the monitor. Edit as needed.
MONITOR_BIN = 'soccerwindow2'
def launch(cmd, name = 'Unknown', necessary = True, supressOutput = True):
"""Launch a process.
Appends to list of processes and (optionally) necProcesses if
necessary flag is True.
Returns: The launched process.
"""
kwargs = {}
if supressOutput:
kwargs = {'stdout': open('/dev/null', 'w'),
'stderr': open('/dev/null', 'w')}
p = subprocess.Popen(cmd.split(' '), shell = False, **kwargs)
processes.append(p)
if necessary:
necProcesses.append([p,name])
return p
def main(args):
"""Sets up the teams, launches the server and monitor, starts the trainer.
"""
if args.logging and not os.path.exists(args.logDir):
os.makedirs(args.logDir)
num_agents = args.offenseAgents + args.defenseAgents
binary_dir = os.path.dirname(os.path.realpath(__file__))
server_port = args.port
coach_port = args.port + 1
olcoach_port = args.port + 2
serverCommand = os.path.join(binary_dir, SERVER_BIN)
serverOptions = ' server::port=%i server::coach_port=%i ' \
'server::olcoach_port=%i server::coach=1 ' \
'server::game_logging=%i server::text_logging=%i ' \
'server::hfo_logging=%i server::hfo_log_dir=%s ' \
'server::game_log_dir=%s server::text_log_dir=%s '\
'server::synch_mode=%i server::hfo=1 ' \
'server::fullstate_l=%i server::fullstate_r=%i ' \
'server::coach_w_referee=1 server::hfo_max_trial_time=%i ' \
'server::hfo_max_trials=%i server::hfo_max_frames=%i ' \
'server::hfo_offense_on_ball=%i server::random_seed=%i ' \
'server::hfo_max_untouched_time=%i ' \
'server::hfo_min_ball_pos_x=%f ' \
'server::hfo_max_ball_pos_x=%f ' \
'server::say_msg_size=%i ' \
'server::record_messages=%i' \
%(server_port, coach_port, olcoach_port,
args.logging, args.logging, args.logging,
args.logDir, args.logDir, args.logDir,
args.sync, args.fullstate, args.fullstate,
args.maxFramesPerTrial, args.numTrials, args.numFrames,
args.offenseOnBall, args.seed, args.maxUntouchedTime,
args.min_ball_x, args.max_ball_x, args.messageSize,
args.verbose)
try:
# Launch the Server
server = launch(serverCommand + serverOptions, name='server',
supressOutput=not args.verbose)
time.sleep(0.2)
assert server.poll() is None,\
'[start.py] Failed to launch Server with command: \"%s\"' \
'\n\nAnother rcssserver may be running on the same port?' \
'\nTry: \"killall -9 rcssserver\"' \
%(serverCommand + serverOptions)
if not args.headless:
monitorCommand = os.path.join(binary_dir, MONITOR_BIN)
monitorOptions = ' --connect --port=%i'%(server_port)
launch(monitorCommand + monitorOptions, name='monitor')
# Launch the Trainer
from Trainer import Trainer
trainer = Trainer(args=args, server_port=server_port, coach_port=coach_port)
trainer.initComm()
# Run
trainer.run(necProcesses, args.offenseTeam, args.defenseTeam)
except KeyboardInterrupt:
print('[start.py] Exiting for CTRL-C')
finally:
print('[start.py] Cleaning up server and other processes')
for p in reversed(processes):
try:
p.terminate()
time.sleep(0.1)
p.kill()
except:
pass
def parseArgs():
import argparse
team_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'teams')
installed_teams = os.listdir(team_dir)
p = argparse.ArgumentParser(description='Start Half Field Offense.',
formatter_class=argparse.RawTextHelpFormatter)
p.add_argument('--headless', dest='headless', action='store_true',
help='Run without a monitor.')
p.add_argument('--trials', dest='numTrials', type=int, default=-1,
help='Number of trials to run.\n'\
'Negative values mean unlimited. Default: -1.')
p.add_argument('--frames', dest='numFrames', type=int, default=-1,
help='Number of frames to run for.\n'\
'Negative values mean unlimited. Default: -1.')
p.add_argument('--frames-per-trial', dest='maxFramesPerTrial', type=int,
default=1000, help='Max number of frames per trial.\n'\
'Negative values mean unlimited. Default: 1000.')
p.add_argument('--untouched-time', dest='maxUntouchedTime', type=int,
default=100, help='Ends trial if ball is untouched for this long.\n'\
'Negative values mean unlimited. Default: 100.')
p.add_argument('--offense-agents', dest='offenseAgents', type=int, default=0,
help='Number of offensive agents. Default: 0.')
p.add_argument('--defense-agents', dest='defenseAgents', type=int, default=0,
help='Number of defensive agents. Default: 0.')
p.add_argument('--offense-npcs', dest='offenseNPCs', type=int, default=0,
help='Number of offensive uncontrolled players. Default: 0.')
p.add_argument('--defense-npcs', dest='defenseNPCs', type=int, default=0,
help='Number of defensive uncontrolled players. Default: 0.')
p.add_argument('--agent-play-goalie', dest='agentPlayGoalie', action='store_true',
default=False, help='Defense-agent plays goalie, rather than defender.')
p.add_argument('--offense-team', dest='offenseTeam', type=str, default='base',
help='Offense team binary. Options: '+str(installed_teams)+'. Default: base.')
p.add_argument('--defense-team', dest='defenseTeam', type=str, default='base',
help='Defense team binary. Options: '+str(installed_teams)+'. Default: base.')
p.add_argument('--no-sync', dest='sync', action='store_false', default=True,
help='Run server in non-sync mode.')
p.add_argument('--port', dest='port', type=int, default=6000,
help='Agent server\'s port. Default: 6000.\n'\
'rcssserver, coach, and ol_coach will be '\
'incrementally allocated the following ports.')
p.add_argument('--no-logging', dest='logging', action='store_false',
default=True, help='Disable rcssserver logging.')
p.add_argument('--log-dir', dest='logDir', default='log/',
help='Directory to store logs. Default: log/')
p.add_argument('--record', dest='record', action='store_true',
help='Record logs of states and actions.')
p.add_argument('--offense-on-ball', dest='offenseOnBall', type=int,
default=0, help='Ball given to the player represented by the value.\n'\
'If value greater than the number of offense players, '\
'ball given to a random offense player.\n'\
'If value non-positive, ball is not given to any player.\n'\
'Default: 0.')
p.add_argument('--fullstate', dest='fullstate', action='store_true',
help='Server provides full-state information to agents.')
p.add_argument('--seed', dest='seed', type=int, default=-1,
help='Seed the server\'s RNG. Default: time.')
p.add_argument('--message-size', dest='messageSize', type=int, default=1000,
help='Message size limit for communication')
p.add_argument('--ball-x-min', dest='min_ball_x', type=float, default=0.0,
help='Ball initialization min x position: [0,1]. Default: 0')
p.add_argument('--ball-x-max', dest='max_ball_x', type=float, default=0.2,
help='Ball initialization max x position: [0,1]. Default: .2')
p.add_argument('--verbose', dest='verbose', action='store_true',
default=False, help='Print verbose output.')
args = p.parse_args()
if args.offenseAgents not in range(0, 11):
p.error('argument --offense-agents: invalid choice: '\
+ str(args.offenseAgents) + ' (choose from [0-10])')
if args.offenseNPCs not in range(0, 11):
p.error('argument --offense-npcs: invalid choice: '\
+ str(args.offenseNPCs) + ' (choose from [0-10])')
if args.defenseAgents not in range(0, 12):
p.error('argument --defense-agents: invalid choice: '\
+ str(args.defenseAgents) + ' (choose from [0-11])')
if args.defenseNPCs not in range(0, 12):
p.error('argument --offense-npcs: invalid choice: '\
+ str(args.defenseNPCs) + ' (choose from [0-11])')
if args.offenseAgents + args.offenseNPCs not in range(1, 11):
p.error('Offense players (offense-agents + offense-npcs): '\
'invalid choice: ' + str(args.offenseAgents + args.offenseNPCs) +\
' (choose from [1,10])')
if args.defenseAgents + args.defenseNPCs not in range(0, 12):
p.error('Defense players (defense-agents + defense-npcs): '\
'invalid choice: ' + str(args.defenseAgents + args.defenseNPCs) +\
' (choose from [0,11])')
if args.offenseTeam not in installed_teams:
p.error('Unrecognized offense team: ' + str(args.offenseTeam))
if args.defenseTeam not in installed_teams:
p.error('Unrecognized defense team: ' + str(args.defenseTeam))
if args.agentPlayGoalie and args.defenseAgents <= 0:
p.error('You must add a --defense-agent before it can play goalie.')
return args
if __name__ == '__main__':
main(parseArgs())
...@@ -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)
......
...@@ -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)
......
...@@ -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,11 +144,11 @@ class HFOEnvironment(object): ...@@ -146,11 +144,11 @@ 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 """
return hfo_lib.statusToString(status) return hfo_lib.statusToString(status.decode('utf-8'))
def getUnum(self): def getUnum(self):
""" Return the uniform number of the agent """ """ Return the uniform number of the agent """
......
...@@ -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