def getUserAction(state, actionFunction): """ Get an action from the user (rather than the agent). Used for debugging and lecture demos. """ import graphicsUtils action = None while True: keys = graphicsUtils.wait_for_keys() if 'Up' in keys: action = 'north' if 'Down' in keys: action = 'south' if 'Left' in keys: action = 'west' if 'Right' in keys: action = 'east' if 'q' in keys: sys.exit(0) if action is None: continue break actions = actionFunction(state) if action not in actions: action = actions[0] return action
def getUserAction(state, actionFunction): """ Get an action from the user (rather than the agent). Used for debugging and lecture demos. """ import graphicsUtils action = None while True: keys = graphicsUtils.wait_for_keys() if "Up" in keys: action = "north" if "Down" in keys: action = "south" if "Left" in keys: action = "west" if "Right" in keys: action = "east" if "q" in keys: sys.exit(0) if action == None: continue break actions = actionFunction(state) if action not in actions: action = actions[0] return action
def getUserAction(state, actionFunction): """ Get an action from the user (rather than the agent). Used for debugging and lecture demos. """ import graphicsUtils action = None while True: keys = graphicsUtils.wait_for_keys() if "Up" in keys: action = "north" if "Down" in keys: action = "south" if "Left" in keys: action = "west" if "Right" in keys: action = "east" if "q" in keys: sys.exit(0) if action == None: continue break actions = actionFunction(state) if action not in actions: action = actions[0] return action
def getAction(self, state): from graphicsUtils import wait_for_keys legal = state.getLegalActions(self.index) stateRepresentation = getStateRepresentation(state) print "State Representation:", stateRepresentation move = None while move == None: self.keys = wait_for_keys() move = self.getMove(legal) if move == None: print "Illegal move. Try again" actionRepresentation = getActionRepresentation(move) print "Action Representation:", actionRepresentation self.saveTraining([stateRepresentation, actionRepresentation]) return move
def getAction( self, state): from graphicsUtils import wait_for_keys legal = state.getLegalActions(self.index) stateRepresentation = getStateRepresentation(state) #print "State Representation:", stateRepresentation move = None while move == None: self.keys = wait_for_keys() move = self.getMove(legal) if move == None: print "Illegal move. Try again" actionRepresentation = getActionRepresentation(move) #print "Action Representation:", actionRepresentation self.saveTraining([self.convertState(state, stateRepresentation), self.convertAction(state, actionRepresentation)]) return move
def animatePacman(self, pacman, prevPacman, image): if self.frameTime < 0: print('Press any key to step forward, "q" to play') keys = gU.wait_for_keys() if 'q' in keys: self.frameTime = 0.1 if self.frameTime > 0.01 or self.frameTime < 0: fx, fy = self.getPosition(prevPacman) px, py = self.getPosition(pacman) frames = 4.0 for i in range(1, int(frames) + 1): pos = px * i / frames + fx * (frames - i) / frames, \ py * i / frames + fy * (frames - i) / frames self.movePacman(pos, self.getDirection(pacman), image) gU.refresh() gU.sleep(abs(self.frameTime) / frames) else: self.movePacman(self.getPosition(pacman), self.getDirection(pacman), image) gU.refresh()
def getUserAction(state, actionFunction): """ Get an action from the user (rather than the agent). Used for debugging and lecture demos. """ import graphicsUtils action = None while True: keys = graphicsUtils.wait_for_keys() if 'Up' in keys: action = 'north' if 'Down' in keys: action = 'south' if 'Left' in keys: action = 'west' if 'Right' in keys: action = 'east' if 'q' in keys: sys.exit(0) if action == None: continue break actions = actionFunction(state) if action not in actions: action = actions[0] return action
def runGames(layout, pacman, ghosts, display, numGames, record, numTraining=0, catchExceptions=False, timeout=30, keyboardGhosts=[], savedDisplay=None): import __main__ import time __main__.__dict__['_display'] = display rules = ClassicGameRules(timeout) games = [] for i in range(numGames): game = None beQuiet = i < numTraining if beQuiet: # Suppress output and graphics import textDisplay gameDisplay = textDisplay.NullGraphics() rules.quiet = True if hasattr(display, 'showTrainingScreen') and display.showTrainingScreen: display.initialize(None, "training") else: if savedDisplay is None: gameDisplay = display else: gameDisplay = savedDisplay rules.quiet = False # If there's keyboard ghosts and not in training mode if len(keyboardGhosts) > 0 and not beQuiet: #for i in range(len(keyboardGhosts)): # keyboardGhosts[i].init() #ghostType = loadAgent("KeyboardGhost", False) #keyboardGhosts = [ghostType( i+1 ) for i in range( len(keyboardGhosts) )] game = rules.newGame(layout, pacman, keyboardGhosts, gameDisplay, beQuiet, catchExceptions) else: game = rules.newGame(layout, pacman, ghosts, gameDisplay, beQuiet, catchExceptions) if beQuiet: game.run() else: if savedDisplay is None: savedDisplay = game.run() savedDisplay = game.run() else: savedDisplay = game.run(savedDisplay) if game.gameQuit: return (games, display) # Show win / loss message if hasattr(display, 'showTrainingScreen'): from graphicsUtils import wait_for_keys display.showResultMessage(not game.state.isWin()) keys = [] if len(keyboardGhosts) > 0: while 'Return' not in keys: keys = wait_for_keys() else: time.sleep(1) display.hideResultMessage() 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] progress = [ float(game.state.getNumFood()) / rules.initialState.getNumFood() 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('Progress Rate: %.2f %%' % ((sum(progress) / len(progress)) * 100)) print('Record: ', ', '.join([['Loss', 'Win'][int(w)] for w in wins])) return (games, display)
def runGames( layout, pacman, ghosts, display, numGames, record, numTraining = 0, catchExceptions=False, timeout=30, keyboardGhosts=[], savedDisplay=None ): import __main__ import time __main__.__dict__['_display'] = display rules = ClassicGameRules(timeout) games = [] for i in range( numGames ): game = None beQuiet = i < numTraining if beQuiet: # Suppress output and graphics import textDisplay gameDisplay = textDisplay.NullGraphics() rules.quiet = True if hasattr(display, 'showTrainingScreen') and display.showTrainingScreen: display.initialize(None, "training") else: if savedDisplay is None: gameDisplay = display else: gameDisplay = savedDisplay rules.quiet = False # If there's keyboard ghosts and not in training mode if len(keyboardGhosts) > 0 and not beQuiet: #for i in range(len(keyboardGhosts)): # keyboardGhosts[i].init() #ghostType = loadAgent("KeyboardGhost", False) #keyboardGhosts = [ghostType( i+1 ) for i in range( len(keyboardGhosts) )] game = rules.newGame( layout, pacman, keyboardGhosts, gameDisplay, beQuiet, catchExceptions) else: game = rules.newGame( layout, pacman, ghosts, gameDisplay, beQuiet, catchExceptions) if beQuiet: game.run() else: if savedDisplay is None: savedDisplay = game.run() else: savedDisplay = game.run(savedDisplay) if game.gameQuit: return (games, display) # Show win / loss message if hasattr(display, 'showTrainingScreen'): from graphicsUtils import wait_for_keys display.showResultMessage(not game.state.isWin()) keys = [] if len(keyboardGhosts) > 0: while 'Return' not in keys: keys = wait_for_keys() else: time.sleep(1) display.hideResultMessage() 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] progress = [float(game.state.getNumFood()) / rules.initialState.getNumFood() 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 'Progress Rate: %.2f %%' % ((sum(progress)/len(progress))*100) print 'Record: ', ', '.join([ ['Loss', 'Win'][int(w)] for w in wins]) return (games, display)