예제 #1
0
def runGames(layout,
             pacman,
             ghosts,
             display,
             numGames,
             record,
             numTraining=0,
             catchExceptions=False,
             timeout=30):
    import __main__
    __main__.__dict__['_display'] = display

    rules = ClassicGameRules(timeout)
    games = []
    traininggames = []

    for i in range(numGames):
        beQuiet = i < numTraining
        if beQuiet:
            # Suppress output and graphics
            import textDisplay
            gameDisplay = textDisplay.NullGraphics()
            rules.quiet = True
        else:
            gameDisplay = display
            rules.quiet = False
        game = rules.newGame(layout, pacman, ghosts, gameDisplay, beQuiet,
                             catchExceptions)
        game.run()
        if not beQuiet:
            games.append(game)
        else:
            traininggames.append(game)

        if record:
            import time, cPickle
            fname = ('recorded-game-%d' % (i + 1)) + '-'.join(
                [str(t) for t in time.localtime()[1:6]])
            f = file(fname, 'w')
            components = {'layout': layout, 'actions': game.moveHistory}
            cPickle.dump(components, f)
            f.close()

    if (numGames - numTraining) > 0:
        scores = [game.state.getScore() for game in games]
        wins = [game.state.isWin() for game in games]
        winRate = wins.count(True) / float(len(wins))
        print 'Average Score:', sum(scores) / float(len(scores))
        print 'number of scores', float(len(scores))
        print 'number of games', float(len(games))
        print 'Scores:       ', ', '.join([str(score) for score in scores])
        print 'Win Rate:      %d/%d (%.2f)' % (wins.count(True), len(wins),
                                               winRate)
        print 'Record:       ', ', '.join([['Loss', 'Win'][int(w)]
                                           for w in wins])

    #training scores and saving them

    trscores = [game.state.getScore() for game in traininggames]
    trepisodes = len(trscores)
    print "\n *****TRAINING STATS******* \n "
    print " no of episodes ", trepisodes
    nptrscore = np.asarray(trscores)
    nptrepsidsdes = np.asarray([t for t in range(0, trepisodes)])
    #print " episodes", nptrepsidsdes,"score",nptrscore
    comb = np.stack((nptrepsidsdes, nptrscore), axis=1)
    #print " final training data \n",comb
    import time
    foutname = ('/home/starshipcrew/results/multiadvice/' + 'trdata-' +
                '-'.join([str(t) for t in time.localtime()[1:6]]) + '.csv')
    np.savetxt(foutname, comb, delimiter=',')
    print "training data saved"
    #
    return games
예제 #2
0
def runGames(layouts,
             agents,
             display,
             length,
             numGames,
             record,
             numTraining,
             redTeamName,
             blueTeamName,
             muteAgents=False,
             catchExceptions=False):

    rules = CaptureRules()
    games = []

    if numTraining > 0:
        print 'Playing %d training games' % numTraining

    for i in range(numGames):
        beQuiet = i < numTraining
        layout = layouts[i]
        if beQuiet:
            # Suppress output and graphics
            import textDisplay
            gameDisplay = textDisplay.NullGraphics()
            rules.quiet = True
        else:
            gameDisplay = display
            rules.quiet = False
        g = rules.newGame(layout, agents, gameDisplay, length, muteAgents,
                          catchExceptions)
        g.run()
        if not beQuiet: games.append(g)

        g.record = None
        if record:
            import time, cPickle, game
            #fname = ('recorded-game-%d' % (i + 1)) +  '-'.join([str(t) for t in time.localtime()[1:6]])
            #f = file(fname, 'w')
            components = {
                'layout': layout,
                'agents': [game.Agent() for a in agents],
                'actions': g.moveHistory,
                'length': length,
                'redTeamName': redTeamName,
                'blueTeamName': blueTeamName
            }
            #f.close()
            print "recorded"
            g.record = cPickle.dumps(components)
            with open('replay-%d' % i, 'wb') as f:
                f.write(g.record)

    if numGames > 1:
        scores = [game.state.data.score for game in games]
        redWinRate = [s > 0 for s in scores].count(True) / float(len(scores))
        blueWinRate = [s < 0 for s in scores].count(True) / float(len(scores))
        print 'Average Score:', sum(scores) / float(len(scores))
        print 'Scores:       ', ', '.join([str(score) for score in scores])
        print 'Red Win Rate:  %d/%d (%.2f)' % ([s > 0
                                                for s in scores].count(True),
                                               len(scores), redWinRate)
        print 'Blue Win Rate: %d/%d (%.2f)' % ([s < 0
                                                for s in scores].count(True),
                                               len(scores), blueWinRate)
        print 'Record:       ', ', '.join([
            ('Blue', 'Tie', 'Red')[max(0, min(2, 1 + s))] for s in scores
        ])
    return games
예제 #3
0
def runGames(layout,
             pacman,
             ghosts,
             display,
             numGames,
             record,
             numTraining=0,
             catchExceptions=False,
             timeout=30):
    import __main__
    __main__.__dict__['_display'] = display

    rules = ClassicGameRules(timeout)
    games = []
    #scores = []
    #wins = []
    #gc.set_debug(gc.DEBUG_UNCOLLECTABLE | gc.DEBUG_STATS | gc.DEBUG_OBJECTS)  # debugging for memory leaks

    import time
    start = time.time()
    for i in range(numGames):
        beQuiet = i < numTraining
        if beQuiet:
            # Suppress output and graphics
            import textDisplay
            gameDisplay = textDisplay.NullGraphics(
            )  # uncomment to not show display during training
            # gameDisplay = display   # comment to not show display during training
            rules.quiet = True
        else:
            gameDisplay = display
            rules.quiet = False
        game = rules.newGame(layout, pacman, ghosts, gameDisplay, beQuiet,
                             catchExceptions)
        game.run()
        if not beQuiet:
            games.append(game)
            #scores.append(game.state.getScore())
            #wins.append(game.state.isWin())

        if record:
            import time, cPickle
            fname = ('recorded-game-%d' % (i + 1)) + '-'.join(
                [str(t) for t in time.localtime()[1:6]])
            f = file(fname, 'w')
            components = {'layout': layout, 'actions': game.moveHistory}
            cPickle.dump(components, f)
            f.close()
        gc.collect()  #por si escaso
        del gc.garbage[:]  # por si escaso

    if (numGames - numTraining) > 0:
        end_training = time.time()
        print "execution time: ", end_training - start
        scores = [game.state.getScore() for game in games]
        wins = [game.state.isWin() for game in games]
        winRate = wins.count(True) / float(len(wins))
        print 'Average Score:', sum(scores) / float(len(scores))
        print 'Scores:       ', ', '.join([str(score) for score in scores])
        print 'Win Rate:      %d/%d (%.2f)' % (wins.count(True), len(wins),
                                               winRate)
        print 'Record:       ', ', '.join([['Loss', 'Win'][int(w)]
                                           for w in wins])
예제 #4
0
def readCommand(argv):
    """
  Processes the command used to run pacman from the command line.
  """
    from optparse import OptionParser
    usageStr = """
  USAGE:      python pacclient.py <options>
  EXAMPLES:   (1) python pacclient.py
                  - starts an interactive game with a random teammate.
  EXAMPLES:   (2) python pacclient.py -2 OffenseAgent
                  - starts an interactive game with a random teammate.
  """
    parser = OptionParser(usageStr)

    parser.add_option('-a',
                      '--agents',
                      help=default('Your team'),
                      default='BaselineAgents')
    parser.add_option('--agentOpts',
                      help=default('Arguments passed to the factory'),
                      default='')
    parser.add_option('-t',
                      '--textgraphics',
                      action='store_true',
                      help='Display output as text only',
                      default=False)
    parser.add_option('-q',
                      '--quiet',
                      action='store_true',
                      help='Display minimal output',
                      default=False)
    # parser.add_option('-G', '--pygame', action='store_true', dest='pygame',
    #                   help='Display output with Pygame graphics (faster)', default=False)
    parser.add_option('-z',
                      '--zoom',
                      type='float',
                      help=default('Zoom in the graphics'),
                      default=1)
    parser.add_option('-s',
                      '--server',
                      help=default('The SERVER to connect to'),
                      default='robots.stanford.edu')
    parser.add_option('-p',
                      '--port',
                      type='int',
                      help=default('The PORT to connect to'),
                      default=7226)
    parser.add_option('-U',
                      '--user',
                      help=default('Your username'),
                      default='guest')
    parser.add_option('-P',
                      '--password',
                      help=default('Your password'),
                      default='guest')
    parser.add_option('-g',
                      '--gamename',
                      help=default('The name of the game you wish to contact'),
                      default='')
    parser.add_option(
        '-f',
        '--fixRandomSeed',
        action='store_true',
        help='Fixes the random seed to always play the same game',
        default=False)

    options, otherjunk = parser.parse_args()
    if len(otherjunk) != 0: raise Exception("Illegal args: " + otherjunk)
    args = dict()

    agentArgs = parseAgentArgs(options.agentOpts)
    args['agentFactoryBuilder'] = loadAgentAccessor(options.agents, agentArgs)

    # Choose a display format
    #if options.pygame:
    # import pygameDisplay
    #  args['display'] = pygameDisplay.PacmanGraphics()
    if options.quiet:
        import textDisplay
        args['display'] = textDisplay.NullGraphics()
    elif options.textgraphics:
        import textDisplay
        args['display'] = textDisplay.PacmanGraphics()
    else:
        import graphicsDisplay
        args['display'] = graphicsDisplay.PacmanGraphics(options.zoom, 0, True)

    if options.fixRandomSeed: random.seed('cs188')

    args['server'] = options.server
    args['port'] = options.port
    args['user'] = options.user
    args['password'] = options.password
    args['gamename'] = options.gamename

    return args
예제 #5
0
def readCommand( argv ):
  """
  Processes the command used to run pacman from the command line.
  """
  from optparse import OptionParser
  usageStr = """
  USAGE:      python pacman.py <options>
  EXAMPLES:   (1) python capture.py
                  - starts a game with two baseline agents
              (2) python capture.py --keys0
                  - starts a two-player interactive game where the arrow keys control agent 0, and all other agents are baseline agents
              (3) python capture.py -p baselineTeam -b myTeam
                  - starts a fully automated game where the pacman team is a baseline team and ghost team is myTeam
  """
  # TODO: Update above according to final defaults
  parser = OptionParser(usageStr)
  parser.add_option('-p', '--pacman', help=default('Pacman team'),
                    default='phase2Team') # TODO: Think about if we should leave this default
  parser.add_option('-g', '--ghost', help=default('Ghost team'),
                    default='None')
  parser.add_option('--pacman-name', help=default('Pacman team name'),
                    default='Pacman')
  parser.add_option('--ghost-name', help=default('Ghost team name'),
                    default='Ghost')
  parser.add_option('--keys0', help='Make agent 0 (first pacman player) a keyboard agent', action='store_true',default=False)
  parser.add_option('--keys1', help='Make agent 1 (second pacman player) a keyboard agent', action='store_true',default=False)
  parser.add_option('--keys2', help='Make agent 2 (first ghost player) a keyboard agent', action='store_true',default=False)
  parser.add_option('--keys3', help='Make agent 3 (second ghost player) a keyboard agent', action='store_true',default=False)
  parser.add_option('-l', '--layout', dest='layout',
                    help=default('the LAYOUT_FILE from which to load the map layout; use RANDOM for a random maze; use RANDOM<seed> to use a specified random seed, e.g., RANDOM23'),
                    metavar='LAYOUT_FILE', default='defaultCapture')
  parser.add_option('-t', '--textgraphics', action='store_true', dest='textgraphics',
                    help='Display output as text only', default=False)

  parser.add_option('-q', '--quiet', action='store_true',
                    help='Display minimal output and no graphics', default=False)

  parser.add_option('-Q', '--super-quiet', action='store_true', dest="super_quiet",
                    help='Same as -q but agent output is also suppressed', default=False)

  parser.add_option('-z', '--zoom', type='float', dest='zoom',
                    help=default('Zoom in the graphics'), default=1)
  parser.add_option('-i', '--time', type='int', dest='time',
                    help=default('TIME limit of a game in moves'), default=1200, metavar='TIME')
  parser.add_option('-n', '--numGames', type='int',
                    help=default('Number of games to play'), default=10)
  parser.add_option('-f', '--fixRandomSeed', action='store_true',
                    help='Fixes the random seed to always play the same game', default=False)
  parser.add_option('--record', action='store_true',
                    help='Writes game histories to a file (named by the time they were played)', default=False)
  parser.add_option('--replay', default=None,
                    help='Replays a recorded game file.')
  # TODO: This currently doesn't work, consider removing or fixing
  parser.add_option('-x', '--numTraining', dest='numTraining', type='int',
                    help=default('How many episodes are training (suppresses output)'), default=0)
  parser.add_option('-c', '--catchExceptions', action='store_true', default=True,
                    help='Catch exceptions and enforce time limits')

  options, otherjunk = parser.parse_args(argv)
  assert len(otherjunk) == 0, "Unrecognized options: " + str(otherjunk)
  args = dict()

  # Choose a display format
  if options.textgraphics:
    import textDisplay
    args['display'] = textDisplay.PacmanGraphics()
  elif options.quiet:
    import textDisplay
    args['display'] = textDisplay.NullGraphics()
  elif options.super_quiet:
    import textDisplay
    args['display'] = textDisplay.NullGraphics()
    args['muteAgents'] = True
  else:
    import captureGraphicsDisplay
    # Hack for agents writing to the display
    captureGraphicsDisplay.FRAME_TIME = 0
    args['display'] = captureGraphicsDisplay.PacmanGraphics(options.pacman, options.ghost, options.zoom, 0, capture=True)
    import __main__
    __main__.__dict__['_display'] = args['display']


  args['pacmanTeamName'] = options.pacman_name
  args['ghostTeamName'] = options.ghost_name

  if options.fixRandomSeed: random.seed('cs188')

  # Special case: recorded games don't use the runGames method or args structure
  if options.replay != None:
    print 'Replaying recorded game %s.' % options.replay
    import cPickle
    recorded = cPickle.load(open(options.replay))
    recorded['display'] = args['display']
    replayGame(**recorded)
    sys.exit(0)

  # Choose a pacman agent
  nokeyboard = options.textgraphics or options.quiet
  pacmanAgents = loadAgents(True, 'phase2Team', nokeyboard, {})
  ghostAgents = loadAgents(False, 'None', nokeyboard, {})

  # Assume 2 agents on the pacman side, and
  # variable amount (0-2) on the ghost side
  args['agents'] = pacmanAgents + ghostAgents

  numKeyboardAgents = 0
  for index, val in enumerate([options.keys0, options.keys1, options.keys2, options.keys3]):
    if not val: continue
    if numKeyboardAgents == 0:
      agent = keyboardAgents.KeyboardAgent(index)
    elif numKeyboardAgents == 1:
      agent = keyboardAgents.KeyboardAgent2(index)
    else:
      raise Exception('Max of two keyboard agents supported')
    numKeyboardAgents += 1
    args['agents'][index] = agent

  # Generate the layouts
  args['layouts'] = generateLayouts(LAYOUT_SEED, ghostAgents)




  args['length'] = options.time
  args['numGames'] = options.numGames
  args['numTraining'] = options.numTraining
  args['record'] = options.record
  args['catchExceptions'] = options.catchExceptions
  return args
