forked from ds4dm/PySCIPOpt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_tree.py
66 lines (49 loc) · 1.76 KB
/
test_tree.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
import pytest
from pyscipopt import Model, Eventhdlr, SCIP_RESULT, SCIP_EVENTTYPE, SCIP_PARAMSETTING
class NodeEventHandler(Eventhdlr):
def __init__(self):
self.calls = []
def eventinit(self):
self.model.catchEvent(SCIP_EVENTTYPE.NODEFOCUSED, self)
def eventexit(self):
self.model.dropEvent(SCIP_EVENTTYPE.NODEFOCUSED, self)
def eventexec(self, event):
self.calls.append('eventexec')
assert event.getType() == SCIP_EVENTTYPE.NODEFOCUSED
node = event.getNode()
if node.getDepth() == 0:
assert node.getParent() is None
assert node.getParentBranchings() is None
return
variables, branchbounds, boundtypes = node.getParentBranchings()
assert len(variables) == 1
assert len(branchbounds) == 1
assert len(boundtypes) == 1
domain_changes = node.getDomchg()
bound_changes = domain_changes.getBoundchgs()
assert len(bound_changes) == 1
def test_tree():
# create solver instance
s = Model()
s.setMaximize()
s.hideOutput()
s.setPresolve(SCIP_PARAMSETTING.OFF)
node_eventhdlr = NodeEventHandler()
s.includeEventhdlr(node_eventhdlr, "NodeEventHandler", "python event handler to catch NODEFOCUSED")
# add some variables
n = 121
x = [s.addVar("x{}".format(i), obj=1.0, vtype="INTEGER") for i in range(n)]
# add some constraints
for i in range(n):
for j in range(i):
dist = min(abs(i - j), abs(n - i - j))
if dist in (1, 3, 4):
s.addCons(x[i] + x[j] <= 1)
# solve problem
s.optimize()
# print solution
assert round(s.getObjVal()) == 36.0
del s
assert len(node_eventhdlr.calls) > 3
if __name__ == "__main__":
test_tree()