PyPSA / linopy

Linear optimization with N-D labeled arrays in Python
https://linopy.readthedocs.io
MIT License
162 stars 44 forks source link

add_constraints lhs eats constant #270

Closed Cellophil closed 5 months ago

Cellophil commented 5 months ago

Adding a constraint where lhs contains a constrant makes the constant vanish:

n.model.add_constraints(n.model.variables["Generator-p"]-1, "=", 0, name="run!")

yields p=0. Would be very comfortable if the rhs is sanitized in the function, or maybe give a warning.

Why and how I got there: I tried to merge many constraints into one vectorized constraint object; for that purpose there's a convenience function "merge" in the linopy package which only works on expressions. So I merged "lhs - rhs" over my custom dimension before creating the constraint. Maybe there's an easier way, coming from a list of constraint objects?

Cellophil commented 5 months ago

probably add_constraints(lhs-lhs.const, sign, rhs-lhs.const) is always correct

FabianHofmann commented 5 months ago

hey @Cellophil, thank you very much that you raised this as this is quite important. I have made a fix in #274.