-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsimulate.py
77 lines (64 loc) · 1.72 KB
/
simulate.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
#!/bin/python3
import os
import sys
import subprocess
import random
from difflib import SequenceMatcher
data = [
(
('2', '3'),
'5'
),
(
('4', '5'),
'9'
),
(
('7', '1'),
'8'
)
]
def compare(a, b):
return SequenceMatcher(None, a, b).ratio()
def run_program(program_id, replicate, *args):
replicate_arg = '0'
if replicate:
replicate_arg = '1'
program_name = str(program_id).rjust(49, '0')
cwd = os.path.abspath(sys.argv[1])
process = subprocess.Popen(('./{}'.format(program_name), replicate_arg, *args), cwd=cwd, stdout=subprocess.PIPE)
output = subprocess.check_output(('xxd', '-p'), stdin=process.stdout)
process.wait()
return output.decode('utf-8')
program_id = 0
out0 = run_program(program_id, True)
print(out0)
candidate = [program_id, program_id]
for i in range(1, 20):
print('Iter:', i)
program_id += 1
candidate[0] = program_id
program_id += 1
candidate[1] = program_id
try:
score1 = 0
score2 = 0
for row in data:
out1 = run_program(candidate[0], False, *row[0])
print(out1)
out2 = run_program(candidate[1], False, *row[0])
print(out2)
score1 += compare(out1, row[1])
score2 += compare(out2, row[1])
score1 = score1 / len(data)
score2 = score2 / len(data)
if score1 > score2:
run_program(candidate[0], True)
elif score1 < score2:
run_program(candidate[1], True)
else:
dice = random.randint(0, 1)
run_program(candidate[dice], True)
except FileNotFoundError:
program_id -= 2
run_program(program_id, True)