4 from pyscipopt
import Model
5 from pyscipopt
import quicksum
11 Tutorial example on how to use AND/OR/XOR constraints.
13 N.B.: standard SCIP XOR constraint works differently from AND/OR by design.
14 The constraint is set with a boolean rhs instead of an integer resultant.
15 cf. http://listserv.zib.de/pipermail/scip/2018-May/003392.html
16 A workaround to get the resultant as variable is here proposed.
23 print(
"* %s *" % name)
24 objSet = bool(m.getObjective().terms.keys())
25 print(
"* Is objective set? %s" % objSet)
27 print(
"* Sense: %s" % m.getObjectiveSense())
30 print(
"%s: %d" % (v, round(m.getVal(v))))
37 x = model.addVar(
"x",
"B")
38 y = model.addVar(
"y",
"B")
39 z = model.addVar(
"z",
"B")
40 r = model.addVar(
"r",
"B")
41 model.addConsAnd([x, y, z], r)
43 model.setObjective(r, sense=
"minimize")
50 x = model.addVar(
"x",
"B")
51 y = model.addVar(
"y",
"B")
52 z = model.addVar(
"z",
"B")
53 r = model.addVar(
"r",
"B")
54 model.addConsOr([x, y, z], r)
56 model.setObjective(r, sense=
"maximize")
63 x = model.addVar(
"x",
"B")
64 y = model.addVar(
"y",
"B")
65 z = model.addVar(
"z",
"B")
67 model.addConsXor([x, y, z], r)
70 printFunc(
"Standard XOR (as boolean)", model)
75 x = model.addVar(
"x",
"B")
76 y = model.addVar(
"y",
"B")
77 z = model.addVar(
"z",
"B")
78 r = model.addVar(
"r",
"B")
79 n = model.addVar(
"n",
"I")
80 model.addCons(r +
quicksum([x, y, z]) == 2 * n)
82 model.setObjective(r, sense=
"maximize")
84 printFunc(
"Custom XOR (as variable)", model)