def newGame(self, layout, pacmanAgent, ghostAgents, display, quiet=False): agents = [pacmanAgent] + ghostAgents[: layout.getNumGhosts()] initState = GameState() initState.initialize(layout, len(ghostAgents)) game = Game(agents, display, self) game.state = initState self.quiet = quiet return game
def newGame( self, layout, pacmanAgent, ghostAgents, display, quiet = False, catchExceptions=False): agents = [pacmanAgent] + ghostAgents[:layout.getNumGhosts()] ## combigns two lists of AgentObjects starting with the pacmanAgent object followed by all the ghostAgents initState = GameState() initState.initialize( layout, len(ghostAgents) ) game = Game(agents, display, self, catchExceptions=catchExceptions) game.state = initState self.initialState = initState.deepCopy() self.quiet = quiet return game
def newGame( self, layout, pacmanAgent, ghostAgents, display, quiet = False, catchExceptions=False): agents = [pacmanAgent] + ghostAgents[:layout.getNumGhosts()] ## combigns two lists of AgentObjects starting with the pacmanAgent object followed by all the ghostAgents initState = GameState() initState.initialize( layout, len(ghostAgents) ) ##This line calls the initialize method from the GameState() class. It passes the layout variable which contains the layout parameters stipulated by the Layout class in layout.py REF_LAYOUT111. game = Game(agents, display, self, catchExceptions=catchExceptions) game.state = initState self.initialState = initState.deepCopy() self.quiet = quiet return game
def newGame( self, layout, pacmanAgent, ghostAgents, display, quiet = False, catchExceptions=False): agents = [pacmanAgent] + ghostAgents[:layout.getNumGhosts()] initState = GameState() initState.initialize( layout, len(ghostAgents) ) game = Game(agents, display, self, catchExceptions=catchExceptions) game.state = initState self.initialState = initState.deepCopy() self.quiet = quiet return game
def newGame( self, layout, pacmanAgent, ghostAgents, display, quiet = False, catchExceptions=False):## This is called from within the runGame function below: 'game = rules.newGame( layout, pacman, ghosts, gameDisplay, beQuiet, catchExceptions)' agents = [pacmanAgent] + ghostAgents[:layout.getNumGhosts()] ## pacmanAgent and ghostAgents where defined in the redCommand method below. It contains the pacman and ghost agent types in the args dictionary which was passed on as attributes to the newGame method. initState = GameState() ## This instantiates the GameState() class which is defined above: 'A GameState specifies the full game state, including the food, capsules, agent configurations and score changes. initState.initialize( layout, len(ghostAgents) ) ## This uses the initialize method above to create an initial game state from a layout array (REF112), ghostAgents contains a list of instanciated ghostAgent objects: 'ghosts': [<ghostAgents.RandomGhost instance at 0x1004fe518>, <ghostAgents.RandomGhost instance at 0x1004fe7a0>, <ghostAgents.RandomGhost instance at 0x1004fe950>, <ghostAgents.RandomGhost instance at 0x1004feb90> game = Game(agents, display, self, catchExceptions=catchExceptions) ## [REF 115]this initializes the Game class which manages the control flow, soliciting actions from agents. game.state = initState self.initialState = initState.deepCopy() ##The difference between shallow and deep copying is only relevant for compound objects (objects that contain other objects, like lists or class instances): ## A shallow copy constructs a new compound object and then (to the extent possible) inserts references into it to the objects found in the original. ## A deep copy constructs a new compound object and then, recursively, inserts copies into it of the objects found in the original. self.quiet = quiet return game
def newGame( self, layout, pacmanAgent, ghostAgents, display, quiet = False, catchExceptions=False): agents = [pacmanAgent] + ghostAgents[:layout.getNumGhosts()] initState = GameState() initState.initialize( layout, len(ghostAgents) ) game = Game(agents, display, self, catchExceptions=catchExceptions) game.state = initState for position in layout.bomb: game.bomb.append((initState.getFramesUntilEnd() - 10, position, 1)) self.initialState = initState.deepCopy() self.quiet = quiet return game
def newGame( self, layout, pacmanAgent, ghostAgents, display, quiet = False, catchExceptions=False): agents = [pacmanAgent] + ghostAgents[:layout.getNumGhosts()] initState = GameState() initState.initialize( layout, len(ghostAgents) ) game = Game(agents, display, self, catchExceptions=catchExceptions) game.state = initState self.initialState = initState.deepCopy() self.quiet = quiet for index in range( 1, len( game.state.data.agentStates ) ): game.state.data.agentStates[index].scaredTimer = SCARED_TIME return game
def newGame( self, layout, pacmanAgent, ghostAgents, display, quiet = False, catchExceptions=False, send_pose_as_service=False, send_pose_with_error=False, ghost_distance_error=0.01, pacman_pose_error=0.01): agents = [pacmanAgent] + ghostAgents[:layout.getNumGhosts()] initState = GameState() initState.initialize( layout, len(ghostAgents) ) game = Game(agents, display, self, catchExceptions=catchExceptions, send_pose_as_service=send_pose_as_service, send_pose_with_error=send_pose_with_error, ghost_distance_error=ghost_distance_error, pacman_pose_error=pacman_pose_error) game.state = initState self.initialState = initState.deepCopy() self.quiet = quiet return game
def newGame( self, layout, pacmanAgent, ghostAgents, display, quiet = False, catchExceptions=False): agents = [pacmanAgent] + ghostAgents[:layout.getNumGhosts()] initState = GameState() initState.initialize( layout, len(ghostAgents) ) game = Game(agents, display, self, catchExceptions=catchExceptions) game.state = initState self.initialState = initState.deepCopy() self.quiet = quiet import sys, traceback print traceback.print_exc(file=sys.stdout) 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())] game = rules.newGame( layout, agents[0], agents[1:], layout.getNumGhosts(), 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 replayGame(layout, actions, display): import pacmanAgents, ghostAgents rules = ClassicGameRules() agents = [pacmanAgents.GreedyAgent()] + [ ghostAgents.RandomGhost(i + 1) for i in range(layout.getNumGhosts()) ] 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 __init__(self, layout, agents, display, state_repr='stack', muteAgents=False, catchExceptions=False): ''' state_repr: state representation, possible values ['stack', 'k-frames', 'dict'] 'stack' - stack walls, food, ghost and pacman representation into a 4D tensor. 'dict' - return the raw dict representation keys=['walls', 'food', 'ghost', 'pacman'], values are matrix/tensor. 'k-frames' - instead of directional descriptors for pacman and ghost, use static descriptors and capture past k frames. ''' # parse state representation. self.state_repr = state_repr self.layout = layout self.agents = agents self.display = display self.muteAgents = muteAgents self.catchExceptions = catchExceptions if self.state_repr.endswith('frames'): bar_pos = self.state_repr.rfind('frames') self.state_k = int(self.state_repr[:bar_pos-1]) self.state_history = [] self.init_state = GameState() self.init_state.initialize(layout, len(agents)) self.game_rule = ClassicGameRules(timeout=100) self.myagent = GameNoWaitAgent() self.init_game = Game([self.myagent] + agents[:layout.getNumGhosts()], display, self.game_rule, catchExceptions = catchExceptions) self.init_game.state = self.init_state # action mapping. self.all_actions = Actions._directions.keys() self.action_to_dir = {action_i: action for (action_i, action) in enumerate(self.all_actions)} self.dir_to_action = {action: action_i for (action_i, action) in enumerate(self.all_actions)} def start_game(): self.game = self.init_game.deepCopy() self.game.init() self.start_game = start_game start_game()
def newGame(self, layout, pacmanAgent, ghostAgents, display, quiet=False, catchExceptions=False): agents = [pacmanAgent] + ghostAgents[:layout.getNumGhosts()] initState = GameState() initState.initialize(layout, len(ghostAgents)) game = Game(agents, display, self, catchExceptions=catchExceptions) game.state = initState self.initialState = initState.deepCopy() self.quiet = quiet import sys, traceback print traceback.print_exc(file=sys.stdout) return game
def replayGame(layout, actions, display): import submission, ghostAgents rules = ClassicGameRules() agents = [submission.ExpectimaxAgent()] + [ ghostAgents.RandomGhost(i + 1) for i in range(layout.getNumGhosts()) ] game = rules.newGame(layout, agents[0], agents[1:], display) state = game.state display.initialize(state.data) for action in actions: state = state.generateSuccessor(*action) display.update(state.data) rules.process(state, game) display.finish()
def newGame(self, layout, horizon, pacmanAgent, ghostAgents, display, quiet=False, catchExceptions=False): agents = [pacmanAgent] + ghostAgents[:layout.getNumGhosts()] initState = GameState() initState.initialize(layout, len(ghostAgents)) game = Game(agents, horizon, display, self, catchExceptions=catchExceptions) game.state = initState self.initialState = initState.deepCopy() self.quiet = quiet return game
def replayGame(layout, actions, display): import submission, ghostAgents rules = ClassicGameRules() # If replaying, change the agent from ExpectimaxAgent to whatever agent with which you want to play agents = [submission.ExpectimaxAgent()] + [ ghostAgents.RandomGhost(i + 1) for i in range(layout.getNumGhosts()) ] 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 newGame(self, layout, pacmanAgent, ghostAgents, display, quiet=False, catchExceptions=False): """ newGame(layout, pacmanAgent, ghostAgents, display, quiet, catchExceptions) layout - board layout name pacmanAgent - Agent name used to run the Pacman ghostAgents - Agent name used for all ghosts """ agents = [pacmanAgent] + ghostAgents[:layout.getNumGhosts()] initState = GameState() initState.initialize(layout, len(ghostAgents)) game = Game(agents, display, self, catchExceptions=catchExceptions) game.state = initState self.initialState = initState.deepCopy() self.quiet = quiet return game
def replayGame(layout, actions, display, numMoves): import game rules = ClassicGameRules() agents = [game.Agent() ] + [game.Agent(i + 1) for i in range(layout.getNumGhosts())] game = rules.newGame(layout, agents[0], agents[1:], display) state = game.state state.movesToGo = numMoves display.initialize(state.data) #print('Moves:',state.numMoves()) for action in actions: # Execute the action #print('Moves:',state.numMoves()) 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 newGame(self, layout, pacmanAgent, ghostAgents, display, quiet=False, limMoves=100, catchExceptions=False): #print('pacmanAgent=',pacmanAgent,'ghostAgents=',ghostAgents,'display=',display,'quite=',quiet) # pacman é a função passada que decide a sua acção em cada momento # ghostagents é a lista de funções passadas que decidem as suas acções em cada momento # (aparentemente posso ter fantasmas de vários tipos) ignoram-se aqueles que se referem a fantasmas fora dos limites do layout # display é um objecto de que classe?????? #print('NewGame: limite de jogadas:',limMoves) agents = [pacmanAgent] + ghostAgents[:layout.getNumGhosts()] initState = GameState() initState.initialize(layout, len(ghostAgents), limMoves) game = Game(agents, display, self, catchExceptions=catchExceptions) game.state = initState self.initialState = initState.deepCopy() self.quiet = quiet return game
def replayGame( layout, actions, display ): import submission, ghostAgents rules = ClassicGameRules() # If replaying, change the agent from ExpectimaxAgent to whatever agent with which you want to play agents = [submission.ExpectimaxAgent()] + [ghostAgents.RandomGhost(i+1) for i in range(layout.getNumGhosts())] 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 runGames(layout, pacman, ghosts, display, numGames, record, numTraining=0, catchExceptions=False, timeout=30): import __main__ __main__.__dict__['_display'] = display rules = ClassicGameRules(timeout) games = [] agents = [pacman] + ghosts[:layout.getNumGhosts()] # calculate the map of distances distMap = util.calculateDistMap(layout.getWalls()) for agent in agents: if 'setDistMap' in dir(agent): agent.setDistMap(distMap) if agent.__class__ == qlearningAgents.ApproximateQAgent: agent.featExtractor.paths = distMap for i in range(numGames): # EDITTED print("Training ep. nr:" + str(i)) # EDITTED 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, agents, 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() # EDITTED print(pacman.getWeights()) print(pacman.getWeightsScared()) # EDITTED 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 replayGame( layout, actions, display ): import pacmanAgents, ghostAgents import csv global GSTATE, PSA rules = ClassicGameRules() agents = [pacmanAgents.GreedyAgent()] + [ghostAgents.RandomGhost(i+1) for i in range(layout.getNumGhosts())] game = rules.newGame( layout, agents[0], agents[1:], display ) state = game.state display.initialize(state.data) c = 0 prevState = state 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) #after pacman moves, pause if c%2 == 0 : PSA[c/2] = (prevState.__str__(),action[1]) #print(prevState.__str__()) time.sleep(1) #record state we respond to prevState = state GSTATE += 1 #if flask, get here c+=1 display.finish()
def get_state_dim(self, layout): pac_ft_size = 2 ghost_ft_size = 2 * layout.getNumGhosts() food_capsule_ft_size = layout.width * layout.height return pac_ft_size + ghost_ft_size + food_capsule_ft_size