-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtests.py
81 lines (76 loc) · 2.84 KB
/
tests.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
import os, sys
import logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s %(message)s', datefmt='%m/%d/%Y %I:%M:%S %p')
import click
import random
import statistics
import subprocess
import time
groups = {"G0": ["F", "R", "U", "B", "L", "D", "F'", "R'", "U'", "B'", "L'", "D'","F2", "R2", "U2", "B2", "L2", "D2", "F'2", "R'2", "U'2", "B'2", "L'2", "D'2"],
"G1": ["R", "U", "L", "D", "R'", "U'", "L'", "D'","F2", "B2", "F'2", "B'2"],
"G2": ["U", "D", "U'", "D'","F2", "R2", "B2", "L2", "F'2", "R'2", "B'2", "L'2"],
"G3": ["F2", "R2", "U2", "B2", "L2", "D2", "F'2", "R'2", "U'2", "B'2", "L'2", "D'2"]}
def scramble(group):
steps_idx = []
for _ in range(20):
rand = random.randint(0, len(group)-1)
while len(steps_idx) and group[rand][0] == group[steps_idx[-1]][0]:
rand = random.randint(0, len(group)-1)
steps_idx.append(rand)
steps = " ".join([group[idx] for idx in steps_idx])
print("Applying", steps, ":")
return steps
@click.command()
@click.argument("group", default="G0")
@click.option("-h", "human", is_flag=True)
def main(group, human):
counts = []
durations = []
errors = 0
success = 0
if human:
human = "-h"
else:
human = ""
# Compile solver
if not os.path.exists("Rubik") or not os.path.exists("Rubik.exe"):
args = ("go", "build")
popen = subprocess.Popen(args)
try:
for i in range(100):
print(i)
mix = scramble(groups[group])
logging.info("Solving...")
if sys.platform == "win32":
args = ("./Rubik.exe", human, mix)
else:
args = ("./Rubik", human, mix)
popen = subprocess.Popen(args, stdout=subprocess.PIPE)
try:
start = time.time()
popen.wait(60)
end = time.time()
output = popen.stdout.read().decode()
except subprocess.TimeoutExpired:
output = ""
if len(output):
res = str(output).replace("\n", "")
if len(res):
count = len(res.split(" "))
counts.append(count)
durations.append(end-start)
logging.info("Done in %d steps" % count)
print(res + "\n")
success += 1
else:
time.sleep(0.1)
else:
logging.info("Error : Timeout\n")
errors += 1
except KeyboardInterrupt:
pass
print("\nAverage solution length: %d" % statistics.mean(counts))
print("Average compute time: %2.2fs" % statistics.mean(durations))
print("%d Success and %d Timeouts, rate = %.2f" % (success, errors, success/(success + errors)))
if __name__ == "__main__":
main()