예제 #6
0
파일: pacman.py 프로젝트: wxin4/Multiagent
def runGames(layout,
             pacman,
             ghosts,
             display,
             numGames,
             record,
             numTraining=0,
             catchExceptions=False,
             timeout=30):
    import __main__
    __main__.__dict__['_display'] = display

    rules = ClassicGameRules(timeout)
    games = []

    for i in range(numGames):
        import time
        gametimestart = time.time()
        # print  "---------------------Game start ------------------"
        beQuiet = i < numTraining
        if beQuiet:
            # Suppress output and graphics
            import textDisplay
            gameDisplay = textDisplay.NullGraphics()
            rules.quiet = True
        else:
            gameDisplay = display
            rules.quiet = False
        game = rules.newGame(layout, pacman, ghosts, gameDisplay, beQuiet,
                             catchExceptions)
        game.run()
        if not beQuiet: games.append(game)

        gamendtime = time.time()
        # print gamendtime- gametimestart, "---------------------Game end ------------------"

        if record:
            import time, cPickle
            fname = ('recorded-game-%d' % (i + 1)) + '-'.join(
                [str(t) for t in time.localtime()[1:6]])
            f = file(fname, 'w')
            components = {'layout': layout, 'actions': game.moveHistory}
            cPickle.dump(components, f)
            f.close()

    if (numGames - numTraining) > 0:
        scores = [game.state.getScore() for game in games]
        wins = [game.state.isWin() for game in games]
        winRate = wins.count(True) / float(len(wins))
        print 'Average Score:', sum(scores) / float(len(scores))
        print 'Scores:       ', ', '.join([str(score) for score in scores])
        print 'Win Rate:      %d/%d (%.2f)' % (wins.count(True), len(wins),
                                               winRate)
        print 'Record:       ', ', '.join([['Loss', 'Win'][int(w)]
                                           for w in wins])

        # return games
        # -------------------------------------------------------
        avgscore = sum(scores) / float(len(scores))
        # depthlevel =
        numofwins = wins.count(True)

    return games, avgscore, numofwins
예제 #7
0
def runGames(layoutName,
             layout,
             pacman,
             ghosts,
             display,
             numGames,
             record,
             pacmanAgent,
             numTraining=0,
             catchExceptions=False,
             timeout=30):
    import __main__
    __main__.__dict__['_display'] = display

    rules = ClassicGameRules(timeout)
    games = []

    for i in range(numGames):
        beQuiet = i < numTraining
        if beQuiet:
            # Suppress output and graphics
            import textDisplay
            gameDisplay = textDisplay.NullGraphics()
            rules.quiet = True
        else:
            gameDisplay = display
            rules.quiet = False
        game = rules.newGame(layout, pacman, ghosts, gameDisplay, beQuiet,
                             catchExceptions)
        game.run()
        if not beQuiet: games.append(game)

        if record:
            import time, pickle
            fname = ('recorded-game-%d' % (i + 1)) + '-'.join(
                [str(t) for t in time.localtime()[1:6]])
            f = file(fname, 'w')
            components = {'layout': layout, 'actions': game.moveHistory}
            pickle.dump(components, f)
            f.close()

    if (numGames - numTraining) > 0:
        scores = [game.state.getScore() for game in games]
        wins = [game.state.isWin() for game in games]
        winRate = wins.count(True) / float(len(wins))
        avgScore = sum(scores) / float(len(scores))
        print('-------------------------------------------')
        print('Average Score:', avgScore)
        print('Scores:       ', ', '.join([str(score) for score in scores]))
        print('Win Rate:      %d/%d (%.2f)' %
              (wins.count(True), len(wins), winRate))
        print('Record:       ',
              ', '.join([['Loss', 'Win'][int(w)] for w in wins]))
        print('-------------------------------------------')

        if numTraining == 50 and numGames == 60 and pacmanAgent == 'ApproximateQAgent' and layoutName == 'mediumClassic':
            while (True):
                inputVar = input("Do you want submit your average score?")
                if inputVar == 'y':
                    submitScore(avgScore, game.state.univ_id,
                                game.state.password)
                    break
                elif inputVar == 'n':
                    break
        else:
            print(
                'Possible to submit only at numTraining=50, numGames=60, agent=ApproximateQAgent, and layout=mediumClassic'
            )

    return games
예제 #8
0
def runGames(layout,
             pacman,
             ghosts,
             display,
             numGames,
             numEpochs,
             agentPolicy,
             alpha,
             gama,
             result,
             new,
             record,
             numTraining=0,
             catchExceptions=False,
             timeout=30):
    import __main__
    __main__.__dict__['_display'] = display

    rules = ClassicGameRules(timeout)
    games = []

    if pacman.__class__.__name__ == 'QLearningAgent':
        file_name = "weights.pac"
        pacman.setAlphaAndGama(alpha, gama)
        pacman.runAgentPolicy(agentPolicy)
        pacman.initalize()

        if result == True:
            pacman.setAgentPolicy(False)
            pacman.setAlphaAndGama(0, gama)

        if new and os.path.isfile("./" + file_name):
            os.remove(file_name)

    if agentPolicy == 'False':
        pacman.setAgentPolicy(False)

    for x in range(numEpochs):
        for i in range(numGames):
            beQuiet = i < numTraining
            if beQuiet:
                # Suppress output and graphics
                import textDisplay
                gameDisplay = textDisplay.NullGraphics()
                rules.quiet = True
            else:
                gameDisplay = display
                rules.quiet = False

            #QLearning important things
            if pacman.__class__.__name__ == 'QLearningAgent':
                feature_weights = []

                try:
                    file = open(file_name, 'rb')

                    for w in range(pacman.getFeaturesCount() *
                                   pacman.getActionsCount()):
                        weight = pickle.load(file)
                        feature_weights.append(weight)

                    file.close()
                    os.remove(file_name)

                except (OSError, IOError) as e:
                    #initialize file if it doesn't exists
                    for w in range(pacman.getFeaturesCount() *
                                   pacman.getActionsCount()):
                        weight = 0
                        feature_weights.append(weight)

                pacman.initializeWeights(feature_weights)

            game = rules.newGame(layout, pacman, ghosts, gameDisplay,
                                 agentPolicy, beQuiet, catchExceptions)
            game.run()
            if not beQuiet: games.append(game)

            if pacman.__class__.__name__ == 'QLearningAgent':
                file = open(file_name, "wb")

                for w in pacman.getWeights():
                    pickle.dump(w, file)

                file.close()

            if record:
                import time, cPickle
                fname = ('recorded-game-%d' % (i + 1)) + '-'.join(
                    [str(t) for t in time.localtime()[1:6]])
                f = file(fname, 'w')
                components = {'layout': layout, 'actions': game.moveHistory}
                cPickle.dump(components, f)
                f.close()

        if (numGames - numTraining) > 0:
            scores = [game.state.getScore() for game in games]
            wins = [game.state.isWin() for game in games]
            winRate = wins.count(True) / float(len(wins))
            print 'Average Score:', sum(scores) / float(len(scores))
            print 'Scores:       ', ', '.join([str(score) for score in scores])
            print 'Win Rate:      %d/%d (%.2f)' % (wins.count(True), len(wins),
                                                   winRate)
            print 'Record:       ', ', '.join([['Loss', 'Win'][int(w)]
                                               for w in wins])

    return games
예제 #9
0
def readCommand(argv):
    """
  Processes the command used to run pacman from the command line.
  """
    from optparse import OptionParser
    usageStr = """
  USAGE:      python pacman.py <options>
  EXAMPLES:   (1) python capture.py
                  - starts an interactive game against two offensive agents
                    (you control the red agent with the arrow keys)
              (2) python capture.py --player2 KeyboardAgent2
                  - starts a two-player interactive game with w,a,s,d & i,j,k,l keys
              (3) python capture.py --player2 DefensiveReflexAgent
                  - starts a fully automated game
  """
    parser = OptionParser(usageStr)

    parser.add_option('-r',
                      '--red',
                      help=default('Red team'),
                      default='BaselineAgents')
    parser.add_option('-b',
                      '--blue',
                      help=default('Blue team'),
                      default='BaselineAgents')
    parser.add_option('--redOpts',
                      help=default('Options for red team (e.g. first=keys)'),
                      default='')
    parser.add_option('--blueOpts',
                      help=default('Options for blue team (e.g. first=keys)'),
                      default='')
    parser.add_option(
        '-l',
        '--layout',
        dest='layout',
        help=default(
            'the LAYOUT_FILE from which to load the map layout; use RANDOM for a random maze'
        ),
        metavar='LAYOUT_FILE',
        default='defaultCapture')
    parser.add_option('-t',
                      '--textgraphics',
                      action='store_true',
                      dest='textgraphics',
                      help='Display output as text only',
                      default=False)

    parser.add_option('-q',
                      '--quiet',
                      action='store_true',
                      help='Display minimal output and no graphics',
                      default=False)

    parser.add_option('-Q',
                      '--super-quiet',
                      action='store_true',
                      dest="super_quiet",
                      help='Same as -q but agent output is also suppressed',
                      default=False)

    parser.add_option('-k',
                      '--numPlayers',
                      type='int',
                      dest='numPlayers',
                      help=default('The maximum number of players'),
                      default=4)
    parser.add_option('-z',
                      '--zoom',
                      type='float',
                      dest='zoom',
                      help=default('Zoom in the graphics'),
                      default=1)
    parser.add_option('-i',
                      '--time',
                      type='int',
                      dest='time',
                      help=default('TIME limit of a game in moves'),
                      default=3000,
                      metavar='TIME')
    parser.add_option('-n',
                      '--numGames',
                      type='int',
                      help=default('Number of games to play'),
                      default=1)
    parser.add_option(
        '-f',
        '--fixRandomSeed',
        action='store_true',
        help='Fixes the random seed to always play the same game',
        default=False)
    parser.add_option(
        '--record',
        action='store_true',
        help=
        'Writes game histories to a file (named by the time they were played)',
        default=False)
    parser.add_option('--replay',
                      default=None,
                      help='Replays a recorded game file.')
    parser.add_option(
        '-x',
        '--numTraining',
        dest='numTraining',
        type='int',
        help=default('How many episodes are training (suppresses output)'),
        default=0)
    parser.add_option('-c',
                      '--catchExceptions',
                      action='store_true',
                      default=False,
                      help='Catch exceptions and enforce time limits')

    options, otherjunk = parser.parse_args(argv)
    assert len(otherjunk) == 0, "Unrecognized options: " + str(otherjunk)
    args = dict()

    # Choose a display format
    #if options.pygame:
    #   import pygameDisplay
    #    args['display'] = pygameDisplay.PacmanGraphics()
    if options.textgraphics:
        import textDisplay
        args['display'] = textDisplay.PacmanGraphics()
    elif options.quiet:
        import textDisplay
        args['display'] = textDisplay.NullGraphics()
    elif options.super_quiet:
        import textDisplay
        args['display'] = textDisplay.NullGraphics()
        args['muteAgents'] = True
    else:
        import graphicsDisplay
        graphicsDisplay.FRAME_TIME = 0
        args['display'] = graphicsDisplay.PacmanGraphics(options.zoom,
                                                         0,
                                                         capture=True)

    if options.fixRandomSeed: random.seed('cs188')

    # Special case: recorded games don't use the runGames method or args structure
    if options.replay != None:
        print 'Replaying recorded game %s.' % options.replay
        import cPickle
        recorded = cPickle.load(open(options.replay))
        recorded['display'] = args['display']
        replayGame(**recorded)
        sys.exit(0)

    # Choose a pacman agent
    redArgs, blueArgs = parseAgentArgs(options.redOpts), parseAgentArgs(
        options.blueOpts)
    if options.numTraining > 0:
        redArgs['numTraining'] = options.numTraining
        blueArgs['numTraining'] = options.numTraining
    nokeyboard = options.textgraphics or options.quiet or options.numTraining > 0
    print '\nRed team %s with %s:' % (options.red, redArgs)
    redAgents = loadAgents(True, options.red, nokeyboard, redArgs)
    print '\nBlue team %s with %s:' % (options.blue, blueArgs)
    blueAgents = loadAgents(False, options.blue, nokeyboard, blueArgs)
    args['agents'] = sum([list(el) for el in zip(redAgents, blueAgents)],
                         [])  # list of agents

    # Choose a layout
    if options.layout == 'RANDOM': options.layout = randomLayout()
    if options.layout.lower().find('capture') == -1:
        raise Exception('You must use a capture layout with capture.py')
    import layout
    args['layout'] = layout.getLayout(options.layout)
    if args['layout'] == None:
        raise Exception("The layout " + options.layout + " cannot be found")

    args['agents'] = args['agents'][:min(args['layout'].getNumGhosts(
    ), options.numPlayers)]
    args['length'] = options.time
    args['numGames'] = options.numGames
    args['numTraining'] = options.numTraining
    args['record'] = options.record
    args['catchExceptions'] = options.catchExceptions
    return args
