-
Notifications
You must be signed in to change notification settings - Fork 54
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support of xarray's diff
function to Variable class
#43
Comments
This does not work either:
Gurobi fails with "infeasible", the lp file looks like this:
Using a rule I was able to add the desired constraints: def diff(m, i):
if i == 0:
# workaround, because rule has to return an AnonymousScalarConstraint
return var[0] - var[0] == 0.
else:
return var[i] - var[i-1] == 1.
m.add_constraints(diff, coords=(pd.RangeIndex(5),)) However, I had to add a workaround to omit the constraint for i=0. If there is a better solution, I would be happy to learn! Thanks! |
Hey @lumbric, indeed the m = linopy.Model()
var = m.add_variables(name="var", lower=xr.DataArray(np.zeros(5), dims='x', coords={'x': np.arange(5)}))
lhs = (var - var.shift(x=1)).sel(x=slice(1, None))
m.add_constraints(lhs == 1.)
m.add_objective(var.sum()) |
Thanks a lot! This seems to be way faster than the rule-based approach above (2.5s compared to 4.4s for (I'll keep the ticket open for the |
diff
function to Variable class
I had a quick look into the implementation, don't really fully grasp all the details yet, basically DataArray methods work if they shift things around (like I had a very quick look at Probably supported correctly by linopy: Potentially dangerous: Harmless, nice to have / necessary: Harmless, but probably not really needed: Don't know: More methods, didn't look through them yet: |
Hey @lumbric this is a very good starting point! And I would love to tackle this issue since it would make |
Okay, will try to see if I find time for a draft These are possible approaches I can think of at the moment:
Do you have any other idea or any opinion on this? I think the most practical solution might be the second one. I can try to make a draft to check if there are any unforeseen downsides of this approach. |
The class
linopy.variables.Variable
inherits methods from its parentxarray.DataArray
. Some of these methods seem to lead to wrong optimization results.Example:
Solving using Gurobi gives the solution:
var: 0.0 1.0 0.0 0.0 0.0
.The expected solution would be:
var: 0.0 1.0 2.0 3.0 4.0
The text was updated successfully, but these errors were encountered: