-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwrite_rejects.py
More file actions
239 lines (204 loc) · 7.96 KB
/
Copy pathwrite_rejects.py
File metadata and controls
239 lines (204 loc) · 7.96 KB
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
from os import path
import json
import argparse
import re
from run_eval import create_options
def parse_args() -> argparse.ArgumentParser:
parser = argparse.ArgumentParser(
description="This runner collections digonstoic information about" \
" agents and their plans from an evaluation set.",
formatter_class=argparse.RawDescriptionHelpFormatter,
)
parser.add_argument(
"--path",
type=str,
required=True,
help="The path to the evaluation folder. This should be"
" the root of the directory",
)
parser.add_argument(
"--collect",
action="store_true",
help="Whether to collect rejected events from the eval runs.",
)
return parser.parse_args()
CONFIG_FILE = "combo_{id:04d}.config"
STACK_FILE = "combo_{id:04d}_{perm}.stack"
REJECT_MATCH = "reject-(.*?)-.*P({id}).*?:(.*?),"
REASON_SWAPS = {
'T1' : 'cannot attack your own territory',
'T2' : 'not enough troops to attack',
'T3' : 'you do not own the attacking territory',
'T4' : 'must leave at least one troop behind',
'T5' : 'not enough troops to transfer'
}
NOTATIONS = {
"Base": "simple",
"DEVS": "devs",
"BT": "bt",
"HTN": "htn",
"MCTS": "mcts",
"BPMN": "bpmn",
"DPN": "dpn",
}
STRATS = {
"rand.": "random",
"def.": "defensive",
"agg.": "aggressive",
}
TABLE_PREFIX = """\\begin{table}[tb]
\\centering
\\caption{
Breakdown of rejected events across simulation runs.
}
\\label{tab:eval:rejected}
\\begin{tabular}{ll|c|ccc|c}
\\toprule
& Strat. & place & \\multicolumn{3}{c|}{attack} & move \\\\
& & & T1 & T2 & T3 & \\\\
\\midrule"""
TABLE_CONTENT = """
Base & \\\\
& rand. &{}&{}&{}&{}&{}\\\\
& def. &{}&{}&{}&{}&{}\\\\
& agg. &{}&{}&{}&{}&{}\\\\
DEVS & \\\\
& rand. &{}&{}&{}&{}&{}\\\\
& def. &{}&{}&{}&{}&{}\\\\
& agg. &{}&{}&{}&{}&{}\\\\
BT & \\\\
& rand. &{}&{}&{}&{}&{}\\\\
& def. &{}&{}&{}&{}&{}\\\\
& agg. &{}&{}&{}&{}&{}\\\\
HTN & \\\\
& rand. &{}&{}&{}&{}&{}\\\\
& def. &{}&{}&{}&{}&{}\\\\
& agg. &{}&{}&{}&{}&{}\\\\
MCTS & \\\\
& rand. &{}&{}&{}&{}&{}\\\\
& def. &{}&{}&{}&{}&{}\\\\
& agg. &{}&{}&{}&{}&{}\\\\
BPMN & \\\\
& rand. &{}&{}&{}&{}&{}\\\\
& def. &{}&{}&{}&{}&{}\\\\
& agg. &{}&{}&{}&{}&{}\\\\
DPN & \\\\
& rand. &{}&{}&{}&{}&{}\\\\
& def. &{}&{}&{}&{}&{}\\\\
& agg. &{}&{}&{}&{}&{}\\\\
"""
TABLE_SUFFIX = """ \\end{tabular}
\\end{table}"""
def format_count(length: int, count:int) -> str:
clen = len(str(count))
if clen < length:
surrounding = (length - clen)
if surrounding % 2 == 0:
return " " * (surrounding // 2) + str(count) + " " * (surrounding // 2)
else:
return " " * (surrounding // 2) + str(count) + " " * (surrounding // 2 + 1)
else:
return str(count)
def work():
# arg parse and create initial reporting structure
args = parse_args()
options = create_options()
rejected = dict(
(key, dict(
(strat, {
"placement" : {}, "attack" : {}, "transfer" : {}
})
for strat
in strats
))
for key, strats
in options.items()
)
# loop through all config files in the eval path
if args.collect:
id = 0
while path.exists(path.join(args.path, CONFIG_FILE.format(id=id))):
config = json.load(open(path.join(args.path, CONFIG_FILE.format(id=id))))
for perm in range(7):
stack = open(path.join(args.path, STACK_FILE.format(id=id, perm=perm)), "r").read()
# for each player, check their config and then collect rejects
for player in range(7):
player_config = config[str(player)]
reject_results = rejected[player_config["type"]][player_config["strat"]]
matcher = re.compile(
REJECT_MATCH.format(id=player),
flags=re.I | re.M
)
matching = re.findall(matcher, stack)
if matching:
for (type, mplayer, reason) in matching:
try :
type = type.lower().strip()
reason = reason.lower().strip()
if reason not in reject_results[type]:
reject_results[type][reason] = 0
reject_results[type][reason] += 1
except Exception as e:
print(f"Could not process rejection match {type} {mplayer} {reason}: {e}")
raise e
id += 1
if (id+1) % 25 == 0:
print(f"Processed {id+1} configurations...")
# save collected rejections and store them in eval path
print(f"Completed processing {id+1} configurations.")
print(json.dumps(rejected, indent=4))
with open(path.join(args.path, "rejected_collection.json"), "w") as f:
f.write(json.dumps(rejected, indent=4))
# collect all reasons seen
seen_attack_reasons = set()
seen_placement_reasons = set()
seen_transfer_reasons = set()
for agent_type, strat_data in rejected.items():
for strat, reject_data in strat_data.items():
for reason in reject_data["placement"].keys():
seen_placement_reasons.add(reason)
for reason in reject_data["attack"].keys():
seen_attack_reasons.add(reason)
for reason in reject_data["transfer"].keys():
seen_transfer_reasons.add(reason)
print("Placement Rejection Reasons:", seen_placement_reasons)
print("Attack Rejection Reasons:", seen_attack_reasons)
print("Transfer Rejection Reasons:", seen_transfer_reasons)
else:
# load collected rejections
print("Attempting to load collected rejections...")
rejected = json.load(open(path.join(args.path, "rejected_collection.json")))
print("Loaded rejection results::")
print(json.dumps(rejected, indent=4))
# write results
print("Writing rejection table...")
with open(path.join(".", "rejects.tex"), "w") as f:
f.write(TABLE_PREFIX)
# handle the content body
ret = "\n"
curr = None
for line in TABLE_CONTENT.splitlines()[1:]:
if len(line.split("&")) == 2:
ret += line + "\n"
notation = line.split("&")[0].strip()
curr = NOTATIONS[notation]
else:
strat = line.split("&")[1].strip()
strat = STRATS[strat]
sum_of_placements = sum(rejected[curr][strat]["placement"].values())
sum_of_transfers = sum(rejected[curr][strat]["transfer"].values())
attack_t1 = rejected[curr][strat]["attack"].get(REASON_SWAPS["T1"], 0)
attack_t2 = rejected[curr][strat]["attack"].get(REASON_SWAPS["T2"], 0)
attack_t3 = rejected[curr][strat]["attack"].get(REASON_SWAPS["T3"], 0)
ret += line.format(
format_count(8, sum_of_placements),
format_count(8, attack_t1),
format_count(8, attack_t2),
format_count(8, attack_t3),
format_count(8, sum_of_transfers)
) + "\n"
f.write(ret)
f.write(TABLE_SUFFIX)
print("Wrote rejection table to rejects.tex")
if __name__ == "__main__":
work()