def expectimax(self, gameState, depth, agentIndex): if depth == 0 or gameState.isWin() or gameState.isLose(): return self.evaluationFunction(gameState) nextAgent = (agentIndex + 1) % gameState.getNumAgents() nextDepth = depth if agentIndex == (gameState.getNumAgents() - 1): nextDepth = depth - 1 if agentIndex == 0: actions = gameState.getLegalActions(agentIndex) successors = [ gameState.generateSuccessor(agentIndex, action) for action in actions ] scores = [ self.expectimax(state, nextDepth, nextAgent) for state in successors ] return max(scores) else: weighted_average = 0.0 ghost = ghostAgents.DirectionalGhost(agentIndex) dist = ghost.getDistribution(gameState) for action, prob in dist.items(): successor = gameState.generateSuccessor(agentIndex, action) score = self.expectimax(successor, nextDepth, nextAgent) weighted_average += prob * score return weighted_average
def buildRandomExperience(self, layoutName, displayActive=False, limit=None): import pacmanAgents, ghostAgents from pacman import ClassicGameRules import layout theLayout = layout.getLayout(layoutName) if theLayout == None: raise Exception("The layout " + layoutName + " cannot be found") display = None # Choose a display format if not displayActive: import textDisplay display = textDisplay.NullGraphics() else: import graphicsDisplay display = graphicsDisplay.PacmanGraphics(frameTime=0.01) counter = 0 finished = False while not finished: rules = ClassicGameRules() agents = [pacmanAgents.RandomAgent()] + [ ghostAgents.DirectionalGhost(i + 1) for i in range(theLayout.getNumGhosts()) ] game = rules.newGame(theLayout, agents[0], agents[1:], display) currentState = game.state display.initialize(currentState.data) while not (currentState.isWin() or currentState.isLose()): action = agents[0].getAction(currentState) newState = util.getSuccessor(agents, display, currentState, action) reward = newState.data.score - currentState.data.score self.remember(currentState, action, reward, newState) currentState = newState counter += 1 if counter % 100 == 0: self.persist() if counter % 2000 == 0: print("Explored " + str(counter) + " states") if limit is not None and counter > limit: finished = True display.finish() self.persist() self.replayMemory.close() print("Done")
def makeGame(displayActive): """ Make a game :param displayActive: True or False to indicate if the display should be active :return: game, agents, display, rules """ if not displayActive: import textDisplay display = textDisplay.NullGraphics() else: import graphicsDisplay display = graphicsDisplay.PacmanGraphics(frameTime=0.01) theLayout = layout.getLayout(layoutName) if theLayout == None: raise Exception("The layout " + layoutName + " cannot be found") rules = ClassicGameRules() agents = [trainedAgent] \ + [ghostAgents.DirectionalGhost(i + 1) for i in range(theLayout.getNumGhosts())] game = rules.newGame(theLayout, agents[0], agents[1:], display) return game
def replayGame(layout, actions, display): import pacmanAgents, ghostAgents rules = ClassicGameRules() agents = [pacmanAgents.GreedyAgent()] + [ ghostAgents.RandomGhost(i + 1) for i in range(layout.getNumGhosts() - 1) ] + [ghostAgents.DirectionalGhost(ayout.getNumGhosts() + 1)] game = rules.newGame(layout, agents[0], agents[1:], display) state = game.state display.initialize(state.data) for action in actions: # Execute the action state = state.generateSuccessor(*action) # Change the display display.update(state.data) # Allow for game specific conditions (winning, losing, etc.) rules.process(state, game) display.finish()
def buildExperience(self, layoutName, displayActive=False, limit=None): import pacmanAgents, ghostAgents from pacman import ClassicGameRules from game import Directions import layout theLayout = layout.getLayout(layoutName) if theLayout == None: raise Exception("The layout " + layoutName + " cannot be found") display = None # Choose a display format if not displayActive: import textDisplay display = textDisplay.NullGraphics() else: import graphicsDisplay display = graphicsDisplay.PacmanGraphics(frameTime=0.01) rules = ClassicGameRules() agents = [pacmanAgents.GreedyAgent()] + [ ghostAgents.DirectionalGhost(i + 1) for i in range(theLayout.getNumGhosts()) ] game = rules.newGame(theLayout, agents[0], agents[1:], display) initialState = game.state display.initialize(initialState.data) exploredStateHashes = {initialState.__hash__()} pendingStates = {initialState} counter = 0 while pendingStates: pendingState = pendingStates.pop() for action in pendingState.getLegalActions(): if action == Directions.STOP: continue try: # Execute the action newState = util.getSuccessor(agents, display, pendingState, action) reward = newState.data.score - pendingState.data.score self.remember(pendingState, action, reward, newState) counter += 1 if not (newState.isWin() or newState.isLose( )) and newState.__hash__() not in exploredStateHashes: exploredStateHashes.add(newState.__hash__()) pendingStates.add(newState) except Exception, e: #print(e) pass if counter % 100 == 0: self.persist() if counter % 2000 == 0: print("Explored " + str(counter) + " states") if limit is not None and counter > limit: break