/
runner.py
88 lines (73 loc) · 3 KB
/
runner.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
#!/usr/bin/env python3
import os
import argparse
import subprocess
import random
import shutil
parser = argparse.ArgumentParser()
parser.add_argument('segment', help='Indicates the segment to run')
parser.add_argument('--dbMinSize', help='Minimum state database size in Mb', required=True)
parser.add_argument('--dbMaxSize', help='Maximum state database size in Mb', required=True)
parser.add_argument('--hashDBCount', help='Number of hash DBs to use', required=True)
parser.add_argument('--hashDBSize', help='Size of hash DBs to use', required=True)
args = parser.parse_args()
# Getting segment ID
segment = args.segment
# Changing directory to the segment's
os.chdir(segment)
# Reading script file
sourceScriptFilePath = "script"
if os.path.isfile(sourceScriptFilePath):
sourceScriptFile = open(sourceScriptFilePath, "r")
sourceScript = sourceScriptFile.read()
sourceScriptFile.close()
else:
print("Could not load script file: " + file_path)
exit(-1)
# Creating folder if not exists
samplesDir="samples"
if not os.path.exists(samplesDir): os.makedirs(samplesDir)
# Loop this forever
while True:
# Producing new RNG number
RNGSeed = random.randint(0,4294967295)
# Building base sample dir
sampleDir = samplesDir + "/" + str(RNGSeed)
if not os.path.exists(sampleDir): os.makedirs(sampleDir)
# Determining file paths
solutionFilePath = sampleDir + "/jaffar.best.sol"
newScriptPath = sampleDir + "/script"
logFilePath = sampleDir + "/jaffar.log"
# Replacing number in script
newScript = ""
for line in sourceScript.splitlines():
if '"RNG Value"' in line: line = ' "RNG Value": ' + str(RNGSeed) + ','
if '"Frequency (s)"' in line: line = ' "Frequency (s)": 0.0,'
if '"Best Solution Path"' in line: line = ' "Best Solution Path": "' + solutionFilePath + '",'
if '"Max Size Lower Bound (Mb)"' in line: line = ' "Max Size Lower Bound (Mb)": ' + args.dbMinSize + ','
if '"Max Size Upper Bound (Mb)"' in line: line = ' "Max Size Upper Bound (Mb)": ' + args.dbMaxSize
if '"Database Count"' in line: line = ' "Database Count": ' + args.hashDBCount + ','
if '"Max Size (Mb)"' in line: line = ' "Max Size (Mb)": ' + args.hashDBSize
newScript += line + '\n'
# Storing new script
newScriptFile = open(newScriptPath, "w")
newScriptFile.write(newScript)
newScriptFile.close()
# Running new seed
print("Running: " + newScriptPath + " ... ", end="", flush=True)
rc=1
stdoutFile = open(logFilePath, "a")
result = subprocess.run(["jaffar", newScriptPath], stdout=stdoutFile, stderr=stdoutFile)
stdoutFile.close()
rc = result.returncode
# Report results
if (rc == 0):
if os.path.isfile(solutionFilePath):
num_lines = sum(1 for line in open(solutionFilePath))
print("Success! Moves: " + str(num_lines))
else:
print("Could not load solution file: " + solutionFilePath)
exit(-1)
else:
print("Fail!")
exit(-1)