def execute(self, grades, moduleDict, solutionDict): starttime = time.time() try: timed_func = TimeoutFunction(pacman.runGames, self.maxTime) try: command = '-l %s -p %s -q' % (self.layoutName, self.agentName) games = timed_func(**pacman.readCommand(command.split())) extra_time = (time.time() - starttime) #if games[0].state.isWin(): moves = games[0].moveHistory passed = 0 for t in self.thresholds: if len(moves) <= t: passed += 1 if passed >= len(self.thresholds): grades.addMessage('PASS: %s' % self.path) else: grades.addMessage('FAIL: %s' % self.path) grades.addMessage('\tExtra credit run-time: %1.2f' % extra_time) grades.addMessage('\tExtra credit total moves %d' % len(moves)) grades.addMessage('\tThresholds: %s' % self.testDict['thresholds']) grades.addMessage('\tPassed %s thresholds: %s points.' % (passed, passed)) grades.addPoints(passed) return True except TimeoutFunctionException: grades.addMessage('FAIL: %s' % self.path) grades.addMessage('\tExtra credit code is too slow') return False except Exception, inst: grades.addMessage('FAIL: %s' % self.path) grades.addMessage('\tExtra credit threw an exception: %s.\n%s' % (str(inst), traceback.format_exc()))
def getSolInfo(self, solveTicTacToe): solutionCounter = 0 AIPlayer = getattr(solveTicTacToe, self.player1)() HumanAgent = getattr(solveTicTacToe, self.player2)() self.gameRules = getattr(solveTicTacToe, 'GameRules')() for i in range(self.iteration): random.seed(time.time()) gameState = getattr(solveTicTacToe, 'GameState')() agentIndex = 0 # 0 for First Player (AI), 1 for Second Player (Human) while True: if agentIndex == 0: timed_func = TimeoutFunction(AIPlayer.getAction, int(self.maxTimeOut)) try: start_time = time.time() action = timed_func(gameState, self.gameRules) except TimeoutFunctionException: print( "***\tERROR: Player %d timed out on a single move, Max %d Seconds!***\t" % (agentIndex + 1, self.maxTimeOut)) return 0, False else: action = HumanAgent.getAction(gameState, self.gameRules) gameState = gameState.generateSuccessor(action) if self.gameRules.isGameOver(gameState.boards): break agentIndex = (agentIndex + 1) % 2 if agentIndex == 1: solutionCounter += 1 print('****Player 1 wins %d/%d games' % (i, self.iteration), end="\r") return solutionCounter, None
def run( self ): """ Main control loop for game play. """ if self.master is not None: self.master.initializeState(self) self.display.initialize(self.state.data) self.numMoves = 0 ###self.display.initialize(self.state.makeObservation(1).data) # inform learning agents of the game start for i in range(len(self.agents)): agent = self.agents[i] if not agent and i == 0: # we only care if pacman is somehow None self.mute(i) # this is a null agent, meaning it failed to load # the other team wins print >>sys.stderr, "Agent %d failed to load" % i self.unmute() self._agentCrash(i, quiet=True) return if ("registerInitialState" in dir(agent)): self.mute(i) if self.catchExceptions: try: timed_func = TimeoutFunction(agent.registerInitialState, int(self.rules.getMaxStartupTime(i))) try: start_time = time.time() timed_func(self.state.deepCopy()) time_taken = time.time() - start_time self.totalAgentTimes[i] += time_taken except TimeoutFunctionException: print >>sys.stderr, "Agent %d ran out of time on startup!" % i self.unmute() self.agentTimeout = True self._agentCrash(i, quiet=True) return except Exception,data: self._agentCrash(i, quiet=False) self.unmute() return else: agent.registerInitialState(self.state.deepCopy()) ## TODO: could this exceed the total time self.unmute()
def run(self): """ Main control loop for game play. (Game) -> None """ self.display.initialise(self.state) self.num_moves = 0 for i in range(len(self.agents)): agent = self.agents[i] if not agent: self.mute(i) # this is a null agent, meaning it failed to load, the other team wins print("Agent %d failed to load" % i, file=sys.stderr) self.unmute() self._agent_crash(i, quiet=True) return if ("register_initial_state" in dir(agent)): self.mute(i) if self.catch_exceptions: try: timed_func = TimeoutFunction( agent.register_initial_state, int(self.rules.getMaxStartupTime(i))) try: start_time = time.time() timed_func(self.state.deepcopy()) time_taken = time.time() - start_time self.total_agent_times[i] += time_taken except TimeoutFunctionException: print("Agent %d ran out of time on startup!" % i, file=sys.stderr) self.unmute() self.agent_timeout = True self._agent_crash(i, quiet=True) return except Exception as data: self._agent_crash(i, quiet=False) self.unmute() return else: agent.register_initial_state(self.state.deepcopy()) ## TODO: could this exceed the total time self.unmute() agent_index = self.starting_index num_agents = len(self.agents) while not self.game_over: # Fetch the next agent agent = self.agents[agent_index] move_time = 0 skip_action = False # Generate an observation of the state if 'observation_function' in dir(agent): self.mute(agent_index) if self.catch_exceptions: try: timed_func = TimeoutFunction( agent.observation_function, int(self.rules.get_move_timeout(agent_index))) try: start_time = time.time() observation = timed_func(self.state.deepcopy()) except TimeoutFunctionException: skip_action = True move_time += time.time() - start_time self.unmute() except Exception as data: self._agent_crash(agent_index, quiet=False) self.unmute() return else: observation = agent.observation_function( self.state.deepcopy()) self.unmute() else: observation = self.state.deepcopy() # Solicit an action action = None self.mute(agent_index) if self.catch_exceptions: try: timed_func = TimeoutFunction( agent.get_action, int(self.rules.get_move_timeout(agent_index)) - int(move_time)) try: start_time = time.time() if skip_action: raise TimeoutFunctionException() action = timed_func(observation) except TimeoutFunctionException: print("Agent %d timed out on a single move!" % agent_index, file=sys.stderr) self.agent_timeout = True self._agent_crash(agent_index, quiet=True) self.unmute() return move_time += time.time() - start_time if move_time > self.rules.get_move_warning_time( agent_index): self.total_agent_time_warnings[agent_index] += 1 print("Agent %d took too long to make a move! This is warning %d" %\ (agent_index, self.total_agent_time_warnings[agent_index]), file=sys.stderr) if self.total_agent_time_warnings[ agent_index] > self.rules.get_max_time_warnings( agent_index): print("Agent %d exceeded the maximum number of warnings: %d" %\ (agent_index, self.total_agent_time_warnings[agent_index]), file=sys.stderr) self.agent_timeout = True self._agent_crash(agent_index, quiet=True) self.unmute() return self.total_agent_times[agent_index] += move_time if self.total_agent_times[ agent_index] > self.rules.get_max_total_time( agent_index): print("Agent %d ran out of time! (time: %1.2f)" %\ (agent_index, self.total_agent_times[agent_index]), file=sys.stderr) self.agent_timeout = True self._agent_crash(agent_index, quiet=True) self.unmute() return self.unmute() except Exception as data: self._agent_crash(agent_index) self.unmute() return else: action = agent.get_action(observation) self.unmute() # Execute the action self.move_history.append((agent_index, action)) if self.catch_exceptions: try: self.state = self.state.successor(agent_index, action) except Exception as data: self.mute(agent_index) self._agent_crash(agent_index) self.unmute() return else: self.state = self.state.successor(agent_index, action) # Change the display self.display.update(self.state, agent_index) # Allow for game specific conditions (winning, losing, etc.) self.rules.process(self.state, self) # Track progress if agent_index == num_agents + 1: self.num_moves += 1 # Next agent agent_index = (agent_index + 1) % num_agents # inform a learning agent of the game result for agent_index, agent in enumerate(self.agents): if "final" in dir(agent): try: self.mute(agent_index) agent.final(self.state) self.unmute() except Exception as data: if not self.catch_exceptions: raise self._agent_crash(agent_index) self.unmute() return self.display.finish()
class Game: """ The Game manages the control flow, soliciting actions from agents. """ def __init__( self, agents, display, rules, master=None, startingIndex=0, muteAgents=False, catchExceptions=False ): """ master is a non-agent, who gets to initialize the game state and to change it at each time step. we'll keep it optional for now. """ self.agentCrashed = False self.agents = agents self.display = display self.rules = rules self.master = master self.startingIndex = startingIndex self.gameOver = False self.muteAgents = muteAgents self.catchExceptions = catchExceptions self.moveHistory = [] self.totalAgentTimes = [0 for agent in agents] self.totalAgentTimeWarnings = [0 for agent in agents] self.agentTimeout = False import cStringIO self.agentOutput = [cStringIO.StringIO() for agent in agents] def getProgress(self): if self.gameOver: return 1.0 else: return self.rules.getProgress(self) def _agentCrash( self, agentIndex, quiet=False): "Helper method for handling agent crashes" if not quiet: traceback.print_exc() self.gameOver = True self.agentCrashed = True self.rules.agentCrash(self, agentIndex) OLD_STDOUT = None OLD_STDERR = None def mute(self, agentIndex): if not self.muteAgents: return global OLD_STDOUT, OLD_STDERR import cStringIO OLD_STDOUT = sys.stdout OLD_STDERR = sys.stderr sys.stdout = self.agentOutput[agentIndex] sys.stderr = self.agentOutput[agentIndex] def unmute(self): if not self.muteAgents: return global OLD_STDOUT, OLD_STDERR # Revert stdout/stderr to originals sys.stdout = OLD_STDOUT sys.stderr = OLD_STDERR def run( self ): """ Main control loop for game play. """ if self.master is not None: self.master.initializeState(self) self.display.initialize(self.state.data) self.numMoves = 0 ###self.display.initialize(self.state.makeObservation(1).data) # inform learning agents of the game start for i in range(len(self.agents)): agent = self.agents[i] if not agent and i == 0: # we only care if pacman is somehow None self.mute(i) # this is a null agent, meaning it failed to load # the other team wins print >>sys.stderr, "Agent %d failed to load" % i self.unmute() self._agentCrash(i, quiet=True) return if ("registerInitialState" in dir(agent)): self.mute(i) if self.catchExceptions: try: timed_func = TimeoutFunction(agent.registerInitialState, int(self.rules.getMaxStartupTime(i))) try: start_time = time.time() timed_func(self.state.deepCopy()) time_taken = time.time() - start_time self.totalAgentTimes[i] += time_taken except TimeoutFunctionException: print >>sys.stderr, "Agent %d ran out of time on startup!" % i self.unmute() self.agentTimeout = True self._agentCrash(i, quiet=True) return except Exception,data: self._agentCrash(i, quiet=False) self.unmute() return else: agent.registerInitialState(self.state.deepCopy()) ## TODO: could this exceed the total time self.unmute() agentIndex = self.startingIndex numAgents = len( self.agents ) while not self.gameOver: # Fetch the next agent agent = self.agents[agentIndex] if agent is None: agentIndex = ( agentIndex + 1 ) % numAgents continue move_time = 0 skip_action = False # Generate an observation of the state if 'observationFunction' in dir( agent ): self.mute(agentIndex) if self.catchExceptions: try: timed_func = TimeoutFunction(agent.observationFunction, int(self.rules.getMoveTimeout(agentIndex))) try: start_time = time.time() observation = timed_func(self.state.deepCopy()) except TimeoutFunctionException: skip_action = True move_time += time.time() - start_time self.unmute() except Exception,data: self._agentCrash(agentIndex, quiet=False) self.unmute() return else: observation = agent.observationFunction(self.state.deepCopy()) self.unmute() else: observation = self.state.deepCopy() # Solicit an action action = None self.mute(agentIndex) if self.catchExceptions: try: timed_func = TimeoutFunction(agent.getAction, int(self.rules.getMoveTimeout(agentIndex)) - int(move_time)) try: start_time = time.time() if skip_action: raise TimeoutFunctionException() action = timed_func( observation ) except TimeoutFunctionException: print >>sys.stderr, "Agent %d timed out on a single move!" % agentIndex self.agentTimeout = True self._agentCrash(agentIndex, quiet=True) self.unmute() return move_time += time.time() - start_time if move_time > self.rules.getMoveWarningTime(agentIndex): self.totalAgentTimeWarnings[agentIndex] += 1 print >>sys.stderr, "Agent %d took too long to make a move! This is warning %d" % (agentIndex, self.totalAgentTimeWarnings[agentIndex]) if self.totalAgentTimeWarnings[agentIndex] > self.rules.getMaxTimeWarnings(agentIndex): print >>sys.stderr, "Agent %d exceeded the maximum number of warnings: %d" % (agentIndex, self.totalAgentTimeWarnings[agentIndex]) self.agentTimeout = True self._agentCrash(agentIndex, quiet=True) self.unmute() return self.totalAgentTimes[agentIndex] += move_time if self.totalAgentTimes[agentIndex] > self.rules.getMaxTotalTime(agentIndex): print >>sys.stderr, "Agent %d ran out of time! (time: %1.2f)" % (agentIndex, self.totalAgentTimes[agentIndex]) self.agentTimeout = True self._agentCrash(agentIndex, quiet=True) self.unmute() return self.unmute() except Exception,data: self._agentCrash(agentIndex) self.unmute() return