예제 #10
0
def runGames(layout,
             pacman,
             ghosts,
             display,
             numGames,
             record,
             numTraining=0,
             catchExceptions=False,
             timeout=30,
             seed=None,
             rounds=0):
    import __main__
    __main__.__dict__['_display'] = display

    if seed is not None:
        if rounds > 0:
            # here load file and clean seed
            with open("random/randomList.pkl", "rb") as input_file:
                l_rand = cPickle.load(input_file)
                if rounds > 1:
                    l_rand = l_rand[:(len(l_rand) - ((rounds - 1) * 10))]
        else:
            r = Random()
            r.seed(seed)
            l_rand = [i for i in range(1, 100001)]
            r.shuffle(l_rand)

    rules = ClassicGameRules(timeout)
    games = []

    for i in range(numGames):

        if seed is not None:
            if l_rand:
                random.seed(l_rand.pop())
            else:
                l_rand = [i for i in range(1, 100001)]
                r.shuffle(l_rand)

        beQuiet = i < numTraining
        if beQuiet:
            # Suppress output and graphics
            import textDisplay
            gameDisplay = textDisplay.NullGraphics()
            rules.quiet = True
        else:
            gameDisplay = display
            rules.quiet = False
        game = rules.newGame(layout, pacman, ghosts, gameDisplay, beQuiet,
                             catchExceptions)

        game.run()

        if not beQuiet:
            games.append(game)

        if record:
            import time
            import pickle
            fname = ('recorded-game-%d' % (i + 1)) + \
                    '-'.join([str(t) for t in time.localtime()[1:6]])
            f = file(fname, 'w')
            components = {'layout': layout, 'actions': game.moveHistory}
            pickle.dump(components, f)
            f.close()

    if (numGames - numTraining) > 0:
        scores = [game.state.getScore() for game in games]
        wins = [game.state.isWin() for game in games]
        winRate = wins.count(True) / float(len(wins))
        print('Average Score:', sum(scores) / float(len(scores)))
        print('Scores:       ',
              ', '.join([str(int(score)) for score in scores]))
        print('Win Rate:      %d/%d (%.2f)' %
              (wins.count(True), len(wins), winRate))
        print('Record:       ',
              ', '.join([['Loss', 'Win'][int(w)] for w in wins]))

    return games
예제 #11
0
def runGames(layouts,
             agents,
             display,
             length,
             numGames,
             record,
             numTraining,
             pacmanTeamName,
             ghostTeamName,
             muteAgents=False,
             catchExceptions=True):

    rules = CaptureRules()
    games = []

    if numTraining > 0:
        print 'Playing %d training games' % numTraining

    for i, l in enumerate(layouts):
        beQuiet = i < numTraining
        if beQuiet:
            # Suppress output and graphics
            import textDisplay
            gameDisplay = textDisplay.NullGraphics()
            rules.quiet = True
        else:
            gameDisplay = display
            rules.quiet = False
        print "Running game with -l RANDOM%d  " % LAYOUT_SEED[i]
        g = rules.newGame(l, agents, gameDisplay, length, muteAgents,
                          catchExceptions)
        g.run()
        print ""
        if not beQuiet: games.append(g)

    #Now, reverse the index on the team, and run the games again
    # temp = agents[1]
    # agents[1] = agents[0]
    # agents[0] = temp

    # reversedGames = []

    # for i, l in enumerate(layouts):
    #   beQuiet = i < numTraining
    #   if beQuiet:
    #       # Suppress output and graphics
    #       import textDisplay
    #       gameDisplay = textDisplay.NullGraphics()
    #       rules.quiet = True
    #   else:
    #       gameDisplay = display
    #       rules.quiet = False
    #   g = rules.newGame( l, agents, gameDisplay, length, muteAgents, catchExceptions )
    #   g.run()
    #   if not beQuiet: reversedGames.append(g)

    print ""
    print "Preliminary Phase 3 Autograder Results:"
    passedCount = 0
    # Output
    print "Regular order"
    for i, game in enumerate(games):
        if game.agentCrashed:
            myTime = 99999999
        else:
            myTime = game.length - game.state.data.timeleft

        currPassed = True
        targetTime = baselineScores[i]
        if myTime <= targetTime:
            passedCount += 1
        else:
            currPassed = False
        print "Game {}: -l RANDOM{} \tTime taken: {} \tTarget time: {} \t {} \t{}".format(
            i, LAYOUT_SEED[i], myTime, targetTime,
            "AGENT CRASHED" if game.agentCrashed else "",
            "" if currPassed else " (NOT PASSED)")

    # print "Reversed order"
    # for i, game in enumerate(reversedGames):
    #   if game.agentCrashed:
    #     myTime = 99999999
    #   else:
    #     myTime = game.length - game.state.data.timeleft

    #   currPassed = True
    #   targetTime = baselineScores[i]
    #   if myTime <= targetTime:
    #     passedCount += 1
    #   else:
    #     currPassed = False
    #   print "Game {}: -l RANDOM{} \tTime taken: {} \tTarget time: {} \t {} \t{}".format(i, LAYOUT_SEED[i], myTime, targetTime, "AGENT CRASHED" if game.agentCrashed else "", "" if currPassed else " (NOT PASSED)")

    print "Summary: ", passedCount, "/", len(LAYOUT_SEED), "reached target"
    return games  #+ reversedGames
예제 #12
0
def runGames(layout,
             pacman,
             ghosts,
             display,
             numGames,
             record,
             numTraining=0,
             catchExceptions=False,
             timeout=30):
    import __main__
    __main__.__dict__['_display'] = display

    rules = ClassicGameRules(timeout)
    games = []
    #### 2016-09-04 ####
    observations = []

    for i in range(numGames):
        beQuiet = i < numTraining
        ''' 
    if beQuiet:
        # Suppress output and graphics
        import textDisplay
        gameDisplay = textDisplay.NullGraphics()
        rules.quiet = True
    else:
        gameDisplay = display
        rules.quiet = False
    '''
        import textDisplay
        gameDisplay = textDisplay.NullGraphics()
        rules.quiet = True

        game = rules.newGame(layout, pacman, ghosts, gameDisplay, beQuiet,
                             catchExceptions)
        #game.run()
        #### 2016-09-04 ####
        observation = game.run()
        observations.extend(observation)
        ####################

        if not beQuiet: games.append(game)
        if record:
            import time, cPickle
            fname = ('recorded-game-%d' % (i + 1)) + '-'.join(
                [str(t) for t in time.localtime()[1:6]])
            f = file(fname, 'w')
            components = {'layout': layout, 'actions': game.moveHistory}
            cPickle.dump(components, f)
            f.close()

    if numGames > 1:
        scores = [game.state.getScore() for game in games]
        wins = [game.state.isWin() for game in games]
        winRate = wins.count(True) / float(len(wins))
        print 'Average Score:', sum(scores) / float(len(scores))
        print 'Scores:       ', ', '.join([str(score) for score in scores])
        print 'Win Rate:      %d/%d (%.2f)' % (wins.count(True), len(wins),
                                               winRate)
        print 'Record:       ', ', '.join([['Loss', 'Win'][int(w)]
                                           for w in wins])

    #### 2016-09-04 ####
    import interviewer
    interviewer.findFeatures(observations)

    return games
예제 #13
0
파일: capture.py 프로젝트: boris081/CS188-1
def runGames(layout,
             agents,
             display,
             length,
             numGames,
             record,
             numTraining,
             pacmanTeamName,
             ghostTeamName,
             muteAgents=False,
             catchExceptions=True):

    rules = CaptureRules()
    games = []

    if numTraining > 0:
        print('Playing %d training games' % numTraining)

    for i in range(numGames):
        beQuiet = i < numTraining
        if beQuiet:
            # Suppress output and graphics
            import textDisplay
            gameDisplay = textDisplay.NullGraphics()
            rules.quiet = True
        else:
            gameDisplay = display
            rules.quiet = False
        g = rules.newGame(layout, agents, gameDisplay, length, muteAgents,
                          catchExceptions)
        g.run()
        if not beQuiet: games.append(g)

        g.record = None
        if record:
            import time, pickle, game
            #fname = ('recorded-game-%d' % (i + 1)) +  '-'.join([str(t) for t in time.localtime()[1:6]])
            #f = file(fname, 'w')
            components = {
                'layout': layout,
                'agents': [game.Agent() for a in agents],
                'actions': g.moveHistory,
                'length': length,
                'pacmanTeamName': pacmanTeamName,
                'ghostTeamName': ghostTeamName
            }
            #f.close()
            print("recorded")
            g.record = pickle.dumps(components)
            with open(
                    'replay_{}_{}'.format(pacmanTeamName,
                                          round(time.time(), 0)), 'wb') as f:
                f.write(g.record)

    if numGames > 1:
        scores = [game.state.data.score for game in games]
        times = [game.length - game.state.data.timeleft for game in games]
        pacmanWinRate = [s > 0
                         for s in scores].count(True) / float(len(scores))
        print('Times:', times)
        print('Average Score:', sum(scores) / float(len(scores)))
        print('Scores:       ', ', '.join([str(score) for score in scores]))
        print('Pacman Win Rate:  %d/%d (%.2f)' %
              ([s > 0
                for s in scores].count(True), len(scores), pacmanWinRate))
    return games
예제 #14
0
파일: capture.py 프로젝트: boris081/CS188-1
def readCommand(argv):
    """
  Processes the command used to run pacman from the command line.
  """
    from optparse import OptionParser
    usageStr = """
  USAGE:      python pacman.py <options>
  EXAMPLES:   (1) python capture.py
                  - starts a game with two baseline agents
              (2) python capture.py --keys0
                  - starts a two-player interactive game where the arrow keys control agent 0, and all other agents are baseline agents
              (3) python capture.py -p baselineTeam -b myTeam
                  - starts a fully automated game where the pacman team is a baseline team and ghost team is myTeam 
  """
    parser = OptionParser(usageStr)

    parser.add_option('-p',
                      '--pacman',
                      help=default('Pacman team'),
                      default='team')
    parser.add_option('--pac0',
                      help=default('Pacman at Index 0'),
                      default='None')
    parser.add_option('--pac1',
                      help=default('Pacman at Index 1'),
                      default='None')

    parser.add_option('-g',
                      '--ghost',
                      help=default('Ghost team'),
                      default='oneGhostTeam')
    parser.add_option('--pacman-name',
                      help=default('Pacman team name'),
                      default='Pacman')
    parser.add_option('--ghost-name',
                      help=default('Ghost team name'),
                      default='Ghost')
    parser.add_option(
        '--pacmanOpts',
        help=default('Options for pacman team (e.g. first=keys)'),
        default='')
    parser.add_option('--ghostOpts',
                      help=default('Options for ghost team (e.g. first=keys)'),
                      default='')
    parser.add_option(
        '--keys0',
        help='Make agent 0 (first pacman player) a keyboard agent',
        action='store_true',
        default=False)
    parser.add_option(
        '--keys1',
        help='Make agent 1 (second pacman player) a keyboard agent',
        action='store_true',
        default=False)
    parser.add_option(
        '--keys2',
        help='Make agent 2 (first ghost player) a keyboard agent',
        action='store_true',
        default=False)
    parser.add_option(
        '--keys3',
        help='Make agent 3 (second ghost player) a keyboard agent',
        action='store_true',
        default=False)
    parser.add_option(
        '-l',
        '--layout',
        dest='layout',
        help=default(
            'the LAYOUT_FILE from which to load the map layout; use RANDOM for a random maze; use RANDOM<seed> to use a specified random seed, e.g., RANDOM23'
        ),
        metavar='LAYOUT_FILE',
        default='defaultCapture')
    parser.add_option('-t',
                      '--textgraphics',
                      action='store_true',
                      dest='textgraphics',
                      help='Display output as text only',
                      default=False)

    parser.add_option('-q',
                      '--quiet',
                      action='store_true',
                      help='Display minimal output and no graphics',
                      default=False)

    parser.add_option('-Q',
                      '--super-quiet',
                      action='store_true',
                      dest="super_quiet",
                      help='Same as -q but agent output is also suppressed',
                      default=False)

    parser.add_option('-z',
                      '--zoom',
                      type='float',
                      dest='zoom',
                      help=default('Zoom in the graphics'),
                      default=1)
    parser.add_option('-i',
                      '--time',
                      type='int',
                      dest='time',
                      help=default('TIME limit of a game in moves'),
                      default=1200,
                      metavar='TIME')
    parser.add_option('-n',
                      '--numGames',
                      type='int',
                      help=default('Number of games to play'),
                      default=1)
    parser.add_option(
        '-f',
        '--fixRandomSeed',
        action='store_true',
        help='Fixes the random seed to always play the same game',
        default=False)
    parser.add_option(
        '--record',
        action='store_true',
        help=
        'Writes game histories to a file (named by the time they were played)',
        default=False)
    parser.add_option('--replay',
                      default=None,
                      help='Replays a recorded game file.')
    # TODO: This currently doesn't work, consider removing or fixing
    parser.add_option(
        '-x',
        '--numTraining',
        dest='numTraining',
        type='int',
        help=default('How many episodes are training (suppresses output)'),
        default=0)
    parser.add_option('-c',
                      '--catchExceptions',
                      action='store_true',
                      default=True,
                      help='Catch exceptions and enforce time limits')

    options, otherjunk = parser.parse_args(argv)
    assert len(otherjunk) == 0, "Unrecognized options: " + str(otherjunk)
    args = dict()

    # Variable to keep track of custom team
    customTeam = False
    if options.pac0 != 'None':
        customTeam = True
        options.pacman_name = 'Team ' + options.pac0 + " + " + options.pac1

    # Choose a display format
    if options.textgraphics:
        import textDisplay
        args['display'] = textDisplay.PacmanGraphics()
    elif options.quiet:
        import textDisplay
        args['display'] = textDisplay.NullGraphics()
    elif options.super_quiet:
        import textDisplay
        args['display'] = textDisplay.NullGraphics()
        args['muteAgents'] = True
    else:
        import captureGraphicsDisplay
        # Hack for agents writing to the display
        captureGraphicsDisplay.FRAME_TIME = 0
        args['display'] = captureGraphicsDisplay.PacmanGraphics(options.pacman,
                                                                options.ghost,
                                                                options.zoom,
                                                                0,
                                                                capture=True)
        import __main__
        __main__.__dict__['_display'] = args['display']

    args['pacmanTeamName'] = options.pacman_name
    args['ghostTeamName'] = options.ghost_name

    if options.fixRandomSeed: random.seed('cs188')

    # Special case: recorded games don't use the runGames method or args structure
    if options.replay != None:
        print('Replaying recorded game %s.' % options.replay)
        import pickle
        recorded = pickle.load(open(options.replay))
        recorded['display'] = args['display']
        replayGame(**recorded)
        sys.exit(0)

    # Choose a pacman agent
    pacmanTeamArgs, ghostTeamArgs = parseAgentArgs(
        options.pacmanOpts), parseAgentArgs(options.ghostOpts)
    if options.numTraining > 0:
        pacmanTeamArgs['numTraining'] = options.numTraining
        ghostTeamArgs['numTraining'] = options.numTraining
    nokeyboard = options.textgraphics or options.quiet or options.numTraining > 0

    # Default, loading from the phasexTeam.py
    if not customTeam:
        pacmanAgents = loadAgents(True, options.pacman, nokeyboard,
                                  pacmanTeamArgs)
        print('Pacman team %s with args %s:' %
              (options.pacman, pacmanTeamArgs))
    # Custom loading
    else:
        pacZero = loadOneAgent(True, options.pac0, 0)
        pacOne = loadOneAgent(True, options.pac1, 1)
        pacmanAgents = [pacZero, pacOne]

    ghostAgents = loadAgents(False, options.ghost, nokeyboard, ghostTeamArgs)
    if ghostAgents:
        print('Ghost team %s with args %s:' % (options.ghost, ghostTeamArgs))

    # Assume 2 agents on the pacman side, and
    # variable amount (0-2) on the ghost side
    args['agents'] = pacmanAgents + ghostAgents

    numKeyboardAgents = 0
    for index, val in enumerate(
        [options.keys0, options.keys1, options.keys2, options.keys3]):
        if not val: continue
        if numKeyboardAgents == 0:
            agent = keyboardAgents.KeyboardAgent(index)
        elif numKeyboardAgents == 1:
            agent = keyboardAgents.KeyboardAgent2(index)
        else:
            raise Exception('Max of two keyboard agents supported')
        numKeyboardAgents += 1
        args['agents'][index] = agent

    # Choose a layout
    import layout
    if options.layout == 'RANDOM':
        args['layout'] = layout.Layout(randomLayout().split('\n'),
                                       maxGhosts=len(ghostAgents))
    elif options.layout.startswith('RANDOM'):
        args['layout'] = layout.Layout(randomLayout(int(
            options.layout[6:])).split('\n'),
                                       maxGhosts=len(ghostAgents))
    elif options.layout.lower().find('capture') == -1:
        raise Exception('You must use a capture layout with capture.py')
    else:
        args['layout'] = layout.getLayout(options.layout,
                                          maxGhosts=len(ghostAgents))

    if args['layout'] == None:
        raise Exception("The layout " + options.layout + " cannot be found")
    args['length'] = options.time
    args['numGames'] = options.numGames
    args['numTraining'] = options.numTraining
    args['record'] = options.record
    args['catchExceptions'] = options.catchExceptions
    return args
