forked from ds4dm/PySCIPOpt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_linexpr.py
223 lines (185 loc) · 5.4 KB
/
test_linexpr.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
import pytest
from pyscipopt import Model
from pyscipopt.scip import Expr, ExprCons, Term, quicksum
@pytest.fixture(scope="module")
def model():
m = Model()
x = m.addVar("x")
y = m.addVar("y")
z = m.addVar("z")
return m, x, y, z
CONST = Term()
def test_term(model):
m, x, y, z = model
assert x[x] == 1.0
assert x[y] == 0.0
def test_operations_linear(model):
m, x, y, z = model
expr = x + y
assert isinstance(expr, Expr)
assert expr[x] == 1.0
assert expr[y] == 1.0
assert expr[z] == 0.0
expr = -x
assert isinstance(expr, Expr)
assert expr[x] == -1.0
assert expr[y] == 0.0
expr = x*4
assert isinstance(expr, Expr)
assert expr[x] == 4.0
assert expr[y] == 0.0
expr = 4*x
assert isinstance(expr, Expr)
assert expr[x] == 4.0
assert expr[y] == 0.0
expr = x + y + x
assert isinstance(expr, Expr)
assert expr[x] == 2.0
assert expr[y] == 1.0
expr = x + y - x
assert isinstance(expr, Expr)
assert expr[x] == 0.0
assert expr[y] == 1.0
expr = 3*x + 1.0
assert isinstance(expr, Expr)
assert expr[x] == 3.0
assert expr[y] == 0.0
assert expr[CONST] == 1.0
expr = 1.0 + 3*x
assert isinstance(expr, Expr)
assert expr[x] == 3.0
assert expr[y] == 0.0
assert expr[CONST] == 1.0
def test_operations_quadratic(model):
m, x, y, z = model
expr = x*x
assert isinstance(expr, Expr)
assert expr[x] == 0.0
assert expr[y] == 0.0
assert expr[CONST] == 0.0
assert expr[Term(x,x)] == 1.0
expr = x*y
assert isinstance(expr, Expr)
assert expr[x] == 0.0
assert expr[y] == 0.0
assert expr[CONST] == 0.0
assert expr[Term(x,y)] == 1.0
expr = (x - 1)*(y + 1)
assert isinstance(expr, Expr)
assert expr[x] == 1.0
assert expr[y] == -1.0
assert expr[CONST] == -1.0
assert expr[Term(x,y)] == 1.0
def test_power_for_quadratic(model):
m, x, y, z = model
expr = x**2 + x + 1
assert isinstance(expr, Expr)
assert expr[Term(x,x)] == 1.0
assert expr[x] == 1.0
assert expr[CONST] == 1.0
assert len(expr.terms) == 3
assert (x**2).terms == (x*x).terms
assert ((x + 3)**2).terms == (x**2 + 6*x + 9).terms
def test_operations_poly(model):
m, x, y, z = model
expr = x*x*x + 2*y*y
assert isinstance(expr, Expr)
assert expr[x] == 0.0
assert expr[y] == 0.0
assert expr[CONST] == 0.0
assert expr[Term(x,x,x)] == 1.0
assert expr[Term(y,y)] == 2.0
assert expr.terms == (x**3 + 2*y**2).terms
def test_degree(model):
m, x, y, z = model
expr = Expr()
assert expr.degree() == 0
expr = Expr() + 3.0
assert expr.degree() == 0
expr = x + 1
assert expr.degree() == 1
expr = x*x + y - 2
assert expr.degree() == 2
expr = (x + 1)*(y + 1)*(x - 1)
assert expr.degree() == 3
def test_inequality(model):
m, x, y, z = model
expr = x + 2*y
cons = expr <= 0
assert isinstance(cons, ExprCons)
assert cons._lhs is None
assert cons._rhs == 0.0
assert cons.expr[x] == 1.0
assert cons.expr[y] == 2.0
assert cons.expr[z] == 0.0
assert cons.expr[CONST] == 0.0
assert CONST not in cons.expr.terms
cons = expr >= 5
assert isinstance(cons, ExprCons)
assert cons._lhs == 5.0
assert cons._rhs is None
assert cons.expr[x] == 1.0
assert cons.expr[y] == 2.0
assert cons.expr[z] == 0.0
assert cons.expr[CONST] == 0.0
assert CONST not in cons.expr.terms
cons = 5 <= x + 2*y - 3
assert isinstance(cons, ExprCons)
assert cons._lhs == 8.0
assert cons._rhs is None
assert cons.expr[x] == 1.0
assert cons.expr[y] == 2.0
assert cons.expr[z] == 0.0
assert cons.expr[CONST] == 0.0
assert CONST not in cons.expr.terms
def test_ranged(model):
m, x, y, z = model
expr = x + 2*y
cons = expr >= 3
ranged = cons <= 5
assert isinstance(ranged, ExprCons)
assert ranged._lhs == 3.0
assert ranged._rhs == 5.0
assert ranged.expr[y] == 2.0
assert ranged.expr[CONST] == 0.0
# again, more or less directly:
ranged = 3 <= (x + 2*y <= 5)
assert isinstance(ranged, ExprCons)
assert ranged._lhs == 3.0
assert ranged._rhs == 5.0
assert ranged.expr[y] == 2.0
assert ranged.expr[CONST] == 0.0
# we must use the parenthesis, because
# x <= y <= z
# is a "chained comparison", which will be interpreted by Python
# to be equivalent to
# (x <= y) and (y <= z)
# where "and" can not be overloaded and the expressions in
# parenthesis are coerced to booleans.
with pytest.raises(TypeError):
ranged = (x + 2*y <= 5) <= 3
with pytest.raises(TypeError):
ranged = 3 >= (x + 2*y <= 5)
with pytest.raises(TypeError):
ranged = (1 <= x + 2*y <= 5)
def test_equation(model):
m, x, y, z = model
equat = 2*x - 3*y == 1
assert isinstance(equat, ExprCons)
assert equat._lhs == equat._rhs
assert equat._lhs == 1.0
assert equat.expr[x] == 2.0
assert equat.expr[y] == -3.0
assert equat.expr[CONST] == 0.0
def test_objective(model):
m, x, y, z = model
# setting linear objective
m.setObjective(x + y)
# using quicksum
m.setObjective(quicksum(2 * v for v in [x, y, z]))
# setting affine objective
m.setObjective(x + y + 1)
assert m.getObjoffset() == 1
# setting nonlinear objective
with pytest.raises(ValueError):
m.setObjective(x ** 2 - y * z)