def execute(self, grades, moduleDict, solutionDict): grades.addMessage('Testing Deep Q Network...') # Load Pacman Agent nographics = False pacmanType = loadAgent("PacmanDeepQAgent", nographics) pacman = pacmanType(self.layout) # Load Ghost Agent ghostType = loadAgent("RandomGhost", nographics) numghosts = 1 ghosts = [ghostType(i + 1) for i in range(numghosts)] numTraining = pacman.model.numTrainingGames # Set by student numGames = numTraining + self.numEvalGames record = False games = runGames(self.layout, self.horizon, pacman, ghosts, self.display, numGames, record, numTraining=numTraining, catchExceptions=False, timeout=30) scores = [game.state.getScore() for game in games] wins = [game.state.isWin() for game in games] winRate = wins.count(True) / float(len(wins)) if winRate < self.winThresh: grades.addMessage( 'FAIL:\nWinRate = {} < {} threshold for full credit'.format( winRate, self.winThresh)) return False elif winRate < self.winThreshEC: grades.addMessage( 'PASS:\nWinRate = {} >= {} threshold for full credit'.format( winRate, self.winThresh)) grades.assignFullCredit() return True else: grades.addMessage( 'PASS:\nWinRate = {} >= {} threshold for extra credit'.format( winRate, self.winThreshEC)) grades.assignFullCredit() grades.addPoints(1) return True
def eval_genomes_single(genomes, config): for id, genome in genomes: genome.fitness = 0 nn_model = neat.nn.FeedForwardNetwork.create(genome, config) pacmanType = pacman.loadAgent("NEATAgent", True) cmd_line_args['pacman'] = pacmanType(nn_model=nn_model) cmd_line_args['display'] = textDisplay.NullGraphics() #cmd_line_args['display'] = graphicsDisplay.PacmanGraphics() games = pacman.runGames(**cmd_line_args) for game in games: genome.fitness += game.state.getScore()
def eval_genomes(genome, config): genome.fitness = 0 nn_model = neat.nn.FeedForwardNetwork.create(genome, config) pacmanType = pacman.loadAgent("NEATAgent", True) cmd_line_args['pacman'] = pacmanType(nn_model=nn_model) cmd_line_args['display'] = textDisplay.NullGraphics() # cmd_line_args['display'] = graphicsDisplay.PacmanGraphics() games = pacman.runGames(**cmd_line_args) result = 0 for game in games: result += game.state.getScore() return float(result) / float(len(games))
def load_setup(options): """ Load the setup for training the agent """ layout_ = layout.getLayout(options.layout) if layout is None: raise Exception("The layout " + options.layout + " cannot be found") # Initialize a Pacman agent pacmanType = loadAgent(options.pacman, nographics=False) pacmanOpts = parseAgentArgs(options.agentArgs) pacman = pacmanType( **pacmanOpts ) # Instantiate Pacman with depth=2 # To Do: allow depth as an argument # Initialize Ghost agents ghostType = loadAgent(options.ghost, nographics=False) ghostOpts = parseAgentArgs(options.ghostArgs) ghosts = [ ghostType(index=i + 1, numTraining=options.numTraining, **ghostOpts) for i in range(options.numGhosts) ] return layout_, pacman, ghosts
def evaluate_candidates(candidates, args): nn_model = args["nn_model"] cmd_line_args = args["cmd_line_args"] candidates_fitness = [] for candidate in candidates: nn_model.set_weights(candidate) pacmanType = pacman.loadAgent("BioAgent", True) cmd_line_args['pacman'] = pacmanType(nn_model=nn_model) cmd_line_args['display'] = textDisplay.NullGraphics() #cmd_line_args['display'] = graphicsDisplay.PacmanGraphics() games = pacman.runGames(**cmd_line_args) candidate_fitness = 0 for game in games: candidate_fitness += game.state.getScore() candidates_fitness.append(candidate_fitness) print(candidates_fitness) return candidates_fitness
def load_ghosts(options, filename): """ Load the ghosts from file :param options: options from commandline :param filename: pkl file that stores the agent :return: ghosts set from pkl file """ # Initialize ghosts given type ghostType = loadAgent(options.ghost, nographics=False) ghostOpts = parseAgentArgs(options.ghostArgs) ghosts = [ ghostType(index=i + 1, numTraining=options.numTraining, **ghostOpts) for i in range(options.numGhosts) ] # Load ghosts from file and turn off training for ghost in ghosts: ghost.turnoff_training() ghost.load_from_file(filename) return ghosts
# Add a stdout reporter to show progress in the terminal. p.add_reporter(neat.StdOutReporter(True)) stats = neat.StatisticsReporter() p.add_reporter(stats) p.add_reporter(neat.Checkpointer(5)) # Run for up to 300 generations (parallel) pe = neat.ParallelEvaluator(4, eval_genomes) winner = p.run(pe.evaluate, 1000) #winner = p.run(eval_genomes_single, 100) # Display the winning genome. print('\nBest genome:\n{!s}'.format(winner)) # Save the winner to disk with open('winner-feedforward.out', 'wb') as f: pickle.dump(winner, f) # Show output of the most fit genome against training data. print('\nOutput:') winner_net = neat.nn.FeedForwardNetwork.create(winner, config) # Load the final agent and run a few games with it pacmanType = pacman.loadAgent("NEATAgent", True) cmd_line_args['pacman'] = pacmanType(nn_model=winner_net) cmd_line_args['display'] = textDisplay.NullGraphics() #cmd_line_args['display'] = graphicsDisplay.PacmanGraphics() cmd_line_args['numGames'] = 10 games = pacman.runGames(**cmd_line_args) pass
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', default=10, help=default('the number of GAMES to play'), metavar='GAMES') parser.add_option('--numDisplay', dest='numDisplay', type='int', default=10, help=default('the number of GAMES to display')) parser.add_option( '-l', '--layout', dest='layout', default='small', help=default( 'the index of LAYOUT_FILE from which to load the map layout')) parser.add_option( '-p', '--pacman', dest='pacman', default='MC', help=default('the agent in the pacmanAgents module to use')) parser.add_option('-a', '--all', dest='testAll', action='store_true', default=False, help=default('if we want to test all the parameters')) parser.add_option( '-s', '--showPlot', dest='showPlot', action='store_true', default=False, help=default('If we want to show the plot of the result')) parser.add_option('--norun', dest='noRun', action='store_true', default=False, help=default('If run the game')) parser.add_option('--savegif', dest='savegif', action='store_true', default=False, help=default('If save the gif files')) options, otherjunk = parser.parse_args(argv) if len(otherjunk) != 0: raise Exception('Command line input not understood: ' + str(otherjunk)) args = dict() ghostType = loadAgent('RandomGhost', False) args['ghosts'] = [ghostType(i + 1) for i in range(10)] args['numGames'] = options.numGames args['numGamesToDisplay'] = options.numDisplay argsList = [] if options.testAll: for layout in layouts: for pacman in pacmans.values(): argsTmp = copy.deepcopy(args) argsTmp['layoutName'] = layout argsTmp['pacman'] = pacman argsList.append(argsTmp) else: args['layoutName'] = options.layout args['pacman'] = pacmans[options.pacman] argsList.append(args) return (argsList, options)
if __name__ == '__main__': cmd_line_args = pacman.readCommand( sys.argv[1:]) # Get game components based on input nn_model = generate_model() prng = Random() prng.seed(time()) ea = ec.DEA(prng) ea.terminator = ec.terminators.evaluation_termination final_pop = ea.evolve(generator=generate_candidate, evaluator=evaluate_candidates, pop_size=1000, maximize=True, max_evaluations=1000, mutation_rate=0.3, crossover_rate=1, nn_model=nn_model, num_elites=1, mp_num_cpus=4, cmd_line_args=cmd_line_args) best = max(final_pop) nn_model.set_weights(best.candidate) print("Final candidate:", best.candidate) pacmanType = pacman.loadAgent("BioAgent", True) cmd_line_args['pacman'] = pacmanType(nn_model=nn_model) cmd_line_args['display'] = graphicsDisplay.PacmanGraphics() cmd_line_args['numGames'] = 10 games = pacman.runGames(**cmd_line_args) pass
def read_command(argv): from optparse import OptionParser usage_str = """ USAGE: python experiment.py <options> EXAMPLES: (1) python experiment.py --layout testRL --pacman=AlphaBetaAgent --ghosts=QLearningGhost --numTraining=10 """ parser = OptionParser(usage_str) 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( '-a', '--agentArgs', dest='agentArgs', help= 'Comma separated values sent to agent. e.g. "opt1=val1,opt2,opt3=val3"' ) parser.add_option( '-g', '--ghosts', dest='ghost', help=default('the ghost agent TYPE in the ghostAgents module to use'), metavar='TYPE', default='QLearningGhost') parser.add_option( '-x', '--numTraining', dest='numTraining', type='int', help=default('How many episodes are training (suppresses output)'), default=10) parser.add_option('-k', '--numghosts', type='int', dest='numGhosts', help=default('The maximum number of ghosts to use'), default=4) 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( '--ghostArgs', dest='ghostArgs', help= 'Comma separated values sent to ghost. e.g. "opt1=val1,opt2,opt3=val3"' ) 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'] is None: raise Exception("The layout " + options.layout + " cannot be found") # Initialize a Pacman agent pacmanType = loadAgent(options.pacman, nographics=False) pacmanOpts = parseAgentArgs(options.agentArgs) pacman = pacmanType( **pacmanOpts ) # Instantiate Pacman with depth=2 # To Do: allow depth as an argument args['pacman'] = pacman # Initialize Ghost agents ghostType = loadAgent(options.ghost, nographics=False) ghostOpts = parseAgentArgs(options.ghostArgs) args['ghosts'] = [ ghostType(index=i + 1, numTraining=options.numTraining, **ghostOpts) for i in range(options.numGhosts) ] # Set number of training episodes for experiment args['numTraining'] = options.numTraining return args, options