예제 #15
0
def runGames(layouts,
             pacman,
             ghosts,
             display,
             numGames,
             record,
             catchExceptions,
             name=None,
             numTraining=0,
             timeout=30,
             numGhosts=None,
             length=30):
    import __main__
    __main__.__dict__['_display'] = display

    rules = ClassicGameRules(timeout)
    if numGhosts == None: numGhosts = layouts[0].getNumGhosts()

    games = []
    for layout in layouts:
        #layout = layouts[min(i,len(layouts)-1)]
        print("Layout : ", layout.name)
        laygames = []
        games.append(laygames)
        for i in range(numGames):
            beQuiet = i < numTraining
            if beQuiet:
                # Suppress output and graphics
                import textDisplay
                gameDisplay = textDisplay.NullGraphics()
                rules.quiet = True
            else:
                gameDisplay = display
                rules.quiet = False
            game = rules.newGame(layout, pacman, ghosts, numGhosts,
                                 gameDisplay, length, catchExceptions)
            game.run()
            if not beQuiet: laygames.append(game)

            if record:
                import time, pickle
                fname = ('recorded-game-%d' % (i + 1)) + '-'.join(
                    [str(t) for t in time.localtime()[1:6]])
                f = file(fname, 'w')
                components = {'layout': layout, 'actions': game.moveHistory}
                pickle.dump(components, f)
                f.close()

        if (numGames - numTraining) > 0:
            scores = [game.state.getScore() for game in laygames]
            timeouts = [game.state.getTimeout() for game in laygames]
            moves = [game.numMoves for game in laygames]
            times = [
                laygames[i].totalAgentTimes[0] /
                float(moves[i]) if float(moves[i]) > 0 else 0
                for i in range(len(laygames))
            ]
            wins = [game.state.isWin() for game in laygames]
            winRate = wins.count(True) / float(len(wins))
            print('Average Score:', sum(scores) / float(len(scores)))
            print('Scores:       ',
                  ', '.join([str(score) for score in scores]))
            print('Moves:        ', ', '.join([str(move) for move in moves]))
            print('Win/Loss:     ',
                  ', '.join([['Loss', 'Win'][int(w)] for w in wins]))
            print('Timeout:      ',
                  ', '.join([str(timeout) for timeout in timeouts]))
            print('AgentTime:    ',
                  ', '.join(["%.3f" % time for time in times]))
            print('Win Rate:      %d/%d (%.2f)' %
                  (wins.count(True), len(wins), winRate))

    return games
예제 #16
0
def readCommand(argv):
    """
    Processes the command used to run pacman from the command line.
    """
    from optparse import OptionParser

    usageStr = """
    USAGE:      python pacman.py <options>
    EXAMPLES:   (1) python pacman.py
                    - starts an interactive game
                (2) python pacman.py --layout smallClassic --zoom 2
                OR  python pacman.py -l smallClassic -z 2
                    - starts an interactive game on a smaller board, zoomed in
    """
    parser = OptionParser(usageStr)

    parser.add_option(
        "-n",
        "--numGames",
        dest="numGames",
        type="int",
        help=default("the number of GAMES to play"),
        metavar="GAMES",
        default=1,
    )
    parser.add_option(
        "-l",
        "--layout",
        dest="layout",
        help=default("the LAYOUT_FILE from which to load the map layout"),
        metavar="LAYOUT_FILE",
        default="mediumClassic",
    )
    parser.add_option(
        "-p",
        "--pacman",
        dest="pacman",
        help=default("the agent TYPE in the pacmanAgents module to use"),
        metavar="TYPE",
        default="KeyboardAgent",
    )
    parser.add_option(
        "-t",
        "--textGraphics",
        action="store_true",
        dest="textGraphics",
        help="Display output as text only",
        default=False,
    )
    parser.add_option(
        "-q",
        "--quietTextGraphics",
        action="store_true",
        dest="quietGraphics",
        help="Generate minimal output and no graphics",
        default=False,
    )
    parser.add_option(
        "-g",
        "--ghosts",
        dest="ghost",
        help=default("the ghost agent TYPE in the ghostAgents module to use"),
        metavar="TYPE",
        default="RandomGhost",
    )
    parser.add_option(
        "-k",
        "--numghosts",
        type="int",
        dest="numGhosts",
        help=default("The maximum number of ghosts to use"),
        default=4,
    )
    parser.add_option(
        "-z",
        "--zoom",
        type="float",
        dest="zoom",
        help=default("Zoom the size of the graphics window"),
        default=1.0,
    )
    parser.add_option(
        "-f",
        "--fixRandomSeed",
        action="store_true",
        dest="fixRandomSeed",
        help="Fixes the random seed to always play the same game",
        default=False,
    )
    parser.add_option(
        "-r",
        "--recordActions",
        action="store_true",
        dest="record",
        help=
        "Writes game histories to a file (named by the time they were played)",
        default=False,
    )
    parser.add_option(
        "--replay",
        dest="gameToReplay",
        help="A recorded game file (pickle) to replay",
        default=None,
    )
    parser.add_option(
        "-a",
        "--agentArgs",
        dest="agentArgs",
        help=
        'Comma separated values sent to agent. e.g. "opt1=val1,opt2,opt3=val3"',
    )
    parser.add_option(
        "-x",
        "--numTraining",
        dest="numTraining",
        type="int",
        help=default("How many episodes are training (suppresses output)"),
        default=0,
    )
    parser.add_option(
        "--frameTime",
        dest="frameTime",
        type="float",
        help=default("Time to delay between frames; <0 means keyboard"),
        default=0.1,
    )
    parser.add_option(
        "-c",
        "--catchExceptions",
        action="store_true",
        dest="catchExceptions",
        help="Turns on exception handling and timeouts during games",
        default=False,
    )
    parser.add_option(
        "--timeout",
        dest="timeout",
        type="int",
        help=default(
            "Maximum length of time an agent can spend computing in a single game"
        ),
        default=30,
    )

    options, otherjunk = parser.parse_args(argv)
    if len(otherjunk) != 0:
        raise Exception("Command line input not understood: " + str(otherjunk))
    args = dict()

    # Fix the random seed
    if options.fixRandomSeed:
        random.seed("cs188")

    # Choose a layout
    args["layout"] = layout.getLayout(options.layout)
    if args["layout"] == None:
        raise Exception("The layout " + options.layout + " cannot be found")

    # Choose a Pacman agent
    noKeyboard = options.gameToReplay == None and (options.textGraphics
                                                   or options.quietGraphics)
    pacmanType = loadAgent(options.pacman, noKeyboard)
    agentOpts = parseAgentArgs(options.agentArgs)
    if options.numTraining > 0:
        args["numTraining"] = options.numTraining
        if "numTraining" not in agentOpts:
            agentOpts["numTraining"] = options.numTraining
    pacman = pacmanType(**agentOpts)  # Instantiate Pacman with agentArgs
    args["pacman"] = pacman

    # Don't display training games
    if "numTrain" in agentOpts:
        options.numQuiet = int(agentOpts["numTrain"])
        options.numIgnore = int(agentOpts["numTrain"])

    # Choose a ghost agent
    ghostType = loadAgent(options.ghost, noKeyboard)
    args["ghosts"] = [ghostType(i + 1) for i in range(options.numGhosts)]

    # Choose a display format
    if options.quietGraphics:
        import textDisplay

        args["display"] = textDisplay.NullGraphics()
    elif options.textGraphics:
        import textDisplay

        textDisplay.SLEEP_TIME = options.frameTime
        args["display"] = textDisplay.PacmanGraphics()
    else:
        import graphicsDisplay

        args["display"] = graphicsDisplay.PacmanGraphics(
            options.zoom, frameTime=options.frameTime)
    args["numGames"] = options.numGames
    args["record"] = options.record
    args["catchExceptions"] = options.catchExceptions
    args["timeout"] = options.timeout

    # Special case: recorded games don't use the runGames method or args structure
    if options.gameToReplay != None:
        print("Replaying recorded game %s." % options.gameToReplay)
        import pickle

        f = open(options.gameToReplay)
        try:
            recorded = pickle.load(f)
        finally:
            f.close()
        recorded["display"] = args["display"]
        replayGame(**recorded)
        sys.exit(0)

    return args
