-
Notifications
You must be signed in to change notification settings - Fork 257
/
Copy pathlogical.py
84 lines (71 loc) · 2.07 KB
/
logical.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
##@file finished/logical.py
# @brief Tutorial example on how to use AND/OR/XOR constraints
from pyscipopt import Model
from pyscipopt import quicksum
"""
AND/OR/XOR CONSTRAINTS
Tutorial example on how to use AND/OR/XOR constraints.
N.B.: standard SCIP XOR constraint works differently from AND/OR by design.
The constraint is set with a boolean rhs instead of an integer resultant.
cf. http://listserv.zib.de/pipermail/scip/2018-May/003392.html
A workaround to get the resultant as variable is here proposed.
"""
def printFunc(name, m):
"""prints results"""
print("* %s *" % name)
objSet = bool(m.getObjective().terms.keys())
print("* Is objective set? %s" % objSet)
if objSet:
print("* Sense: %s" % m.getObjectiveSense())
for v in m.getVars():
if v.name != "n":
print("%s: %d" % (v, round(m.getVal(v))))
print("\n")
# AND
model = Model()
model.hideOutput()
x = model.addVar("x", "B")
y = model.addVar("y", "B")
z = model.addVar("z", "B")
r = model.addVar("r", "B")
model.addConsAnd([x, y, z], r)
model.addCons(x == 1)
model.setObjective(r, sense="minimize")
model.optimize()
printFunc("AND", model)
# OR
model = Model()
model.hideOutput()
x = model.addVar("x", "B")
y = model.addVar("y", "B")
z = model.addVar("z", "B")
r = model.addVar("r", "B")
model.addConsOr([x, y, z], r)
model.addCons(x == 0)
model.setObjective(r, sense="maximize")
model.optimize()
printFunc("OR", model)
# XOR (r as boolean, standard)
model = Model()
model.hideOutput()
x = model.addVar("x", "B")
y = model.addVar("y", "B")
z = model.addVar("z", "B")
r = True
model.addConsXor([x, y, z], r)
model.addCons(x == 1)
model.optimize()
printFunc("Standard XOR (as boolean)", model)
# XOR (r as variable, custom)
model = Model()
model.hideOutput()
x = model.addVar("x", "B")
y = model.addVar("y", "B")
z = model.addVar("z", "B")
r = model.addVar("r", "B")
n = model.addVar("n", "I") # auxiliary
model.addCons(r + quicksum([x, y, z]) == 2 * n)
model.addCons(x == 0)
model.setObjective(r, sense="maximize")
model.optimize()
printFunc("Custom XOR (as variable)", model)