PySCIPOpt
5.1.1
Python Interface for the SCIP Optimization Suite
even.py
Go to the documentation of this file.
1
3
4
5
21
22
from
pyscipopt
import
Model
23
24
verbose =
False
25
sdic = {0:
"even"
, 1:
"odd"
}
26
27
28
def
parity
(number):
29
try
:
30
assert
number == int(round(number))
31
m = Model()
32
m.hideOutput()
33
34
37
x = m.addVar(
"x"
, vtype=
"I"
, lb=
None
, ub=
None
)
# ub=None is default
38
n = m.addVar(
"n"
, vtype=
"I"
, lb=
None
)
39
s = m.addVar(
"s"
, vtype=
"B"
)
40
41
45
m.addCons(x == number)
46
47
m.addCons(s == x - 2 * n)
48
m.setObjective(s)
49
m.optimize()
50
51
assert
m.getStatus() ==
"optimal"
52
if
verbose:
53
for
v
in
m.getVars():
54
print(
"%s %d"
% (v, m.getVal(v)))
55
print(
"%d%%2 == %d?"
% (m.getVal(x), m.getVal(s)))
56
print(m.getVal(s) == m.getVal(x) % 2)
57
58
xval = m.getVal(x)
59
sval = m.getVal(s)
60
sstr = sdic[sval]
61
print(
"%d is %s"
% (xval, sstr))
62
except
(AssertionError, TypeError):
63
print(
"%s is neither even nor odd!"
% number.__repr__())
64
65
66
if
__name__ ==
"__main__"
:
67
import
sys
68
from
ast
import
literal_eval
as
leval
69
70
example_values = [0, 1, 1.5,
"hallo welt"
, 20, 25, -101, -15., -10, -int(2 ** 31), int(2 ** 31 - 1),
71
int(2 ** 63) - 1]
72
try
:
73
try
:
74
n = leval(sys.argv[1])
75
except
ValueError:
76
n = sys.argv[1]
77
parity
(n)
78
except
IndexError:
79
for
n
in
example_values:
80
parity
(n)
even.parity
def parity(number)
Definition:
even.py:28
examples
finished
even.py
Generated on Fri Aug 2 2024 15:02:13 for PySCIPOpt by
1.8.17