예제 #17
0
def readCommand(argv):
    """
    Processes the command used to run pacman from the command line.
    """
    from optparse import OptionParser
    usageStr = 
    parser = OptionParser(usageStr)

    parser.add_option('-n', '--numGames', dest='numGames', type='int',
                      help=default('the number of GAMES to play'), metavar='GAMES', default=6000)
    parser.add_option('-l', '--layout', dest='layout',
                      help=default(
                          'the LAYOUT_FILE from which to load the map layout'),
                      metavar='LAYOUT_FILE', default='smallGrid')
    parser.add_option('-p', '--pacman', dest='pacman',
                      help=default(
                          'the agent TYPE in the pacmanAgents module to use'),
                      metavar='TYPE', default='PacmanDQN')
    parser.add_option('-t', '--textGraphics', action='store_true', dest='textGraphics',
                      help='Display output as text only', default=False)
    parser.add_option('-q', '--quietTextGraphics', action='store_true', dest='quietGraphics',
                      help='Generate minimal output and no graphics', default=False)
    parser.add_option('-g', '--ghosts', dest='ghost',
                      help=default(
                          'the ghost agent TYPE in the ghostAgents module to use'),
                      metavar='TYPE', default='RandomGhost')
    parser.add_option('-k', '--numghosts', type='int', dest='numGhosts',
                      help=default('The maximum number of ghosts to use'), default=4)
    parser.add_option('-z', '--zoom', type='float', dest='zoom',
                      help=default('Zoom the size of the graphics window'), default=1.0)
    parser.add_option('-f', '--fixRandomSeed', action='store_true', dest='fixRandomSeed',
                      help='Fixes the random seed to always play the same game', default=False)
    parser.add_option('-r', '--recordActions', action='store_true', dest='record',
                      help='Writes game histories to a file (named by the time they were played)', default=False)
    parser.add_option('--replay', dest='gameToReplay',
                      help='A recorded game file (pickle) to replay', default=None)
    parser.add_option('-a', '--agentArgs', dest='agentArgs',
                      help='Comma separated values sent to agent. e.g. "opt1=val1,opt2,opt3=val3"')
    parser.add_option('-x', '--numTraining', dest='numTraining', type='int',
                      help=default('How many episodes are training (suppresses output)'), default=5000)
    parser.add_option('--frameTime', dest='frameTime', type='float',
                      help=default('Time to delay between frames; <0 means keyboard'), default=0.1)
    parser.add_option('-c', '--catchExceptions', action='store_true', dest='catchExceptions',
                      help='Turns on exception handling and timeouts during games', default=False)
    parser.add_option('--timeout', dest='timeout', type='int',
                      help=default('Maximum length of time an agent can spend computing in a single game'), default=30)

    options, otherjunk = parser.parse_args(argv)
    if len(otherjunk) != 0:
        raise Exception('Command line input not understood: ' + str(otherjunk))
    args = dict()

    # Fix the random seed
    if options.fixRandomSeed:
        random.seed('cs188')

    # Choose a layout
    args['layout'] = layout.getLayout(options.layout)
    if args['layout'] == None:
        raise Exception("The layout " + options.layout + " cannot be found")

    # Choose a Pacman agent
    noKeyboard = options.gameToReplay == None and (
        options.textGraphics or options.quietGraphics)
    pacmanType = loadAgent(options.pacman, noKeyboard)
    agentOpts = parseAgentArgs(options.agentArgs)

    agentOpts['width'] = layout.getLayout(options.layout).width
    agentOpts['height'] = layout.getLayout(options.layout).height

    if options.numTraining > 0:
        args['numTraining'] = options.numTraining
        if 'numTraining' not in agentOpts:
            agentOpts['numTraining'] = options.numTraining
    pacman = pacmanType(agentOpts)  # Instantiate Pacman with agentArgs
    args['pacman'] = pacman
    pacman.width = agentOpts['width']
    pacman.height = agentOpts['height']

    # Don't display training games
    if 'numTrain' in agentOpts:
        options.numQuiet = int(agentOpts['numTrain'])
        options.numIgnore = int(agentOpts['numTrain'])

    # Choose a ghost agent
    ghostType = loadAgent(options.ghost, noKeyboard)
    args['ghosts'] = [ghostType(i + 1) for i in range(options.numGhosts)]

    # Choose a display format
    if options.quietGraphics:
        import textDisplay
        args['display'] = textDisplay.NullGraphics()
    elif options.textGraphics:
        import textDisplay
        textDisplay.SLEEP_TIME = options.frameTime
        args['display'] = textDisplay.PacmanGraphics()
    else:
        import graphicsDisplay
        args['display'] = graphicsDisplay.PacmanGraphics(
            options.zoom, frameTime=options.frameTime)
    args['numGames'] = options.numGames
    args['record'] = options.record
    args['catchExceptions'] = options.catchExceptions
    args['timeout'] = options.timeout

    # Special case: recorded games don't use the runGames method or args
    # structure
    if options.gameToReplay is not None:
        print(('Replaying recorded game %s.' % options.gameToReplay))
        import pickle
        f = open(options.gameToReplay)
        try:
            recorded = pickle.load(f)
        finally:
            f.close()
        recorded['display'] = args['display']
        replayGame(**recorded)
        sys.exit(0)

    return args
예제 #18
0
def runGames(layout,
             pacmen,
             ghosts,
             display,
             numGames,
             record,
             numTraining=0,
             catchExceptions=False,
             timeout=30):
    import __main__
    __main__.__dict__['_display'] = display

    rules = ClassicGameRules(timeout)
    games = []

    replay = {}
    minibatch_size = 6  #whoami
    action_values = [[] for k in range(len(pacmen))]
    for i in range(numGames):
        beQuiet = i < numTraining
        if beQuiet:
            # Suppress output and graphics
            import textDisplay
            gameDisplay = textDisplay.NullGraphics()
            rules.quiet = True
        else:
            gameDisplay = display
            rules.quiet = False
        game = rules.newGame(layout, pacmen, ghosts, gameDisplay, beQuiet,
                             catchExceptions)
        replay = game.run(replay, minibatch_size)  #whoami
        if not beQuiet: games.append(game)
        for k, pac in enumerate(pacmen):
            action_values[k].append(pac.action_values)

        if record and i >= numGames - 10:
            import time, pickle
            fname = ('./records/recorded-game-%d' % (i + 1)) + '-'.join(
                [str(t) for t in time.localtime()[1:6]])
            f = open(fname, 'wb')
            components = {'layout': layout, 'actions': game.moveHistory}
            pickle.dump(components, f)
            f.close()

    if (numGames - numTraining) > 0:
        scores = [game.state.getScore() for game in games]
        wins = [game.state.isWin() for game in games]

        import pickle
        components = {'action_values': action_values}
        f = open('action_values', 'wb+')
        pickle.dump(components, f)
        f.close()

        f = open('scores', 'wb+')
        components = {'scores': scores}
        pickle.dump(components, f)
        f.close()

        winRate = wins.count(True) / float(len(wins))
        print('Average Score:', sum(scores) / float(len(scores)))
        print('Scores:       ', ', '.join([str(score) for score in scores]))
        print('Win Rate:      %d/%d (%.2f)' %
              (wins.count(True), len(wins), winRate))
        print('Record:       ',
              ', '.join([['Loss', 'Win'][int(w)] for w in wins]))

    return games
예제 #19
0
def runGames(layout,
             pacman,
             ghosts,
             display,
             numGames,
             record,
             numTraining=0,
             catchExceptions=False,
             timeout=30,
             singleStep=False,
             logIt=False):
    # import __main__
    # __main__.__dict__['_display'] = display

    singleStepModus = PacmanGlobals.singleSteps
    print " log it? " + str(PacmanGlobals.logIt)

    # create logfile
    if PacmanGlobals.logIt:
        fileName = raw_input("Enter your input: ")
        logz = open(fileName + ".txt", "wb")

    # print str(singleStepModus)

    rules = ClassicGameRules(timeout)
    games = []

    for i in range(numGames):
        beQuiet = i < numTraining
        if beQuiet:
            # Suppress output and graphics
            import textDisplay
            gameDisplay = textDisplay.NullGraphics()
            rules.quiet = True
        else:
            gameDisplay = display
            rules.quiet = False
        game = rules.newGame(layout, pacman, ghosts, gameDisplay, beQuiet,
                             catchExceptions, singleStepModus)
        game.run(singleStepModus, beQuiet, i)
        if not beQuiet: games.append(game)

        if record:
            import time, cPickle
            fname = ('recorded-game-%d' % (i + 1)) + '-'.join(
                [str(t) for t in time.localtime()[1:6]])
            f = file(fname, 'w')
            components = {'layout': layout, 'actions': game.moveHistory}
            cPickle.dump(components, f)
            f.close()

    if (numGames - numTraining) > 0:
        scores = [game.state.getScore() for game in games]
        wins = [game.state.isWin() for game in games]
        winRate = wins.count(True) / float(len(wins))
        print 'Average Score:', sum(scores) / float(len(scores))
        print 'Scores:       ', ', '.join([str(score) for score in scores])
        print 'Win Rate:      %d/%d (%.2f)' % (wins.count(True), len(wins),
                                               winRate)
        print 'Record:       ', ', '.join([['Loss', 'Win'][int(w)]
                                           for w in wins])

        wRate = '%d/%d (%.2f)' % (wins.count(True), len(wins), winRate)

        logz.write('Average Score:' + str(sum(scores) / float(len(scores))))
        #logz.write('Scores:       ' + ', '.join([str(score) for score in scores]))
        logz.write(str('\nWin rate ' + wRate))
        #logz.write('Record:       ' + ', '.join([ ['Loss', 'Win'][int(w)] for w in wins]))

    return games
예제 #20
0
def runGames(layout,
             pacman,
             ghosts,
             display,
             numGames,
             record,
             numTraining=0,
             catchExceptions=False,
             timeout=30):
    import __main__
    __main__.__dict__['_display'] = display

    games = []

    if USE_SEQ:
        print('pos seq: ')
        print(POS_SEQ[0:numGames / CHANGE_EVERY])
        print('neg seq: ')
        print(NEG_SEQ[0:numGames / CHANGE_EVERY])

    for i in range(numGames):

        ## BL ADDED: decide whether the positive or negative rewards are turned off.
        if i % CHANGE_EVERY == 0:
            if USE_SEQ:
                pos_reward = POS_SEQ[i / CHANGE_EVERY]
                neg_reward = NEG_SEQ[i / CHANGE_EVERY]
            else:
                if random.uniform(0, 1) < TURN_OFF_POS:
                    pos_reward = False
                else:
                    pos_reward = True
                if random.uniform(0, 1) < TURN_OFF_NEG:
                    neg_reward = False
                else:
                    neg_reward = True
            # if pos_reward: Rmax = Rmax * 10
            # if neg_reward: Rmin = Rmin * 10

        rules = ClassicGameRules(timeout, pos_reward, neg_reward)

        beQuiet = i < numTraining
        if beQuiet:
            # Suppress output and graphics
            import textDisplay
            gameDisplay = textDisplay.NullGraphics()
            rules.quiet = True
        else:
            gameDisplay = display
            rules.quiet = False

        game = rules.newGame(layout, pacman, ghosts, gameDisplay, beQuiet,
                             catchExceptions)
        game.run()
        if not beQuiet: games.append(game)

        if record:
            import time, cPickle
            fname = ('recorded-game-%d' % (i + 1)) + '-'.join(
                [str(t) for t in time.localtime()[1:6]])
            f = file(fname, 'w')
            components = {'layout': layout, 'actions': game.moveHistory}
            cPickle.dump(components, f)
            f.close()

    if (numGames - numTraining) > 0:
        scores = [game.state.getScore() for game in games]
        wins = [game.state.isWin() for game in games]
        winRate = wins.count(True) / float(len(wins))
        print 'Average Score:', sum(scores) / float(len(scores))
        print 'Scores:       ', ', '.join([str(score) for score in scores])
        print 'Win Rate:      %d/%d (%.2f)' % (wins.count(True), len(wins),
                                               winRate)
        print 'Record:       ', ', '.join([['Loss', 'Win'][int(w)]
                                           for w in wins])

    return games
def readCommand(argv):
    """
    Processes the command used to run pacman from the command line.
    """
    from optparse import OptionParser
    usageStr = """
    USAGE:      python pacman.py <options>
    EXAMPLES:   (1) python pacman.py
                    - starts an interactive game
                (2) python pacman.py --layout smallClassic --zoom 2
                OR  python pacman.py -l smallClassic -z 2
                    - starts an interactive game on a smaller board, zoomed in
    """
    parser = OptionParser(usageStr)

    parser.add_option('-n', '--numGames', dest='numGames', type='int',
                      help=default('the number of GAMES to play'), metavar='GAMES', default=1)
    parser.add_option('-l', '--layout', dest='layout',
                      help=default(
                          'the LAYOUT_FILE from which to load the map layout'),
                      metavar='LAYOUT_FILE', default='mediumClassic')
    parser.add_option('-p', '--pacman', dest='pacman',
                      help=default(
                          'the agent TYPE in the pacmanAgents module to use'),
                      metavar='TYPE', default='KeyboardAgent')
    parser.add_option('-t', '--textGraphics', action='store_true', dest='textGraphics',
                      help='Display output as text only', default=False)
    parser.add_option('-q', '--quietTextGraphics', action='store_true', dest='quietGraphics',
                      help='Generate minimal output and no graphics', default=False)
    parser.add_option('-g', '--ghosts', dest='ghost',
                      help=default(
                          'the ghost agent TYPE in the ghostAgents module to use'),
                      metavar='TYPE', default='RandomGhost')
    parser.add_option('-k', '--numghosts', type='int', dest='numGhosts',
                      help=default('The maximum number of ghosts to use'), default=4)
    parser.add_option('-z', '--zoom', type='float', dest='zoom',
                      help=default('Zoom the size of the graphics window'), default=1.0)
    parser.add_option('-f', '--fixRandomSeed', action='store_true', dest='fixRandomSeed',
                      help='Fixes the random seed to always play the same game', default=False)
    parser.add_option('-r', '--recordActions', action='store_true', dest='record',
                      help='Writes game histories to a file (named by the time they were played)',
                      default=False)
    parser.add_option('-o','--replay', dest='gameToReplay',
                      help='A recorded game file (pickle) to replay',
                      default=None)
                      #default="recorded/recorded-game-326-22-10-59-19")
    parser.add_option('-a', '--agentArgs', dest='agentArgs',
                      help='Comma separated values sent to agent. e.g. "opt1=val1,opt2,opt3=val3"')
    parser.add_option('-x', '--numTraining', dest='numTraining', type='int',
                      help=default('How many episodes are training (suppresses output)'), default=0)
    parser.add_option('--frameTime', dest='frameTime', type='float',
                      help=default('Time to delay between frames; <0 means keyboard'), default=0.1)
    parser.add_option('-c', '--catchExceptions', action='store_true', dest='catchExceptions',
                      help='Turns on exception handling and timeouts during games', default=False)
    parser.add_option('--timeout', dest='timeout', type='int',
                      help=default('Maximum length of time an agent can spend computing in a single game'), default=30)

    options, otherjunk = parser.parse_args(argv)
    if len(otherjunk) != 0:
        raise Exception('Command line input not understood: ' + str(otherjunk))
    args = dict()

    # Fix the random seed
    if options.fixRandomSeed:
        random.seed('cs188')

    # Choose a layout
    args['layout'] = layout.getLayout(options.layout)
    if args['layout'] == None:
        raise Exception("The layout " + options.layout + " cannot be found")
    
    # First convert notebooks .ipynb to .py ##################
    """
    pythonPathStr = os.path.expandvars("$PYTHONPATH")
    if pythonPathStr.find(';') == -1:
        pythonPathDirs = pythonPathStr.split(':')
    else:
        pythonPathDirs = pythonPathStr.split(';')
    pythonPathDirs.append('.')

    for moduleDir in pythonPathDirs:
        if not os.path.isdir(moduleDir):
            continue
        from subprocess import call
        notebooksNames = [f for f in os.listdir(moduleDir) if f.endswith('gents.ipynb')]
        print("Converting notebooks to .py... ", end="")
        for notebook in notebooksNames:
            try:
                command = ('ipython nbconvert --to script %s' % notebook)
                call(command, shell=True)
            except ImportError:
                continue
        print("Done.")
    """
    ##########################################################
    
    # Choose a Pacman agent
    noKeyboard = options.gameToReplay == None and (
        options.textGraphics or options.quietGraphics)
    pacmanType = loadAgent(options.pacman, noKeyboard)
    agentOpts = parseAgentArgs(options.agentArgs)
    if options.numTraining > 0:
        args['numTraining'] = options.numTraining
        if 'numTraining' not in agentOpts:
            agentOpts['numTraining'] = options.numTraining
    pacman = pacmanType(**agentOpts)  # Instantiate Pacman with agentArgs
    args['pacman'] = pacman

    # Don't display training games
    if 'numTrain' in agentOpts:
        options.numQuiet = int(agentOpts['numTrain'])
        options.numIgnore = int(agentOpts['numTrain'])

    # Choose a ghost agent
    ghostType = loadAgent(options.ghost, noKeyboard)
    args['ghosts'] = [ghostType(i+1) for i in range(options.numGhosts)]

    # Choose a display format
    if options.quietGraphics:
        import textDisplay
        args['display'] = textDisplay.NullGraphics()
    elif options.textGraphics:
        import textDisplay
        textDisplay.SLEEP_TIME = options.frameTime
        args['display'] = textDisplay.PacmanGraphics()
    else:
        import graphicsDisplay
        args['display'] = graphicsDisplay.PacmanGraphics(
            options.zoom, frameTime=options.frameTime)
    args['numGames'] = options.numGames
    args['record'] = options.record
    args['catchExceptions'] = options.catchExceptions
    args['timeout'] = options.timeout

    # Special case: recorded games don't use the runGames method or args structure
    if options.gameToReplay != None:
        print('Replaying recorded game %s.' % options.gameToReplay)
        import pickle
        import sys
        #f = open(options.gameToReplay) #maybe was for py2.x?
        f = open(options.gameToReplay, 'rb')
        try:
            recorded = pickle.load(f)
        finally:
            f.close()
        recorded['display'] = args['display']
        replayGame(**recorded)
        #sys.exit(0)
        #sys.exit()

    return args
