PySCIPOpt  5.1.1
Python Interface for the SCIP Optimization Suite
logical.py
Go to the documentation of this file.
1 
3 """
4 N.B.: standard SCIP XOR constraint works differently from AND/OR by design.
5 The constraint is set with a boolean rhs instead of an integer resultant.
6 cf. http://listserv.zib.de/pipermail/scip/2018-May/003392.html
7 A workaround to get the resultant as variable is here proposed.
8 
9 Public Domain, WTFNMFPL Public Licence
10 """
11 from pyscipopt import Model
12 from pyscipopt import quicksum
13 
14 
15 def _init():
16  model = Model()
17  model.hideOutput()
18  x = model.addVar("x", "B")
19  y = model.addVar("y", "B")
20  z = model.addVar("z", "B")
21  return model, x, y, z
22 
23 
24 def _optimize(name, m):
25  m.optimize()
26  print("* %s constraint *" % name)
27  objSet = bool(m.getObjective().terms.keys())
28  print("* Is objective set? %s" % objSet)
29  if objSet:
30  print("* Sense: %s" % m.getObjectiveSense())
31  status = m.getStatus()
32  print("* Model status: %s" % status)
33  if status == 'optimal':
34  for v in m.getVars():
35  if v.name != "n":
36  print("%s: %d" % (v, round(m.getVal(v))))
37  else:
38  print("* No variable is printed if model status is not optimal")
39  print("")
40 
41 
42 def and_constraint(v=1, sense="minimize"):
43  """ AND constraint """
44  assert v in [0, 1], "v must be 0 or 1 instead of %s" % v.__repr__()
45  model, x, y, z = _init()
46  r = model.addVar("r", "B")
47  model.addConsAnd([x, y, z], r)
48  model.addCons(x == v)
49  model.setObjective(r, sense=sense)
50  _optimize("AND", model)
51 
52 
53 def or_constraint(v=0, sense="maximize"):
54  """ OR constraint"""
55  assert v in [0, 1], "v must be 0 or 1 instead of %s" % v.__repr__()
56  model, x, y, z = _init()
57  r = model.addVar("r", "B")
58  model.addConsOr([x, y, z], r)
59  model.addCons(x == v)
60  model.setObjective(r, sense=sense)
61  _optimize("OR", model)
62 
63 
64 def xors_constraint(v=1):
65  """ XOR (r as boolean) standard constraint"""
66  assert v in [0, 1], "v must be 0 or 1 instead of %s" % v.__repr__()
67  model, x, y, z = _init()
68  r = True
69  model.addConsXor([x, y, z], r)
70  model.addCons(x == v)
71  _optimize("Standard XOR (as boolean)", model)
72 
73 
74 def xorc_constraint(v=0, sense="maximize"):
75  """ XOR (r as variable) custom constraint"""
76  assert v in [0, 1], "v must be 0 or 1 instead of %s" % v.__repr__()
77  model, x, y, z = _init()
78  r = model.addVar("r", "B")
79  n = model.addVar("n", "I") # auxiliary
80  model.addCons(r + quicksum([x, y, z]) == 2 * n)
81  model.addCons(x == v)
82  model.setObjective(r, sense=sense)
83  _optimize("Custom XOR (as variable)", model)
84 
85 
86 if __name__ == "__main__":
pyscipopt.expr.quicksum
def quicksum(termlist)
Definition: expr.pxi:357
logical.xors_constraint
def xors_constraint(v=1)
Definition: logical.py:64
logical.xorc_constraint
def xorc_constraint(v=0, sense="maximize")
Definition: logical.py:74
logical.or_constraint
def or_constraint(v=0, sense="maximize")
Definition: logical.py:53
logical.and_constraint
def and_constraint(v=1, sense="minimize")
Definition: logical.py:42