Skip to content

Commit c561760

Browse files
committed
rename to add_piecewise_constraints
1 parent ad61632 commit c561760

2 files changed

Lines changed: 27 additions & 27 deletions

File tree

linopy/model.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -597,7 +597,7 @@ def add_sos_constraints(
597597

598598
variable.attrs.update(sos_type=sos_type, sos_dim=sos_dim)
599599

600-
def add_piecewise_constraint(
600+
def add_piecewise_constraints(
601601
self,
602602
expr: Variable | LinearExpression | dict[str, Variable | LinearExpression],
603603
breakpoints: DataArray,
@@ -675,15 +675,15 @@ def add_piecewise_constraint(
675675
>>> m = Model()
676676
>>> x = m.add_variables(name="x")
677677
>>> breakpoints = xr.DataArray([0, 10, 50, 100], dims=["bp"])
678-
>>> m.add_piecewise_constraint(x, breakpoints, dim="bp")
678+
>>> m.add_piecewise_constraints(x, breakpoints, dim="bp")
679679
680680
Using an expression:
681681
682682
>>> m = Model()
683683
>>> x = m.add_variables(name="x")
684684
>>> y = m.add_variables(name="y")
685685
>>> breakpoints = xr.DataArray([0, 10, 50, 100], dims=["bp"])
686-
>>> m.add_piecewise_constraint(x + y, breakpoints, dim="bp")
686+
>>> m.add_piecewise_constraints(x + y, breakpoints, dim="bp")
687687
688688
Multiple linked variables (e.g., power-efficiency curve):
689689
@@ -695,7 +695,7 @@ def add_piecewise_constraint(
695695
... [[0, 50, 100], [0.8, 0.95, 0.9]],
696696
... coords={"var": ["power", "efficiency"], "bp": [0, 1, 2]},
697697
... )
698-
>>> m.add_piecewise_constraint(
698+
>>> m.add_piecewise_constraints(
699699
... {"power": power, "efficiency": efficiency},
700700
... breakpoints,
701701
... link_dim="var",

test/test_piecewise_constraints.py

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def test_basic_single_variable(self) -> None:
2323
[0, 10, 50, 100], dims=["bp"], coords={"bp": [0, 1, 2, 3]}
2424
)
2525

26-
m.add_piecewise_constraint(x, breakpoints, dim="bp")
26+
m.add_piecewise_constraints(x, breakpoints, dim="bp")
2727

2828
# Check lambda variables were created
2929
assert f"pwl0{PWL_LAMBDA_SUFFIX}" in m.variables
@@ -50,7 +50,7 @@ def test_single_variable_with_coords(self) -> None:
5050
coords={"generator": generators, "bp": bp_coords},
5151
)
5252

53-
m.add_piecewise_constraint(x, breakpoints, dim="bp")
53+
m.add_piecewise_constraints(x, breakpoints, dim="bp")
5454

5555
# Lambda should have both generator and bp dimensions
5656
lambda_var = m.variables[f"pwl0{PWL_LAMBDA_SUFFIX}"]
@@ -73,7 +73,7 @@ def test_dict_of_variables(self) -> None:
7373
coords={"var": ["power", "efficiency"], "bp": [0, 1, 2]},
7474
)
7575

76-
m.add_piecewise_constraint(
76+
m.add_piecewise_constraints(
7777
{"power": power, "efficiency": efficiency},
7878
breakpoints,
7979
link_dim="var",
@@ -100,7 +100,7 @@ def test_dict_with_coordinates(self) -> None:
100100
},
101101
)
102102

103-
m.add_piecewise_constraint(
103+
m.add_piecewise_constraints(
104104
{"power": power, "efficiency": efficiency},
105105
breakpoints,
106106
link_dim="var",
@@ -130,7 +130,7 @@ def test_auto_detect_link_dim(self) -> None:
130130
)
131131

132132
# Should auto-detect link_dim="var"
133-
m.add_piecewise_constraint(
133+
m.add_piecewise_constraints(
134134
{"power": power, "efficiency": efficiency},
135135
breakpoints,
136136
dim="bp",
@@ -152,7 +152,7 @@ def test_auto_detect_fails_with_no_match(self) -> None:
152152
)
153153

154154
with pytest.raises(ValueError, match="Could not auto-detect link_dim"):
155-
m.add_piecewise_constraint(
155+
m.add_piecewise_constraints(
156156
{"power": power, "efficiency": efficiency},
157157
breakpoints,
158158
dim="bp",
@@ -174,7 +174,7 @@ def test_nan_masking(self) -> None:
174174
coords={"bp": [0, 1, 2, 3]},
175175
)
176176

177-
m.add_piecewise_constraint(x, breakpoints, dim="bp")
177+
m.add_piecewise_constraints(x, breakpoints, dim="bp")
178178

179179
# Lambda for NaN breakpoint should be masked
180180
lambda_var = m.variables[f"pwl0{PWL_LAMBDA_SUFFIX}"]
@@ -200,7 +200,7 @@ def test_explicit_mask(self) -> None:
200200
coords={"generator": generators, "bp": [0, 1, 2]},
201201
)
202202

203-
m.add_piecewise_constraint(x, breakpoints, dim="bp", mask=mask)
203+
m.add_piecewise_constraints(x, breakpoints, dim="bp", mask=mask)
204204

205205
# Should still create variables and constraints
206206
assert f"pwl0{PWL_LAMBDA_SUFFIX}" in m.variables
@@ -214,7 +214,7 @@ def test_skip_nan_check(self) -> None:
214214
breakpoints = xr.DataArray([0, 10, 50], dims=["bp"], coords={"bp": [0, 1, 2]})
215215

216216
# Should work with skip_nan_check=True
217-
m.add_piecewise_constraint(x, breakpoints, dim="bp", skip_nan_check=True)
217+
m.add_piecewise_constraints(x, breakpoints, dim="bp", skip_nan_check=True)
218218

219219
# All lambda variables should be valid (no masking)
220220
lambda_var = m.variables[f"pwl0{PWL_LAMBDA_SUFFIX}"]
@@ -238,7 +238,7 @@ def test_multi_dimensional(self) -> None:
238238
coords={"generator": generators, "time": timesteps, "bp": [0, 1, 2, 3]},
239239
)
240240

241-
m.add_piecewise_constraint(x, breakpoints, dim="bp")
241+
m.add_piecewise_constraints(x, breakpoints, dim="bp")
242242

243243
# Lambda should have all dimensions
244244
lambda_var = m.variables[f"pwl0{PWL_LAMBDA_SUFFIX}"]
@@ -259,7 +259,7 @@ def test_invalid_vars_type(self) -> None:
259259
with pytest.raises(
260260
ValueError, match="must be a Variable, LinearExpression, or dict"
261261
):
262-
m.add_piecewise_constraint("invalid", breakpoints, dim="bp") # type: ignore
262+
m.add_piecewise_constraints("invalid", breakpoints, dim="bp") # type: ignore
263263

264264
def test_missing_dim(self) -> None:
265265
"""Test error when breakpoints don't have the required dim."""
@@ -269,7 +269,7 @@ def test_missing_dim(self) -> None:
269269
breakpoints = xr.DataArray([0, 10, 50], dims=["wrong"])
270270

271271
with pytest.raises(ValueError, match="must have dimension"):
272-
m.add_piecewise_constraint(x, breakpoints, dim="bp")
272+
m.add_piecewise_constraints(x, breakpoints, dim="bp")
273273

274274
def test_non_numeric_dim(self) -> None:
275275
"""Test error when dim coordinates are not numeric."""
@@ -283,7 +283,7 @@ def test_non_numeric_dim(self) -> None:
283283
)
284284

285285
with pytest.raises(ValueError, match="numeric coordinates"):
286-
m.add_piecewise_constraint(x, breakpoints, dim="bp")
286+
m.add_piecewise_constraints(x, breakpoints, dim="bp")
287287

288288
def test_expression_support(self) -> None:
289289
"""Test that LinearExpression is supported as input."""
@@ -294,7 +294,7 @@ def test_expression_support(self) -> None:
294294
breakpoints = xr.DataArray([0, 10, 50], dims=["bp"], coords={"bp": [0, 1, 2]})
295295

296296
# Should work with a LinearExpression
297-
m.add_piecewise_constraint(x + y, breakpoints, dim="bp")
297+
m.add_piecewise_constraints(x + y, breakpoints, dim="bp")
298298

299299
# Check constraints were created
300300
assert f"pwl0{PWL_LINK_SUFFIX}" in m.constraints
@@ -308,7 +308,7 @@ def test_link_dim_not_in_breakpoints(self) -> None:
308308
breakpoints = xr.DataArray([0, 50, 100], dims=["bp"], coords={"bp": [0, 1, 2]})
309309

310310
with pytest.raises(ValueError, match="not found in breakpoints dimensions"):
311-
m.add_piecewise_constraint(
311+
m.add_piecewise_constraints(
312312
{"power": power, "efficiency": efficiency},
313313
breakpoints,
314314
link_dim="var",
@@ -328,7 +328,7 @@ def test_link_dim_coords_mismatch(self) -> None:
328328
)
329329

330330
with pytest.raises(ValueError, match="don't match expression keys"):
331-
m.add_piecewise_constraint(
331+
m.add_piecewise_constraints(
332332
{"power": power, "efficiency": efficiency},
333333
breakpoints,
334334
link_dim="var",
@@ -348,8 +348,8 @@ def test_auto_name_generation(self) -> None:
348348
bp1 = xr.DataArray([0, 10, 50], dims=["bp"], coords={"bp": [0, 1, 2]})
349349
bp2 = xr.DataArray([0, 20, 80], dims=["bp"], coords={"bp": [0, 1, 2]})
350350

351-
m.add_piecewise_constraint(x, bp1, dim="bp")
352-
m.add_piecewise_constraint(y, bp2, dim="bp")
351+
m.add_piecewise_constraints(x, bp1, dim="bp")
352+
m.add_piecewise_constraints(y, bp2, dim="bp")
353353

354354
assert f"pwl0{PWL_LAMBDA_SUFFIX}" in m.variables
355355
assert f"pwl1{PWL_LAMBDA_SUFFIX}" in m.variables
@@ -361,7 +361,7 @@ def test_custom_name(self) -> None:
361361

362362
breakpoints = xr.DataArray([0, 10, 50], dims=["bp"], coords={"bp": [0, 1, 2]})
363363

364-
m.add_piecewise_constraint(x, breakpoints, dim="bp", name="my_pwl")
364+
m.add_piecewise_constraints(x, breakpoints, dim="bp", name="my_pwl")
365365

366366
assert f"my_pwl{PWL_LAMBDA_SUFFIX}" in m.variables
367367
assert f"my_pwl{PWL_CONVEX_SUFFIX}" in m.constraints
@@ -382,7 +382,7 @@ def test_piecewise_written_to_lp(self, tmp_path) -> None:
382382
coords={"bp": [0, 1, 2]},
383383
)
384384

385-
m.add_piecewise_constraint(x, breakpoints, dim="bp")
385+
m.add_piecewise_constraints(x, breakpoints, dim="bp")
386386

387387
# Add a simple objective to make it a valid LP
388388
m.add_objective(x)
@@ -418,7 +418,7 @@ def test_solve_single_variable(self) -> None:
418418
coords={"var": ["x", "cost"], "bp": [0, 1, 2]},
419419
)
420420

421-
m.add_piecewise_constraint(
421+
m.add_piecewise_constraints(
422422
{"x": x, "cost": cost}, breakpoints, link_dim="var", dim="bp"
423423
)
424424

@@ -453,7 +453,7 @@ def test_solve_efficiency_curve(self) -> None:
453453
coords={"var": ["power", "efficiency"], "bp": [0, 1, 2, 3, 4]},
454454
)
455455

456-
m.add_piecewise_constraint(
456+
m.add_piecewise_constraints(
457457
{"power": power, "efficiency": efficiency},
458458
breakpoints,
459459
link_dim="var",
@@ -498,7 +498,7 @@ def test_solve_multi_generator(self) -> None:
498498
},
499499
)
500500

501-
m.add_piecewise_constraint(
501+
m.add_piecewise_constraints(
502502
{"power": power, "cost": cost}, breakpoints, link_dim="var", dim="bp"
503503
)
504504

0 commit comments

Comments
 (0)