-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
199 lines (167 loc) · 8.67 KB
/
main.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
import sys
import os
import datetime
import subprocess
import multiprocessing
import re
from collections import namedtuple
from problog.util import init_logger
from mapl_cirup import MaplCirup
TIMEOUT = 10 * 60 # seconds
RUNS = 10
def run_experiment(input_file, discount, error, res):
mc = MaplCirup(input_file)
res.put((mc.size(), mc.compile_time()))
mc.value_iteration(discount=discount, error=error)
res.put((mc.value_iteration_time(), mc.tot_time(), mc.iterations()))
def run_experimental_evaluation():
# create directory to save results
res_dir = 'results'
if not os.path.exists(res_dir):
os.makedirs(res_dir)
print("=== Experimental Evaluation ===")
timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M")
with open(res_dir + '/' + timestamp + '_exp-eval.csv', 'w') as fres:
header = "run,solver,filename,family,var_num,timeout,discount,error," \
"circuit_size,compile_time,vi_time,tot_time,vi_iterations\n"
fres.write(header)
for run in range(1, RUNS + 1):
# mapl-cirup
solver = "mapl-cirup"
print("\n\nMAPL-CIRUP\n")
for root, dirs, files in os.walk("./examples"):
family = os.path.basename(os.path.normpath(root))
for file in files:
if file.endswith(".pl"):
# retrieve var number
var_num = 'n'
var_num_pattern = re.compile("_v([0-9]+)")
match = re.search(var_num_pattern, file)
if match:
var_num = match.group(1)
# execute experiments
input_file = os.path.join(root, file)
print("\n\n-> Executing mapl-cirup (run %s) on %s..." % (run, input_file))
res = multiprocessing.Queue()
discount = 0.9
error = 0.1
p = multiprocessing.Process(target=run_experiment, args=(input_file, discount, error, res))
p.start()
p.join(TIMEOUT)
if p.is_alive():
p.kill()
if res.empty():
# compilation didn't finish
fres.write("%s,%s,%s,%s,%s,%s,%s,%s,na,na,na,na,na\n" %
(run, solver, input_file, family, var_num, TIMEOUT, discount, error))
else:
# compilation finished, but value iteration not
size, compile_time = res.get()
fres.write("%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,na,%s,na\n" %
(run, solver, input_file, family, var_num, TIMEOUT, discount, error,
size, compile_time, compile_time))
else:
# everything finished
size, compile_time = res.get()
vi_time, tot_time, iterations = res.get()
fres.write("%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s\n" %
(run, solver, input_file, family, var_num, TIMEOUT, discount, error, size,
compile_time, vi_time, tot_time, iterations))
fres.flush()
# spudd
solver = "spudd"
print("\n\nSPUDD\n")
for root, dirs, files in os.walk("./examples"):
family = os.path.basename(os.path.normpath(root))
for file in files:
if file.endswith(".dat"):
# retrieve var number
var_num = 'n'
var_num_pattern = re.compile("_v([0-9]+)")
match = re.search(var_num_pattern, file)
if match:
var_num = match.group(1)
# execute experiments
input_file = os.path.join(root, file)
print("\n\n-> Executing spudd (run %s) on %s..." % (run, input_file))
# find discount and error in input file
discount = "na"
error = "na"
discount_pattern = re.compile("discount\s+([0-9\.]+)")
error_pattern = re.compile("tolerance\s+([0-9\.]+)")
for line in open(input_file):
match = re.search(discount_pattern, line)
if match:
discount = round(float(match.group(1)), 2)
match = re.search(error_pattern, line)
if match:
error = match.group(1)
try:
spudd_proc = subprocess.Popen(
["./examples/executables/spudd-linux", input_file, "-o", res_dir + "/spudd"]
)
spudd_proc.communicate(timeout=TIMEOUT)
# find stats
size = "na"
vi_time = "na"
iterations = "na"
size_pattern = re.compile("Total number of nodes allocated:\s([0-9]+)")
vi_time_pattern = re.compile("\sFinal execution time:\s+([0-9\.]+)")
iterations_pattern = re.compile("\sIterations to convergence\s([0-9]+)")
for line in open(res_dir + "/spudd-stats.dat"):
match = re.search(size_pattern, line)
if match:
size = match.group(1)
match = re.search(vi_time_pattern, line)
if match:
vi_time = round(float(match.group(1)), 2)
match = re.search(iterations_pattern, line)
if match:
iterations = match.group(1)
fres.write("%s,%s,%s,%s,%s,%s,%s,%s,%s,na,%s,%s,%s\n" %
(run, solver, input_file, family, var_num, TIMEOUT, discount, error, size,
vi_time, vi_time, iterations))
os.remove(res_dir + "/spudd-stats.dat")
os.remove(res_dir + "/spudd-OPTDual.ADD")
except subprocess.TimeoutExpired:
spudd_proc.kill()
fres.write("%s,%s,%s,%s,%s,%s,%s,%s,na,na,na,na,na\n" %
(run, solver, input_file, family, var_num, TIMEOUT, discount, error))
fres.flush()
def main(argv):
args = argparser().parse_args(argv)
if args.experiments:
run_experimental_evaluation()
else:
if args.file is None:
input_file = input("File path: ")
else:
input_file = args.file
init_logger(args.verbose)
mc: MaplCirup = MaplCirup(input_file)
print("Compile time: %s" % mc.compile_time())
# print("Minimize time: %s" % mc.minimize_time())
print("Circuit size: %s" % mc.size())
# mc.value_iteration(discount=0.9, error=0.01)
# mc.value_iteration(discount=0.9, error=0.1)
# mc.value_iteration(discount=0.9)
# mc.value_iteration(discount=0.9, horizon=1)
mc.value_iteration(horizon=40)
# mc.value_iteration() # = immediate reward
print("Value iteration time: %s" % mc.value_iteration_time())
print("Total time: %s" % mc.tot_time())
print('\nNumber of iterations: ' + str(mc.iterations()) + '\n')
# mc.view_dot()
# mc.print_explicit_policy()
def argparser():
import argparse
parser = argparse.ArgumentParser(description="MArkov PLanning with CIRcuit bellman UPdates.")
parser.add_argument('-f', '--file', help='Path to the input file')
# TODO: Implement the following parameter
# parser.add_argument('-o', '--output', type=str, default=None,
# help='Write output to given file (default: stdout)')
parser.add_argument('-v', '--verbose', help='Verbose mode')
parser.add_argument('--experiments', help='Run experimental evaluation', action='store_true')
return parser
if __name__ == '__main__':
main(sys.argv[1:])