예제 #22
0
def readCommand(argv):
    """
    Processes the command used to run pacman from the command line.
    """
    from optparse import OptionParser
    usageStr = """
    USAGE:      python pacman.py <options>
    EXAMPLES:   (1) python pacman.py
                    - starts an interactive game
                (2) python pacman.py --layout smallClassic --zoom 2
                OR  python pacman.py -l smallClassic -z 2
                    - starts an interactive game on a smaller board, zoomed in
    """
    parser = OptionParser(usageStr)

    parser.add_option('-n',
                      '--numGames',
                      dest='numGames',
                      type='int',
                      help=default('the number of GAMES to play'),
                      metavar='GAMES',
                      default=1)
    parser.add_option(
        '-l',
        '--layout',
        dest='layout',
        help=default('the LAYOUT_FILE from which to load the map layout'),
        metavar='LAYOUT_FILE',
        default='mediumClassic')
    parser.add_option(
        '-p',
        '--pacman',
        dest='pacman',
        help=default('the agent TYPE in the pacmanAgents module to use'),
        metavar='TYPE',
        default='KeyboardAgent')
    parser.add_option('-t',
                      '--textGraphics',
                      action='store_true',
                      dest='textGraphics',
                      help='Display output as text only',
                      default=False)
    parser.add_option('-q',
                      '--quietTextGraphics',
                      action='store_true',
                      dest='quietGraphics',
                      help='Generate minimal output and no graphics',
                      default=False)
    parser.add_option(
        '-g',
        '--ghosts',
        dest='ghost',
        help=default('the ghost agent TYPE in the ghostAgents module to use'),
        metavar='TYPE',
        default='RandomGhost')
    parser.add_option('-k',
                      '--numghosts',
                      type='int',
                      dest='numGhosts',
                      help=default('The maximum number of ghosts to use'),
                      default=4)
    parser.add_option('-z',
                      '--zoom',
                      type='float',
                      dest='zoom',
                      help=default('Zoom the size of the graphics window'),
                      default=1.0)
    parser.add_option(
        '-f',
        '--fixRandomSeed',
        action='store_true',
        dest='fixRandomSeed',
        help='Fixes the random seed to always play the same game',
        default=False)
    parser.add_option(
        '-r',
        '--recordActions',
        action='store_true',
        dest='record',
        help=
        'Writes game histories to a file (named by the time they were played)',
        default=False)
    parser.add_option('--replay',
                      dest='gameToReplay',
                      help='A recorded game file (pickle) to replay',
                      default=None)
    parser.add_option(
        '-a',
        '--agentArgs',
        dest='agentArgs',
        help=
        'Comma separated values sent to agent. e.g. "opt1=val1,opt2,opt3=val3"'
    )
    parser.add_option(
        '-x',
        '--numTraining',
        dest='numTraining',
        type='int',
        help=default('How many episodes are training (suppresses output)'),
        default=0)
    parser.add_option(
        '--frameTime',
        dest='frameTime',
        type='float',
        help=default('Time to delay between frames; <0 means keyboard'),
        default=0.1)
    parser.add_option(
        '-c',
        '--catchExceptions',
        action='store_true',
        dest='catchExceptions',
        help='Turns on exception handling and timeouts during games',
        default=False)
    parser.add_option(
        '--timeout',
        dest='timeout',
        type='int',
        help=default(
            'Maximum length of time an agent can spend computing in a single game'
        ),
        default=30)
    ####################################################################################################################
    # CPSC 481 - Add option to trigger our Ghost Agents...
    ####################################################################################################################
    parser.add_option(
        '-o',
        '--originalGhosts',
        action='store_true',
        dest='originalGhosts',
        help='Load CPSC481 original ghosts Blinky, Pinky, Inky, Clyde',
        default=False)
    parser.add_option('--blinky',
                      action='store_true',
                      dest='blinky',
                      help='Load CPSC481 Blinky',
                      default=False)
    parser.add_option('--pinky',
                      action='store_true',
                      dest='pinky',
                      help='Load CPSC481 Pinky',
                      default=False)
    parser.add_option('--inky',
                      action='store_true',
                      dest='inky',
                      help='Load CPSC481 Inky',
                      default=False)
    parser.add_option('--clyde',
                      action='store_true',
                      dest='clyde',
                      help='Load CPSC481 Clyde',
                      default=False)
    parser.add_option('-d',
                      '--drawPath',
                      action='store_true',
                      dest='drawPath',
                      help='Draw path to target',
                      default=False)
    parser.add_option(
        '--reinforcementLearning',
        dest='weights',
        help=
        'Comma separated values of initial weights for qLearning features. e.g. "1,2,3,4"'
    )
    ####################################################################################################################

    options, otherjunk = parser.parse_args(argv)
    if len(otherjunk) != 0:
        raise Exception('Command line input not understood: ' + str(otherjunk))
    args = dict()

    # Fix the random seed
    if options.fixRandomSeed: random.seed('cs188')

    # Choose a layout
    args['layout'] = layout.getLayout(options.layout)
    if args['layout'] == None:
        raise Exception("The layout " + options.layout + " cannot be found")

    # Choose a Pacman agent
    noKeyboard = options.gameToReplay == None and (options.textGraphics
                                                   or options.quietGraphics)
    pacmanType = loadAgent(options.pacman, noKeyboard)
    agentOpts = parseAgentArgs(options.agentArgs)
    if options.numTraining > 0:
        args['numTraining'] = options.numTraining
        if 'numTraining' not in agentOpts:
            agentOpts['numTraining'] = options.numTraining
    pacman = pacmanType(**agentOpts)  # Instantiate Pacman with agentArgs
    args['pacman'] = pacman

    # Don't display training games
    if 'numTrain' in agentOpts:
        options.numQuiet = int(agentOpts['numTrain'])
        options.numIgnore = int(agentOpts['numTrain'])

    ####################################################################################################################
    # CPSC 481 - We now have individual ghost classes now.... run them all with "-o" or individually i.e. "--blinky"
    ####################################################################################################################
    if options.originalGhosts is True:
        blinky = loadAgent('Blinky', noKeyboard)
        pinky = loadAgent('Pinky', noKeyboard)
        inky = loadAgent('Inky', noKeyboard)
        clyde = loadAgent('Clyde', noKeyboard)
        args['ghosts'] = [blinky(1), pinky(2), inky(3), clyde(4)]
    else:
        if options.blinky or options.pinky or options.inky or options.clyde:
            args['ghosts'] = []
            if options.blinky:
                args['ghosts'].append(
                    loadAgent('Blinky', noKeyboard)(len(args['ghosts']) + 1))
            if options.pinky:
                args['ghosts'].append(
                    loadAgent('Pinky', noKeyboard)(len(args['ghosts']) + 1))
            if options.inky:
                args['ghosts'].append(
                    loadAgent('Inky', noKeyboard)(len(args['ghosts']) + 1))
            if options.clyde:
                args['ghosts'].append(
                    loadAgent('Clyde', noKeyboard)(len(args['ghosts']) + 1))
        else:  # Default Ghosts...
            ghostType = loadAgent(options.ghost, noKeyboard)
            args['ghosts'] = [
                ghostType(i + 1) for i in range(options.numGhosts)
            ]

    import __main__
    if options.drawPath:
        __main__.__dict__['_drawPath'] = True
    else:
        __main__.__dict__['_drawPath'] = False

    # Attempt to bring in command line starting weights...
    if options.weights:
        __main__.__dict__['_weights'] = parseWeights(options.weights)
        __main__.__dict__['_reinforcementLearning'] = True
    else:
        __main__.__dict__['_reinforcementLearning'] = False

    ####################################################################################################################

    # Choose a display format
    if options.quietGraphics:
        import textDisplay
        args['display'] = textDisplay.NullGraphics()
    elif options.textGraphics:
        import textDisplay
        textDisplay.SLEEP_TIME = options.frameTime
        args['display'] = textDisplay.PacmanGraphics()
    else:
        import graphicsDisplay
        args['display'] = graphicsDisplay.PacmanGraphics(
            options.zoom, frameTime=options.frameTime)
    args['numGames'] = options.numGames
    args['record'] = options.record
    args['catchExceptions'] = options.catchExceptions
    args['timeout'] = options.timeout

    # Special case: recorded games don't use the runGames method or args structure
    if options.gameToReplay != None:
        print 'Replaying recorded game %s.' % options.gameToReplay
        import cPickle
        f = open(options.gameToReplay)
        try:
            recorded = cPickle.load(f)
        finally:
            f.close()
        recorded['display'] = args['display']
        replayGame(**recorded)
        sys.exit(0)

    return args
예제 #23
0
def runGames(layout,
             pacman,
             ghosts,
             display,
             numGames,
             record,
             numTraining=0,
             catchExceptions=False,
             timeout=30,
             endlessRun=False):
    import __main__
    __main__.__dict__['_display'] = display

    rules = ClassicGameRules(timeout)
    games = []

    if endlessRun == False:

        for i in range(numGames):
            beQuiet = i < numTraining
            if beQuiet:
                # Suppress output and graphics
                import textDisplay
                gameDisplay = textDisplay.NullGraphics()
                rules.quiet = True
            else:
                gameDisplay = display
                rules.quiet = False
            game = rules.newGame(layout, pacman, ghosts, gameDisplay, beQuiet,
                                 catchExceptions)
            game.run()
            if not beQuiet: games.append(game)

            if record:
                import time, cPickle
                fname = ('recorded-game-%d' % (i + 1)) + '-'.join(
                    [str(t) for t in time.localtime()[1:6]])
                f = file(fname, 'w')
                components = {'layout': layout, 'actions': game.moveHistory}
                cPickle.dump(components, f)
                f.close()

        if (numGames - numTraining) > 0:
            scores = [game.state.getScore() for game in games]
            wins = [game.state.isWin() for game in games]
            winRate = wins.count(True) / float(len(wins))
            print 'Average Score:', sum(scores) / float(len(scores))
            print 'Scores:       ', ', '.join([str(score) for score in scores])
            print 'Win Rate:      %d/%d (%.2f)' % (wins.count(True), len(wins),
                                                   winRate)
            print 'Record:       ', ', '.join([['Loss', 'Win'][int(w)]
                                               for w in wins])
    else:
        won = True
        total_score = 0
        level = 1
        while won:

            beQuiet = 1 < numTraining
            if beQuiet:
                # Suppress output and graphics
                import textDisplay
                gameDisplay = textDisplay.NullGraphics()
                rules.quiet = True
            else:
                gameDisplay = display
                rules.quiet = False

            runGenerator("randomTest.lay", level)
            layout = ilayout.getLayout("randomTest.lay")

            game = rules.newGame(layout, pacman, ghosts, gameDisplay, beQuiet,
                                 catchExceptions)
            game.state.data.score = total_score

            game.run()
            total_score += 500
            level += 1
            if not beQuiet: games.append(game)

            if record:
                import time, cPickle
                fname = ('recorded-game-%d' % (i + 1)) + '-'.join(
                    [str(t) for t in time.localtime()[1:6]])
                f = file(fname, 'w')
                components = {'layout': layout, 'actions': game.moveHistory}
                cPickle.dump(components, f)
                f.close()
            won = game.state.isWin()

    return games
