refac: introduce consistent convention for linopy operations with subsets and supersets#572
refac: introduce consistent convention for linopy operations with subsets and supersets#572FabianHofmann wants to merge 14 commits intomasterfrom
Conversation
Add le(), ge(), eq() methods to LinearExpression and Variable classes, mirroring the pattern of add/sub/mul/div methods. These methods support the join parameter for flexible coordinate alignment when creating constraints.
Consolidate repetitive alignment handling in _add_constant and _apply_constant_op into a single _align_constant method. This eliminates code duplication and makes the alignment behavior (handling join parameter, fill_value, size-aware defaults) testable and maintainable in one place.
numpy_to_dataarray no longer inflates ndim beyond arr.ndim, fixing lower-dim numpy arrays as constraint RHS. Also reject higher-dim constant arrays (numpy/pandas) consistently with DataArray behavior. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
|
@FBumann, if you have some time, could you run this branch on flexopt and check it does not change results? |
|
I'll do it this evening. |
|
@FabianHofmann I really like the effort to making the broadcasting in linopy more predictable. As far as i understand it, the convention is:
My thoughts on the current convention:The default That said, i see the following issues:
m = linopy.Model()
time = pd.RangeIndex(5, name="time")
subset_time = pd.RangeIndex(3, name="time")
factor = xr.DataArray([0, 1, 2, 3, 4, 5], dims=["time"], coords={"time": [0, 1, 2, 3, 4, 5]})
x = m.add_variables(lower=0, coords=[time], name="x")
y = m.add_variables(lower=0, coords=[subset_time], name="y")
print(y + x + factor)
>>>LinearExpression [time: 5]:
---------------------------
[0]: +1 y[0] + 1 x[0]
[1]: +1 y[1] + 1 x[1] + 1
[2]: +1 y[2] + 1 x[2] + 2
[3]: +1 x[3] + 3
[4]: +1 x[4] + 4
print(y + factor + x )
>>>LinearExpression [time: 5]:
---------------------------
[0]: +1 y[0] + 1 x[0]
[1]: +1 y[1] + 1 x[1] + 1
[2]: +1 y[2] + 1 x[2] + 2
[3]: +1 x[3]
[4]: +1 x[4]I'll try to come up with something that resolves this. |
|
@FabianHofmann After some thought i find the matter more complicated than expected. |
|
@FabianHofmann I will not be able to finalize my review on this. Ill be gone for 2 weeks. |
Related to #550 #571
Changes proposed in this Pull Request
Establish a consistent coordinate alignment when linopy objects interact with DataArrays that have subset or superset coordinates
TODO:
linopy.alignaddition/multiplication/comparison+subset/superset` + order + object typesChecklist
doc.doc/release_notes.rstof the upcoming release is included.