-
Notifications
You must be signed in to change notification settings - Fork 0
/
lab1_1a.py
55 lines (47 loc) · 2.08 KB
/
lab1_1a.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
from datetime import datetime
import subprocess
import os
import json
def run_greedy_100_times():
trials = {}
for i in range(100):
data = {}
for filename in os.listdir('graphs_to_test'):
path = 'graphs_to_test/' + filename.strip()
start_time = datetime.now()
subprocess.run(["./greedy " + path + " > " +
filename + '.results'], shell=True)
end_time = datetime.now()
with open(filename + '.results', 'r') as in_file:
lines = in_file.readlines()
colors = lines[len(lines)-1].split(' ')[0]
subprocess.run('rm ' + filename + '.results', shell=True)
total_time = (end_time - start_time).total_seconds()
data[filename] = {'time': total_time, 'colors': colors}
trials[i] = data
with open('lab1_1a_greedy_results.json', 'w') as out_file:
json.dump(trials, out_file)
def run_random_100_times(iterations):
iter_data = {}
for iteration in range(iterations):
trials = {}
for i in range(100):
data = {}
for filename in os.listdir('graphs_to_test'):
path = 'graphs_to_test/' + filename.strip()
start_time = datetime.now()
subprocess.run(["./random " + path + ' ' + str(2**iteration) + " > " +
filename + '.results'], shell=True)
end_time = datetime.now()
with open(filename + '.results', 'r') as in_file:
lines = in_file.readlines()
colors = lines[len(lines) - 1].split(' ')[0]
subprocess.run('rm ' + filename + '.results', shell=True)
total_time = (end_time - start_time).total_seconds()
data[filename] = {'time': total_time, 'colors': colors}
trials[i] = data
iter_data['iteration_' + str(iteration)] = trials
with open('lab1_1a_random_results.json', 'w') as out_file:
json.dump(iter_data, out_file)
run_random_100_times(7)
run_greedy_100_times()