예제 #24
0
def runGames(layout,
             pacman,
             ghosts,
             display,
             numGames,
             record,
             numTraining=0,
             catchExceptions=False,
             timeout=30):
    import __main__
    __main__.__dict__['_display'] = display

    rules = ClassicGameRules(timeout)
    games = []

    for i in range(numGames):
        beQuiet = i < numTraining
        if beQuiet:
            # Suppress output and graphics
            import textDisplay
            gameDisplay = textDisplay.NullGraphics()
            rules.quiet = True
        else:
            gameDisplay = display
            rules.quiet = False

        ######################################
        # CPSC 481 - Attempting to do work..
        ######################################
        if __main__.__dict__['_reinforcementLearning']:
            weights = __main__.__dict__['_weights']
            game = rules.newReinforcementGame(layout, pacman, ghosts,
                                              gameDisplay, weights, beQuiet,
                                              catchExceptions)
            updatedWeights = game.run(
            )  # decide whether updated weights should be within game.run() or elsewhere..
            sys.stdout.write('pacman.runGames() weights = ')
            # print updatedWeights
            for index, updatedWeight in enumerate(updatedWeights):
                if index == len(updatedWeights) - 1:
                    print updatedWeight
                else:
                    sys.stdout.write(str(updatedWeight) + ',')
            __main__.__dict__['_weights'] = updatedWeights
        else:
            game = rules.newGame(layout, pacman, ghosts, gameDisplay, beQuiet,
                                 catchExceptions)
            game.run()
        ######################################
        if not beQuiet: games.append(game)

        if record:
            import time, cPickle
            fname = ('recorded-game-%d' % (i + 1)) + '-'.join(
                [str(t) for t in time.localtime()[1:6]])
            f = file(fname, 'w')
            components = {'layout': layout, 'actions': game.moveHistory}
            cPickle.dump(components, f)
            f.close()

    if (numGames - numTraining) > 0:
        scores = [game.state.getScore() for game in games]
        wins = [game.state.isWin() for game in games]
        winRate = wins.count(True) / float(len(wins))
        print 'Average Score:', sum(scores) / float(len(scores))
        print 'Scores:       ', ', '.join([str(score) for score in scores])
        print 'Win Rate:      %d/%d (%.2f)' % (wins.count(True), len(wins),
                                               winRate)
        print 'Record:       ', ', '.join([['Loss', 'Win'][int(w)]
                                           for w in wins])

    return games
예제 #25
0
import importlib
import numpy as np

from pacman import *
import textDisplay

students = {
    'tokf': np.nan,
    'your_student_name': np.nan,
    'another_student': np.nan,
    'more_students': np.nan,
}

if __name__ == '__main__':
    args = readCommand(sys.argv[1:])  # Get game components based on input
    args['display'] = textDisplay.NullGraphics()
    args['numGames'] = 10

    for key in students:
        try:
            module = importlib.import_module(key + '.CompetitionPacman')
            args['pacman'] = module.CompAgent()
            # exec('from key import CompetitionPacman')
            out = runGames(**args)
            scores = [o.state.getScore() for o in out]

            students[key] = np.mean(scores)
        except ImportError as e:
            print('Error with', key)
            print(e)
예제 #26
0
def runGames(layouts,
             agents,
             agents_self,
             display,
             length,
             numGames,
             record,
             numTraining,
             pacmanTeamName,
             ghostTeamName,
             muteAgents=False,
             catchExceptions=True):

    rules = CaptureRules()
    games = []

    if numTraining > 0:
        print('Playing %d training games' % numTraining)

    for i, l in enumerate(layouts):
        beQuiet = i < numTraining
        if beQuiet:
            # Suppress output and graphics
            import textDisplay
            gameDisplay = textDisplay.NullGraphics()
            rules.quiet = True
        else:
            gameDisplay = display
            rules.quiet = False
        print("Running game with -l RANDOM%d  " % LAYOUT_SEED[i])
        g = rules.newGame(l, agents, gameDisplay, length, muteAgents,
                          catchExceptions)
        g.run()
        print("")
        if not beQuiet: games.append(g)

    print("")
    #print "Preliminary Phase 3 Autograder Results:"
    print(
        "*Results for sanity test against the staffbot: your agent should finish most games without crashing or timing out (time taken < 1200)*"
    )
    passedCount = 0
    points = 0
    # Output
    print("Regular order")
    for i, game in enumerate(games):
        if game.agentCrashed:
            myTime = 99999999
        else:
            myTime = game.length - game.state.data.timeleft

        currPassed = True
        targetTime = sanityScores[i]
        if myTime <= targetTime:
            passedCount += 1
        else:
            currPassed = False
        print(
            "Game {}: -l RANDOM{} \tTime taken: {} \tTarget time: {} \t {} \t{}"
            .format(i, LAYOUT_SEED[i], myTime, targetTime,
                    "AGENT CRASHED" if game.agentCrashed else "",
                    "" if currPassed else " (NOT PASSED)"))

    print("Sanity Test Summary: ", passedCount, "/", len(LAYOUT_SEED),
          "passed sanity test")
    print("Sanity Test Score: ", (passedCount / float(len(LAYOUT_SEED))), "/",
          "1.0")

    # if currPassed == len(LAYOUT_SEED):
    #   points += 1
    #   print "Passed sanity test with staffbot, received 1/1 on this part"

    # Self Team Testing (where you pair up with yourself)
    games_self = []
    for i, l in enumerate(layouts):
        beQuiet = i < numTraining
        if beQuiet:
            # Suppress output and graphics
            import textDisplay
            gameDisplay = textDisplay.NullGraphics()
            rules.quiet = True
        else:
            gameDisplay = display
            rules.quiet = False
        print("Running game with -l RANDOM%d  " % LAYOUT_SEED[i])
        g = rules.newGame(l, agents_self, gameDisplay, length, muteAgents,
                          catchExceptions)
        g.run()
        print("")
        if not beQuiet: games_self.append(g)

    print("")
    print(
        "*Results for self-play test, where your agent cooperates against a version of itself*"
    )
    passedCount_self = 0
    myTimes_self = []
    for i, game in enumerate(games_self):
        if game.agentCrashed:
            myTime = 99999999
        else:
            myTime = game.length - game.state.data.timeleft

        myTimes_self.append(myTime)
        currPassed = True
        print("Game {}: -l RANDOM{} \tTime taken: {} \t {} \t{}".format(
            i, LAYOUT_SEED[i], myTime,
            "AGENT CRASHED" if game.agentCrashed else "",
            "" if currPassed else " (NOT PASSED)"))

    bestEight = sorted(myTimes_self)[:8]
    bestEightAvg = sum(bestEight) / 8.0
    print("Self-Play Summary: Average time for best 8 games is {}".format(
        bestEightAvg))
    return games + games_self
예제 #27
0
def readCommand(argv):
    """
  Processes the command used to run pacman from the command line.
  """
    from optparse import OptionParser
    usageStr = """
  USAGE:      python pacman.py <options>
  EXAMPLES:   (1) python capture.py
                  - starts a game with two baseline agents
              (2) python capture.py --keys0
                  - starts a two-player interactive game where the arrow keys control agent 0, and all other agents are baseline agents
              (3) python capture.py -r baselineTeam -b myTeam
                  - starts a fully automated game where the red team is a baseline team and blue team is myTeam
  """
    parser = OptionParser(usageStr)

    parser.add_option('-r',
                      '--red',
                      help=default('Red team'),
                      default='baselineTeam')
    parser.add_option('-b',
                      '--blue',
                      help=default('Blue team'),
                      default='baselineTeam')
    parser.add_option('--red-name',
                      help=default('Red team name'),
                      default='Red')
    parser.add_option('--blue-name',
                      help=default('Blue team name'),
                      default='Blue')
    parser.add_option('--redOpts',
                      help=default('Options for red team (e.g. first=keys)'),
                      default='')
    parser.add_option('--blueOpts',
                      help=default('Options for blue team (e.g. first=keys)'),
                      default='')
    parser.add_option('--keys0',
                      help='Make agent 0 (first red player) a keyboard agent',
                      action='store_true',
                      default=False)
    parser.add_option('--keys1',
                      help='Make agent 1 (second red player) a keyboard agent',
                      action='store_true',
                      default=False)
    parser.add_option('--keys2',
                      help='Make agent 2 (first blue player) a keyboard agent',
                      action='store_true',
                      default=False)
    parser.add_option(
        '--keys3',
        help='Make agent 3 (second blue player) a keyboard agent',
        action='store_true',
        default=False)
    parser.add_option(
        '-l',
        '--layout',
        dest='layout',
        help=default(
            'the LAYOUT_FILE from which to load the map layout; use RANDOM for a random maze; use RANDOM<seed> to use a specified random seed, e.g., RANDOM23'
        ),
        metavar='LAYOUT_FILE',
        default='defaultCapture')
    parser.add_option('-t',
                      '--textgraphics',
                      action='store_true',
                      dest='textgraphics',
                      help='Display output as text only',
                      default=False)

    parser.add_option('-q',
                      '--quiet',
                      action='store_true',
                      help='Display minimal output and no graphics',
                      default=False)

    parser.add_option('-Q',
                      '--super-quiet',
                      action='store_true',
                      dest="super_quiet",
                      help='Same as -q but agent output is also suppressed',
                      default=False)

    parser.add_option('-z',
                      '--zoom',
                      type='float',
                      dest='zoom',
                      help=default('Zoom in the graphics'),
                      default=1)
    parser.add_option('-i',
                      '--time',
                      type='int',
                      dest='time',
                      help=default('TIME limit of a game in moves'),
                      default=1200,
                      metavar='TIME')
    parser.add_option('-n',
                      '--numGames',
                      type='int',
                      help=default('Number of games to play'),
                      default=1)
    parser.add_option(
        '-f',
        '--fixRandomSeed',
        action='store_true',
        help='Fixes the random seed to always play the same game',
        default=False)
    parser.add_option(
        '--record',
        action='store_true',
        help=
        'Writes game histories to a file (named by the time they were played)',
        default=False)
    parser.add_option('--replay',
                      default=None,
                      help='Replays a recorded game file.')
    parser.add_option('--replay-delay',
                      type='float',
                      dest='delay',
                      help=default('Delay step in a replay'),
                      default=0.03)
    parser.add_option(
        '-x',
        '--numTraining',
        dest='numTraining',
        type='int',
        help=default('How many episodes are training (suppresses output)'),
        default=0)
    parser.add_option('-c',
                      '--catchExceptions',
                      action='store_true',
                      default=False,
                      help='Catch exceptions and enforce time limits')

    options, otherjunk = parser.parse_args(argv)
    assert len(otherjunk) == 0, "Unrecognized options: " + str(otherjunk)
    args = dict()

    # Choose a display format
    #if options.pygame:
    #   import pygameDisplay
    #    args['display'] = pygameDisplay.PacmanGraphics()
    if options.textgraphics:
        import textDisplay
        args['display'] = textDisplay.PacmanGraphics()
    elif options.quiet:
        import textDisplay
        args['display'] = textDisplay.NullGraphics()
    elif options.super_quiet:
        import textDisplay
        args['display'] = textDisplay.NullGraphics()
        args['muteAgents'] = True
    else:
        import captureGraphicsDisplay
        # Hack for agents writing to the display
        captureGraphicsDisplay.FRAME_TIME = 0
        args['display'] = captureGraphicsDisplay.PacmanGraphics(options.red,
                                                                options.blue,
                                                                options.zoom,
                                                                0,
                                                                capture=True)
        import __main__
        __main__.__dict__['_display'] = args['display']

    args['redTeamName'] = options.red_name
    args['blueTeamName'] = options.blue_name

    if options.fixRandomSeed: random.seed('cs188')

    # Special case: recorded games don't use the runGames method or args structure
    if options.replay != None:
        print 'Replaying recorded game %s.' % options.replay
        import cPickle
        recorded = cPickle.load(open(options.replay))
        recorded['display'] = args['display']
        recorded['delay'] = options.delay
        replayGame(**recorded)
        sys.exit(0)

    # Choose a pacman agent
    redArgs, blueArgs = parseAgentArgs(options.redOpts), parseAgentArgs(
        options.blueOpts)
    if options.numTraining > 0:
        redArgs['numTraining'] = options.numTraining
        blueArgs['numTraining'] = options.numTraining
    nokeyboard = options.textgraphics or options.quiet or options.numTraining > 0
    print '\nRed team %s with %s:' % (options.red, redArgs)
    redAgents = loadAgents(True, options.red, nokeyboard, redArgs)
    print '\nBlue team %s with %s:' % (options.blue, blueArgs)
    blueAgents = loadAgents(False, options.blue, nokeyboard, blueArgs)
    args['agents'] = sum([list(el) for el in zip(redAgents, blueAgents)],
                         [])  # list of agents

    if None in blueAgents or None in redAgents:
        if None in blueAgents:
            print '\nBlue team failed to load!\n'
        if None in redAgents:
            print '\nRed team failed to load!\n'
        raise Exception('No teams found!')

    numKeyboardAgents = 0
    for index, val in enumerate(
        [options.keys0, options.keys1, options.keys2, options.keys3]):
        if not val: continue
        if numKeyboardAgents == 0:
            agent = keyboardAgents.KeyboardAgent(index)
        elif numKeyboardAgents == 1:
            agent = keyboardAgents.KeyboardAgent2(index)
        else:
            raise Exception('Max of two keyboard agents supported')
        numKeyboardAgents += 1
        args['agents'][index] = agent

    # Choose a layout
    import layout
    layouts = []
    for i in range(options.numGames):
        if options.layout == 'RANDOM':
            l = layout.Layout(randomLayout().split('\n'))
        elif options.layout.startswith('RANDOM'):
            l = layout.Layout(
                randomLayout(int(options.layout[6:])).split('\n'))
        elif options.layout.lower().find('capture') == -1:
            raise Exception('You must use a capture layout with capture.py')
        else:
            l = layout.getLayout(options.layout)
        if l == None:
            raise Exception("The layout " + options.layout +
                            " cannot be found")

        layouts.append(l)

    args['layouts'] = layouts
    args['length'] = options.time
    args['numGames'] = options.numGames
    args['numTraining'] = options.numTraining
    args['record'] = options.record
    args['catchExceptions'] = options.catchExceptions
    return args
