-
Notifications
You must be signed in to change notification settings - Fork 0
/
testscript.py
executable file
·270 lines (257 loc) · 10.4 KB
/
testscript.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
from sys import argv
import logging
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
from subprocess import run, PIPE, STDOUT
from ast import literal_eval
def nextline(f):
try:
line = next(f).strip()
while line == "" or line[0] == '#':
line=next(f).strip()
except StopIteration:
return ""
return line
def testequalpolyhedra(ineq1,ineq2,testpoints):
for x in testpoints:
#
inP1 = 1
A, b = ineq1
logging.debug("Left:")
logging.debug(f"A: {A}")
logging.debug(f"b: {b}")
logging.debug(f"x: {x}")
for i in range(len(A)):
lhs = sum(A[i][j]*x[j] for j in range(len(A[i])))
if lhs < b[i]-0.0000000000003:
inP1 = -1
break
elif lhs < b[i]+0.0000000000003:
inP1 = 0
inP2 = 1
A, b = ineq2
#logging.debug("Right:")
#logging.debug(f"A: {A}")
#logging.debug(f"b: {b}")
for i in range(len(A)):
lhs = sum(A[i][j]*x[j] for j in range(len(A[i])))
if lhs < b[i]-0.00000000000001:
inP2 = -1
break
elif lhs < b[i]+0.00000000000001:
inP2 = 0
if inP1 - inP2 > 1 or inP2 - inP1 > 1:
return False
return True
def test_x_or_y(output,A,b):
if output[0]:
x = output[1]
for i in range(len(A)):
if sum(A[i][j]*x[j] for j in range(len(x)))<b[i]:
return False
if not output[0]:
y = output[1]
for i in range(len(y)):
if y[i] < 0:
return False
for j in range(len(A[0])):
ytransposedai = sum(y[i]*A[i][j] for i in range(len(y)))
if ytransposedai < -0.00000000000001 or ytransposedai > 0.00000000000001:
return False
if sum(y[i]*b[i] for i in range(len(y))) <= 0:
return False
return True
def test_project(instancefile,solutionfile,testpointfile):
with open(instancefile,'r') as instances:
with open(solutionfile,'r') as solutions:
inputline = instances.readline()
while inputline !="":
assert inputline[0]=='k'
k = inputline.split()[1]
inputline = instances.readline()
with open("tempinput.in",'w') as tempinput:
while inputline != "" and inputline[0] != 'k':
tempinput.write(inputline)
inputline = instances.readline()
run(["truncate", "-s", "-1", "tempinput.in"])
result = run(["./fourier-motzkin.sh", "project", "tempinput.in", k, "outfile.ot"], stdout=PIPE, stderr=STDOUT)
print("output test project: "+result.stdout.decode('utf-8'))
solA = []
solutionline = solutions.readline()
assert solutionline=="A\n"
solutionline = solutions.readline()
while solutionline != "b\n":
solA.append(list(map(float,solutionline.split(" "))))
solutionline = solutions.readline()
solutionline = solutions.readline()
solb = list(map(float,solutionline.split(" ")))
with open("outfile.ot",'r') as outfile:
A = []
outline = nextline(outfile)
assert outline=="A"
outline = nextline(outfile)
while outline != "" and outline != "b":
A.append(list(map(float,outline.split(" "))))
outline = nextline(outfile)
if outline == "" or outline == "\n":
raise Exception("No correct output file")
outline = nextline(outfile)
if outline == "" or outline == "\n":
print("Empty b vector")
b = []
else:
b = list(map(float,outline.split(" ")))
n = len(solA[0])
with open(testpointfile,'r') as testpoints:
points = []
testline = testpoints.readline()
assert testline[0] == "n"
while testline != "n {}\n".format(n):
testline = testpoints.readline()
testline = testpoints.readline()
while testline != "" and testline != "\n" and testline[0] != 'n':
points.append(list(map(float,testline.split(" "))))
testline = testpoints.readline()
if not testequalpolyhedra((A,b),(solA,solb),points):
return False
return True
def test_image(instancefile,solutionfile,testpointfile):
with open(instancefile,'r') as instances:
with open(solutionfile,'r') as solutions:
inputline = instances.readline()
while inputline !="":
assert inputline[0]=='M'
inputline = instances.readline()
with open("tempmatrix.in",'w') as tempmatrix:
while inputline != "" and inputline != "A\n":
tempmatrix.write(inputline)
inputline = instances.readline()
with open("temppolyhedron.in",'w') as temppolyhedron:
while inputline != "" and inputline[0] != 'M':
temppolyhedron.write(inputline)
inputline = instances.readline()
run(["truncate", "-s", "-1", "tempmatrix.in"])
run(["truncate", "-s", "-1", "temppolyhedron.in"])
result = run(["./fourier-motzkin.sh", "image", "temppolyhedron.in", "tempmatrix.in", "outfile.ot"], stdout=PIPE, stderr=STDOUT)
print("output test image: "+result.stdout.decode('utf-8'))
solA = []
solutionline = solutions.readline()
assert solutionline=="A\n"
solutionline = solutions.readline()
while solutionline != "b\n":
solA.append(list(map(float,solutionline.split(" "))))
solutionline = solutions.readline()
solutionline = solutions.readline()
solb = list(map(float,solutionline.split(" ")))
with open("outfile.ot",'r') as outfile:
A = []
outline = nextline(outfile)
assert outline=="A"
outline = nextline(outfile)
while outline != "" and outline != "b":
A.append(list(map(float,outline.split(" "))))
outline = nextline(outfile)
if outline == "":
raise Exception("No correct output file")
outline = nextline(outfile)
if outline == "" or outline == "\n":
print("Empty b vector")
b = []
else:
b = list(map(float,outline.split(" ")))
n = len(solA[0])
with open(testpointfile,'r') as testpoints:
points = []
testline = testpoints.readline()
assert testline[0] == "n"
while testline != "n {}\n".format(n):
testline = testpoints.readline()
testline = testpoints.readline()
while testline != "" and testline != "\n" and testline[0] != 'n':
points.append(list(map(float,testline.split(" "))))
testline = testpoints.readline()
if not testequalpolyhedra((A,b),(solA,solb),points):
return False
return True
def test_H_representation(instancefile,solutionfile,testpointfile):
with open(instancefile,'r') as instances:
with open(solutionfile,'r') as solutions:
inputline = instances.readline()
while inputline !="":
assert inputline[0]=='X'
inputline = instances.readline()
with open("tempinput.in",'w') as tempinput:
while inputline != "" and inputline != "X\n":
tempinput.write(inputline)
inputline = instances.readline()
run(["truncate", "-s", "-1", "tempinput.in"])
result = run(["./fourier-motzkin.sh", "H_representation", "tempinput.in", "outfile.ot"], stdout=PIPE, stderr=STDOUT)
print("output test H_representation: "+result.stdout.decode('utf-8'))
solA = []
solutionline = solutions.readline()
assert solutionline=="A\n"
solutionline = solutions.readline()
while solutionline != "b\n":
solA.append(list(map(float,solutionline.split(" "))))
solutionline = solutions.readline()
solutionline = solutions.readline()
solb = list(map(float,solutionline.split(" ")))
with open("outfile.ot",'r') as outfile:
A = []
outline = nextline(outfile)
assert outline=="A"
outline = nextline(outfile)
while outline != "" and outline != "b":
A.append(list(map(float,outline.split(" "))))
outline = nextline(outfile)
if outline == "":
raise Exception("No correct output file")
outline = nextline(outfile)
b = list(map(float,outline.split(" ")))
n = len(solA[0])
with open(testpointfile,'r') as testpoints:
points = []
testline = testpoints.readline()
assert testline[0] == "n"
while testline != "n {}\n".format(n):
testline = testpoints.readline()
testline = testpoints.readline()
while testline != "" and testline != "\n" and testline[0] != 'n':
points.append(list(map(float,testline.split(" "))))
testline = testpoints.readline()
if not testequalpolyhedra((A,b),(solA,solb),points):
return False
return True
def test_compute_x_or_y(instancefile):
with open(instancefile,'r') as instances:
inputline = instances.readline()
while inputline !="":
assert inputline=="A\n"
with open("tempinput.in",'w') as tempinput:
tempinput.write(inputline)
A = []
inputline = instances.readline()
while inputline != "" and inputline != "b\n":
tempinput.write(inputline)
A.append(list(map(float,inputline.split(" "))))
inputline = instances.readline()
tempinput.write(inputline)
inputline = instances.readline()
tempinput.write(inputline)
b = list(map(float,inputline.split(" ")))
inputline = instances.readline()
run(["truncate", "-s", "-1", "tempinput.in"])
result = run(["./fourier-motzkin.sh", "compute_x_or_y", "tempinput.in"],
stdout=PIPE, stderr=PIPE, encoding='utf-8')
print("output test compute_x_or_y: "+result.stderr)
output = literal_eval(result.stdout)
print(output)
if not test_x_or_y(output,A,b):
return False
return True
#print("Test project: {}".format(test_project("./project_instances.dat","./project_solutions.dat","./testpoints.dat")))
print("Test image: {}".format(test_image("./image_instances.dat","./image_solutions.dat","./testpoints.dat")))
print("Test H_representation: {}".format(test_H_representation("./H_representation_instances.dat",
"./H_representation_solutions.dat",
"./testpoints.dat")))
print("Test compute_x_or_y: {}".format(test_compute_x_or_y("project_solutions.dat")))