def readCommand( argv ): """ Processes the command used to run from the command line. """ import getopt import gui # Set default options options = {'layout': 'small', 'numships': 1, 'sensortype': 'deterministic', 'player': 'human', 'inference': 'exact', 'zoom' : None, 'noise' : 0.0, 'showships': False, 'showbeliefs': True} args = {} # This dictionary will hold the objects used by the game # Read input from the command line commands = ['help', 'layout=', 'ships=', 'sensortype=', 'player', 'inference', 'zoom=', 'showships', 'hidebeliefs' ] try: opts = getopt.getopt( argv, "hl:k:s:p:i:z:n:m:wq", commands ) except getopt.GetoptError: print USAGE_STRING sys.exit( 2 ) for option, value in opts[0]: if option in ['--help', '-h']: print USAGE_STRING sys.exit( 0 ) if option in ['--player', '-p']: options['player'] = value if option in ['--inference', '-i']: options['inference'] = value if option in ['--layout', '-l']: options['layout'] = value if option in ['--ships', '-k']: options['numships'] = int( value ) if option in ['--sensortype', '-s']: options['sensortype'] = value if option in ['--zoom', '-z']: options['zoom'] = float( value ) if option in ['--showships', '-w']: options['showships'] = True if option in ['--hidebeliefs', '-q']: options['showbeliefs'] = False # numships args['numships'] = options['numships'] # Choose a layout boardSizes = { 'test': (3,3), 'small': (4,6), 'medium': (6,10), 'large': (10,16) } args['layout'] = Layout( boardSizes[ options['layout'] ] ) # auto-scaling the gui numSquares = args['layout'].cols if not options['zoom']: options['zoom'] = 1 - (numSquares / 32.0) # scale with zoom gui.scaleGridSize(options['zoom']) # sensor distribution import sensorDistributions if options['sensortype'] == 'deterministic': args['sensors'] = Sensors(sensorDistributions.deterministicSensorReadingDistribution) elif options['sensortype'] == 'noisy': args['sensors'] = Sensors(sensorDistributions.noisySensorReadingDistribution) import battleshipAgent agentBuilder = None if options['player'] == 'human': agentBuilder = lambda game: battleshipAgent.StaticKeyboardAgent(battleshipAgent.ExactStaticInferenceModule(game), game) if options['player'] == 'vpi': agentBuilder = lambda game: battleshipAgent.StaticVPIAgent(battleshipAgent.ExactStaticInferenceModule(game), game) if agentBuilder == None: raise 'Agent not specd correctly!' args['agent'] = agentBuilder # show ships, beliefs if options['showships']: gui.SHOW_SHIPS = True if not options['showbeliefs']: gui.SHOW_BELIEFS = False return args
def readCommand(argv): """ Processes the command used to run from the command line. """ import getopt import gui # Set default options options = { 'layout': 'small', 'numships': 1, 'sensortype': 'deterministic', 'player': 'human', 'inference': 'exact', 'zoom': None, 'noise': 0.0, 'motion': 'basic', 'samples': 10000, 'showships': False, 'showbeliefs': True } args = {} # This dictionary will hold the objects used by the game # Read input from the command line commands = [ 'help', 'layout=', 'ships=', 'sensortype=', 'player', 'inference', 'zoom=', 'motionnoise=', 'motiontype=', 'samples=', 'showships', 'hidebeliefs' ] try: opts = getopt.getopt(argv, "hl:k:s:p:i:z:n:m:r:wq", commands) except getopt.GetoptError: print USAGE_STRING sys.exit(2) for option, value in opts[0]: if option in ['--help', '-h']: print USAGE_STRING sys.exit(0) if option in ['--player', '-p']: options['player'] = value if option in ['--inference', '-i']: options['inference'] = value if option in ['--layout', '-l']: options['layout'] = value if option in ['--ships', '-k']: options['numships'] = int(value) if option in ['--sensortype', '-s']: options['sensortype'] = value if option in ['--zoom', '-z']: options['zoom'] = float(value) if option in ['--motionnoise', '-n']: options['noise'] = float(value) if option in ['--motiontype', '-m']: options['motion'] = value if option in ['--samples', '-r']: options['samples'] = int(value) if option in ['--showships', '-w']: options['showships'] = True if option in ['--hidebeliefs', '-q']: options['showbeliefs'] = False # numships args['numships'] = options['numships'] # Choose a layout boardSizes = { 'test': (3, 3), 'small': (4, 6), 'medium': (6, 10), 'large': (10, 16) } args['layout'] = Layout(boardSizes[options['layout']]) # auto-scaling the gui numSquares = args['layout'].cols if not options['zoom']: options['zoom'] = 1 - (numSquares / 32.0) # scale with zoom gui.scaleGridSize(options['zoom']) # sensor distribution import sensorDistributions if options['sensortype'] == 'deterministic': args['sensors'] = Sensors( sensorDistributions.deterministicSensorReadingDistribution) elif options['sensortype'] == 'noisy': args['sensors'] = Sensors( sensorDistributions.noisySensorReadingDistribution) # time args['motion'] = None timed = False if options['motion'] != 'basic' or options['noise'] > 0: timed = True gui.USE_TIME = True args['motion'] = Motion(args['layout'], options['motion'], options['noise']) import battleshipAgent agentBuilder = None if not timed: if options['player'] == 'human': agentBuilder = lambda game: battleshipAgent.StaticKeyboardAgent( battleshipAgent.ExactStaticInferenceModule(game), game) if options['player'] == 'vpi': agentBuilder = lambda game: battleshipAgent.StaticVPIAgent( battleshipAgent.ExactStaticInferenceModule(game), game) else: if options['inference'] == 'exact': agentBuilder = lambda game: battleshipAgent.DynamicKeyboardAgent( battleshipAgent.ExactDynamicInferenceModule(game), game) if options['inference'] == 'approximate': agentBuilder = lambda game: battleshipAgent.DynamicKeyboardAgent( battleshipAgent.ApproximateDynamicInferenceModule( game, options['samples']), game) if agentBuilder == None: raise 'Agent not spec\'d correctly!' args['agent'] = agentBuilder # show ships, beliefs if options['showships']: gui.SHOW_SHIPS = True if not options['showbeliefs']: gui.SHOW_BELIEFS = False return args
def readCommand( argv ): """ Processes the command used to run from the command line. """ import getopt import gui # Set default options options = {'prior': 'uniform', 'fixrandomseed': False, 'layout': 'small', 'numghosts': 1, 'sensortype': 'deterministic', 'player': 'human', 'inference': 'exact', 'zoom' : None, 'noise' : 0.0, 'motion': 'basic', 'samples': 10000, 'showghosts': False, 'showbeliefs': True} args = {} # This dictionary will hold the objects used by the game # Read input from the command line commands = ['help', 'prior=', 'fixrandomseed', 'layout=', 'ghosts=', 'sensortype=', 'player=', 'inference=', 'zoom=', 'motionnoise=', 'motiontype=', 'samples=', 'showghosts', 'hidebeliefs'] try: opts = getopt.getopt( argv, "hl:k:s:p:i:z:n:m:r:wq", commands ) except getopt.GetoptError: print USAGE_STRING sys.exit( 2 ) for option, value in opts[0]: if option in ['--help', '-h']: print USAGE_STRING sys.exit( 0 ) if option in ['--player', '-p']: options['player'] = value if option in ['--inference', '-i']: options['inference'] = value if option in ['--layout', '-l']: options['layout'] = value if option in ['--ghosts', '-k']: options['numghosts'] = int( value ) if option in ['--sensortype', '-s']: options['sensortype'] = value if option in ['--zoom', '-z']: options['zoom'] = float( value ) if option in ['--motionnoise', '-n']: options['noise'] = float( value ) if option in ['--motiontype', '-m']: options['motion'] = value if option in ['--samples', '-r']: options['samples'] = int( value ) if option in ['--showghosts', '-w']: options['showghosts'] = True if option in ['--hidebeliefs', '-q']: options['showbeliefs'] = False if option in ['--prior']: options['prior'] = value if option in ['--fixrandomseed']: options['fixrandomseed'] = True # numghosts args['numghosts'] = options['numghosts'] # Choose a layout boardSizes = { 'tiny': (2,1), 'test': (3,3), 'small': (4,6), 'medium': (6,10), 'large': (10,16) } args['layout'] = Layout( boardSizes[ options['layout'] ] ) # auto-scaling the gui numSquares = args['layout'].cols if not options['zoom']: options['zoom'] = 1 - (numSquares / 32.0) # scale with zoom gui.scaleGridSize(options['zoom']) # sensor distribution import sensorDistributions if options['sensortype'] == 'deterministic': args['sensors'] = Sensors(sensorDistributions.deterministicSensorReadingDistribution) elif options['sensortype'] == 'noisy': args['sensors'] = Sensors(sensorDistributions.noisySensorReadingDistribution) elif options['sensortype'] == 'simple': args['sensors'] = Sensors(sensorDistributions.simpleSensorReadingDistribution) else: raise Exception('Sensor type %s is unknown', options['sensortype']) # time args['motion'] = None timed = False if options['motion'] != 'basic' or options['noise'] > 0: timed = True gui.USE_TIME = True args['motion'] = Motion(args['layout'], options['motion'], options['noise']) import ghostbusterAgent agentBuilder = None if not timed: if options['player'] == 'human': agentBuilder = lambda game: ghostbusterAgent.StaticKeyboardAgent(staticInferenceModule.ExactStaticInferenceModule(game), game) if options['player'] == 'vpi': agentBuilder = lambda game: ghostbusterAgent.StaticVPIAgent(staticInferenceModule.ExactStaticInferenceModule(game), game) else: if options['inference'] == 'exact': agentBuilder = lambda game: ghostbusterAgent.DynamicKeyboardAgent(dynamicInferenceModule.ExactDynamicInferenceModule(game), game) if options['inference'] == 'approximate': agentBuilder = lambda game: ghostbusterAgent.DynamicKeyboardAgent(dynamicInferenceModule.ApproximateDynamicInferenceModule(game, options['samples']), game) if agentBuilder == None: raise 'Agent not spec\'d correctly!' args['agent'] = agentBuilder # show ghosts, beliefs if options['showghosts']: gui.SHOW_GHOSTS = True if not options['showbeliefs']: gui.SHOW_BELIEFS = False # prior args['prior'] = options['prior'] == 'uniform' # random seed if options['fixrandomseed']: random.seed(1) return args