예제 #28
0
def readCommand(argv):
    """
  Processes the command used to run pacman from the command line.
  """
    from optparse import OptionParser
    usageStr = """
  USAGE:      python pacman.py <options>
  EXAMPLES:   (1) python pacman.py
                  - starts an interactive game
              (2) python pacman.py --layout smallClassic --zoom 2
              OR  python pacman.py -l smallClassic -z 2
                  - starts an interactive game on a smaller board, zoomed in
  """
    parser = OptionParser(usageStr)

    parser.add_option('-n',
                      '--numGames',
                      dest='numGames',
                      type='int',
                      help=default('the number of GAMES to play'),
                      metavar='GAMES',
                      default=1)
    parser.add_option(
        '-l',
        '--layout',
        dest='layouts',
        help=default('the LAYOUT_FILE from which to load the map layout'),
        metavar='LAYOUT_FILE',
        default=None)
    parser.add_option(
        '-p',
        '--pacman',
        dest='pacman',
        help=default('the agent TYPE in the competitionAgents module to use'),
        metavar='TYPE',
        default='KeyboardAgent')
    parser.add_option('--name',
                      metavar='NAME',
                      dest='name',
                      help=default('Name for this agent'),
                      default=None)
    parser.add_option('-t',
                      '--textGraphics',
                      action='store_true',
                      dest='textGraphics',
                      help='Display output as text only',
                      default=False)
    parser.add_option('-q',
                      '--quietTextGraphics',
                      action='store_true',
                      dest='quietGraphics',
                      help='Generate minimal output and no graphics',
                      default=False)
    parser.add_option(
        '-g',
        '--ghosts',
        dest='ghost',
        help=default('the ghost agent TYPE in the ghostAgents module to use'),
        metavar='TYPE',
        default=None)
    parser.add_option('-k',
                      '--numghosts',
                      type='int',
                      dest='numGhosts',
                      help=default('The maximum number of ghosts to use'),
                      default=6)
    parser.add_option('-z',
                      '--zoom',
                      type='float',
                      dest='zoom',
                      help=default('Zoom the size of the graphics window'),
                      default=1.0)
    parser.add_option(
        '-f',
        '--fixRandomSeed',
        action='store_true',
        dest='fixRandomSeed',
        help='Fixes the random seed to always play the same game',
        default=False)
    parser.add_option(
        '-r',
        '--recordActions',
        action='store_true',
        dest='record',
        help=
        'Writes game histories to a file (named by the time they were played)',
        default=False)
    parser.add_option('--replay',
                      dest='gameToReplay',
                      help='A recorded game file (pickle) to replay',
                      default=None)
    parser.add_option(
        '-a',
        '--agentArgs',
        dest='agentArgs',
        help=
        'Comma separated values sent to agent. e.g. "opt1=val1,opt2,opt3=val3"'
    )
    parser.add_option(
        '-x',
        '--numTraining',
        dest='numTraining',
        type='int',
        help=default('How many episodes are training (suppresses output)'),
        default=0)
    parser.add_option(
        '--frameTime',
        dest='frameTime',
        type='float',
        help=default('Time to delay between frames; <0 means keyboard'),
        default=0.1)
    parser.add_option('-i',
                      '--time',
                      type='int',
                      dest='time',
                      help=default('TIME limit of a game in moves'),
                      default=3000,
                      metavar='TIME')
    parser.add_option(
        '-c',
        '--catchExceptions',
        action='store_true',
        dest='catchExceptions',
        help='Turns on exception handling and timeouts during games',
        default=True)
    parser.add_option(
        '--timeout',
        dest='timeout',
        type='int',
        help=default(
            'Maximum length of time an agent can spend computing in a single game'
        ),
        default=3000)

    options, otherjunk = parser.parse_args(argv)
    if len(otherjunk) != 0:
        raise Exception('Command line input not understood: ' + str(otherjunk))
    args = dict()

    # Fix the random seed
    if options.fixRandomSeed:
        print("Random seed reset")
        random.seed('cs188')
        random.getstate()

    # Choose a layout
    if options.layouts == None:
        lays = [
            f for f in os.listdir('./layouts')
            if os.path.isfile(os.path.join("./layouts", f))
        ]
        lays = [f for f in lays if re.match(r'level.*lay$', f)]
        lays.sort()
    else:
        lays = options.layouts.split(
            ',')  # split if multiple layouts specified
    print("Layouts : ", lays)
    # now load these layouts
    #if options.numGames==None: options.numGames=len(lays) # if not given play every layout
    layobj = []
    for i in range(len(lays)):
        layobj.append(layout.getLayout(lays[i]))
        if layobj[i] == None:
            raise Exception("The layout " + lays[i] + " cannot be found")
    args['layouts'] = layobj

    # Choose a Pacman agent
    noKeyboard = options.gameToReplay == None and (options.textGraphics
                                                   or options.quietGraphics)
    pacmanType = loadAgent(options.pacman, noKeyboard)
    agentOpts = parseAgentArgs(options.agentArgs)
    if options.numTraining > 0:
        args['numTraining'] = options.numTraining
        if 'numTraining' not in agentOpts:
            agentOpts['numTraining'] = options.numTraining
    pacman = pacmanType(**agentOpts)  # Instantiate Pacman with agentArgs
    args['pacman'] = pacman

    # Don't display training games
    if 'numTrain' in agentOpts:
        options.numQuiet = int(agentOpts['numTrain'])
        options.numIgnore = int(agentOpts['numTrain'])

    # Choose a ghost agent
    if options.ghost == None:
        import ghostAgents
        ghostnames = [f for f in dir(ghostAgents) if re.match(r'.*Ghost$', f)]
        ghostnames.reverse()
    else:
        ghostnames = options.ghost.split(
            ',')  # split if multiple layouts specified
    print("Ghosts : ", ghostnames)
    ghostobj = []
    for i in range(len(ghostnames)):
        ghostclass = loadAgent(ghostnames[i],
                               noKeyboard)  # load abstract class
        ghostobj.append(ghostclass(i + 1))  # generate instantiation
    args['ghosts'] = ghostobj
    args['numGhosts'] = options.numGhosts

    #ghostType = loadAgent(options.ghost, noKeyboard)
    #args['ghosts'] = [ghostType( i+1 ) for i in range( options.numGhosts )]

    # Choose a display format
    if options.quietGraphics:
        import textDisplay
        args['display'] = textDisplay.NullGraphics()
    elif options.textGraphics:
        import textDisplay
        textDisplay.SLEEP_TIME = options.frameTime
        args['display'] = textDisplay.PacmanGraphics()
    else:
        import graphicsDisplay
        args['display'] = graphicsDisplay.PacmanGraphics(
            options.zoom, frameTime=options.frameTime)
    args['numGames'] = options.numGames
    args['length'] = options.time
    args['record'] = options.record
    args['catchExceptions'] = options.catchExceptions
    args['timeout'] = options.timeout
    args['name'] = options.name

    # Special case: recorded games don't use the runGames method or args structure
    if options.gameToReplay != None:
        print('Replaying recorded game %s.' % options.gameToReplay)
        import pickle
        f = open(options.gameToReplay)
        try:
            recorded = pickle.load(f)
        finally:
            f.close()
        recorded['display'] = args['display']
        replayGame(**recorded)
        sys.exit(0)

    return args
예제 #29
0
def readCommand( argv ):

    from optparse import OptionParser
    usageStr = """
    USAGE:      python pacman.py <options>
    EXAMPLES:   (1) python pacman.py
                    - starts an interactive game
                (2) python pacman.py --layout smallClassic --zoom 2
                OR  python pacman.py -l smallClassic -z 2
                    - starts an interactive game on a smaller board, zoomed in
    """
    parser = OptionParser(usageStr)

    parser.add_option('-n', '--numGames', dest='numGames', type='int',
                      help=default('the number of GAMES to play'), metavar='GAMES', default=1)
    parser.add_option('-l', '--layout', dest='layout',
                      help=default('the LAYOUT_FILE from which to load the map layout'),
                      metavar='LAYOUT_FILE', default='mediumClassic')
    parser.add_option('-p', '--pacman', dest='pacman',
                      help=default('the agent TYPE in the pacmanAgents module to use'),
                      metavar='TYPE', default='KeyboardAgent')
    parser.add_option('-t', '--textGraphics', action='store_true', dest='textGraphics',
                      help='Display output as text only', default=False)
    parser.add_option('-q', '--quietTextGraphics', action='store_true', dest='quietGraphics',
                      help='Generate minimal output and no graphics', default=False)
    parser.add_option('-g', '--ghosts', dest='ghost',
                      help=default('the ghost agent TYPE in the ghostAgents module to use'),
                      metavar = 'TYPE', default='RandomGhost')
    parser.add_option('-k', '--numghosts', type='int', dest='numGhosts',
                      help=default('The maximum number of ghosts to use'), default=4)
    parser.add_option('-z', '--zoom', type='float', dest='zoom',
                      help=default('Zoom the size of the graphics window'), default=1.0)
    parser.add_option('-f', '--fixRandomSeed', action='store_true', dest='fixRandomSeed',
                      help='Fixes the random seed to always play the same game', default=False)
    parser.add_option('-r', '--recordActions', action='store_true', dest='record',
                      help='Writes game histories to a file (named by the time they were played)', default=False)
    parser.add_option('--replay', dest='gameToReplay',
                      help='A recorded game file (pickle) to replay', default=None)
    parser.add_option('-a','--agentArgs',dest='agentArgs',
                      help='Comma separated values sent to agent. e.g. "opt1=val1,opt2,opt3=val3"')
    parser.add_option('-x', '--numTraining', dest='numTraining', type='int',
                      help=default('How many episodes are training (suppresses output)'), default=0)
    parser.add_option('--frameTime', dest='frameTime', type='float',
                      help=default('Time to delay between frames; <0 means keyboard'), default=0.1)
    parser.add_option('-c', '--catchExceptions', action='store_true', dest='catchExceptions',
                      help='Turns on exception handling and timeouts during games', default=False)
    parser.add_option('--timeout', dest='timeout', type='int',
                      help=default('Maximum length of time an agent can spend computing in a single game'), default=30)

    options, otherjunk = parser.parse_args(argv)
    if len(otherjunk) != 0:
        raise Exception('Command line input not understood: ' + str(otherjunk))
    args = dict()


    if options.fixRandomSeed: random.seed('Intro to AI')


    args['layout'] = layout.getLayout( options.layout )
    if args['layout'] == None: raise Exception("The layout " + options.layout + " cannot be found")


    noKeyboard = options.gameToReplay == None and (options.textGraphics or options.quietGraphics)
    pacmanType = loadAgent(options.pacman, noKeyboard)
    agentOpts = parseAgentArgs(options.agentArgs)
    if options.numTraining > 0:
        args['numTraining'] = options.numTraining
        if 'numTraining' not in agentOpts: agentOpts['numTraining'] = options.numTraining
    pacman = pacmanType(**agentOpts)
    args['pacman'] = pacman


    if 'numTrain' in agentOpts:
        options.numQuiet = int(agentOpts['numTrain'])
        options.numIgnore = int(agentOpts['numTrain'])


    ghostType = loadAgent(options.ghost, noKeyboard)
    args['ghosts'] = [ghostType( i+1 ) for i in range( options.numGhosts )]


    if options.quietGraphics:
        import textDisplay
        args['display'] = textDisplay.NullGraphics()
    elif options.textGraphics:
        import textDisplay
        textDisplay.SLEEP_TIME = options.frameTime
        args['display'] = textDisplay.PacmanGraphics()
    else:
        import graphicsDisplay
        args['display'] = graphicsDisplay.PacmanGraphics(options.zoom, frameTime = options.frameTime)
    args['numGames'] = options.numGames
    args['record'] = options.record
    args['catchExceptions'] = options.catchExceptions
    args['timeout'] = options.timeout


    if options.gameToReplay != None:
        print ('Replaying recorded game %s.' % options.gameToReplay)

        import cPickle
        f = open(options.gameToReplay)
        try: recorded = cPickle.load(f)
        finally: f.close()
        recorded['display'] = args['display']
        replayGame(**recorded)
        sys.exit(0)

    return args
예제 #30
0
def runGames(layout_name,
             pacman,
             ghosts,
             display,
             numGames,
             record,
             numTraining=0,
             catchExceptions=False,
             timeout=30):
    import __main__
    __main__.__dict__['_display'] = display
    rules = ClassicGameRules(timeout)
    layout2 = layout.getLayout(layout_name)
    pacman.selfTesting = False
    pacman.currentTrainingEpisode = 0
    games = []
    testScores = []
    testWins = []
    for i in range(numGames):
        beQuiet = i < numTraining
        testing = i >= numTraining
        # beQuiet = i < 100
        beQuiet = False
        # beQuiet = True
        if beQuiet:
            # Suppress output and graphics
            import textDisplay
            gameDisplay = textDisplay.NullGraphics()
            # rules.quiet = True
        else:
            # import textDisplay
            # gameDisplay = textDisplay.NullGraphics()
            gameDisplay = display
            rules.quiet = False

        game = rules.newGame(layout2, pacman, ghosts, gameDisplay, beQuiet,
                             testing, catchExceptions)
        game.run()

        if not beQuiet: games.append(game)

        if record:
            import time, cPickle
            fname = ('recorded-game-%d' % (i + 1)) + '-'.join(
                [str(t) for t in time.localtime()[1:6]])
            f = file(fname, 'w')
            components = {'layout': layout, 'actions': game.moveHistory}
            cPickle.dump(components, f)
            f.close()

        if (i % testInterval == 0) and i > 0:
            pacman.selfTesting = True
            pacman.currentTrainingEpisode = i

            try:
                epsilonCopy = pacman.arbitrator.epsilon
                alphaCopy = pacman.arbitrator.alpha
                pacman.arbitrator.epsilon = 0.00
                pacman.arbitrator.alpha = 0.0
            except Exception, data:
                sys.exc_clear()
                epsilonCopy = pacman.epsilon
                alphaCopy = pacman.alpha
                pacman.epsilon = 0.00
                pacman.alpha = 0.0

            try:
                arbitratorEpsilonCopy = pacman.arbitratorEpsilon
                pacman.arbitratorEpsilon = 0.01
            except Exception, data:
                sys.exc_clear()
                pass

            scores = []
            sumScore = 0.0
            winCount = 0
            for j in range(testRepitition):
                game = rules.newGame(layout2, pacman, ghosts, gameDisplay,
                                     True, True, catchExceptions)
                game.run()
                sumScore += game.state.getScore()
                scores.append(round(game.state.getScore(), 2))
                if game.state.isWin():
                    winCount += 1
            print "Testing at", i, scores, "Won", winCount
            testScores.append(round(sumScore / testRepitition, 2))
            testWins.append(winCount)
            print testScores
            print testWins
            pacman.selfTesting = False

            try:
                pacman.arbitrator.epsilon = epsilonCopy
                pacman.arbitrator.alpha = alphaCopy
            except Exception, data:
                sys.exc_clear()
                pacman.epsilon = epsilonCopy
                pacman.alpha = alphaCopy