-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_sharpASPSR.py
More file actions
143 lines (117 loc) · 3.9 KB
/
run_sharpASPSR.py
File metadata and controls
143 lines (117 loc) · 3.9 KB
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
#!/usr/bin/env python3
import os
import argparse
import subprocess
import shutil
import sys
import time
parser = argparse.ArgumentParser()
parser.add_argument("-i", "--i", help="input ASP program", required=True)
parser.add_argument("-t", "--t", help="timeout", default=5000, required=False, type=float)
args = parser.parse_args()
ganak_binary = "ganak"
clark = "clark"
if not os.path.exists(f"./{ganak_binary}"):
print(f"Tool {ganak_binary} does not exist")
sys.exit(1)
if shutil.which("gringo") is None:
print("gringo is not installed. Please install gringo")
sys.exit(1)
if not os.path.exists(f"./{clark}"):
print(f"Tool {clark} does not exist")
sys.exit(1)
if not os.path.exists(args.i):
print("input " + args.i + " does not exist")
sys.exit(1)
try:
gringo_proc = subprocess.Popen(
["gringo", "-o", "smodels", args.i],
stdout=subprocess.PIPE,
stderr=subprocess.DEVNULL,
)
subprocess.run(
[f"./{clark}", "-dimacs", "-output", args.i],
stdin=gringo_proc.stdout,
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
check=True,
)
gringo_proc.stdout.close()
gringo_proc.wait()
except subprocess.CalledProcessError:
print("Failed to compute formulas phi_1 and phi_2 !!!")
print("Some issue with input file")
sys.exit(1)
if not os.path.exists("model_" + args.i + ".out"):
print("Cannot compute formulas phi_1 and phi_2 !!!")
print("Some issue with input file")
print("model_" + args.i + ".out does not exist")
sys.exit(1)
if not os.path.exists("non_sm_" + args.i + ".out"):
print("Cannot compute formulas phi_1 and phi_2 !!!")
print("Some issue with input file")
print("non_sm_" + args.i + ".out does not exist")
sys.exit(1)
def parse_count(output_file):
with open(output_file, "r", encoding="utf-8") as f:
for line in f:
if line.startswith("c s exact arb int"):
parts = line.strip().split()
return int(parts[-1])
return None
def run_ganak(formula_file, output_file, timeout_seconds):
start = time.time()
try:
with open(output_file, "w", encoding="utf-8") as f:
subprocess.run(
[
f"./{ganak_binary}",
"--verb", "0",
"--prob", "0",
"--maxcache", "4000",
formula_file,
],
stdout=f,
stderr=subprocess.PIPE,
text=True,
timeout=timeout_seconds,
check=True,
)
except subprocess.TimeoutExpired:
print("SharpASP-SR timeouted")
sys.exit(1)
except subprocess.CalledProcessError:
print("SharpASP-SR failed")
sys.exit(1)
elapsed = time.time() - start
count = parse_count(output_file)
if count is None:
print("SharpASP-SR timeouted")
sys.exit(1)
return count, elapsed
total_time = float(args.t)
first_count = None
first_time = None
second_count = None
second_time = None
print("The formulas phi_1 and phi_2 are computed already !!!")
print("Invoking the first counter call ... ")
first_count, first_time = run_ganak(
"model_" + args.i + ".out",
f"output_{args.i}.out",
total_time,
)
print("SharpASP-SR: first count done, count: {0} and time: {1:.3f}".format(first_count, first_time))
total_time = total_time - first_time
if total_time <= 0:
print("SharpASP-SR timeouted")
sys.exit(1)
print("Invoking the second counter call ... ")
second_count, second_time = run_ganak(
"non_sm_" + args.i + ".out",
f"output_{args.i}.out",
total_time,
)
print("SharpASP-SR: second count done, count: {0} and time: {1:.3f}".format(second_count, second_time))
print("SharpASP-SR: Number of answer sets: {0}".format(first_count - second_count))
print("SharpASP-SR: Total time: {0:.3f}".format(first_time + second_time))