def test(): argvList = "-q q3".split(" ") options = readCommand(argvList) if options.generateSolutions: confirmGenerate() codePaths = options.studentCode.split(',') moduleDict = {} for cp in codePaths: moduleName = re.match('.*?([^/]*)\.py', cp).group(1) moduleDict[moduleName] = loadModuleFile( moduleName, os.path.join(options.codeRoot, cp)) moduleName = re.match('.*?([^/]*)\.py', options.testCaseCode).group(1) moduleDict['projectTestClasses'] = loadModuleFile( moduleName, os.path.join(options.codeRoot, options.testCaseCode)) if options.runTest != None: runTest(options.runTest, moduleDict, printTestCase=options.printTestCase, display=getDisplay(True, options)) else: evaluate(options.generateSolutions, options.testRoot, moduleDict, gsOutput=options.gsOutput, edxOutput=options.edxOutput, muteOutput=options.muteOutput, printTestCase=options.printTestCase, questionToGrade=options.gradeQuestion, display=getDisplay(options.gradeQuestion != None, options))
def test(self): args = pacman.readCommand([ '-p', 'RectangularRoomCleaner', '-l', layout, '-q', '--frameTime', '0', '--timeout', '1' ]) games = pacman.runGames(**args) self.assertTrue(games[0].state.isWin())
def main(pacman_agent): total_scores = 0. total_weight = 0. for n_case, case in enumerate(evaluation_cases): print('running game {} / {}, {}'.format(n_case + 1, len(evaluation_cases), case[0])) layout, score_weight = case # Run the pacman game and get its score pacman_cmd = 'python pacman.py --pacman {} -l {} -q' pacman_cmd_args = pacman_cmd.format(pacman_agent, layout) # skip 'python pacman.py' in the command line arguments above args = pacman.readCommand(pacman_cmd_args.split()[2:]) games = pacman.runGames(**args) # Take the average of the game scores. Note that there should be only # one game in games, unless `-n` is used in pacman.py scores = [game.state.getScore() for game in games] game_score = sum(scores) / len(scores) total_scores += game_score * score_weight total_weight += score_weight final_score = total_scores / total_weight print("Final score: ", final_score)
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 execute(self, grades, moduleDict, solutionDict): self.addMessage('Grading agent using command: python pacman.py %s'% (self.pacmanParams,)) pacman.TEST_MODULE = moduleDict startTime = time.time() games = pacman.runGames(** pacman.readCommand(self.pacmanParams.split(' '))) totalTime = time.time() - startTime numGames = len(games) stats = {'time': totalTime, 'wins': [g.state.isWin() for g in games].count(True), 'games': games, 'scores': [g.state.getScore() for g in games], 'timeouts': [g.agentTimeout for g in games].count(True), 'crashes': [g.agentCrashed for g in games].count(True)} averageScore = sum(stats['scores']) / float(len(stats['scores'])) nonTimeouts = numGames - stats['timeouts'] wins = stats['wins'] def gradeThreshold(value, minimum, thresholds, name): points = 0 passed = (minimum == None) or (value >= minimum) if passed: for t in thresholds: if value >= t: points += 1 return (passed, points, value, minimum, thresholds, name) results = [gradeThreshold(averageScore, self.scoreMinimum, self.scoreThresholds, "average score"), gradeThreshold(nonTimeouts, self.nonTimeoutMinimum, self.nonTimeoutThresholds, "games not timed out"), gradeThreshold(wins, self.winsMinimum, self.winsThresholds, "wins")] totalPoints = 0 for passed, points, value, minimum, thresholds, name in results: if minimum == None and len(thresholds)==0: continue # print passed, points, value, minimum, thresholds, name totalPoints += points if not passed: assert points == 0 self.addMessage("%s %s (fail: below minimum value %s)" % (value, name, minimum)) else: self.addMessage("%s %s (%s of %s points)" % (value, name, points, len(thresholds))) if minimum != None: self.addMessage(" Grading scheme:") self.addMessage(" < %s: fail" % (minimum,)) if len(thresholds)==0 or minimum != thresholds[0]: self.addMessage(" >= %s: 0 points" % (minimum,)) for idx, threshold in enumerate(thresholds): self.addMessage(" >= %s: %s points" % (threshold, idx+1)) elif len(thresholds) > 0: self.addMessage(" Grading scheme:") self.addMessage(" < %s: 0 points" % (thresholds[0],)) for idx, threshold in enumerate(thresholds): self.addMessage(" >= %s: %s points" % (threshold, idx+1)) if any([not passed for passed, _, _, _, _, _ in results]): totalPoints = 0 return self.testPartial(grades, totalPoints, self.maxPoints)
def __init__( self, index = 0 ): #start ros node rospy.init_node('pacman_game', anonymous=True) self.r = rospy.Rate(10) # 10hz # game attributes # game_attributes = ["-p", "RosWaitServiceAgent", "-l", "smallClassic", "-k", "4"] # game_attributes = ["-p", "RosServiceWithErrorsAgent", "-l", "smallClassic", "-k", "4"] game_attributes = ["-p", "RosServiceWithErrorsAgent", "-l", "originalClassic", "-k", "4"] #a=["-p", "RosWaitServiceAgent", "-l", "originalClassic", "-k", "4"] #a=["-p", "RosWaitServiceAgent", "-l", "mediumGrid", "-k", "4"] self.args = pacman.readCommand(game_attributes) self.args['send_pose_as_service'] = True self.args['send_pose_with_error'] = True self.args['ghost_distance_error'] = 0.01 self.args['pacman_pose_error'] = 0.01 # service and variables to start new game and end it self.start_game_srv = rospy.Service('/pacman/start_game', StartGame, self.start_game_service) self.end_game_client = rospy.ServiceProxy('/pacman/end_game', EndGame) self.start_game = False self.show_gui = False self.game_counter = 0
def main(pacman_agent): total_scores = 0. total_weight = 0. for n_case, case in enumerate(evaluation_cases): print('running game {} / {}, {}'.format(n_case + 1, len(evaluation_cases), case[0])) layout, score_weight = case # Run the pacman game and get its score pacman_cmd = 'python pacman.py --pacman {} -l {} -q' pacman_cmd_args = pacman_cmd.format(pacman_agent, layout) # skip 'python pacman.py' in the command line arguments above args = pacman.readCommand(pacman_cmd_args.split()[2:]) games = pacman.runGames(**args) # Take the average of the game scores. Note that there should be only # one game in games, unless `-n` is used in pacman.py scores = [game.state.getScore() for game in games] game_score = sum(scores) / len(scores) total_scores += game_score * score_weight total_weight += score_weight final_score = total_scores / total_weight print("Final score: ", final_score) # Generate results.json score_fields = {} score_fields['score'] = final_score score_fields['visibility'] = 'visible' score_fields['leaderboard'] = [{"name": "Score", "value": final_score}] score_fields['output'] = 'successfully run {} games!'.format( len(evaluation_cases))
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 debug(): for idx, argvStr in enumerate(argvStrList): start = 0 if idx in range(0, 8): print "********", idx, "*********" argvList = argvStr.split(" ") args = pacman.readCommand(argvList) pacman.runGames(**args)
def setUp(self): "setUp() - Initialize a Pacman board for testing" # Set up game arguments self.args = readCommand(self.argstr) # Initialize the agents self.studentAgent = self.args['pacman'] self.goldAgent = GoldTimidAgent() #layout, pacmanAgent, ghostAgents, display, quiet, catchExceptions self.rules = ClassicGameRules(self.args['timeout']) self.dist = 3
def runq4(): """ Runs their expectimax agent a few times and checks for victory! """ random.seed(SEED) nGames = 20 print 'Running your agent %d times to compute the average score...' % nGames params = '-l smallClassic -p ExpectimaxAgent -a evalFn=better -q -n %d -c' % nGames games = pacman.runGames(**pacman.readCommand(params.split(' '))) timeouts = [game.agentTimeout for game in games].count(True) wins = [game.state.isWin() for game in games].count(True) averageWinScore = 0 if wins >= nGames / 2: averageWinScore = average([game.state.getScore() if game.state.isWin() else None for game in games]) return timeouts, wins, averageWinScore
def main(pacman_agent): n_trials = 2 if os.path.exists('../autograder'): print("POSTOJIIII ") else: print("NE POSTOJI") final_scores = [] for _ in range(n_trials): total_scores = 0. total_weight = 0. for n_case, case in enumerate(evaluation_cases): print('running game {} / {}, {}'.format(n_case + 1, len(evaluation_cases), case[0])) layout, score_weight = case # Run the pacman game and get its score pacman_cmd = 'python pacman.py --pacman {} -l {} -q' pacman_cmd_args = pacman_cmd.format(pacman_agent, layout) # skip 'python pacman.py' in the command line arguments above args = pacman.readCommand(pacman_cmd_args.split()[2:]) games = pacman.runGames(**args) # Take the average of the game scores. Note that there should be only # one game in games, unless `-n` is used in pacman.py scores = [game.state.getScore() for game in games] game_score = sum(scores) / len(scores) total_scores += game_score * score_weight total_weight += score_weight final_score_i = total_scores / total_weight final_scores.append(final_score_i) final_score = sum(final_scores) / n_trials # Write scores for gradescope to read if os.path.exists('../autograder'): # print("USAOUSAO USAO USAO USAO USAO ") # Generate results.json score_fields = {} score_fields['score'] = final_score score_fields['visibility'] = 'visible' score_fields['leaderboard'] = [{"name": "Score", "value": final_score}] score_fields['output'] = 'successfully run {} games!'.format( len(evaluation_cases)) write_output(score_fields)
def runq4(): """ Runs their expectimax agent a few times and checks for victory! """ random.seed(SEED) nGames = 20 print 'Running your agent %d times to compute the average score...' % nGames print 'The timeout message (if any) is obtained by running the game once, rather than %d times' % nGames params = '-l smallClassic -p ExpectimaxAgent -a evalFn=better -q -n %d -c --timeout=30000' % nGames games = pacman.runGames(**pacman.readCommand(params.split(' '))) timeouts = [game.agentTimeout for game in games].count(True) wins = [game.state.isWin() for game in games].count(True) averageWinScore = 0 if wins >= nGames / 2: averageWinScore = average([game.state.getScore() if game.state.isWin() else None for game in games]) print 'Average score of winning games: %d \n' % averageWinScore return timeouts, wins, averageWinScore
def __init__(self, layout, numGames, numGhosts, numTraining): argv = [] argv.append("-pInterfaceAgent") argv.append("-l{}".format(layout)) argv.append("-n{}".format(numGames)) argv.append("-x{}".format(numTraining)) argv.append("-k{}".format(numGhosts)) args = readCommand(argv) observation_shape = (6, args['layout'].width, args['layout'].height) self.action_space = ActionSpace() self.observation_space = ObservationSpace(observation_shape) thread = Thread(target=runGames, kwargs=args) thread.start() self.agent = args['pacman'] self.getAction_CV = self.agent.getAction_CV self.update_CV = self.agent.update_CV
def evaluate(self): # # TO DO: # call pac.runGames and run the game with this particle's parameters # base = ["-p", "PacmanQAgent", "-x", "2000", "-n", "2100", "-l", "smallGrid", "-q", "-a"] config = "epsilon=" + str(self.position_i[0]) + ",alpha=" + str(self.position_i[1]) + ",gamma=" + str(self.position_i[2]) base.append(config) print(config) args = pac.readCommand( base ) # Get game components based on input averageScore = pac.runGames( **args ) #averageScore=random.uniform(1,500) self.score_i=averageScore # save the averageScore this iteration had # check to see if the current position is an individual best if self.score_i>self.score_best_i or self.score_best_i==-1: self.pos_best_i=list(self.position_i) self.score_best_i=self.score_i
def interactive(): args = readCommand( "--layout testMaze --pacman KeyboardAgent --createPolicy".split()) return runGames(**args)
def runPacman(): rospy.init_node('pacman_interface', anonymous=True) a=["-p", "RosWaitAgent", "-l", "originalClassic", "-k", "4"] args = pacman.readCommand(a) pacman.runGames(**args)
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() if __name__ == '__main__': cmd_line_args = pacman.readCommand( sys.argv[1:]) # Get game components based on input # Config file config_file = "../config-neat" # Load configuration. config = neat.Config(neat.DefaultGenome, neat.DefaultReproduction, neat.DefaultSpeciesSet, neat.DefaultStagnation, config_file) # Create the population, which is the top-level object for a NEAT run. p = neat.Population(config) # Load from checkpoint #if cmd_line_args["checkpoint-neat"] != -1: # p = neat.Checkpointer.restore_checkpoint('neat-checkpoint-'+str(cmd_line_args["checkpoint-neat"]))
if __name__ == '__main__': if PACMAN: # PacMan str_args = [ '-l', 'mediumClassic', '-g', 'DirectionalGhost', '--frameTime', '0', '-n', '100' ] str_args = [ '-l', 'TinyMaze', '-g', 'DirectionalGhost', '--frameTime', '0', '-n', '100' ] str_args = [ '-l', 'TestMaze', '-g', 'DirectionalGhost', '--frameTime', '0', '-n', '100' ] args = readCommand(str_args) # args['display'] = textDisplay.NullGraphics() # Disable rendering args['pacman'] = MCTSagent() out = runGames(**args) scores = [o.state.getScore() for o in out] print(scores) else: # TicTacToe winner = [] n = 200 for i in range(n): winner.append( play_game(MCTSagent(), human=TICTACTOE_VS_HUMAN,
""" This script is used for combining the local assignment files with the central script files, and starting the pacman program. """ import sys, os, re, importlib dir_local = os.getcwd() dir_central = os.path.join(dir_local, '..', 'scripts') sys.path.append(dir_central) # append the scripts dir to the path os.chdir(dir_central) # import pacman files from central scripts directory import pacman os.chdir(dir_local) # import asssignment files from local directory, overriding central files for file in os.listdir(): if file.endswith('.py') and file != os.path.basename(__file__): importlib.import_module(file[:-3]) os.chdir(dir_central) # change back to central directory for script execution if len(sys.argv) == 1: args = re.split(r' *', input("Enter any command line arguments?")) sys.argv.extend([a for a in args if a != '']) args = pacman.readCommand(sys.argv[1:]) # Get game components based on input pacman.runGames(**args)
def test_running(self): try: args = readCommand(['-l', 'testMaze', '--pacman', 'GoWestAgent']) runGames(**args) except Exception as e: self.fail('The test environment was not set up correctly: \n{}'.format(e.message))
return Directions.STOP return self.buffer.pop() def socketReceive(actionStr): print "RECEIVE" parts = actionStr.split() if parts[0] == self.agentKey: self.buffer.push(actionStr) class PlayerNetworkMasterAgent(NetworkMasterAgent): def __init__(self): NetworkMasterAgent.__init__(self, KeyboardAgent(), "PLAYER") class PlayerNetworkSlaveAgent(NetworkSlaveAgent): def __init__(self): NetworkSlaveAgent.__init__(self, "PLAYER") if __name__ == '__main__': factory = WebSocketClientFactory('ws://localhost:9000') factory.protocol = ClientProtocol connectWS(factory) #reactor.run() thread = Thread(target = reactor.run, args = (False,)) thread.start() args = readCommand(sys.argv[1:]) runner = lambda: runGames(**args) runner() #thread = Thread(target = runner) #thread.start()
for feat, feat_value in self.getFeatures(state, action).items(): self.weights[feat] = self.getWeight( feat) + self.alpha * error * feat_value def computeError(self, state, action, nextState, reward): return (reward + self.discount * self.getValue(nextState)) - self.getQValue( state, action) def final(self, state): "Called at the end of each game." # call the super-class final method PacmanQAgent.final(self, state) # did we finish training? if self.episodesSoFar == self.numTraining: # you might want to print your weights here for debugging "*** YOUR CODE HERE ***" pass if __name__ == '__main__': # for debug and to use the Profiler of PyCharm import pacman, os cmd = "pacman.py -p ApproximateQAgent -a extractor=SimpleExtractor -x 25 -n 30 -l trickyClassic" # cmd = "pacman.py -p PacmanQAgent -x 200 -n 200 -l smallGrid" # os.system("pacman.py -p PacmanQAgent -x 200 -n 200 -l smallGrid") argv = cmd.split(' ')[1:] args = pacman.readCommand(argv) pacman.runGames(**args)
from pacman import runGames, readCommand from time import time layout_list = [ "originalClassic", "testClassic", "trappedClassic", "trickyClassic", "smallClassic", "minimaxClassic", "openClassic", "mediumClassic", "contestClassic", "capsuleClassic" ] ghost_agents = ["DirectionalGhost", "RandomGhost"] for curr_layout in layout_list: print("Current layout: ", curr_layout) games_counter = 10 args = readCommand([ "-n", str(games_counter), "-k", "2", "-l", curr_layout, "-p", "CompetitionAgent", "-q", "-g", ghost_agents[1] ]) # Get game components based on input start = time() avg = runGames(**args) end = time() print("Current layout AVG completion time: ", float(end - start) / float(games_counter)) print("\n")
import pacman as pac import sys if __name__ == '__main__': """ Used to start a game, with pacman module """ base = [ "-p", "PacmanQAgent", "-x", "2000", "-n", "2010", "-l", "smallGrid", "-a" ] base.append("epsilon=0.1,alpha=0.3,gamma=0.7") args = pac.readCommand(base) # Get game components based on input averageScore = pac.runGames(**args) print(averageScore) pass #--- EXAMPLE ------------------------------------------------------------------+ # python pso.py -p PacmanQAgent -x 2000 -n 2010 -l smallGrid -a epsilon=0.1,alpha=0.3,gamma=0.7