forked from ja3537/A-Magical-Code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbenchmark.py
176 lines (151 loc) · 4.67 KB
/
benchmark.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import argparse
import multiprocessing
import random
import sys
from functools import partial
from importlib import import_module
from itertools import product
from string import ascii_letters, ascii_lowercase, digits, punctuation
import matplotlib.pyplot as plt
import numpy as np
# ==================
# Message Generators
# ==================
def uniform_random_message(character_set: str, length: int) -> str:
return "".join(random.choices(character_set, k=length))
# ===============
# Message Domains
# ===============
# name -> generator(length)
MESSAGE_DOMAINS = {
"lower": partial(uniform_random_message, ascii_lowercase),
"alpha": partial(uniform_random_message, ascii_letters),
"alphanum": partial(uniform_random_message, ascii_letters + digits),
"password": partial(uniform_random_message, ascii_letters + digits + punctuation),
}
# ==============
# Argparse setup
# ==============
parser = argparse.ArgumentParser()
parser.add_argument(
"--agent",
"-a",
type=int,
required=True,
help="which agent to use from 1 to 8",
)
parser.add_argument(
"--trials",
"-t",
default=50,
type=int,
help="number of trials per length/shuffle datapoint. defaults to 50",
)
parser.add_argument(
"--min-length",
"-L",
default=1,
type=int,
help="minimum message length. defaults to 1",
)
parser.add_argument(
"--max-length",
"-l",
default=10,
type=int,
help="maximum message length. defaults to 10",
)
parser.add_argument(
"--min-shuffles",
"-N",
default=0,
type=int,
help="minimum shuffle amount. defaults to 0",
)
parser.add_argument(
"--max-shuffles",
"-n",
default=100,
type=int,
help="maximum shuffle amount. defaults to 100",
)
parser.add_argument(
"--domain",
"-d",
default="lower",
help="message domain. one of: " + ", ".join(MESSAGE_DOMAINS.keys()),
)
parser.add_argument(
"--disable-threading",
default=False,
action="store_true",
help="disable multithreading for benchmarking. makes script run on systems where multithreading isn't working but makes benchmark run more slowly",
)
pargs = parser.parse_args()
agent_name = f"agent{pargs.agent}"
agent_module = import_module(f".{agent_name}", "agents")
# ===========================
# Benchmarking infrastructure
# ===========================
def shuffle(n, deck):
rng = np.random.default_rng()
shuffles = rng.integers(0, 52, n)
for pos in shuffles:
top_card = deck[0]
deck = deck[1:]
deck = deck[:pos] + [top_card] + deck[pos:]
return deck
# Suppress stdout
# https://stackoverflow.com/questions/2828953/silence-the-stdout-of-a-function-in-python-without-trashing-sys-stdout-and-resto
class DummyFile(object):
def write(self, x):
pass
def flush(self):
pass
def run_trial(args: tuple[int, int]) -> tuple[int, int, float]:
# Suppress stdout
save_stdout = sys.stdout
sys.stdout = DummyFile()
agent = agent_module.Agent()
length, n = args
successes = 0
for _ in range(pargs.trials):
word = MESSAGE_DOMAINS[pargs.domain](length)
deck = agent.encode(word)
shuffled = shuffle(n, deck)
out = agent.decode(shuffled)
if word == out:
successes += 1
# Restore stdout
sys.stdout = save_stdout
return length, n, successes / pargs.trials
if __name__ == "__main__":
lengths = list(range(pargs.min_length, pargs.max_length + 1))
shuffle_amounts = list(range(pargs.min_shuffles, pargs.max_shuffles + 1))
work = list(product(lengths, shuffle_amounts))
pool = multiprocessing.Pool(multiprocessing.cpu_count())
print("Collecting results...")
if pargs.disable_threading:
results = map(run_trial, work)
else:
results = pool.imap_unordered(run_trial, work, chunksize=4)
collected_results = {}
for result in results:
length, n, recovery_rate = result
if not length in collected_results:
collected_results[length] = []
collected_results[length].append((n, recovery_rate))
# ======================
# Plot benchmark results
# ======================
print("Plotting...")
for length in collected_results:
ys = [recovery_rate for _, recovery_rate in sorted(collected_results[length])]
plt.plot(shuffle_amounts, ys, label=str(length))
# https://stackoverflow.com/questions/4700614/how-to-put-the-legend-outside-the-plot
plt.legend(bbox_to_anchor=(1.04, 1), loc="upper left")
plt.title(f"Shuffle Count vs. Recovery Rate (Agent {pargs.agent})")
plt.xlabel("Number of shuffles")
plt.ylabel("Recovery rate")
plt.tight_layout()
plt.savefig("benchmark.png", dpi=300)