forked from scipopt/PySCIPOpt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_short.py
89 lines (74 loc) · 2.6 KB
/
test_short.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
from pyscipopt import Model
import pytest
import os
# This test requires a directory link in tests/ to check/ in the main SCIP directory.
testset = []
primalsolutions = {}
dualsolutions = {}
tolerance = 1e-5
infinity = 1e20
testsetpath = 'check/testset/short.test'
solufilepath = 'check/testset/short.solu'
if not all(os.path.isfile(fn) for fn in [testsetpath, solufilepath]):
if pytest.__version__ < "3.0.0":
pytest.skip("Files for testset `short` not found (symlink missing?)")
else:
pytestmark = pytest.mark.skip
else:
with open(testsetpath, 'r') as f:
for line in f.readlines():
testset.append('check/' + line.rstrip('\n'))
with open(solufilepath, 'r') as f:
for line in f.readlines():
if len(line.split()) == 2:
[s, name] = line.split()
else:
[s, name, value] = line.split()
if s == '=opt=':
primalsolutions[name] = float(value)
dualsolutions[name] = float(value)
elif s == '=inf=':
primalsolutions[name] = infinity
dualsolutions[name] = infinity
elif s == '=best=':
primalsolutions[name] = float(value)
elif s == '=best dual=':
dualsolutions[name] = float(value)
# status =unkn= needs no data
def relGE(v1, v2, tol = tolerance):
if v1 is None or v2 is None:
return True
else:
reltol = tol * max(abs(v1), abs(v2), 1.0)
return (v1 - v2) >= -reltol
def relLE(v1, v2, tol = tolerance):
if v1 is None or v2 is None:
return True
else:
reltol = tol * max(abs(v1), abs(v2), 1.0)
return (v1 - v2) <= reltol
@pytest.mark.parametrize('instance', testset)
def test_instance(instance):
s = Model()
s.hideOutput()
s.readProblem(instance)
s.optimize()
name = os.path.split(instance)[1]
if name.rsplit('.',1)[1].lower() == 'gz':
name = name.rsplit('.',2)[0]
else:
name = name.rsplit('.',1)[0]
# we do not need the solution status
primalbound = s.getObjVal()
dualbound = s.getDualbound()
# get solution data from solu file
primalsolu = primalsolutions.get(name, None)
dualsolu = dualsolutions.get(name, None)
if s.getObjectiveSense() == 'minimize':
assert relGE(primalbound, dualsolu)
assert relLE(dualbound, primalsolu)
else:
if( primalsolu == infinity ): primalsolu = -infinity
if( dualsolu == infinity ): dualsolu = -infinity
assert relLE(primalbound, dualsolu)
assert relGE(dualbound, primalsolu)