Conversation
…None) instead of 0.
…k for deep copy independence.
There was a problem hiding this comment.
@bobbyxng I can't comment on the actual dualisation so I'm focussing on cleanliness and efficiency of code. I've opened another PR (#629) in which I've made some suggestions (I started writing them in comments but they became a bit too verbose).
I feel like it shouldn't be necessary to have your lookup dictionary or rule and that you should be able to store your coefficients your mapping from constraint name to dual variables. I haven't investigated it in detail but the linear expression rule is probably reasonably slow to build compared to a vectorised array operation. I tried with a dummy model with 10000 timesteps and 2 active spatial nodes and it took 20 seconds to build. I can see dualisation therefore exploding on practical models, so more vectorisation is probably necessary.
You still also need:
- unit tests
- docs
BTW, for ease of review, it would be easier if you opened this PR with the copy branch as the base, rather than master. It will automatically revert to master when copy is merged.
| logger.debug(f"Constraint '{name}' is fully masked, skipping.") | ||
| continue | ||
|
|
||
| if sign_vals[0] == "=": |
There was a problem hiding this comment.
Is it possible that a constraint has different signs for different array elements? If yes, you would need to use a ufunc to map the signs to lower and upper bound arrays of the same shape as con.labels, rather than just taking the first value.



Changes proposed in this Pull Request
Model.dualize(), a method that constructs the LP dual of a linopy model, andModel.bounds_to_constraints(), a preprocessing step that converts variable bounds to explicit constraints so they are correctly reflected in the dual.bounds_to_constraints(), so that they appear in the constraint matrix and are correctly reflected in the dual.m2.variables[con_name].solutionandm1.constraints[con_name].dualwithout sign adjustments.m.copy()The motivation behind this was an automated LP dualizer (originally developed for adaptive robust optimization for energy system planning) which requires constructing an independent dual model without modifying the original. While similar implementations exist in other modelling frameworks, notably JuMP's
Dualization.jlhttps://jump.dev/JuMP.jl/stable/packages/Dualization/, this feature was still missing in linopy.Checklist
doc.doc/release_notes.rstof the upcoming release is included.