-
Notifications
You must be signed in to change notification settings - Fork 0
/
ag-sv-mso.py
162 lines (127 loc) · 6.07 KB
/
ag-sv-mso.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
#!/usr/bin/env python3
import os
import time
import subprocess
import sys
from os import listdir
import os.path, subprocess
from subprocess import STDOUT,PIPE
from subprocess import check_output
import argparse
class ActionRunToComplete(argparse.Action):
def __call__(self, parser, namespace, values, option_string=None):
# print(values)
BuildAllocationGame(values)
SolveMSO(values[-1])
ComputeShapleyValues([values[0], values[-1]])
setattr(namespace, self.dest, values)
class ActionBuildAllocation(argparse.Action):
def __call__(self, parser, namespace, values, option_string=None):
# print(values)
BuildAllocationGame(values)
setattr(namespace, self.dest, values)
class ActionSolveMSO(argparse.Action):
def __call__(self, parser, namespace, values, option_string=None):
# print(values)
SolveMSO(values[0])
setattr(namespace, self.dest, values)
class ActionComputeSV(argparse.Action):
def __call__(self, parser, namespace, values, option_string=None):
ComputeShapleyValues(values)
setattr(namespace, self.dest, values)
def BuildAllocationGame(values):
outdir = values[0]
res = values[1]
val = values[2]
pub = values[3]
try:
ans = check_output(['java', '-cp', 'ShapleyMSO.jar:commons-io-2.5.jar', "testparser.LedaNewParser", outdir, res, val, pub])
#java -cp ShapleyMSO.jar:commons-io-2.5.jar testparser.LedaNewParser "/home/sequoia/out" "roma30res.csv" "roma30val.csv" "roma30expectedpub.csv"
except subprocess.CalledProcessError as e:
raise RuntimeError("command '{}' return with error (code {}): {}".format(e.cmd, e.returncode, e.output))
#stdout,stderr = proc.communicate(stdin)
solution = ans.decode("utf8")
print(solution)
def ComputeShapleyValues(values):
print(values)
outdir = values[0]
instance = values[1]
os.chdir("/home/")
try:
ans = check_output(['java', '-cp', '.:ShapleyMSO.jar:commons-io-2.5.jar', "sequoia.banzaf.ShapleyMSO", outdir, instance])
except subprocess.CalledProcessError as e:
raise RuntimeError("command '{}' return with error (code {}): {}".format(e.cmd, e.returncode, e.output))
#stdout,stderr = proc.communicate(stdin)
solution = ans.decode("utf8")
print(solution)
def SolveMSO(instance):
os.environ['SEQUOIA'] = "/home/sequoia"
os.environ['LD_LIBRARY_PATH'] = os.environ['SEQUOIA'] + "/src/.libs/:.:/lib/"
sequoia_home = os.path.expandvars("$SEQUOIA")
workspace = sequoia_home+"/out/"+instance
out_dir = workspace+"/sequoia"
os.system("""rm -rf """+out_dir)
os.system("""mkdir """+out_dir)
os.chdir(sequoia_home+"""/src/""")
for level in listdir(workspace+"/f1"):
#print(level)
if os.path.isdir(workspace+"/f1/"+level):
for graph in listdir(workspace+"/f1/"+level):
if graph != ".DS_Store" and "Icon" not in graph and graph != "color":
print("Instance: " + graph)
graph_path = workspace+"/f1/"+level+"/"+graph
start = time.time()
proc = subprocess.Popen(["./sequoia -T4 -f ../../fl.mso -e CardCounting -g " + graph_path + " -c 1000000"], stdout=subprocess.PIPE, shell=True)
(out, err) = proc.communicate()
output = out.decode("utf-8").strip()
end = time.time()
t = end - start
# print(output)
if "Solution" not in output:
print("Danger, no solution has been found!!!!")
print("Errors?",err)
else:
#print(out_dir+"/"+graph.strip(".leda")+".out")
f = open(out_dir+"/"+graph.strip(".leda")+".out","w+")
f.write(graph.split(".")[0].split("_")[4]+";")
f.write(output[output.find("Solution"):])
tempo = "\nExecution time: " + str(t)
f.write(tempo)
f.close()
print("Found solution!")
#print(output[output.find("Solution"):])
print()
for level in listdir(workspace+"/f2"):
#print(level)
if os.path.isdir(workspace+"/f2/"+level):
for graph in listdir(workspace+"/f2/"+level):
if graph != ".DS_Store" and "Icon" not in graph and graph != "color":
print("Instance: " + graph)
graph_path = workspace+"/f2/"+level+"/"+graph
start = time.time()
proc = subprocess.Popen(["./sequoia -T4 -f ../../fl.mso -e CardCounting -g " + graph_path + " -c 1000000"], stdout=subprocess.PIPE, shell=True)
(out, err) = proc.communicate()
end = time.time()
t = end - start
output = out.decode("utf-8").strip()
# print(output)
if "Solution" not in output:
print("Danger, no solution has been found!!!!")
print("Errors?",err)
else:
#print(out_dir+"/"+graph.strip(".leda")+".out")
f = open(out_dir+"/"+graph.strip(".leda")+".out","w+")
f.write(graph.split(".")[0].split("_")[4]+";")
f.write(output[output.find("Solution"):])
tempo = "\nExecution time: " + str(t)
f.write(tempo)
f.close()
print("Found solution!")
#print(output[output.find("Solution"):])
if __name__ == '__main__':
my_parser = argparse.ArgumentParser()
my_parser.add_argument('-b', nargs='+', action=ActionBuildAllocation)
my_parser.add_argument('-c', nargs='+', action=ActionComputeSV)
my_parser.add_argument('-s', nargs=1, action=ActionSolveMSO)
my_parser.add_argument('-a', nargs='+', action=ActionRunToComplete)
args = my_parser.parse_args()