-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlaunch.py
152 lines (126 loc) · 4.61 KB
/
launch.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
from math import isnan
from typing import Tuple
import sys, getopt
from data.data import AGENT_CLASS_TYPE, REP
from scripts.auto import App as Auto
from scripts.average import App as Average
from scripts.play import App as Play
# parse console arguments
def parseOptions(opts, args) -> Tuple[type, dict[str, object], int]:
app: type = None
p: int = AGENT_CLASS_TYPE.CTRL
g1: int = AGENT_CLASS_TYPE.OGNL
g2: int = AGENT_CLASS_TYPE.RAND
itr: int = 1
for opt, arg in opts:
# print help text and terminate
if opt == "h":
printHelp()
sys.exit()
# set app file
elif opt in ("-a", "--app"):
if arg in ("0", "play"):
app = Play
elif arg in ("1", "auto"):
app = Auto
p = AGENT_CLASS_TYPE.RAND
elif arg in ("2", "average"):
app = Average
p = AGENT_CLASS_TYPE.RAND
else:
printHelp()
sys.exit(2)
# set first ghost type
elif opt in ("-f", "--ghostF"):
if arg in ("2", "aggressive"):
g1 = AGENT_CLASS_TYPE.AGGR
elif arg in ("3", "mdp"):
g1 = AGENT_CLASS_TYPE.SMDP
elif arg in ("4", "td"):
g1 = AGENT_CLASS_TYPE.GDQL
elif arg in ("5", "ne"):
g1 = AGENT_CLASS_TYPE.NEAT
# set second ghost type
elif opt in ("-s", "--ghostS"):
if arg in ("2", "aggressive"):
g2 = AGENT_CLASS_TYPE.AGGR
elif arg in ("3", "mdp"):
g2 = AGENT_CLASS_TYPE.SMDP
elif arg in ("4", "td"):
g2 = AGENT_CLASS_TYPE.GDQL
elif arg in ("5", "ne"):
g2 = AGENT_CLASS_TYPE.NEAT
# set iterations
elif opt in ("-i", "--iterations"):
itr = int(arg)
if isnan(itr):
print("Iteration count not a valid number")
sys.exit(2)
secondary = g2
if g2 == AGENT_CLASS_TYPE.SMDP or g2 == AGENT_CLASS_TYPE.GDQL or g2 == AGENT_CLASS_TYPE.NEAT:
secondary = {
REP.INKY: AGENT_CLASS_TYPE.NONE,
REP.CLYDE: AGENT_CLASS_TYPE.NONE,
REP.PINKY: g2,
}
return app, {REP.PACMAN: p, REP.BLINKY: g1, "secondary": secondary}, itr
# print help text
def printHelp() -> None:
print("launch.py -a <app option> -f <first ghost variant> -s <secont ghost variant> -i <number of games>")
print("\n-a --app <app option> options: (required)")
print("\tplay [player control app]")
print("\tauto [baseline agent app]")
print("\taverage [average performance app w/o gui]")
print("\n-f --ghostF <first ghost variant> options: (optional, default = 1)")
print("\t1 [variant 1 - original]")
print("\t2 [variant 2 - hyperaggressive]")
print("\t3 [variant 3 - mdp based]")
print("\t4 [variant 4 - td learning based]")
print("\t5 [variant 5 - neuroevolutionary based]\n")
print("\n-s --ghostS <second ghost variant> options: (optional, default = \"random\")")
print("\t2 [variant 2 - hyperaggressive]")
print("\t3 [variant 3 - mdp based]")
print("\t4 [variant 4 - td learning based]")
print("\t5 [variant 5 - neuroevolutionary based]")
print("\trandom [random original variant - inky, clyde, or pinky]\n")
print("\n-i --iterations (optional, default = 1)")
print("\t<any integer value>")
class Launcher:
def __init__(self, app: type, config: dict[str, object]) -> None:
self.config = config
self.app = app
# run app
def run(self) -> None:
self.app(self.config).start()
if __name__ == "__main__":
try:
opts, args = getopt.getopt(sys.argv[1:], "ha:f:s:i:", ["app=", "ghostF=", "ghostS=", "iterations="])
except:
printHelp()
sys.exit(2)
if len(opts) < 1:
printHelp()
sys.exit(2)
# parse options
app, agents, iterations = parseOptions(opts, args)
# create base config
baseConfig: dict[str, object] = {
"enablePwrPlt": True,
"gameSpeed": 0.2,
"genomes": {
REP.BLINKY: ("saves", "ghost", 33),
REP.PINKY: ("saves", "ghost", 33),
},
"neuralnets": {
REP.PACMAN: ("saves", "pacman", 63),
REP.BLINKY: ("saves", "ghost", 35),
REP.PINKY: ("saves", "ghost", 35),
}
}
# add iteration data
baseConfig["iterations"] = iterations
# add agent data
baseConfig["agents"] = agents
# create launcher and execute
launcher: Launcher = Launcher(app, baseConfig)
launcher.run()