-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.py
87 lines (69 loc) · 2.58 KB
/
run.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import imp
import os
from argparse import ArgumentParser, ArgumentTypeError
from pacman_module.pacman import runGame
from pacman_module.ghostAgents import GreedyGhost, RandyGhost, LeftyGhost
def restricted_float(x):
x = float(x)
if x < 0.1 or x > 1.0:
raise ArgumentTypeError("%r not in range [0.1, 1.0]" % (x,))
return x
def positive_integer(x):
x = int(x)
if x < 0:
raise ArgumentTypeError("%r is not >= 0" % (x,))
return x
def load_agent_from_file(filepath):
class_mod = None
expected_class = 'PacmanAgent'
mod_name, file_ext = os.path.splitext(os.path.split(filepath)[-1])
if file_ext.lower() == '.py':
py_mod = imp.load_source(mod_name, filepath)
elif file_ext.lower() == '.pyc':
py_mod = imp.load_compiled(mod_name, filepath)
if hasattr(py_mod, expected_class):
class_mod = getattr(py_mod, expected_class)
return class_mod
ghosts = {}
ghosts["greedy"] = GreedyGhost
ghosts["randy"] = RandyGhost
ghosts["lefty"] = LeftyGhost
if __name__ == '__main__':
usage = """
USAGE: python run.py <game_options> <agent_options>
EXAMPLES: (1) python run.py
- plays a game with the human agent
in small maze
"""
parser = ArgumentParser(usage)
parser.add_argument('--seed', help='RNG seed', type=int, default=1)
parser.add_argument('--nghosts', help='Number of ghosts',
type=int, default=0)
parser.add_argument(
'--agentfile',
help='Python file containing a `PacmanAgent` class.',
default="humanagent.py")
parser.add_argument(
'--ghostagent',
help='Ghost agent available in the `ghostAgents` module.',
choices=["lefty", "greedy", "randy"], default="greedy")
parser.add_argument(
'--layout',
help='Maze layout (from layout folder).',
default="small")
parser.add_argument(
'--silentdisplay',
help="Disable the graphical display of the game.",
action="store_true")
args = parser.parse_args()
agent = load_agent_from_file(args.agentfile)(args)
gagt = ghosts[args.ghostagent]
if (args.nghosts > 0):
gagts = [gagt(i + 1) for i in range(args.nghosts)]
else:
gagts = []
total_score, total_computation_time, total_expanded_nodes = runGame(
args.layout, agent, gagts, not args.silentdisplay, expout=0)
print("Total score : " + str(total_score))
print("Total computation time (seconds) : " + str(total_computation_time))
print("Total expanded nodes : " + str(total_expanded_nodes))