start.py 3.61 KB
Newer Older
Matthew Hausknecht's avatar
Matthew Hausknecht committed
1 2 3
#!/usr/bin/env python
# encoding: utf-8

4
import subprocess, os, time, numpy, sys
5
from signal import SIGKILL
Matthew Hausknecht's avatar
Matthew Hausknecht committed
6

7 8 9 10 11 12 13
# Global list of all/essential running processes
processes, necProcesses = [], []
# Command to run the rcssserver. Edit as needed.
SERVER_CMD = 'rcssserver server::port=6000 server::coach_port=6001 \
server::olcoach_port=6002 server::coach=1 server::game_log_dir=/tmp \
server::text_log_dir=/tmp'
# Command to run the monitor. Edit as needed.
Matthew Hausknecht's avatar
Matthew Hausknecht committed
14 15
MONITOR_CMD = 'rcssmonitor'

16 17 18 19 20
def getAgentDirCmd(name, first):
  """ Returns the team name, command, and directory to run a team. """
  cmd = './start.sh -t %s' % name
  dir = os.path.dirname(os.path.realpath(__file__))
  return name, cmd, dir
Matthew Hausknecht's avatar
Matthew Hausknecht committed
21

22 23
def launch(cmd, necessary=True, supressOutput=True, name='Unknown'):
  """Launch a process.
Matthew Hausknecht's avatar
Matthew Hausknecht committed
24

25 26
  Appends to list of processes and (optionally) necProcesses if
  necessary flag is True.
Matthew Hausknecht's avatar
Matthew Hausknecht committed
27

28
  Returns: The launched process.
Matthew Hausknecht's avatar
Matthew Hausknecht committed
29

30
  """
Matthew Hausknecht's avatar
Matthew Hausknecht committed
31 32 33 34 35 36 37 38 39
  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

40
def main(args, team1='left', team2='right', rng=numpy.random.RandomState()):
41 42 43
  """Sets up the teams, launches the server and monitor, starts the
  trainer.
  """
Matthew Hausknecht's avatar
Matthew Hausknecht committed
44
  serverOptions = ''
45
  if args.sync:
Matthew Hausknecht's avatar
Matthew Hausknecht committed
46
    serverOptions += ' server::synch_mode=on'
47 48 49 50
  team1, team1Cmd, team1Dir = getAgentDirCmd(team1, True)
  team2, team2Cmd, team2Dir = getAgentDirCmd(team2, False)
  assert os.path.isdir(team1Dir)
  assert os.path.isdir(team2Dir)
Matthew Hausknecht's avatar
Matthew Hausknecht committed
51
  try:
52 53
    # Launch the Server
    launch(SERVER_CMD + serverOptions, name='server')
Matthew Hausknecht's avatar
Matthew Hausknecht committed
54
    time.sleep(0.2)
55
    if not args.headless:
Matthew Hausknecht's avatar
Matthew Hausknecht committed
56
      launch(MONITOR_CMD,name='monitor')
57
    # Launch the Trainer
Matthew Hausknecht's avatar
Matthew Hausknecht committed
58
    from Trainer import Trainer
59
    trainer = Trainer(args=args, rng=rng)
Matthew Hausknecht's avatar
Matthew Hausknecht committed
60
    trainer.initComm()
61
    # Start Team1
Matthew Hausknecht's avatar
Matthew Hausknecht committed
62 63 64
    os.chdir(team1Dir)
    launch(team1Cmd,False)
    trainer.waitOnTeam(True) # wait to make sure of team order
65
    # Start Team2
Matthew Hausknecht's avatar
Matthew Hausknecht committed
66 67 68
    os.chdir(team2Dir)
    launch(team2Cmd,False)
    trainer.waitOnTeam(False)
69
    # Make sure all players are connected
Matthew Hausknecht's avatar
Matthew Hausknecht committed
70 71
    trainer.checkIfAllPlayersConnected()
    trainer.setTeams()
72
    # Run HFO
Matthew Hausknecht's avatar
Matthew Hausknecht committed
73 74
    trainer.run(necProcesses)
  except KeyboardInterrupt:
75
    print '[start.py] Exiting for CTRL-C'
Matthew Hausknecht's avatar
Matthew Hausknecht committed
76 77 78
  finally:
    for p in processes:
      try:
79
        p.send_signal(SIGKILL)
Matthew Hausknecht's avatar
Matthew Hausknecht committed
80 81
      except:
        pass
82
      time.sleep(0.1)
Matthew Hausknecht's avatar
Matthew Hausknecht committed
83

84
def parseArgs(args=None):
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
  import argparse
  p = argparse.ArgumentParser(description='Start Half Field Offense.')
  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')
  p.add_argument('--frames', dest='numFrames', type=int, default=-1,
                 help='Number of frames to run for')
  p.add_argument('--offense', dest='numOffense', type=int, default=4,
                 help='Number of offensive players')
  p.add_argument('--defense', dest='numDefense', type=int, default=4,
                 help='Number of defensive players')
  p.add_argument('--play-defense', dest='play_offense',
                 action='store_false', default=True,
                 help='Put the learning agent on defensive team')
  p.add_argument('--no-agent', dest='no_agent', action='store_true',
                 help='Don\'t use a learning agent.')
  p.add_argument('--no-sync', dest='sync', action='store_false', default=True,
                 help='Run server in non-sync mode')
104 105 106 107
  return p.parse_args(args=args)

if __name__ == '__main__':
  main(parseArgs())