This repository has been archived by the owner on Dec 6, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathautograder.py
66 lines (52 loc) · 2.2 KB
/
autograder.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import json
import argparse
import glob
import os
import pacman
def get_test_cases():
tests = glob.glob('layouts/*.lay')
return [(t, 1.) for i, t in enumerate(tests)]
evaluation_cases = get_test_cases()
def main(pacman_agent):
n_trials = 2
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
print("Final score: ", final_score)
# Write scores for gradescope to read
if os.path.exists('/autograder'):
# 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 write_output(score_fields):
with open('/autograder/results/results.json', 'w') as output_file:
json.dump(score_fields, output_file)
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--pacman', default='myAgents.py')
args = parser.parse_args()
main(args.pacman)