-
Notifications
You must be signed in to change notification settings - Fork 79
Expand file tree
/
Copy pathtest_model.py
More file actions
355 lines (263 loc) · 11 KB
/
test_model.py
File metadata and controls
355 lines (263 loc) · 11 KB
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
#!/usr/bin/env python3
"""
Test function defined in the Model class.
"""
from __future__ import annotations
import copy as pycopy
import weakref
from pathlib import Path
from tempfile import gettempdir
import numpy as np
import pytest
import xarray as xr
from linopy import EQUAL, Model, available_solvers
from linopy.testing import (
assert_conequal,
assert_equal,
assert_linequal,
assert_model_equal,
)
target_shape: tuple[int, int] = (10, 10)
def test_model_repr() -> None:
m: Model = Model()
m.__repr__()
def test_model_force_dims_names() -> None:
m: Model = Model(force_dim_names=True)
with pytest.raises(ValueError):
m.add_variables([-5], [10])
def test_model_solver_dir() -> None:
d: str = gettempdir()
m: Model = Model(solver_dir=d)
assert m.solver_dir == Path(d)
def test_model_is_weakrefable() -> None:
m: Model = Model()
ref = weakref.ref(m)
assert ref() is m
def test_model_weakkeydict_use_case() -> None:
# third-party extensions rely on WeakKeyDictionary for per-instance storage
registry: weakref.WeakKeyDictionary[Model, str] = weakref.WeakKeyDictionary()
m: Model = Model()
registry[m] = "extension-state"
assert registry[m] == "extension-state"
del m
import gc
gc.collect()
assert len(registry) == 0
def test_model_variable_getitem() -> None:
m = Model()
x = m.add_variables(name="x")
assert m["x"].labels == x.labels
def test_coefficient_range() -> None:
m: Model = Model()
lower: xr.DataArray = xr.DataArray(
np.zeros((10, 10)), coords=[range(10), range(10)]
)
upper: xr.DataArray = xr.DataArray(np.ones((10, 10)), coords=[range(10), range(10)])
x = m.add_variables(lower, upper)
y = m.add_variables()
m.add_constraints(1 * x + 10 * y, EQUAL, 0)
assert m.coefficientrange["min"].con0 == 1
assert m.coefficientrange["max"].con0 == 10
def test_objective() -> None:
m: Model = Model()
lower: xr.DataArray = xr.DataArray(
np.zeros((10, 10)), coords=[range(10), range(10)]
)
upper: xr.DataArray = xr.DataArray(np.ones((10, 10)), coords=[range(10), range(10)])
x = m.add_variables(lower, upper, name="x")
y = m.add_variables(lower, upper, name="y")
obj1 = (10 * x + 5 * y).sum()
m.add_objective(obj1)
assert m.objective.vars.size == 200
# test overwriting
obj2 = (2 * x).sum()
m.add_objective(obj2, overwrite=True)
# test Tuple
obj3 = [(2, x)]
m.add_objective(obj3, overwrite=True)
# test objective range
assert m.objectiverange.min() == 2
assert m.objectiverange.max() == 2
# test objective with constant which is not supported
with pytest.raises(ValueError):
m.objective = m.objective + 3
def test_remove_variable() -> None:
m: Model = Model()
lower: xr.DataArray = xr.DataArray(
np.zeros((10, 10)), coords=[range(10), range(10)]
)
upper: xr.DataArray = xr.DataArray(np.ones((10, 10)), coords=[range(10), range(10)])
x = m.add_variables(lower, upper, name="x")
y = m.add_variables(name="y")
m.add_constraints(1 * x + 10 * y, EQUAL, 0)
obj = (10 * x + 5 * y).sum()
m.add_objective(obj)
assert "x" in m.variables
m.remove_variables("x")
assert "x" not in m.variables
assert not m.constraints.con0.vars.isin(x.labels).any()
assert not m.objective.vars.isin(x.labels).any()
def test_remove_constraint() -> None:
m: Model = Model()
x = m.add_variables()
m.add_constraints(x, EQUAL, 0, name="x")
m.remove_constraints("x")
assert not len(m.constraints.labels)
def test_remove_constraints_with_list() -> None:
m: Model = Model()
x = m.add_variables()
y = m.add_variables()
m.add_constraints(x, EQUAL, 0, name="constraint_x")
m.add_constraints(y, EQUAL, 0, name="constraint_y")
m.remove_constraints(["constraint_x", "constraint_y"])
assert "constraint_x" not in m.constraints.labels
assert "constraint_y" not in m.constraints.labels
assert not len(m.constraints.labels)
def test_remove_objective() -> None:
m: Model = Model()
lower: xr.DataArray = xr.DataArray(np.zeros((2, 2)), coords=[range(2), range(2)])
upper: xr.DataArray = xr.DataArray(np.ones((2, 2)), coords=[range(2), range(2)])
x = m.add_variables(lower, upper, name="x")
y = m.add_variables(lower, upper, name="y")
obj = (10 * x + 5 * y).sum()
m.add_objective(obj)
m.remove_objective()
assert not len(m.objective.vars)
def test_assert_model_equal() -> None:
m: Model = Model()
lower: xr.DataArray = xr.DataArray(
np.zeros((10, 10)), coords=[range(10), range(10)]
)
upper: xr.DataArray = xr.DataArray(np.ones((10, 10)), coords=[range(10), range(10)])
x = m.add_variables(lower, upper, name="x")
y = m.add_variables(name="y")
m.add_constraints(1 * x + 10 * y, EQUAL, 0)
obj = (10 * x + 5 * y).sum()
m.add_objective(obj)
assert_model_equal(m, m)
@pytest.fixture(scope="module")
def copy_test_model() -> Model:
"""Small representative model used across copy tests."""
m: Model = Model()
lower: xr.DataArray = xr.DataArray(
np.zeros((10, 10)), coords=[range(10), range(10)]
)
upper: xr.DataArray = xr.DataArray(np.ones((10, 10)), coords=[range(10), range(10)])
x = m.add_variables(lower, upper, name="x")
y = m.add_variables(name="y")
m.add_constraints(1 * x + 10 * y, EQUAL, 0)
m.add_objective((10 * x + 5 * y).sum())
return m
@pytest.fixture(scope="module")
def solved_copy_test_model(copy_test_model: Model) -> Model:
"""Solved representative model used across solved-copy tests."""
m = copy_test_model.copy(deep=True)
m.solve()
return m
def test_model_copy_unsolved(copy_test_model: Model) -> None:
"""Copy of unsolved model is structurally equal and independent."""
m = copy_test_model.copy(deep=True)
c = m.copy(include_solution=False)
assert_model_equal(m, c)
# independence: mutating copy does not affect source
c.add_variables(name="z")
assert "z" not in m.variables
def test_model_copy_unsolved_with_solution_flag(copy_test_model: Model) -> None:
"""Unsolved model with include_solution=True has no extra solve artifacts."""
m = copy_test_model.copy(deep=True)
c_include_solution = m.copy(include_solution=True)
c_exclude_solution = m.copy(include_solution=False)
assert_model_equal(c_include_solution, c_exclude_solution)
assert c_include_solution.status == "initialized"
assert c_include_solution.termination_condition == ""
assert c_include_solution.objective.value is None
def test_model_copy_shallow(copy_test_model: Model) -> None:
"""Shallow copy has independent wrappers sharing underlying data buffers."""
m = copy_test_model.copy(deep=True)
c = m.copy(deep=False)
assert c is not m
assert c.variables is not m.variables
assert c.constraints is not m.constraints
assert c.objective is not m.objective
# wrappers are distinct, but shallow copy shares payload buffers
c.variables["x"].lower.values[0, 0] = 123.0
assert m.variables["x"].lower.values[0, 0] == 123.0
def test_model_deepcopy_protocol(copy_test_model: Model) -> None:
"""copy.deepcopy(model) dispatches to Model.__deepcopy__ and stays independent."""
m = copy_test_model.copy(deep=True)
c = pycopy.deepcopy(m)
assert_model_equal(m, c)
# Test independence: mutations to copy do not affect source
# 1. Variable mutation: add new variable
c.add_variables(name="z")
assert "z" not in m.variables
# 2. Variable data mutation (bounds): verify buffers are independent
original_lower = m.variables["x"].lower.values[0, 0].item()
new_lower = 999
c.variables["x"].lower.values[0, 0] = new_lower
assert c.variables["x"].lower.values[0, 0] == new_lower
assert m.variables["x"].lower.values[0, 0] == original_lower
# 3. Constraint coefficient mutation: deep copy must not leak back
original_con_coeff = m.constraints["con0"].coeffs.values.flat[0].item()
new_con_coeff = original_con_coeff + 42
c.constraints["con0"].coeffs.values.flat[0] = new_con_coeff
assert c.constraints["con0"].coeffs.values.flat[0] == new_con_coeff
assert m.constraints["con0"].coeffs.values.flat[0] == original_con_coeff
# 4. Objective expression coefficient mutation: deep copy must not leak back
original_obj_coeff = m.objective.expression.coeffs.values.flat[0].item()
new_obj_coeff = original_obj_coeff + 20
c.objective.expression.coeffs.values.flat[0] = new_obj_coeff
assert c.objective.expression.coeffs.values.flat[0] == new_obj_coeff
assert m.objective.expression.coeffs.values.flat[0] == original_obj_coeff
# 5. Objective sense mutation
original_sense = m.objective.sense
c.objective.sense = "max"
assert c.objective.sense == "max"
assert m.objective.sense == original_sense
@pytest.mark.skipif(not available_solvers, reason="No solver installed")
class TestModelCopySolved:
def test_model_deepcopy_protocol_excludes_solution(
self, solved_copy_test_model: Model
) -> None:
"""copy.deepcopy on solved model drops solve state by default."""
m = solved_copy_test_model
c = pycopy.deepcopy(m)
assert c.status == "initialized"
assert c.termination_condition == ""
assert c.objective.value is None
for v in m.variables:
assert_equal(
c.variables[v].data[c.variables.dataset_attrs],
m.variables[v].data[m.variables.dataset_attrs],
)
for con in m.constraints:
assert_conequal(c.constraints[con], m.constraints[con], strict=False)
assert_linequal(c.objective.expression, m.objective.expression)
assert c.objective.sense == m.objective.sense
def test_model_copy_solved_with_solution(
self, solved_copy_test_model: Model
) -> None:
"""Copy with include_solution=True preserves solve state."""
m = solved_copy_test_model
c = m.copy(include_solution=True)
assert_model_equal(m, c)
def test_model_copy_solved_without_solution(
self, solved_copy_test_model: Model
) -> None:
"""Copy with include_solution=False (default) drops solve state but preserves problem structure."""
m = solved_copy_test_model
c = m.copy(include_solution=False)
# solve state is dropped
assert c.status == "initialized"
assert c.termination_condition == ""
assert c.objective.value is None
# problem structure is preserved — compare only dataset_attrs to exclude solution/dual
for v in m.variables:
assert_equal(
c.variables[v].data[c.variables.dataset_attrs],
m.variables[v].data[m.variables.dataset_attrs],
)
for con in m.constraints:
assert_conequal(c.constraints[con], m.constraints[con], strict=False)
assert_linequal(c.objective.expression, m.objective.expression)
assert c.objective.sense == m.objective.sense